source: trunk/pin/include/p2phash.ch@ 6

Last change on this file since 6 was 1, checked in by bart, 18 years ago

Initial checkin of PIN.EXE source code
this contains memory fixes for larger PPD files

File size: 4.6 KB
Line 
1// included into convert.c
2// hash functions
3// something that just works and should not be looked at
4
5
6
7/*****************************************************************************\
8**
9** FUNCTION = MakeHashKey
10**
11** DESCRIPTION = Given an inpust psz string will create a ulong "key"
12** The key is generated viewing the string as seires of ulongs
13** and add them up.
14**
15\*****************************************************************************/
16
17ULONG MakeHashKey( PSZ pszValue )
18{
19 int i;
20 int iLen;
21 int iLongs;
22 int iRem;
23 ULONG ulKey = 0;
24 PULONG pulL;
25
26 iLen = strlen( pszValue ); /* length of input strings */
27 iLongs = iLen / sizeof( ULONG ); /* number of longs in string */
28 iRem = iLen % sizeof( ULONG ); /* bytes left over */
29 pulL = (PULONG)pszValue;
30
31 if ( iLongs ) /* if any longs */
32 {
33 for ( i = 0; i < iLongs; i ++ ) /* for each long in string */
34 {
35 ulKey += *pulL; /* add together */
36 pulL++;
37 }
38 }
39
40 switch ( iRem ) /* Switch on remainder bytes(s) */
41 { /* Need to ignore extra bytes of last word */
42 case 0:
43 break;
44 case 1:
45 ulKey += *pulL & 0x000000FF;
46 break;
47 case 2:
48 ulKey += *pulL & 0x0000FFFF;
49 break;
50 case 3:
51 ulKey += *pulL & 0x00FFFFFF;
52 }
53
54 return ulKey;
55
56}
57
58
59/*****************************************************************************\
60**
61** FUNCTION = AddHashEntry
62**
63** DESCRIPTION = Adds entry to hash table
64**
65\*****************************************************************************/
66
67VOID AddHashEntry( LONG lSlot, PWORDELEMENT pW )
68{
69 PWORDELEMENT pWOld;
70
71 /*
72 ** If no entrys for slot just put in
73 */
74 if ( aHashTable[ lSlot ] == NULL )
75 {
76 aHashTable[ lSlot ] = pW;
77 return;
78 }
79
80 /*
81 ** Someone in slot, add to end chain
82 */
83
84 pWOld = aHashTable[ lSlot ];
85
86 while ( pWOld->pNext ) /* Loop through to end of chain */
87 {
88 pWOld = pWOld->pNext;
89 }
90
91 pWOld->pNext = pW;
92
93 return;
94
95}
96
97
98/*****************************************************************************\
99**
100** FUNCTION = BuildHashTable
101**
102** DESCRIPTION = Builds an array of pointers one for each word
103**
104\*****************************************************************************/
105
106VOID BuildHashTable( VOID )
107{
108 int i, j, k, iCount, iAdjust;
109 PWORDELEMENT pW;
110 LONG lSlot;
111
112 /* Zero out hash table */
113 memset( aHashTable, 0, sizeof( aHashTable ) );
114
115 iCount = 0;
116 for ( i = 0; i < PSLISTCOUNT; i++ )
117 {
118 iCount += sListSize[ i ];
119 }
120
121 pPSKeyWords = (PWORDELEMENT)malloc( iCount * sizeof( WORDELEMENT ) );
122
123 pW = pPSKeyWords;
124
125 k = 0;
126 iAdjust = 128;
127 for ( i = 0; i < PSLISTCOUNT; i++ ) /* For each list */
128 {
129 for ( j = 0; j < sListSize[ i ]; j++ ) /* For each element in the list */
130 {
131 pW->pszWord = achPSKeyWords + sPSKeyWordOffset[ k ];
132 pW->sList = i;
133 pW->pNext = NULL;
134
135 /*
136 ** For the first 127 (0-126) add 128 to element ( sets high bit )
137 ** for 127 to 254 subtract 126 ( 128 - 254 = 126 )
138 ** Cant use 0 or 255 as index since they have special meaning
139 */
140 pW->sIndex = k + iAdjust;
141
142 lSlot = MakeHashKey( pW->pszWord ) % HASHSLOTS ;
143
144 AddHashEntry( lSlot, pW );
145
146 k++;
147 pW++;
148 }
149
150 iAdjust -= 254;
151 }
152
153/* Debug area ****************/
154#if 0
155///*
156//** Dump out list
157//*/
158//printf( "KeyWord structures\n" );
159//pW = pPSKeyWords;
160//for ( i = 0; i < iCount; i++ )
161//{
162// printf( "%3.3d, %2.2d, %s\n", pW->sIndex, pW->sList, pW->pszWord );
163// pW++;
164//}
165//
166///*
167//** Dump out hash table
168//*/
169//printf( "Hash table\n" );
170//for ( i = 0; i < HASHSLOTS; i++ )
171//{
172// pW = aHashTable[ i ];
173//
174// printf("Slot %4.4d ",i);
175// while( pW )
176// {
177// printf( "%s ",pW->pszWord );
178// pW = pW->pNext;
179// }
180// printf( "\n" );
181//}
182#endif
183}
184
185
186/*****************************************************************************\
187**
188** FUNCTION = SearchHashTable
189**
190** DESCRIPTION = Searches hash table for match
191**
192\*****************************************************************************/
193
194PWORDELEMENT SearchHashTable( PSZ pszString )
195{
196 PWORDELEMENT pW;
197 ULONG ulKey;
198
199 ulKey = MakeHashKey( pszString ) % HASHSLOTS; /* Make key from input string */
200
201 pW = aHashTable[ ulKey ];
202
203 while ( pW ) /* Loop thru list */
204 {
205 if ( strcmp( pszString, pW->pszWord ) == 0 )
206 {
207 break; /* If match break */
208 }
209
210 pW = pW->pNext;
211 }
212
213 return pW;
214}
215
216/* @V3.0CMPS01 end */
217
Note: See TracBrowser for help on using the repository browser.