home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / src / pl-table.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  4KB  |  193 lines

  1. /*  pl-table.c,v 1.1.1.1 1992/05/26 11:52:27 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: generic support for (numeric) hashTables
  8. */
  9.  
  10. #include "pl-incl.h"
  11.  
  12. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. Anonymous hash-tables.  This  is  rather  straightforward.   A  peculiar
  14. thing  on these and a number of dedicated hashtables build in the system
  15. is that the last symbol of  a  specific  hash  value  is  not  the  NULL
  16. pointer,  but  a reference pointer to the next entry.  This allows us to
  17. enumerate  all  entries  of  the  table  with  only  one   word   status
  18. information.   This  is  more  efficient  and  simple to handle with the
  19. interface for non-deterministic C functions (current_predicate/2, ...).
  20.  
  21. This module also can allocate from the local stack for temporary  tables
  22. needed by foreign language functions.
  23. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  24.  
  25. Table
  26. newHTable(size)
  27. int size;
  28. { Symbol *p;
  29.   int n;
  30.   Table ht;
  31.  
  32.   DEBUG(9, printf("Creating hash table (size=%d)\n", size));
  33.   ht = (Table) allocHeap(sizeof(struct table) + (size - 1) * sizeof(Symbol));
  34.   DEBUG(9, printf("Allocated hash table\n"));
  35.   ht->size = size;
  36.  
  37.   for(n=0, p = &ht->entries[0]; n < size-1; n++, p++)
  38.     *p = (Symbol) makeRef(p+1);
  39.   *p = (Symbol) NULL;
  40.  
  41.   DEBUG(9, printf("Returning ht=%ld\n", ht));
  42.   return ht;
  43. }
  44.  
  45. Symbol
  46. lookupHTable(ht, name)
  47. Table ht;
  48. Void name;
  49. { register Symbol s = ht->entries[pointerHashValue(name, ht->size)];
  50.  
  51.   DEBUG(9, printf("lookupHTable(%ld, %ld) --> ", ht, name));
  52.   for(;s && !isRef((word)s); s = s->next)
  53.     if (s->name == (word)name)
  54.     { DEBUG(9, printf("Symbol=%ld, value=%ld\n", s, s->value));
  55.       return s;
  56.     }
  57.  
  58.   DEBUG(9, printf("Symbol = NULL\n"));
  59.   return (Symbol) NULL;
  60. }
  61.  
  62. bool
  63. addHTable(ht, name, value)
  64. Table ht;
  65. Void name;
  66. Void value;
  67. { register Symbol s;
  68.   register int v = pointerHashValue(name, ht->size);
  69.  
  70.   DEBUG(9, printf("addHTable(%ld, %ld, %ld) ... ", ht, name, value));
  71.   if (lookupHTable(ht, name) != (Symbol) NULL)
  72.     fail;
  73.   s = (Symbol) allocHeap(sizeof(struct symbol));
  74.   s->name = (word)name;
  75.   s->value = (word)value;
  76.   s->next = ht->entries[v];
  77.   ht->entries[v] = s;
  78.  
  79.   DEBUG(9, printf("ok\n"));
  80.   succeed;
  81. }  
  82.  
  83. bool
  84. deleteHTable(ht, name)
  85. Table ht;
  86. Void name;
  87. { register int v = pointerHashValue(name, ht->size);
  88.   register Symbol *s = &ht->entries[v];
  89.   Symbol symb = *s;
  90.  
  91.   for(;symb && !isRef((word)symb); s = &symb->next)
  92.   { symb = *s;
  93.     if (symb->name == (word)name)
  94.     { *s = symb->next;
  95.       freeHeap(symb, sizeof(struct symbol));
  96.       succeed;
  97.     }
  98.   }
  99.  
  100.   fail;
  101. }
  102.  
  103. Symbol
  104. nextHTable(ht, s)
  105. Table ht;
  106. register Symbol s;
  107. { s = s->next;
  108.   while(s != (Symbol) NULL && isRef((word)s) )
  109.     s = *((Symbol *)unRef(s));
  110.  
  111.   return s;
  112. }
  113.  
  114. Symbol
  115. firstHTable(ht)
  116. Table ht;
  117. { register Symbol s = ht->entries[0];
  118.  
  119.   while(s != (Symbol) NULL && isRef((word)s) )
  120.     s = *((Symbol *)unRef(s));
  121.  
  122.   return s;
  123. }  
  124.  
  125. void
  126. clearHTable(ht)
  127. Table ht;
  128. { int n;
  129.   register Symbol s;
  130.  
  131.   for(n=0; n < ht->size; n++)
  132.   { s = ht->entries[n];
  133.     while(s && !isRef((word)s))
  134.     { register Symbol q = s->next;
  135.       freeHeap(s, sizeof(struct symbol));
  136.       s = q;
  137.     }
  138.     ht->entries[n] = s;
  139.   }
  140. }
  141.  
  142.         /********************************
  143.         *     TABLES ON LOCAL STACK     *
  144.         *********************************/
  145.  
  146. Table
  147. newLocalTable(size)
  148. int size;
  149. { Symbol *p;
  150.   int n;
  151.   Table ht;
  152.  
  153.   ht = (Table) allocLocal(sizeof(struct table) + (size - 1) * sizeof(Symbol));
  154.   ht->size = size;
  155.  
  156.   for(n=0, p = &ht->entries[0]; n < size; n++, p++)
  157.     *p = (Symbol) NULL;
  158.  
  159.   return ht;
  160. }
  161.  
  162. Symbol
  163. lookupLocalTable(ht, name)
  164. Table ht;
  165. Void name;
  166. { register Symbol s = ht->entries[pointerHashValue(name, ht->size)];
  167.  
  168.   for( ; s; s = s->next )
  169.     if ( s->name == (word)name )
  170.       return s;
  171.  
  172.   return NULL;
  173. }
  174.  
  175. bool
  176. addLocalTable(ht, name, value)
  177. Table ht;
  178. Void name;
  179. Void value;
  180. { register Symbol s;
  181.   register int v = pointerHashValue(name, ht->size);
  182.  
  183.   if (lookupLocalTable(ht, name) != (Symbol) NULL)
  184.     fail;
  185.   s = (Symbol) allocLocal(sizeof(struct symbol));
  186.   s->name = (word)name;
  187.   s->value = (word)value;
  188.   s->next = ht->entries[v];
  189.   ht->entries[v] = s;
  190.  
  191.   succeed;
  192. }
  193.