| 1: | /* Created by SharpDevelop. | |
| 2: | * User: Eduardo Rocha Monteiro ([email protected]) | |
| 3: | * Date: 27/11/2004 | |
| 4: | * Time: 19:35 | |
| 5: | */ | |
| 6: | ||
| 7: | using System; | |
| 8: | using System.IO; | |
| 9: | using System.Collections; | |
| 10: | ||
| 11: | namespace Compilador.AnalisadorLexicoN | |
| 12: | { | |
| 13: | /// <summary> | |
| 14: | /// Description of FPrimHash. | |
| 15: | /// </summary> | |
| 16: | public class FPrimHash | |
| 17: | { | |
| 18: | static int _hashTop; | |
| 19: | ArrayList _funcaoPrim = new ArrayList(); | |
| 20: | FPrimHash first; | |
| 21: | FPrimHash next; | |
| 22: | ||
| 23: | public FPrimHash() | |
| 24: | { | |
| 25: | ||
| 26: | } | |
| 27: | ||
| 28: | public static FPrimHash Open(int limite) | |
| 29: | { | |
| 30: | FPrimHash f = new FPrimHash(); | |
| 31: | f.first=f; | |
| 32: | for (int i=0;i<limite;i++) | |
| 33: | { | |
| 34: | f.next=new FPrimHash(); | |
| 35: | f.next.first=f.first; | |
| 36: | f=f.next; | |
| 37: | } | |
| 38: | return f.first; | |
| 39: | } | |
| 40: | ||
| 41: | public FPrimHash(string listaFprimTxt,int hashTop) | |
| 42: | { | |
| 43: | _hashTop = hashTop; | |
| 44: | first = Load(listaFprimTxt); | |
| 45: | } | |
| 46: | ||
| 47: | public FPrimHash Load(string funcoesTxt) | |
| 48: | { | |
| 49: | FPrimHash f = FPrimHash.Open(_hashTop); | |
| 50: | FPrim aux = new FPrim(); | |
| 51: | StreamReader sr = new StreamReader(funcoesTxt); | |
| 52: | string funcaoStr; | |
| 53: | f.first=f; | |
| 54: | while ((funcaoStr = sr.ReadLine()) != null) | |
| 55: | { | |
| 56: | aux = LoadLine(funcaoStr); | |
| 57: | f=f.Add(aux,Hash(aux._nome,_hashTop)); | |
| 58: | f=f.next; | |
| 59: | } | |
| 60: | return f.first; | |
| 61: | } | |
| 62: | ||
| 63: | public FPrimHash Add(FPrim fPrim,int posicao) | |
| 64: | { | |
| 65: | int i = 0; | |
| 66: | FPrimHash fh = first; | |
| 67: | while(i < posicao) | |
| 68: | { | |
| 69: | fh = fh.next; | |
| 70: | i ++; | |
| 71: | } | |
| 72: | fh._funcaoPrim.Add(fPrim); // NOTE: este o Add do arraylist! | |
| 73: | return fh; | |
| 74: | } | |
| 75: | ||
| 76: | public FPrim LoadLine(string line) | |
| 77: | { | |
| 78: | FPrim fprim = new FPrim(); | |
| 79: | string aux=""; | |
| 80: | int i = 0; | |
| 81: | while(line.Substring(i,1)!=" ") | |
| 82: | { | |
| 83: | aux+=line.Substring(i,1); | |
| 84: | i++; | |
| 85: | } | |
| 86: | fprim._nome = aux; | |
| 87: | i++; | |
| 88: | aux=""; | |
| 89: | while(i < line.Length) | |
| 90: | { | |
| 91: | aux+=line.Substring(i,1); | |
| 92: | i++; | |
| 93: | } | |
| 94: | fprim._aridade = Convert.ToInt32(aux); | |
| 95: | return fprim; | |
| 96: | } | |
| 97: | ||
| 98: | public bool QueryBool(string fprim) | |
| 99: | { | |
| 100: | FPrimHash fh = first; | |
| 101: | int posicao = FPrimHash.Hash(fprim,_hashTop); | |
| 102: | int i = 0; | |
| 103: | while (i<posicao) | |
| 104: | { | |
| 105: | fh = fh.next; | |
| 106: | i++; | |
| 107: | } | |
| 108: | int range = fh._funcaoPrim.Count; | |
| 109: | int j = 0; | |
| 110: | FPrim fp = new FPrim(); | |
| 111: | while(j<range) | |
| 112: | { | |
| 113: | fp = (FPrim)fh._funcaoPrim[j]; | |
| 114: | if (fp._nome == fprim) | |
| 115: | return true; | |
| 116: | j++; | |
| 117: | } | |
| 118: | return false; | |
| 119: | } | |
| 120: | ||
| 121: | ||
| 122: | public FPrim GetFPrim(string fprim) | |
| 123: | { | |
| 124: | FPrimHash fh = first; | |
| 125: | int posicao = FPrimHash.Hash(fprim,_hashTop); | |
| 126: | int i = 0; | |
| 127: | while (i<posicao) | |
| 128: | { | |
| 129: | fh = fh.next; | |
| 130: | i++; | |
| 131: | } | |
| 132: | int range = fh._funcaoPrim.Count; | |
| 133: | int j = 0; | |
| 134: | FPrim fp = new FPrim(); | |
| 135: | while(j<range) | |
| 136: | { | |
| 137: | fp = (FPrim)fh._funcaoPrim[j]; | |
| 138: | if (fp._nome == fprim) | |
| 139: | return fp; | |
| 140: | j++; | |
| 141: | } | |
| 142: | return null; | |
| 143: | } | |
| 144: | ||
| 145: | public static int Hash(string buf,int limite) | |
| 146: | { | |
| 147: | int num; | |
| 148: | num=buf.GetHashCode(); | |
| 149: | if (num > 0 ) | |
| 150: | return num%limite; | |
| 151: | else | |
| 152: | return -(num%limite); | |
| 153: | } | |
| 154: | ||
| 155: | public void Imprime() | |
| 156: | { | |
| 157: | FPrimHash fh = first; | |
| 158: | int i = 0; | |
| 159: | int j; | |
| 160: | FPrim fp = new FPrim(); | |
| 161: | ||
| 162: | while (i<_hashTop) | |
| 163: | { | |
| 164: | j = 0; | |
| 165: | Console.Write(i); | |
| 166: | Console.Write(" "); | |
| 167: | int range = fh._funcaoPrim.Count; | |
| 168: | while (j<range) | |
| 169: | { | |
| 170: | fp =(FPrim) fh._funcaoPrim[j]; | |
| 171: | Console.WriteLine(fp._nome); | |
| 172: | j++; | |
| 173: | } | |
| 174: | ||
| 175: | if(j==0) | |
| 176: | Console.WriteLine("VAZIO"); | |
| 177: | fh = fh.next; | |
| 178: | i++; | |
| 179: | } | |
| 180: | } | |
| 181: | ||
| 182: | } | |
| 183: | } | |
| 184: | ||
| 185: |
This page was automatically generated by SharpDevelop.