home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / gen / List.hP < prev    next >
Text File  |  1996-10-12  |  6KB  |  280 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19.  
  20. #ifndef _<T>List_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _<T>List_h 1
  25.  
  26. #include <Pix.h>
  27. #include "<T>.defs.h"
  28.  
  29. #ifndef _<T>_typedefs
  30. #define _<T>_typedefs 1
  31. typedef void (*<T>Procedure)(<T&>);
  32. typedef <T>  (*<T>Mapper)(<T&>);
  33. typedef <T>  (*<T>Combiner)(<T&>, <T&>);
  34. typedef int  (*<T>Predicate)(<T&>);
  35. typedef int  (*<T>Comparator)(<T&>, <T&>);
  36. #endif
  37.  
  38. struct <T>ListNode
  39. {
  40.   <T>ListNode*          tl;
  41.   short                 ref;
  42.   <T>                   hd;
  43. };
  44.  
  45. extern <T>ListNode Nil<T>ListNode;
  46.  
  47. class <T>List
  48. {
  49. protected:
  50.   <T>ListNode*          P;
  51.  
  52.                         <T>List(<T>ListNode* p);
  53. public:
  54.                         <T>List();
  55.                         <T>List(<T&> head);
  56.                         <T>List(<T&> head, const <T>List& tl);
  57.                         <T>List(<T>List& a);
  58.                         <T>List(Pix p);
  59.                         ~<T>List();
  60.  
  61.   <T>List&              operator = (const <T>List& a);
  62.  
  63.   int                   null();
  64.   int                   valid();
  65.                         operator const void* ();
  66.   int                   operator ! ();
  67.  
  68.   int                   length();
  69.   int                   list_length();
  70.  
  71.   <T>&                  get();
  72.   <T>&                  head();
  73.   <T>&                  operator [] (int n);
  74.  
  75.   <T>List               nth(int n);
  76.   <T>List               tail();
  77.   <T>List               last();
  78.  
  79.   <T>List               find(<T&> targ);
  80.   <T>List               find(<T>List& targ);
  81.   int                   contains(<T&> targ);
  82.   int                   contains(<T>List& targ);
  83.   int                   position(<T&> targ);
  84.  
  85.   friend <T>List        copy(const <T>List& a);
  86.   friend <T>List        concat(<T>List& a, <T>List& b);
  87.   friend <T>List        append(<T>List& a, <T>List& b);
  88.   friend <T>List        map(<T>Mapper f, <T>List& a);
  89.   friend <T>List        merge(<T>List& a, <T>List& b, <T>Comparator f);
  90.   friend <T>List        combine(<T>Combiner f, <T>List& a, <T>List& b);
  91.   friend <T>List        reverse(<T>List& a);
  92.   friend <T>List        select(<T>Predicate f, <T>List& a);        
  93. #undef remove
  94.   friend <T>List        remove(<T&> targ, <T>List& a);
  95.   friend <T>List        remove(<T>Predicate f, <T>List& a);
  96.   friend <T>List        subst(<T&> old, <T&> repl, <T>List& a);
  97.  
  98.   void                  push(<T&> x);
  99.   <T>                   pop();
  100.  
  101.   void                  set_tail(<T>List& p);
  102.   void                  append(<T>List& p);
  103.   void                  prepend(<T>List& p);
  104.   void                  del(<T&> targ);
  105.   void                  del(<T>Predicate f);
  106.   void                  select(<T>Predicate f);
  107.   void                  subst(<T&> old, <T&> repl);
  108.   void                  reverse();
  109.   void                  sort(<T>Comparator f);
  110.  
  111.   void                  apply(<T>Procedure f);
  112.   <T>                   reduce(<T>Combiner f, <T&> base);
  113.  
  114.   friend int            operator == (const <T>List& a, const <T>List& b);
  115.   friend inline int     operator != (const <T>List& a, const <T>List& b);
  116.  
  117.   Pix                   first();
  118.   void                  next(Pix& p);
  119.   Pix                   seek(<T&> item) const;
  120.   <T>&                  operator () (Pix p);
  121.   const <T>&            operator () (Pix p) const;
  122.   int                   owns(Pix p);
  123.  
  124.   void                  error(const char*);
  125.   int                   OK();
  126. };
  127.  
  128.  
  129. inline void reference(<T>ListNode* p)
  130. {
  131.   if (p->ref >= 0) ++p->ref;
  132. }
  133.  
  134. inline void dereference(<T>ListNode* p)
  135. {
  136.   while (p->ref > 0 && --p->ref == 0)
  137.   {
  138.     <T>ListNode* n = p->tl;
  139.     delete(p);
  140.     p = n;
  141.   }
  142. }
  143.  
  144.  
  145. inline <T>ListNode* new<T>ListNode(const <T&> h)
  146. {
  147.   <T>ListNode* p = new <T>ListNode;
  148.   p->ref = 1;
  149.   p->hd = h;
  150.   return p;
  151. }
  152.  
  153. inline <T>ListNode* new<T>ListNode(<T&> h, <T>ListNode* t)
  154. {
  155.   <T>ListNode* p = new <T>ListNode;
  156.   p->ref = 1;
  157.   p->hd = h;
  158.   p->tl = t;
  159.   return p;
  160. }
  161.  
  162.  
  163. inline <T>List::~<T>List()
  164. {
  165.   dereference(P);
  166. }
  167.  
  168. inline <T>List::<T>List()
  169. {
  170.   P = &Nil<T>ListNode;
  171. }
  172.  
  173. inline <T>List::<T>List(<T>ListNode* p)
  174. {
  175.   P = p;
  176. }
  177.  
  178. inline <T>List::<T>List(<T&> head)
  179. {
  180.   P = new<T>ListNode(head);
  181.   P->tl = &Nil<T>ListNode;
  182. }
  183.  
  184. inline <T>List::<T>List(<T&> head, const <T>List& tl)
  185. {
  186.   P = new<T>ListNode(head, tl.P);
  187.   reference(P->tl);
  188. }
  189.  
  190. inline <T>List::<T>List(<T>List& a)
  191. {
  192.   reference(a.P);
  193.   P = a.P;
  194. }
  195.  
  196.  
  197. inline <T>& <T>List::get()
  198. {
  199.   return P->hd;
  200. }
  201.  
  202. inline <T>& <T>List::head()
  203. {
  204.   return P->hd;
  205. }
  206.  
  207.  
  208. inline <T>List <T>List::tail()
  209. {
  210.   reference(P->tl);
  211.   return <T>List(P->tl);
  212. }
  213.  
  214.  
  215.  
  216. inline int <T>List::null()
  217. {
  218.   return P == &Nil<T>ListNode;
  219. }
  220.  
  221. inline int <T>List::valid()
  222. {
  223.   return P != &Nil<T>ListNode;
  224. }
  225.  
  226. inline <T>List::operator const void* ()
  227. {
  228.   return (P == &Nil<T>ListNode)? 0 : this;
  229. }
  230.  
  231. inline int <T>List::operator ! ()
  232. {
  233.   return (P == &Nil<T>ListNode);
  234. }
  235.  
  236.  
  237. inline void <T>List::push(<T&> head)
  238. {
  239.   <T>ListNode* oldp = P;
  240.   P = new<T>ListNode(head, oldp);
  241. }
  242.  
  243.  
  244. inline int operator != (const <T>List& x, const <T>List& y)
  245. {
  246.   return !(x == y);
  247. }
  248.  
  249. inline Pix <T>List::first()
  250. {
  251.   return (P == &Nil<T>ListNode)? 0 : Pix(P);
  252. }
  253.  
  254. inline <T>& <T>List::operator () (Pix p)
  255. {
  256.   return ((<T>ListNode*)p)->hd;
  257. }
  258.  
  259. inline const <T>& <T>List::operator () (Pix p) const
  260. {
  261.   return ((const <T>ListNode*)p)->hd;
  262. }
  263.  
  264. inline void <T>List::next(Pix& p)
  265. {
  266.   if (p != 0)
  267.   {
  268.     p = Pix(((<T>ListNode*)p)->tl);
  269.     if (p == &Nil<T>ListNode) p = 0;
  270.   }
  271. }
  272.  
  273. inline <T>List::<T>List(Pix p)
  274. {
  275.   P = (<T>ListNode*)p;
  276.   reference(P);
  277. }
  278.  
  279. #endif
  280.