|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: L T
Length: 7126 (0x1bd6) Types: TextFile Names: »List.hP«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦cc8755de2⟧ »./libg++-1.36.1.tar.Z« └─⟦23757c458⟧ └─⟦this⟧ »libg++/g++-include/List.hP«
// This may look like C code, but it is really -*- C++ -*- /* Copyright (C) 1988 Free Software Foundation written by Doug Lea (dl@rocky.oswego.edu) This file is part of GNU CC. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the GNU CC General Public License for full details. Everyone is granted permission to copy, modify and redistribute GNU CC, but only under the conditions described in the GNU CC General Public License. A copy of this license is supposed to have been given to you along with GNU CC so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ #ifndef _<T>List_h #pragma once #define _<T>List_h 1 #ifndef _<T>_typedefs #define _<T>_typedefs 1 typedef void (*<T>Procedure)(<T&>); typedef <T> (*<T>Mapper)(<T&>); typedef <T> (*<T>Combiner)(<T&>, <T&>); typedef int (*<T>Predicate)(<T&>); typedef int (*<T>Comparator)(<T&>, <T&>); #endif #include <Pix.h> struct <T>ListNode { <T>ListNode* tl; short ref; <T> hd; }; extern <T>ListNode Nil<T>ListNode; inline void reference(<T>ListNode* p) { if (p->ref >= 0) ++p->ref; } inline void dereference(<T>ListNode* p) { while (p->ref > 0 && --p->ref == 0) { <T>ListNode* n = p->tl; delete(p); p = n; } } inline <T>ListNode* new<T>ListNode(<T&> h) { <T>ListNode* p = new <T>ListNode; p->ref = 1; p->hd = h; return p; } inline <T>ListNode* new<T>ListNode(<T&> h, <T>ListNode* t) { <T>ListNode* p = new <T>ListNode; p->ref = 1; p->hd = h; p->tl = t; return p; } class <T>List { protected: <T>ListNode* P; <T>List(<T>ListNode* p); public: <T>List(); <T>List(<T&> head); <T>List(<T&> head, <T>List& tl); <T>List(<T>List& a); <T>List(Pix p); ~<T>List(); <T>List& operator = (<T>List& a); int null(); int valid(); operator const void* (); int operator ! (); int length(); int list_length(); <T>& get(); <T>& head(); <T>& operator [] (int n); <T>List nth(int n); <T>List tail(); <T>List last(); <T>List find(<T&> targ); <T>List find(<T>List& targ); int contains(<T&> targ); int contains(<T>List& targ); int position(<T&> targ); friend <T>List copy(<T>List& a); friend <T>List concat(<T>List& a, <T>List& b); friend <T>List append(<T>List& a, <T>List& b); friend <T>List map(<T>Mapper f, <T>List& a); friend <T>List merge(<T>List& a, <T>List& b, <T>Comparator f); friend <T>List combine(<T>Combiner f, <T>List& a, <T>List& b); friend <T>List reverse(<T>List& a); friend <T>List select(<T>Predicate f, <T>List& a); friend <T>List remove(<T&> targ, <T>List& a); friend <T>List remove(<T>Predicate f, <T>List& a); friend <T>List subst(<T&> old, <T&> repl, <T>List& a); void push(<T&> x); <T> pop(); void set_tail(<T>List& p); void append(<T>List& p); void prepend(<T>List& p); void del(<T&> targ); void del(<T>Predicate f); void select(<T>Predicate f); void subst(<T&> old, <T&> repl); void reverse(); void sort(<T>Comparator f); void apply(<T>Procedure f); <T> reduce(<T>Combiner f, <T&> base); friend int operator == (<T>List& a, <T>List& b); friend int operator != (<T>List& a, <T>List& b); Pix first(); void next(Pix& p); Pix seek(<T&> item); <T>& operator () (Pix p); int owns(Pix p); void error(char*); int OK(); }; inline <T>List::~<T>List() { dereference(P); } inline <T>List::<T>List() { P = &Nil<T>ListNode; } inline <T>List::<T>List(<T>ListNode* p) { P = p; } inline <T>List::<T>List(<T&> head) { P = new<T>ListNode(head); P->tl = &Nil<T>ListNode; } inline <T>List::<T>List(<T&> head, <T>List& tl) { P = new<T>ListNode(head, tl.P); reference(P->tl); } inline <T>List::<T>List(<T>List& a) { reference(a.P); P = a.P; } inline <T>List& <T>List::operator = (<T>List& a) { reference(a.P); dereference(P); P = a.P; return *this; } inline <T>& <T>List::get() { return P->hd; } inline <T>& <T>List::head() { return P->hd; } inline <T>List <T>List::tail() { reference(P->tl); return <T>List(P->tl); } inline void <T>List::set_tail(<T>List& a) { reference(a.P); dereference(P->tl); P->tl = a.P; } inline int <T>List::null() { return P == &Nil<T>ListNode; } inline int <T>List::valid() { return P != &Nil<T>ListNode; } inline <T>List::operator const void* () { return (P == &Nil<T>ListNode)? 0 : this; } inline int <T>List::operator ! () { return (P == &Nil<T>ListNode); } inline <T>List <T>List::nth(int n) { for (<T>ListNode* p = P; n-- > 0; p = p->tl); reference(p); return <T>List(p); } inline <T>& <T>List::operator [] (int n) { for (<T>ListNode* p = P; n-- > 0; p = p->tl); return (p->hd); } inline <T>List <T>List::last() { <T>ListNode* p = P; if (p != &Nil<T>ListNode) while (p->tl != &Nil<T>ListNode) p = p->tl; reference(p); return <T>List(p); } inline void <T>List::push(<T&> head) { <T>ListNode* oldp = P; P = new<T>ListNode(head, oldp); } inline <T> <T>List::pop() { <T> res = P->hd; reference(P->tl); dereference(P); P = P->tl; return res; } inline void <T>List::append(<T>List& l) { <T>ListNode* p = P; <T>ListNode* a = l.P; reference(a); if (p != &Nil<T>ListNode) { while (p->tl != &Nil<T>ListNode) p = p->tl; p->tl = a; } else P = a; } inline int <T>List::length() { int l = 0; for (<T>ListNode* p = P; p != &Nil<T>ListNode; p = p->tl) ++l; return l; } inline int operator != (<T>List& x, <T>List& y) { return !(x == y); } inline Pix <T>List::first() { return (P == &Nil<T>ListNode)? 0 : Pix(P); } inline <T>& <T>List::operator () (Pix p) { return ((<T>ListNode*)p)->hd; } inline void <T>List::next(Pix& p) { if (p != 0) { p = Pix(((<T>ListNode*)p)->tl); if (p == &Nil<T>ListNode) p = 0; } } inline <T>List::<T>List(Pix p) { P = (<T>ListNode*)p; reference(P); } #endif