home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / libg++-2.5.3-bin.lha / lib / g++-include / gen / List.hP < prev    next >
Text File  |  1994-02-21  |  6KB  |  274 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, 675 Mass Ave, Cambridge, MA 02139, 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, <T>List& tl);
  57.                         <T>List(<T>List& a);
  58.                         <T>List(Pix p);
  59.                         ~<T>List();
  60.  
  61.   <T>List&              operator = (<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(<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 == (<T>List& a, <T>List& b);
  115.   friend int            operator != (<T>List& a, <T>List& b);
  116.  
  117.   Pix                   first();
  118.   void                  next(Pix& p);
  119.   Pix                   seek(<T&> item);
  120.   <T>&                  operator () (Pix p);
  121.   int                   owns(Pix p);
  122.  
  123.   void                  error(const char*);
  124.   int                   OK();
  125. };
  126.  
  127.  
  128. inline void reference(<T>ListNode* p)
  129. {
  130.   if (p->ref >= 0) ++p->ref;
  131. }
  132.  
  133. inline void dereference(<T>ListNode* p)
  134. {
  135.   while (p->ref > 0 && --p->ref == 0)
  136.   {
  137.     <T>ListNode* n = p->tl;
  138.     delete(p);
  139.     p = n;
  140.   }
  141. }
  142.  
  143.  
  144. inline <T>ListNode* new<T>ListNode(<T&> h)
  145. {
  146.   <T>ListNode* p = new <T>ListNode;
  147.   p->ref = 1;
  148.   p->hd = h;
  149.   return p;
  150. }
  151.  
  152. inline <T>ListNode* new<T>ListNode(<T&> h, <T>ListNode* t)
  153. {
  154.   <T>ListNode* p = new <T>ListNode;
  155.   p->ref = 1;
  156.   p->hd = h;
  157.   p->tl = t;
  158.   return p;
  159. }
  160.  
  161.  
  162. inline <T>List::~<T>List()
  163. {
  164.   dereference(P);
  165. }
  166.  
  167. inline <T>List::<T>List()
  168. {
  169.   P = &Nil<T>ListNode;
  170. }
  171.  
  172. inline <T>List::<T>List(<T>ListNode* p)
  173. {
  174.   P = p;
  175. }
  176.  
  177. inline <T>List::<T>List(<T&> head)
  178. {
  179.   P = new<T>ListNode(head);
  180.   P->tl = &Nil<T>ListNode;
  181. }
  182.  
  183. inline <T>List::<T>List(<T&> head, <T>List& tl)
  184. {
  185.   P = new<T>ListNode(head, tl.P);
  186.   reference(P->tl);
  187. }
  188.  
  189. inline <T>List::<T>List(<T>List& a)
  190. {
  191.   reference(a.P);
  192.   P = a.P;
  193. }
  194.  
  195.  
  196. inline <T>& <T>List::get()
  197. {
  198.   return P->hd;
  199. }
  200.  
  201. inline <T>& <T>List::head()
  202. {
  203.   return P->hd;
  204. }
  205.  
  206.  
  207. inline <T>List <T>List::tail()
  208. {
  209.   reference(P->tl);
  210.   return <T>List(P->tl);
  211. }
  212.  
  213.  
  214.  
  215. inline int <T>List::null()
  216. {
  217.   return P == &Nil<T>ListNode;
  218. }
  219.  
  220. inline int <T>List::valid()
  221. {
  222.   return P != &Nil<T>ListNode;
  223. }
  224.  
  225. inline <T>List::operator const void* ()
  226. {
  227.   return (P == &Nil<T>ListNode)? 0 : this;
  228. }
  229.  
  230. inline int <T>List::operator ! ()
  231. {
  232.   return (P == &Nil<T>ListNode);
  233. }
  234.  
  235.  
  236. inline void <T>List::push(<T&> head)
  237. {
  238.   <T>ListNode* oldp = P;
  239.   P = new<T>ListNode(head, oldp);
  240. }
  241.  
  242.  
  243. inline int operator != (<T>List& x, <T>List& y)
  244. {
  245.   return !(x == y);
  246. }
  247.  
  248. inline Pix <T>List::first()
  249. {
  250.   return (P == &Nil<T>ListNode)? 0 : Pix(P);
  251. }
  252.  
  253. inline <T>& <T>List::operator () (Pix p)
  254. {
  255.   return ((<T>ListNode*)p)->hd;
  256. }
  257.  
  258. inline void <T>List::next(Pix& p)
  259. {
  260.   if (p != 0)
  261.   {
  262.     p = Pix(((<T>ListNode*)p)->tl);
  263.     if (p == &Nil<T>ListNode) p = 0;
  264.   }
  265. }
  266.  
  267. inline <T>List::<T>List(Pix p)
  268. {
  269.   P = (<T>ListNode*)p;
  270.   reference(P);
  271. }
  272.  
  273. #endif
  274.