home *** CD-ROM | disk | FTP | other *** search
- // 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>DLList_h
- #define _<T>DLList_h 1
-
- #ifndef _<T>DLListNode_h
- #define _<T>DLListNode_h 1
-
- struct <T>DLListNode
- {
- <T>DLListNode* bk;
- <T>DLListNode* fd;
- <T> hd;
- <T>DLListNode();
- <T>DLListNode(<T&> h,
- <T>DLListNode* p = 0,
- <T>DLListNode* n = 0);
- ~<T>DLListNode();
- };
-
- inline <T>DLListNode::<T>DLListNode() {}
-
- inline <T>DLListNode::<T>DLListNode(<T&> h, <T>DLListNode* p = 0,
- <T>DLListNode* n = 0)
- {
- hd = h;
- bk = p;
- fd = n;
- }
-
- inline <T>DLListNode::~<T>DLListNode() {}
-
- typedef <T>DLListNode* <T>DLListNodePtr;
-
- #endif
-
- class <T>DLListTrav;
-
- class <T>DLList
- {
- friend class <T>DLListTrav;
-
- <T>DLListNode* h;
-
- public:
- <T>DLList();
- <T>DLList(<T>DLList& a);
- ~<T>DLList();
-
- <T>DLList& operator = (<T>DLList& a);
-
- int null();
- int empty();
- int valid();
- const void* operator void* ();
- int operator ! ();
- int length();
- void clear();
-
- void prepend(<T&> item);
- void append(<T&> item);
-
- <T>& front();
- <T> remove_front();
- void del_front();
-
- <T>& rear();
- <T> remove_rear();
- void del_rear();
-
- void error(const char* msg);
- };
-
- class <T>DLListTrav
- {
- friend class <T>DLList;
-
- <T>DLList* L;
- <T>DLListNode* current;
- public:
- <T>DLListTrav(<T>DLList& l, int dir = 1);
- ~<T>DLListTrav();
-
- int null();
- int valid();
- const void* operator void* ();
- int operator ! ();
-
- void advance(int dir = 1);
- <T>& get();
- void reset(int dir = 1);
- void reset(<T>DLList& l, int dir = 1);
- void insert_after(<T&>item);
- void insert_before(<T&>item);
- void del(int dir = 1);
- };
-
-
- extern void default_<T>DLList_error_handler(char*);
- extern one_arg_error_handler_t <T>DLList_error_handler;
-
- extern one_arg_error_handler_t
- set_<T>DLList_error_handler(one_arg_error_handler_t f);
-
-
- inline <T>DLList::~<T>DLList()
- {
- clear();
- }
-
- inline <T>DLList::<T>DLList()
- {
- h = 0;
- }
-
-
- inline int <T>DLList::null()
- {
- return h == 0;
- }
-
- inline int <T>DLList::empty()
- {
- return h == 0;
- }
-
- inline int <T>DLList::valid()
- {
- return h != 0;
- }
-
- inline const void* <T>DLList::operator void* ()
- {
- return (h == 0)? 0 : this;
- }
-
- inline int <T>DLList::operator ! ()
- {
- return h == 0;
- }
-
- inline int <T>DLList::length()
- {
- if (h == 0)
- return 0;
- else
- {
- int l = 1;
- for (<T>DLListNode* p = h->fd; p != h; p = p->fd) ++l;
- return l;
- }
- }
-
-
- inline <T>DLListTrav::<T>DLListTrav(<T>DLList& a, int dir = 1)
- {
- L = &a;
- if ((current = L->h) != 0 && dir < 0) current = current->bk;
- }
-
- inline void <T>DLListTrav::reset(<T>DLList& a, int dir = 1)
- {
- L = &a;
- if ((current = L->h) != 0 && dir < 0) current = current->bk;
- }
-
- inline void <T>DLListTrav::reset(int dir = 1)
- {
- if ((current = L->h) != 0 && dir < 0) current = current->bk;
- }
-
- inline <T>DLListTrav::~<T>DLListTrav() {}
-
- inline int <T>DLListTrav::null()
- {
- return current == 0;
- }
-
- inline int <T>DLListTrav::valid()
- {
- return current != 0;
- }
-
- inline const void* <T>DLListTrav::operator void* ()
- {
- return (current == 0)? 0 : this;
- }
-
- inline int <T>DLListTrav::operator ! ()
- {
- return (current == 0);
- }
-
- inline void <T>DLListTrav::advance(int dir = 1)
- {
- if (current != 0)
- {
- if (dir < 0)
- current = (current == L->h)? 0 : current->bk;
- else
- current = (current == L->h->bk)? 0 : current->fd;
- }
- }
-
- inline <T>& <T>DLListTrav::get()
- {
- if (current == 0)
- (*<T>DLList_error_handler)("get from null traverser");
- return current->hd;
- }
-
-
- inline <T>& <T>DLList::front()
- {
- if (h == 0)
- error("front: empty list");
- return h->hd;
- }
-
- inline <T>& <T>DLList::rear()
- {
- if (h == 0)
- error("rear: empty list");
- return h->bk->hd;
- }
-
-
-
-
- #endif
-