home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / vhset.ccp < prev    next >
Text File  |  1993-07-23  |  6KB  |  265 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #include "<T>.VHSet.h"
  25.  
  26. /* codes for status fields */
  27.  
  28. #define EMPTYCELL   0
  29. #define VALIDCELL   1
  30. #define DELETEDCELL 2
  31.  
  32.  
  33. <T>VHSet::<T>VHSet(int sz = DEFAULT_INITIAL_CAPACITY)
  34. {
  35.   tab = new <T>[size = sz];
  36.   status = new char[size];
  37.   for (int i = 0; i < size; ++i) status[i] = EMPTYCELL;
  38.   count = 0;
  39. }
  40.  
  41. <T>VHSet::<T>VHSet(<T>VHSet& a)
  42. {
  43.   tab = new <T>[size = a.size];
  44.   status = new char[size];
  45.   for (int i = 0; i < size; ++i) status[i] = EMPTYCELL;
  46.   count = 0;
  47.   for (Pix p = a.first(); p; a.next(p)) add(a(p));
  48. }
  49.  
  50.  
  51. /* 
  52.  * hashing method: double hash based on high bits of hash fct,
  53.  * followed by linear probe. Can't do too much better if table
  54.  * sizes not constrained to be prime.
  55. */
  56.  
  57.  
  58. static inline doublehashinc(unsigned int h, int s)
  59. {
  60.   return ((h / s) % s) >? 1;
  61. }
  62.  
  63. Pix <T>VHSet::seek(<T&> key)
  64. {
  65.   unsigned int hashval = <T>HASH(key);
  66.   int h = hashval % size;
  67.   for (int i = 0; i <= size; ++i)
  68.   {
  69.     if (status[h] == EMPTYCELL)
  70.       return 0;
  71.     else if (status[h] == VALIDCELL && <T>EQ(key, tab[h]))
  72.       return Pix(&tab[h]);
  73.     if (i == 0)
  74.       h = (h + doublehashinc(hashval, size)) % size;
  75.     else if (++h >= size)
  76.       h -= size;
  77.   }
  78.   return 0;
  79. }
  80.  
  81.  
  82. Pix <T>VHSet::add(<T&> item)
  83. {
  84.   if (size <= count + 1)
  85.     resize();
  86.  
  87.   int bestspot = -1;
  88.   unsigned int hashval = <T>HASH(item);
  89.   int h = hashval % size;
  90.   for (int i = 0; i <= size; ++i)
  91.   {
  92.     if (status[h] == EMPTYCELL)
  93.     {
  94.       if (bestspot < 0) bestspot = h;
  95.       tab[bestspot] = item;
  96.       status[bestspot] = VALIDCELL;
  97.       ++count;
  98.       return Pix(&tab[bestspot]);
  99.     }
  100.     else if (status[h] == DELETEDCELL)
  101.     {
  102.       if (bestspot < 0) bestspot = h;
  103.     }
  104.     else if (<T>EQ(tab[h],item))
  105.       return Pix(&tab[h]);
  106.  
  107.     if (i == 0)
  108.       h = (h + doublehashinc(hashval, size)) % size;
  109.     else if (++h >= size)
  110.       h -= size;
  111.   }
  112.   tab[bestspot] = item;
  113.   status[bestspot] = VALIDCELL;
  114.   ++count;
  115.   return Pix(&tab[bestspot]);
  116.  
  117. }
  118.  
  119.  
  120. void <T>VHSet::del(<T&> key)
  121. {
  122.   unsigned int hashval = <T>HASH(key);
  123.   int h = hashval % size;
  124.   for (int i = 0; i <= size; ++i)
  125.   {
  126.     if (status[h] == EMPTYCELL)
  127.       return;
  128.     else if (status[h] == VALIDCELL && <T>EQ(key, tab[h]))
  129.     {
  130.       status[h] = DELETEDCELL;
  131.       --count;
  132.       return;
  133.     }
  134.     if (i == 0)
  135.       h = (h + doublehashinc(hashval, size)) % size;
  136.     else if (++h >= size)
  137.       h -= size;
  138.   }
  139. }
  140.  
  141.  
  142. void <T>VHSet::clear()
  143. {
  144.   for (int i = 0; i < size; ++i) status[i] = EMPTYCELL;
  145.   count = 0;
  146. }
  147.  
  148. void <T>VHSet::resize(int newsize = 0)
  149. {
  150.   if (newsize <= count)
  151.   {
  152.     newsize = DEFAULT_INITIAL_CAPACITY;
  153.     while (newsize <= count) newsize <<= 1;
  154.   }
  155.   <T>* oldtab = tab;
  156.   char* oldstatus = status;
  157.   int oldsize = size;
  158.   tab = new <T>[size = newsize];
  159.   status = new char[size];
  160.   for (int i = 0; i < size; ++i) status[i] = EMPTYCELL;
  161.   count = 0;
  162.   for (i = 0; i < oldsize; ++i) if (oldstatus[i] == VALIDCELL) add(oldtab[i]);
  163.   delete [oldsize] oldtab;
  164.   delete oldstatus;
  165. }
  166.  
  167. Pix <T>VHSet::first()
  168. {
  169.   for (int pos = 0; pos < size; ++pos)
  170.     if (status[pos] == VALIDCELL) return Pix(&tab[pos]);
  171.   return 0;
  172. }
  173.  
  174. void <T>VHSet::next(Pix& i)
  175. {
  176.   if (i == 0) return;
  177.   int pos = ((unsigned)i - (unsigned)tab) / sizeof(<T>) + 1;
  178.   for (; pos < size; ++pos)
  179.     if (status[pos] == VALIDCELL)
  180.     {
  181.       i = Pix(&tab[pos]);
  182.       return;
  183.     }
  184.   i = 0;
  185. }
  186.   
  187. int <T>VHSet:: operator == (<T>VHSet& b)
  188. {
  189.   if (count != b.count)
  190.     return 0;
  191.   else
  192.   {
  193.     for (int i = 0; i < size; ++i)
  194.       if (status[i] == VALIDCELL && b.seek(tab[i]) == 0)
  195.           return 0;
  196.     for (i = 0; i < b.size; ++i)
  197.       if (b.status[i] == VALIDCELL && seek(b.tab[i]) == 0)
  198.           return 0;
  199.     return 1;
  200.   }
  201. }
  202.  
  203. int <T>VHSet::operator <= (<T>VHSet& b)
  204. {
  205.   if (count > b.count)
  206.     return 0;
  207.   else
  208.   {
  209.     for (int i = 0; i < size; ++i)
  210.       if (status[i] == VALIDCELL && b.seek(tab[i]) == 0)
  211.           return 0;
  212.     return 1;
  213.   }
  214. }
  215.  
  216. void <T>VHSet::operator |= (<T>VHSet& b)
  217. {
  218.   if (&b == this || b.count == 0)
  219.     return;
  220.   for (int i = 0; i < b.size; ++i)
  221.     if (b.status[i] == VALIDCELL) add(b.tab[i]);
  222. }
  223.  
  224. void <T>VHSet::operator &= (<T>VHSet& b)
  225. {
  226.   if (&b == this || count == 0)
  227.     return;
  228.   for (int i = 0; i < size; ++i)
  229.   {
  230.     if (status[i] == VALIDCELL && b.seek(tab[i]) == 0)
  231.     {
  232.       status[i] = DELETEDCELL;
  233.       --count;
  234.     }
  235.   }
  236. }
  237.  
  238. void <T>VHSet::operator -= (<T>VHSet& b)
  239. {
  240.   for (int i = 0; i < size; ++i)
  241.   {
  242.     if (status[i] == VALIDCELL && b.seek(tab[i]) != 0)
  243.     {
  244.       status[i] = DELETEDCELL;
  245.       --count;
  246.     }
  247.   }
  248. }
  249.  
  250. int <T>VHSet::OK()
  251. {
  252.   int v = tab != 0;
  253.   v &= status != 0;
  254.   int n = 0;
  255.   for (int i = 0; i < size; ++i) 
  256.   {
  257.     if (status[i] == VALIDCELL) ++n;
  258.     else if (status[i] != DELETEDCELL && status[i] != EMPTYCELL)
  259.       v = 0;
  260.   }
  261.   v &= n == count;
  262.   if (!v) error("invariant failure");
  263.   return v;
  264. }
  265.