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 / CHBag.ccP < prev    next >
Text File  |  1996-10-12  |  4KB  |  211 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>.CHBag.h"
  23.  
  24. // The nodes are linked together serially via a version
  25. // of a trick used in some vtables: odd pointers are
  26. // actually links to the next table entry. 
  27. // Not terrible, but not wonderful either
  28.  
  29. static inline int goodCHptr(<T>CHNode* t)
  30. {
  31.   return ((((unsigned)t) & 1) == 0);
  32. }
  33.  
  34. static inline <T>CHNode* index_to_CHptr(int i)
  35. {
  36.   return (<T>CHNode*)((i << 1) + 1);
  37. }
  38.  
  39. static inline int CHptr_to_index(<T>CHNode* t)
  40. {
  41.   return ( ((unsigned) t) >> 1);
  42. }
  43.  
  44. <T>CHBag::<T>CHBag(unsigned int sz)
  45. {
  46.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
  47.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  48.   count = 0;
  49. }
  50.  
  51. <T>CHBag::<T>CHBag(<T>CHBag& a)
  52. {
  53.   tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
  54.   for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
  55.   count = 0;
  56.   for (Pix p = a.first(); p; a.next(p)) add(a(p));
  57. }
  58.  
  59.  
  60. Pix <T>CHBag::seek(<T&> key, Pix i)
  61. {
  62.   <T>CHNode* p = (<T>CHNode*)i;
  63.   if (p == 0 || !<T>EQ(p->hd, key))
  64.   {
  65.     unsigned int h = <T>HASH(key) % size;
  66.     for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
  67.       if (<T>EQ(key, t->hd))
  68.         return Pix(t);
  69.   }
  70.   else
  71.   {
  72.     for (p = p->tl; goodCHptr(p); p = p->tl) 
  73.       if (<T>EQ(p->hd, key))
  74.         return Pix(p);
  75.   }
  76.   return 0;
  77. }
  78.  
  79. int <T>CHBag::nof(<T&> key)
  80. {
  81.   int n = 0;
  82.   unsigned int h = <T>HASH(key) % size;
  83.   for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl) 
  84.     if (<T>EQ(key, t->hd)) ++n;
  85.   return n;
  86. }
  87.  
  88.  
  89. Pix <T>CHBag::add(<T&> item)
  90. {
  91.   unsigned int h = <T>HASH(item) % size;
  92.   <T>CHNode* t = new <T>CHNode(item);
  93.   t->tl = tab[h];
  94.   tab[h] = t;
  95.   ++count;
  96.   return Pix(t);
  97. }
  98.  
  99. void <T>CHBag::del(<T&> key)
  100. {
  101.   unsigned int h = <T>HASH(key) % size;
  102.  
  103.   <T>CHNode* t = tab[h]; 
  104.   <T>CHNode* trail = t;
  105.   while (goodCHptr(t))
  106.   {
  107.     if (<T>EQ(key, t->hd))
  108.     {
  109.       if (trail == t)
  110.         tab[h] = t->tl;
  111.       else
  112.         trail->tl = t->tl;
  113.       delete t;
  114.       --count;
  115.       return;
  116.     }
  117.     trail = t;
  118.     t = t->tl;
  119.   }
  120. }
  121.  
  122. void <T>CHBag::remove(<T&> key)
  123. {
  124.   unsigned int h = <T>HASH(key) % size;
  125.  
  126.   <T>CHNode* t = tab[h]; 
  127.   <T>CHNode* trail = t;
  128.   while (goodCHptr(t))
  129.   {
  130.     if (<T>EQ(key, t->hd))
  131.     {
  132.       --count;
  133.       if (trail == t)
  134.       {
  135.         tab[h] = t->tl;
  136.         delete t;
  137.         t = trail = tab[h];
  138.       }
  139.       else
  140.       {
  141.         trail->tl = t->tl;
  142.         delete t;
  143.         t = trail->tl;
  144.       }
  145.     }
  146.     else
  147.     {
  148.       trail = t;
  149.       t = t->tl;
  150.     }
  151.   }
  152. }
  153.  
  154.  
  155. void <T>CHBag::clear()
  156. {
  157.   for (unsigned int i = 0; i < size; ++i)
  158.   {
  159.     <T>CHNode* p = tab[i];
  160.     tab[i] = index_to_CHptr(i+1);
  161.     while (goodCHptr(p))
  162.     {
  163.       <T>CHNode* nxt = p->tl;
  164.       delete(p);
  165.       p = nxt;
  166.     }
  167.   }
  168.   count = 0;
  169. }
  170.  
  171. Pix <T>CHBag::first()
  172. {
  173.   for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
  174.   return 0;
  175. }
  176.  
  177. void <T>CHBag::next(Pix& p)
  178. {
  179.   if (p == 0) return;
  180.   <T>CHNode* t = ((<T>CHNode*)p)->tl;
  181.   if (goodCHptr(t))
  182.     p = Pix(t);
  183.   else
  184.   {
  185.     for (unsigned int i = CHptr_to_index(t); i < size; ++i) 
  186.     {
  187.       if (goodCHptr(tab[i]))
  188.       {
  189.         p =  Pix(tab[i]);
  190.         return;
  191.       }
  192.     }
  193.     p = 0;
  194.   }
  195. }
  196.  
  197. int <T>CHBag::OK()
  198. {
  199.   int v = tab != 0;
  200.   int n = 0;
  201.   for (unsigned int i = 0; i < size; ++i)
  202.   {
  203.     <T>CHNode* p;
  204.     for (p = tab[i]; goodCHptr(p); p = p->tl) ++n;
  205.     v &= CHptr_to_index(p) == i + 1;
  206.   }
  207.   v &= count == n;
  208.   if (!v) error("invariant failure");
  209.   return v;
  210. }
  211.