home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / gen / CHSet.ccP < prev    next >
Text File  |  1996-10-12  |  6KB  |  274 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include "<T>.CHSet.h"
  23.  
  24. // A CHSet is implemented as an array (tab) of buckets, each of which
  25. // contains a pointer to a list of <T>CHNodes.  Each node contains a
  26. // pointer to the next node in the list, and a pointer to the <T>.
  27. // The end of the list is marked by a next node pointer which is odd
  28. // when considered as an integer (least significant bit = 1).  The
  29. // assumption is that CHNodes will all begin on even addresses.  If
  30. // the odd pointer is right-shifted by one bit, it becomes the index
  31. // within the tab array of the next bucket (that is, bucket i has
  32. // next bucket pointer 2*(i+1)+1).
  33.  
  34. // The bucket pointers are initialized by the constructor and
  35. // used to support the next(Pix&) method.
  36.  
  37. // This implementation is not portable to machines with different
  38. // pointer and integer sizes, or on which CHNodes might be aligned on
  39. // odd byte boundaries, but allows the same pointer to be used for
  40. // chaining within a bucket and to the next bucket.
  41.  
  42.  
  43. static inline int goodCHptr(<T>CHNode* t)
  44. {
  45.   return ((((unsigned)t) & 1) == 0);
  46. }
  47.  
  48. static inline <T>CHNode* index_to_CHptr(int i)
  49. {
  50.   return (<T>CHNode*)((i << 1) + 1);
  51. }
  52.  
  53. static inline int CHptr_to_index(<T>CHNode* t)
  54. {
  55.   return ( ((unsigned) t) >> 1);
  56. }
  57.  
  58. <T>CHSet::<T>CHSet(unsigned int sz)
  59. {
  60.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
  61.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  62.   count = 0;
  63. }
  64.  
  65. <T>CHSet::<T>CHSet(<T>CHSet& a)
  66. {
  67.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
  68.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  69.   count = 0;
  70.   for (Pix p = a.first(); p; a.next(p)) add(a(p));
  71. }
  72.  
  73.  
  74. Pix <T>CHSet::seek(<T&> key)
  75. {
  76.   unsigned int h = <T>HASH(key) % size;
  77.  
  78.   for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  79.     if (<T>EQ(key, t->hd))
  80.       return Pix(t);
  81.  
  82.   return 0;
  83. }
  84.  
  85.  
  86. Pix <T>CHSet::add(<T&> item)
  87. {
  88.   unsigned int h = <T>HASH(item) % size;
  89.   <T>CHNode* t;
  90.  
  91.   for (t = tab[h]; goodCHptr(t); t = t->tl)
  92.     if (<T>EQ(item, t->hd))
  93.       return Pix(t);
  94.  
  95.   ++count;
  96.   t = new <T>CHNode(item, tab[h]);
  97.   tab[h] = t;
  98.   return Pix(t);
  99. }
  100.  
  101.  
  102. void <T>CHSet::del(<T&> key)
  103. {
  104.   unsigned int h = <T>HASH(key) % size;
  105.  
  106.   <T>CHNode* t = tab[h]; 
  107.   <T>CHNode* trail = t;
  108.   while (goodCHptr(t))
  109.   {
  110.     if (<T>EQ(key, t->hd))
  111.     {
  112.       if (trail == t)
  113.         tab[h] = t->tl;
  114.       else
  115.         trail->tl = t->tl;
  116.       delete t;
  117.       --count;
  118.       return;
  119.     }
  120.     trail = t;
  121.     t = t->tl;
  122.   }
  123. }
  124.  
  125.  
  126. void <T>CHSet::clear()
  127. {
  128.   for (unsigned int i = 0; i < size; ++i)
  129.   {
  130.     <T>CHNode* p = tab[i];
  131.     tab[i] = index_to_CHptr(i+1);
  132.     while (goodCHptr(p))
  133.     {
  134.       <T>CHNode* nxt = p->tl;
  135.       delete(p);
  136.       p = nxt;
  137.     }
  138.   }
  139.   count = 0;
  140. }
  141.  
  142. Pix <T>CHSet::first()
  143. {
  144.   for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
  145.   return 0;
  146. }
  147.  
  148. void <T>CHSet::next(Pix& p)
  149. {
  150.   if (p == 0) return;
  151.   <T>CHNode* t = ((<T>CHNode*)p)->tl;
  152.   if (goodCHptr(t))
  153.     p = Pix(t);
  154.   else
  155.   {
  156.     for (unsigned int i = CHptr_to_index(t); i < size; ++i) 
  157.     {
  158.       if (goodCHptr(tab[i]))
  159.       {
  160.         p =  Pix(tab[i]);
  161.         return;
  162.       }
  163.     }
  164.     p = 0;
  165.   }
  166. }
  167.  
  168. int <T>CHSet::operator == (<T>CHSet& b)
  169. {
  170.   if (count != b.count)
  171.     return 0;
  172.   else
  173.   {
  174.     <T>CHNode* p;
  175.     for (unsigned int i = 0; i < size; ++i)
  176.       for (p = tab[i]; goodCHptr(p); p = p->tl)
  177.         if (b.seek(p->hd) == 0)
  178.           return 0;
  179.     for (unsigned int i = 0; i < b.size; ++i)
  180.       for (p = b.tab[i]; goodCHptr(p); p = p->tl)
  181.         if (seek(p->hd) == 0)
  182.           return 0;
  183.     return 1;
  184.   }
  185. }
  186.  
  187. int <T>CHSet::operator <= (<T>CHSet& b)
  188. {
  189.   if (count > b.count)
  190.     return 0;
  191.   else
  192.   {
  193.     for (unsigned int i = 0; i < size; ++i)
  194.       for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl)
  195.         if (b.seek(p->hd) == 0)
  196.           return 0;
  197.     return 1;
  198.   }
  199. }
  200.  
  201. void <T>CHSet::operator |= (<T>CHSet& b)
  202. {
  203.   if (&b == this || b.count == 0)
  204.     return;
  205.   for (unsigned int i = 0; i < b.size; ++i)
  206.     for (<T>CHNode* p = b.tab[i]; goodCHptr(p); p = p->tl)
  207.       add(p->hd);
  208. }
  209.  
  210. void <T>CHSet::operator &= (<T>CHSet& b)
  211. {
  212.   for (unsigned int i = 0; i < size; ++i)
  213.   {
  214.     <T>CHNode* t = tab[i]; 
  215.     <T>CHNode* trail = t;
  216.     while (goodCHptr(t))
  217.     {
  218.       <T>CHNode* nxt = t->tl;
  219.       if (b.seek(t->hd) == 0)
  220.       {
  221.         if (trail == tab[i])
  222.           trail = tab[i] = nxt;
  223.         else
  224.           trail->tl = nxt;
  225.         delete t;
  226.         --count;
  227.       }
  228.       else
  229.         trail = t;
  230.       t = nxt;
  231.     }
  232.   }
  233. }
  234.  
  235. void <T>CHSet::operator -= (<T>CHSet& b)
  236. {
  237.   for (unsigned int i = 0; i < size; ++i)
  238.   {
  239.     <T>CHNode* t = tab[i]; 
  240.     <T>CHNode* trail = t;
  241.     while (goodCHptr(t))
  242.     {
  243.       <T>CHNode* nxt = t->tl;
  244.       if (b.seek(t->hd) != 0)
  245.       {
  246.         if (trail == tab[i])
  247.           trail = tab[i] = nxt;
  248.         else
  249.           trail->tl = nxt;
  250.         delete t;
  251.         --count;
  252.       }
  253.       else
  254.         trail = t;
  255.       t = nxt;
  256.     }
  257.   }
  258. }
  259.  
  260. int <T>CHSet::OK()
  261. {
  262.   int v = tab != 0;
  263.   int n = 0;
  264.   for (unsigned int i = 0; i < size; ++i)
  265.   {
  266.     <T>CHNode* p;
  267.     for (p = tab[i]; goodCHptr(p); p = p->tl) ++n;
  268.     v &= CHptr_to_index(p) == i + 1;
  269.   }
  270.   v &= count == n;
  271.   if (!v) error("invariant failure");
  272.   return v;
  273. }
  274.