home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / AVLASSOC.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  6.9 KB  |  287 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><U>AVLAssoc_h
  26. #define _<T><U>AVLAssoc_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>_typedefs
  38. #define _<U>_typedefs 1
  39. typedef void (*<U>Procedure)(<U&>);
  40. typedef <U>  (*<U>Mapper)(<U&>);
  41. typedef <U>  (*<U>Combiner)(<U&>, <U&>);
  42. typedef int  (*<U>Predicate)(<U&>);
  43. typedef int  (*<U>Comparator)(<U&>, <U&>);
  44. #endif
  45.  
  46.  
  47. #ifndef _<T><U>AVLAssocNode
  48. #define _<T><U>AVLAssocNode
  49.  
  50. struct <T><U>AVLAssocNode
  51. {
  52.   <T><U>AVLAssocNode*    lt;
  53.   <T><U>AVLAssocNode*    rt;
  54.   <T>                    key;
  55.   <U>                    cont;
  56.   char                   stat;
  57.                          <T><U>AVLAssocNode(<T&> k,
  58.                                             <T><U>AVLAssocNode* l=0, 
  59.                                             <T><U>AVLAssocNode* r=0);
  60.                          <T><U>AVLAssocNode(<T&> k, <U&> c,
  61.                                             <T><U>AVLAssocNode* l=0, 
  62.                                             <T><U>AVLAssocNode* r=0);
  63.                          ~<T><U>AVLAssocNode();
  64. };
  65.  
  66.  
  67. inline <T><U>AVLAssocNode::<T><U>AVLAssocNode(<T&> k, 
  68.                                               <T><U>AVLAssocNode* l=0, 
  69.                                               <T><U>AVLAssocNode* r=0)
  70. {
  71.   key = k;
  72.   lt = l;
  73.   rt = r;
  74.   stat = 0;
  75. }
  76.  
  77. inline <T><U>AVLAssocNode::<T><U>AVLAssocNode(<T&> k, <U&> c,
  78.                                               <T><U>AVLAssocNode* l=0, 
  79.                                               <T><U>AVLAssocNode* r=0)
  80. {
  81.   key = k;
  82.   cont = c;
  83.   lt = l;
  84.   rt = r;
  85.   stat = 0;
  86. }
  87.  
  88. inline <T><U>AVLAssocNode::~<T><U>AVLAssocNode() {}
  89.  
  90. typedef <T><U>AVLAssocNode* <T><U>AVLAssocNodePtr;
  91.  
  92. #endif
  93.  
  94. class          <T><U>AVLAssoc;
  95. class          <T><U>AVLAssocTrav;
  96.  
  97. class <T><U>AVLAssoc
  98. {
  99.   friend class          <T><U>AVLAssocTrav;
  100.  
  101.   <T><U>AVLAssocNode*   root;
  102.   int                   cnt;
  103.  
  104.                         <T><U>AVLAssoc(<T><U>AVLAssocNode* p, int l);
  105.   <T><U>AVLAssocNode*   leftmost();
  106.   <T><U>AVLAssocNode*   rightmost();
  107.   <T><U>AVLAssocNode*   pred(<T><U>AVLAssocNode* t);
  108.   <T><U>AVLAssocNode*   succ(<T><U>AVLAssocNode* t);
  109.   void                  _kill(<T><U>AVLAssocNode* t);
  110.   void                  _add(<T><U>AVLAssocNode*& t);
  111.   void                  _del(<T><U>AVLAssocNode* p, <T><U>AVLAssocNode*& t);
  112.  
  113. public:
  114.  
  115.   static <T>Comparator  key_key_comparison_function;
  116.   int                   key_key_cmp(<T&>a, <T&> b);
  117.  
  118.                         <T><U>AVLAssoc();
  119.                         <T><U>AVLAssoc(<T><U>AVLAssoc& a);
  120.                         ~<T><U>AVLAssoc();
  121.  
  122.   <T><U>AVLAssoc&       operator = (<T><U>AVLAssoc& a);
  123.  
  124.   int                   count();
  125.   int                   empty();
  126.   void                  clear();
  127.  
  128.   <U>&                  operator [] (<T&> k);
  129.   int                   contains(<T&> key);
  130.   int                   del(<T&> key);
  131.  
  132.   void                  apply(<U>Procedure f);
  133.  
  134.   void                  error(const char* msg);
  135. };
  136.  
  137. class <T><U>AVLAssocTrav
  138. {
  139.   <T><U>AVLAssoc*             h;
  140.   <T><U>AVLAssocNode*         current;
  141.  
  142. public:
  143.                         <T><U>AVLAssocTrav(<T><U>AVLAssoc& l, int dir = 1);
  144.                         ~<T><U>AVLAssocTrav();
  145.  
  146.   int                   null();
  147.   int                   valid();
  148.   const void*           operator void* ();
  149.   int                   operator ! ();
  150.   void                  advance(int dir = 1);
  151.   void                  reset(int dir = 1);
  152.   void                  reset(<T><U>AVLAssoc& l, int dir = 1);
  153.   <U>&                  get();
  154.   const <T>&            key();
  155. };
  156.  
  157.  
  158. inline int <T><U>AVLAssoc::key_key_cmp(<U&>a, <U&> b)
  159. {
  160. #ifdef <T>COMPARISONFUNCTION
  161.   return <T>COMPARISONFUNCTION(a, b);
  162. #else
  163.   return (*<T><U>AVLAssoc::key_key_comparison_function)(a, b);
  164. #endif
  165. }
  166.  
  167. inline <T><U>AVLAssoc::<T><U>AVLAssoc()
  168. {
  169.   root = 0;
  170.   cnt = 0;
  171. }
  172.  
  173.  
  174. inline <T><U>AVLAssoc::<T><U>AVLAssoc(<T><U>AVLAssocNode* p, int l)
  175. {
  176.   root = p;
  177.   cnt = l;
  178. }
  179.  
  180. inline <T><U>AVLAssoc::~<T><U>AVLAssoc()
  181. {
  182.   _kill(root);
  183. }
  184.  
  185. inline int <T><U>AVLAssoc::count()
  186. {
  187.   return cnt;
  188. }
  189.  
  190. inline int <T><U>AVLAssoc::empty()
  191. {
  192.   return cnt == 0;
  193. }
  194.  
  195.  
  196. inline void <T><U>AVLAssocTrav::reset(int dir = 1)
  197. {
  198.   if (dir < 0)
  199.     current = h->rightmost();
  200.   else
  201.     current = h->leftmost();
  202. }
  203.  
  204.  
  205. inline void <T><U>AVLAssocTrav::advance(int dir = 1)
  206. {
  207.   if (current != 0)
  208.   {
  209.     if (dir < 0)
  210.       current = h->pred(current);
  211.     else
  212.       current = h->succ(current);
  213.   }
  214. }
  215.  
  216. inline <T><U>AVLAssocTrav::<T><U>AVLAssocTrav(<T><U>AVLAssoc& a, int dir = 1)
  217. {
  218.   h = &a;
  219.   reset(dir);
  220. }
  221.  
  222. inline void <T><U>AVLAssocTrav::reset(<T><U>AVLAssoc& a, int dir = 1)
  223. {
  224.   h = &a;
  225.   reset(dir);
  226. }
  227.  
  228. inline <T><U>AVLAssocTrav::~<T><U>AVLAssocTrav() {}
  229.  
  230. inline int <T><U>AVLAssocTrav::null()
  231. {
  232.   return current == 0;
  233. }
  234.  
  235. inline int <T><U>AVLAssocTrav::valid()
  236. {
  237.   return current != 0;
  238. }
  239.  
  240. inline const void* <T><U>AVLAssocTrav::operator void* ()
  241. {
  242.   return (current == 0)? 0 : this;
  243. }
  244.  
  245. inline int <T><U>AVLAssocTrav::operator ! ()
  246. {
  247.   return (current == 0);
  248. }
  249.  
  250.  
  251. inline <U>& <T><U>AVLAssocTrav::get()
  252. {
  253.   if (current ==  0)
  254.     h->error("get of null traverser");
  255.   return current->cont;
  256. }
  257.  
  258. inline const <T>& <T><U>AVLAssocTrav::key()
  259. {
  260.   if (current ==  0)
  261.     h->error("get of null traverser");
  262.   return current->key;
  263. }
  264.  
  265. inline void <T><U>AVLAssoc::clear()
  266. {
  267.   _kill(root);
  268.   cnt = 0;
  269.   root = 0;
  270. }
  271.  
  272. inline void <T><U>AVLAssoc::apply(<U>Procedure f)
  273. {
  274.   for (<T><U>AVLAssocNode* t = leftmost(); t != 0; t = succ(t))
  275.     (*f)(t->cont);
  276. }
  277.  
  278.  
  279. extern void default_<T><U>AVLAssoc_error_handler(char*);
  280. extern one_arg_error_handler_t <T><U>AVLAssoc_error_handler;
  281.  
  282. extern one_arg_error_handler_t 
  283.         set_<T><U>AVLAssoc_error_handler(one_arg_error_handler_t f);
  284.  
  285.  
  286. #endif
  287.