perfhlib.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. *$Log: perfhlib.c,v $
  3. * Revision 1.5 93/09/29 14:45:02 emmerik
  4. * Oops, didn't do the casts last check in
  5. *
  6. * Revision 1.4 93/09/29 14:41:45 emmerik
  7. * Added casts to mod instructions to keep the SVR4 compiler happy
  8. *
  9. *
  10. * Perfect hashing function library. Contains functions to generate perfect
  11. * hashing functions
  12. */
  13. #include "perfhlib.h"
  14. #include "PatternCollector.h"
  15. #include "msvc_fixes.h"
  16. #include <stdio.h>
  17. #include <cassert>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /* Private data structures */
  21. //static int NumEntry; /* Number of entries in the hash table (# keys) */
  22. //static int EntryLen; /* Size (bytes) of each entry (size of keys) */
  23. //static int SetSize; /* Size of the char set */
  24. //static char SetMin; /* First char in the set */
  25. //static int NumVert; /* c times NumEntry */
  26. //static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
  27. static uint16_t *T1, *T2; /* Pointers to T1[i], T2[i] */
  28. static int *graphNode; /* The array of edges */
  29. static int *graphNext; /* Linked list of edges */
  30. static int *graphFirst;/* First edge at a vertex */
  31. static int numEdges; /* An edge counter */
  32. static bool *visited; /* Array of bools: whether visited */
  33. static bool *deleted; /* Array of bools: whether deleted */
  34. /* Private prototypes */
  35. static void duplicateKeys(int v1, int v2);
  36. void PerfectHash::setHashParams(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,
  37. int _NumVert)
  38. {
  39. /* These parameters are stored in statics so as to obviate the need for
  40. passing all these (or defererencing pointers) for every call to hash()
  41. */
  42. NumEntry = _NumEntry;
  43. EntryLen = _EntryLen;
  44. SetSize = _SetSize;
  45. SetMin = _SetMin;
  46. NumVert = _NumVert;
  47. /* Allocate the variable sized tables etc */
  48. if ((T1base = (uint16_t *)malloc(EntryLen * SetSize * sizeof(uint16_t))) == 0)
  49. {
  50. goto BadAlloc;
  51. }
  52. if ((T2base = (uint16_t *)malloc(EntryLen * SetSize * sizeof(uint16_t))) == 0)
  53. {
  54. goto BadAlloc;
  55. }
  56. if ((graphNode = (int *)malloc((NumEntry*2 + 1) * sizeof(int))) == 0)
  57. {
  58. goto BadAlloc;
  59. }
  60. if ((graphNext = (int *)malloc((NumEntry*2 + 1) * sizeof(int))) == 0)
  61. {
  62. goto BadAlloc;
  63. }
  64. if ((graphFirst = (int *)malloc((NumVert + 1) * sizeof(int))) == 0)
  65. {
  66. goto BadAlloc;
  67. }
  68. if ((g = (short *)malloc((NumVert+1) * sizeof(short))) == 0)
  69. {
  70. goto BadAlloc;
  71. }
  72. if ((visited = (bool *)malloc((NumVert+1) * sizeof(bool))) == 0)
  73. {
  74. goto BadAlloc;
  75. }
  76. if ((deleted = (bool *)malloc((NumEntry+1) * sizeof(bool))) == 0)
  77. {
  78. goto BadAlloc;
  79. }
  80. return;
  81. BadAlloc:
  82. printf("Could not allocate memory\n");
  83. hashCleanup();
  84. exit(1);
  85. }
  86. void PerfectHash::hashCleanup(void)
  87. {
  88. /* Free the storage for variable sized tables etc */
  89. if (T1base) free(T1base);
  90. if (T2base) free(T2base);
  91. if (graphNode) free(graphNode);
  92. if (graphNext) free(graphNext);
  93. if (graphFirst) free(graphFirst);
  94. if (g) free(g);
  95. if (visited) free(visited);
  96. if (deleted) free(deleted);
  97. }
  98. void PerfectHash::map(PatternCollector *collector)
  99. {
  100. m_collector = collector;
  101. assert(nullptr!=collector);
  102. int i, j, c;
  103. uint16_t f1, f2;
  104. bool cycle;
  105. uint8_t *keys;
  106. c = 0;
  107. do
  108. {
  109. initGraph();
  110. cycle = false;
  111. /* Randomly generate T1 and T2 */
  112. for (i=0; i < SetSize*EntryLen; i++)
  113. {
  114. T1base[i] = rand() % NumVert;
  115. T2base[i] = rand() % NumVert;
  116. }
  117. for (i=0; i < NumEntry; i++)
  118. {
  119. f1 = 0; f2 = 0;
  120. keys = m_collector->getKey(i);
  121. for (j=0; j < EntryLen; j++)
  122. {
  123. T1 = T1base + j * SetSize;
  124. T2 = T2base + j * SetSize;
  125. f1 += T1[keys[j] - SetMin];
  126. f2 += T2[keys[j] - SetMin];
  127. }
  128. f1 %= (uint16_t)NumVert;
  129. f2 %= (uint16_t)NumVert;
  130. if (f1 == f2)
  131. {
  132. /* A self loop. Reject! */
  133. printf("Self loop on vertex %d!\n", f1);
  134. cycle = true;
  135. break;
  136. }
  137. addToGraph(numEdges++, f1, f2);
  138. }
  139. if (cycle or (cycle = isCycle())) /* OK - is there a cycle? */
  140. {
  141. printf("Iteration %d\n", ++c);
  142. }
  143. else
  144. {
  145. break;
  146. }
  147. }
  148. while (/* there is a cycle */ 1);
  149. }
  150. /* Initialise the graph */
  151. void PerfectHash::initGraph()
  152. {
  153. int i;
  154. for (i=1; i <= NumVert; i++)
  155. {
  156. graphFirst[i] = 0;
  157. }
  158. for (i= -NumEntry; i <= NumEntry; i++)
  159. {
  160. /* No need to init graphNode[] as they will all be filled by successive
  161. calls to addToGraph() */
  162. graphNext[NumEntry+i] = 0;
  163. }
  164. numEdges = 0;
  165. }
  166. /* Add an edge e between vertices v1 and v2 */
  167. /* e, v1, v2 are 0 based */
  168. void PerfectHash::addToGraph(int e, int v1, int v2)
  169. {
  170. e++; v1++; v2++; /* So much more convenient */
  171. graphNode[NumEntry+e] = v2; /* Insert the edge information */
  172. graphNode[NumEntry-e] = v1;
  173. graphNext[NumEntry+e] = graphFirst[v1]; /* Insert v1 to list of alphas */
  174. graphFirst[v1]= e;
  175. graphNext[NumEntry-e] = graphFirst[v2]; /* Insert v2 to list of omegas */
  176. graphFirst[v2]= -e;
  177. }
  178. bool PerfectHash::DFS(int parentE, int v)
  179. {
  180. int e, w;
  181. /* Depth first search of the graph, starting at vertex v, looking for
  182. cycles. parent and v are origin 1. Note parent is an EDGE,
  183. not a vertex */
  184. visited[v] = true;
  185. /* For each e incident with v .. */
  186. for (e = graphFirst[v]; e; e = graphNext[NumEntry+e])
  187. {
  188. uint8_t *key1;
  189. if (deleted[abs(e)])
  190. {
  191. /* A deleted key. Just ignore it */
  192. continue;
  193. }
  194. key1 = m_collector->getKey(abs(e)-1);
  195. w = graphNode[NumEntry+e];
  196. if (visited[w])
  197. {
  198. /* Did we just come through this edge? If so, ignore it. */
  199. if (abs(e) != abs(parentE))
  200. {
  201. /* There is a cycle in the graph. There is some subtle code here
  202. to work around the distinct possibility that there may be
  203. duplicate keys. Duplicate keys will always cause unit
  204. cycles, since f1 and f2 (used to select v and w) will be the
  205. same for both. The edges (representing an index into the
  206. array of keys) are distinct, but the key values are not.
  207. The logic is as follows: for the candidate edge e, check to
  208. see if it terminates in the parent vertex. If so, we test
  209. the keys associated with e and the parent, and if they are
  210. the same, we can safely ignore e for the purposes of cycle
  211. detection, since edge e adds nothing to the cycle. Cycles
  212. involving v, w, and e0 will still be found. The parent
  213. edge was not similarly eliminated because at the time when
  214. it was a candidate, v was not yet visited.
  215. We still have to remove the key from further consideration,
  216. since each edge is visited twice, but with a different
  217. parent edge each time.
  218. */
  219. /* We save some stack space by calculating the parent vertex
  220. for these relatively few cases where it is needed */
  221. int parentV = graphNode[NumEntry-parentE];
  222. if (w == parentV)
  223. {
  224. uint8_t *key2;
  225. key2=m_collector->getKey(abs(parentE)-1);
  226. if (memcmp(key1, key2, EntryLen) == 0)
  227. {
  228. printf("Duplicate keys with edges %d and %d (",
  229. e, parentE);
  230. m_collector->dispKey(abs(e)-1);
  231. printf(" & ");
  232. m_collector->dispKey(abs(parentE)-1);
  233. printf(")\n");
  234. deleted[abs(e)] = true; /* Wipe the key */
  235. }
  236. else
  237. {
  238. /* A genuine (unit) cycle. */
  239. printf("There is a unit cycle involving vertex %d and edge %d\n", v, e);
  240. return true;
  241. }
  242. }
  243. else
  244. {
  245. /* We have reached a previously visited vertex not the
  246. parent. Therefore, we have uncovered a genuine cycle */
  247. printf("There is a cycle involving vertex %d and edge %d\n", v, e);
  248. return true;
  249. }
  250. }
  251. }
  252. else /* Not yet seen. Traverse it */
  253. {
  254. if (DFS(e, w))
  255. {
  256. /* Cycle found deeper down. Exit */
  257. return true;
  258. }
  259. }
  260. }
  261. return false;
  262. }
  263. bool PerfectHash::isCycle(void)
  264. {
  265. int v, e;
  266. for (v=1; v <= NumVert; v++)
  267. {
  268. visited[v] = false;
  269. }
  270. for (e=1; e <= NumEntry; e++)
  271. {
  272. deleted[e] = false;
  273. }
  274. for (v=1; v <= NumVert; v++)
  275. {
  276. if (not visited[v])
  277. {
  278. if (DFS(-32767, v))
  279. {
  280. return true;
  281. }
  282. }
  283. }
  284. return false;
  285. }
  286. void PerfectHash::traverse(int u)
  287. {
  288. int w, e;
  289. visited[u] = true;
  290. /* Find w, the neighbours of u, by searching the edges e associated with u */
  291. e = graphFirst[1+u];
  292. while (e)
  293. {
  294. w = graphNode[NumEntry+e]-1;
  295. if (!visited[w])
  296. {
  297. g[w] = (abs(e)-1 - g[u]) % NumEntry;
  298. if (g[w] < 0) g[w] += NumEntry; /* Keep these positive */
  299. traverse(w);
  300. }
  301. e = graphNext[NumEntry+e];
  302. }
  303. }
  304. void PerfectHash::assign(void)
  305. {
  306. int v;
  307. for (v=0; v < NumVert; v++)
  308. {
  309. g[v] = 0; /* g is sparse; leave the gaps 0 */
  310. visited[v] = false;
  311. }
  312. for (v=0; v < NumVert; v++)
  313. {
  314. if (!visited[v])
  315. {
  316. g[v] = 0;
  317. traverse(v);
  318. }
  319. }
  320. }
  321. int PerfectHash::hash(uint8_t *string)
  322. {
  323. uint16_t u, v;
  324. int j;
  325. u = 0;
  326. for (j=0; j < EntryLen; j++)
  327. {
  328. T1 = T1base + j * SetSize;
  329. u += T1[string[j] - SetMin];
  330. }
  331. u %= NumVert;
  332. v = 0;
  333. for (j=0; j < EntryLen; j++)
  334. {
  335. T2 = T2base + j * SetSize;
  336. v += T2[string[j] - SetMin];
  337. }
  338. v %= NumVert;
  339. return (g[u] + g[v]) % NumEntry;
  340. }
  341. #if 0
  342. void dispRecord(int i);
  343. void
  344. duplicateKeys(int v1, int v2)
  345. {
  346. int i, j;
  347. uint8_t *keys;
  348. int u, v;
  349. v1--; v2--; /* These guys are origin 1 */
  350. printf("Duplicate keys:\n");
  351. for (i=0; i < NumEntry; i++)
  352. {
  353. getKey(i, &keys);
  354. u = 0;
  355. for (j=0; j < EntryLen; j++)
  356. {
  357. T1 = T1base + j * SetSize;
  358. u += T1[keys[j] - SetMin];
  359. }
  360. u %= NumVert;
  361. if ((u != v1) and (u != v2)) continue;
  362. v = 0;
  363. for (j=0; j < EntryLen; j++)
  364. {
  365. T2 = T2base + j * SetSize;
  366. v += T2[keys[j] - SetMin];
  367. }
  368. v %= NumVert;
  369. if ((v == v2) or (v == v1))
  370. {
  371. printf("Entry #%d key: ", i+1);
  372. for (j=0; j < EntryLen; j++) printf("%02X ", keys[j]);
  373. printf("\n");
  374. dispRecord(i+1);
  375. }
  376. }
  377. exit(1);
  378. }
  379. #endif