home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / CHAINHT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  4.2 KB  |  164 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.  
  25. #ifndef _<T>ChainedHashTable_h
  26. #define _<T>ChainedHashTable_h 1
  27.  
  28. #ifndef _<T>_typedefs
  29. #define _<T>_typedefs 1
  30. typedef void (*<T>Procedure)(<T&>);
  31. typedef <T>  (*<T>Mapper)(<T&>);
  32. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  33. typedef int  (*<T>Predicate)(<T&>);
  34. typedef int  (*<T>Comparator)(<T&>, <T&>);
  35. #endif
  36.  
  37. #ifndef _<U>_HashFunction
  38. #define _<U>_HashFunction 1
  39. typedef unsigned int  (*<U>HashFunction)(<U&>);
  40. typedef <U&>  (*<T>KeyAccess)(<T&>);
  41. #endif
  42.  
  43. #include "<T>SLListNode.h"
  44. class          <T>ChainedHashTable;
  45. class          <T>ChainedHashTableTrav;
  46.  
  47.  
  48. class <T>ChainedHashTable
  49. {
  50.   friend class          <T>ChainedHashTableTrav;
  51.  
  52.   <T>SLListNode**       tab;
  53.   int                   size;
  54.   int                   cnt;
  55.   <U>HashFunction       hash;
  56.   <T>KeyAccess          getkey;
  57.  
  58. public:
  59.                         <T>ChainedHashTable(int sz, 
  60.                                             <U>HashFunction h,
  61.                                             <T>KeyAccess acc);
  62.                         ~<T>ChainedHashTable();
  63.  
  64.   int                   count();
  65.   int                   empty();
  66.   void                  clear();
  67.  
  68.   int                   add(<T&> item);
  69.   int                   add_or_access(<T>& item);
  70.   int                   access(<U&> key, <T>& item);
  71.   int                   contains(<U&> key);
  72.   int                   del(<U&> key);
  73.  
  74.   void                  apply(<T>Procedure f);
  75.  
  76.   friend int            operator == (<T>ChainedHashTable& a, 
  77.                                      <T>ChainedHashTable& b);
  78.   friend int            operator != (<T>ChainedHashTable& a, 
  79.                                      <T>ChainedHashTable& b);
  80.  
  81.   void                  error(const char* msg);
  82. };
  83.  
  84. class <T>ChainedHashTableTrav
  85. {
  86.   <T>ChainedHashTable*  h;
  87.   <T>SLListNode* p;
  88.   int                   pos;
  89.  
  90.  
  91. public:
  92.                         <T>ChainedHashTableTrav(<T>ChainedHashTable& l);
  93.                         ~<T>ChainedHashTableTrav();
  94.  
  95.   int                   null();
  96.   int                   valid();
  97.   const void*           operator void* ();
  98.   int                   operator ! ();
  99.   void                  advance();
  100.   <T&>                  get();
  101. };
  102.  
  103.  
  104. inline <T>ChainedHashTable::~<T>ChainedHashTable()
  105. {
  106.   clear();
  107.   delete tab;
  108. }
  109.  
  110. inline int <T>ChainedHashTable::count()
  111. {
  112.   return cnt;
  113. }
  114.  
  115. inline int <T>ChainedHashTable::empty()
  116. {
  117.   return cnt == 0;
  118. }
  119.  
  120. inline int operator != (<T>ChainedHashTable& x, <T>ChainedHashTable& y)
  121. {
  122.   return !(x == y);
  123. }
  124.  
  125. inline <T>ChainedHashTableTrav::~<T>ChainedHashTableTrav() {}
  126.  
  127. inline int <T>ChainedHashTableTrav::null()
  128. {
  129.   return pos < 0;
  130. }
  131.  
  132. inline int <T>ChainedHashTableTrav::valid()
  133. {
  134.   return pos >= 0;
  135. }
  136.  
  137. inline const void* <T>ChainedHashTableTrav::operator void* ()
  138. {
  139.   return (pos < 0)? 0 : this;
  140. }
  141.  
  142. inline int <T>ChainedHashTableTrav::operator ! ()
  143. {
  144.   return (pos >= 0);
  145. }
  146.  
  147.  
  148. inline <T&> <T>ChainedHashTableTrav::get()
  149. {
  150.   if (p == 0)
  151.     h->error("operation on null traverser");
  152.   return p->hd;
  153. }
  154.  
  155.  
  156. extern void default_<T>ChainedHashTable_error_handler(char*);
  157. extern one_arg_error_handler_t <T>ChainedHashTable_error_handler;
  158.  
  159. extern one_arg_error_handler_t 
  160.         set_<T>ChainedHashTable_error_handler(one_arg_error_handler_t f);
  161.  
  162.  
  163. #endif
  164.