home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dccsrcoo.zip / perfhlib.cpp < prev    next >
C/C++ Source or Header  |  1997-04-09  |  11KB  |  459 lines

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