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