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