home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / CHAINHT.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  4.5 KB  |  214 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 <stream.h>
  25. #include "<T>ChainedHashTable.h"
  26.  
  27. // error handling
  28.  
  29.  
  30. void default_<T>ChainedHashTable_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal <T>ChainedHashTable error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t <T>ChainedHashTable_error_handler = default_<T>ChainedHashTable_error_handler;
  37.  
  38. one_arg_error_handler_t set_<T>ChainedHashTable_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = <T>ChainedHashTable_error_handler;
  41.   <T>ChainedHashTable_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void <T>ChainedHashTable::error(const char* msg)
  46. {
  47.   (*<T>ChainedHashTable_error_handler)(msg);
  48. }
  49.  
  50. <T>ChainedHashTable::<T>ChainedHashTable(int sz, <U>HashFunction f, 
  51.                                          <T>KeyAccess acc)
  52. {
  53.   tab = (<T>SLListNode**)(new <T>SLListNodePtr[size = sz]);
  54.   for (int i = 0; i < size; ++i) tab[i] = 0;
  55.   hash = f;
  56.   getkey = acc;
  57.   cnt = 0;
  58. }
  59.  
  60.  
  61. int <T>ChainedHashTable::access(<U&> key, <T>& item)
  62. {
  63.   int h = (*hash)(key) % size;
  64.  
  65.   for (<T>SLListNode* t = tab[h]; t != 0; t = t->tl)
  66.     if ((*getkey)(t->hd) == key)
  67.     {
  68.       item = t->hd;
  69.       return 1;
  70.     }
  71.   return 0;
  72. }
  73.  
  74. int <T>ChainedHashTable::contains(<U&> key)
  75. {
  76.   int h = (*hash)(key) % size;
  77.  
  78.   for (<T>SLListNode* t = tab[h]; t != 0; t = t->tl)
  79.     if ((*getkey)(t->hd) == key)
  80.       return 1;
  81.   return 0;
  82. }
  83.  
  84. int <T>ChainedHashTable::add_or_access(<T>& item)
  85. {
  86.   int h = (*hash)((*getkey)(item)) % size;
  87.  
  88.   for (<T>SLListNode* t = tab[h]; t != 0; t = t->tl)
  89.     if ((*getkey)(t->hd) == (*getkey)(item))
  90.     {
  91.       item = t->hd;
  92.       return 1;
  93.     }
  94.  
  95.   t = new <T>SLListNode(item);
  96.   t->tl = tab[h];
  97.   tab[h] = t;
  98.   ++cnt;
  99.   return 0;
  100. }
  101.  
  102. int <T>ChainedHashTable::add(<T&> item)
  103. {
  104.   int h = (*hash)((*getkey)(item)) % size;
  105.  
  106.   for (<T>SLListNode* t = tab[h]; t != 0; t = t->tl)
  107.     if ((*getkey)(t->hd) == (*getkey)(item))
  108.       return 0;
  109.  
  110.   t = new <T>SLListNode(item);
  111.   t->tl = tab[h];
  112.   tab[h] = t;
  113.   ++cnt;
  114.   return 1;
  115. }
  116.  
  117.  
  118. int <T>ChainedHashTable::del(<U&> key)
  119. {
  120.   int h = (*hash)(key) % size;
  121.  
  122.   <T>SLListNode* t = tab[h]; 
  123.   <T>SLListNode* trail = t;
  124.   while (t != 0)
  125.   {
  126.     if (getkey(t->hd) == key)
  127.     {
  128.       if (trail == t)
  129.         tab[h] = t->tl;
  130.       else
  131.         trail->tl = t->tl;
  132.       delete t;
  133.       --cnt;
  134.       return 1;
  135.     }
  136.     trail = t;
  137.     t = t->tl;
  138.   }
  139.   return 0;
  140. }
  141.  
  142. void <T>ChainedHashTable::apply(<T>Procedure f)
  143. {
  144.   for (int i = 0; i < size; ++i)
  145.   {
  146.     for (<T>SLListNode* p = tab[i]; p != 0; p = p->tl)
  147.       (*f)(p->hd);
  148.   }
  149. }
  150.  
  151. void <T>ChainedHashTable::clear()
  152. {
  153.   for (int i = 0; i < size; ++i)
  154.   {
  155.     <T>SLListNode* p = tab[i];
  156.     tab[i] = 0;
  157.     while (p != 0)
  158.     {
  159.       <T>SLListNode* nxt = p->tl;
  160.       delete(p);
  161.       p = nxt;
  162.     }
  163.   }
  164.   cnt = 0;
  165. }
  166.  
  167. <T>ChainedHashTableTrav::<T>ChainedHashTableTrav(<T>ChainedHashTable& a)
  168. {
  169.   h = &a;
  170.   for (int i = 0; i < h->size; ++i)
  171.   {
  172.     if (h->tab[i] != 0)
  173.     {
  174.       pos = i;
  175.       p = h->tab[i];
  176.       return;
  177.     }
  178.   }
  179.   pos = -1;
  180.   p = 0;
  181. }
  182.  
  183. void <T>ChainedHashTableTrav::advance()
  184. {
  185.   if (p->tl != 0)
  186.     p = p->tl;
  187.   else
  188.   {
  189.     for (int i = pos + 1; i < h->size; ++i)
  190.     {
  191.       if (h->tab[i] != 0)
  192.       {
  193.         pos = i;
  194.         p = h->tab[i];
  195.         return;
  196.       }
  197.     }
  198.     pos = -1;
  199.     p = 0;
  200.   }
  201. }
  202.  
  203. int operator == (<T>ChainedHashTable& a, <T>ChainedHashTable& b)
  204. {
  205.   if (a.cnt != b.cnt)
  206.     return 0;
  207.   for (<T>ChainedHashTableTrav p(a); p; p.advance())
  208.     if (!b.contains(p.get()))
  209.       return 0;
  210.   return 1;
  211. }
  212.  
  213.   
  214.