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.
- */
-
- #include <stream.h>
- #include "<T>DLList.h"
-
- // error handling
-
-
- void default_<T>DLList_error_handler(char* msg)
- {
- cerr << "Fatal <T>DLList error. " << msg << "\n";
- exit(1);
- }
-
- one_arg_error_handler_t <T>DLList_error_handler = default_<T>DLList_error_handler;
-
- one_arg_error_handler_t set_<T>DLList_error_handler(one_arg_error_handler_t f)
- {
- one_arg_error_handler_t old = <T>DLList_error_handler;
- <T>DLList_error_handler = f;
- return old;
- }
-
- void <T>DLList::error(const char* msg)
- {
- (*<T>DLList_error_handler)(msg);
- }
-
- <T>DLList::<T>DLList(<T>DLList& a)
- {
- if (a.h == 0)
- h = 0;
- else
- {
- <T>DLListNode* p = a.h;
- <T>DLListNode* t = new <T>DLListNode(p->hd);
- h = t;
- p = p->fd;
- while (p != a.h)
- {
- <T>DLListNode* n = new <T>DLListNode(p->hd);
- t->fd = n;
- n->bk = t;
- t = n;
- p = p->fd;
- }
- t->fd = h;
- h->bk = t;
- return;
- }
- }
-
- inline <T>DLList& <T>DLList::operator = (<T>DLList& a)
- {
- if (h != a.h)
- {
- clear();
- if (a.h != 0)
- {
- <T>DLListNode* p = a.h;
- <T>DLListNode* t = new <T>DLListNode(p->hd);
- h = t;
- p = p->fd;
- while (p != a.h)
- {
- <T>DLListNode* n = new <T>DLListNode(p->hd);
- t->fd = n;
- n->bk = t;
- t = n;
- p = p->fd;
- }
- t->fd = h;
- h->bk = t;
- }
- }
- return *this;
- }
- void <T>DLList::clear()
- {
- if (h == 0)
- return;
-
- <T>DLListNode* p = h->fd;
- h->fd = 0;
- h = 0;
-
- while (p != 0)
- {
- <T>DLListNode* nxt = p->fd;
- delete(p);
- p = nxt;
- }
- }
-
-
- void <T>DLList::prepend(<T&> item)
- {
- <T>DLListNode* t = new <T>DLListNode(item);
- if (h == 0)
- t->fd = t->bk = h = t;
- else
- {
- t->fd = h->fd;
- t->bk = h;
- h->fd = t;
- h = t;
- }
- }
-
- void <T>DLList::append(<T&> item)
- {
- <T>DLListNode* t = new <T>DLListNode(item);
- if (h == 0)
- t->fd = t->bk = h = t;
- else
- {
- t->fd = h;
- t->bk = h->bk;
- h->bk->fd = t;
- h->bk = t;
- }
- }
-
-
- void <T>DLListTrav::insert_after(<T&> item)
- {
- if (current == 0)
- {
- L->error("insert_after: null traverser");
- return;
- }
- <T>DLListNode* t = new <T>DLListNode(item, current, current->fd);
- current->fd->bk = t;
- current->fd = t;
- }
-
- void <T>DLListTrav::insert_before(<T&> item)
- {
- if (current == 0)
- {
- L->error("insert_before: null traverser");
- return;
- }
- <T>DLListNode* t = new <T>DLListNode(item, current->bk, current);
- current->bk->fd = t;
- current->bk = t;
- if (current == L->h)
- L->h = t;
- }
-
- void <T>DLListTrav::del(int dir = 1)
- {
- if (current == 0)
- {
- L->error("del: null traverser");
- return;
- }
- <T>DLListNode* t = current;
- if (t->fd == t)
- current = L->h = 0;
- else
- {
- if (dir < 0)
- {
- if (t == L->h)
- current == 0;
- else
- current = t->bk;
- }
- else
- {
- if (t == L->h->bk)
- current = 0;
- else
- current = t->fd;
- }
- t->bk->fd = t->fd;
- t->fd->bk = t->bk;
- if (t == L->h)
- L->h = t->fd;
- }
- delete t;
- }
-
- <T> <T>DLList::remove_front()
- {
- if (h == 0)
- error("remove_front of empty list");
- <T>DLListNode* t = h;
- <T> res = t->hd;
- if (h->fd == h)
- h = 0;
- else
- {
- h->fd->bk = h->bk;
- h->bk->fd = h->fd;
- h = h->fd;
- }
- delete t;
- return res;
- }
-
-
- void <T>DLList::del_front()
- {
- if (h == 0)
- error("del_front of empty list");
- <T>DLListNode* t = h;
- if (h->fd == h)
- h = 0;
- else
- {
- h->fd->bk = h->bk;
- h->bk->fd = h->fd;
- h = h->fd;
- }
- delete t;
- }
-
- <T> <T>DLList::remove_rear()
- {
- if (h == 0)
- error("remove_rear of empty list");
- <T>DLListNode* t = h->bk;
- <T> res = t->hd;
- if (h->fd == h)
- h = 0;
- else
- {
- t->fd->bk = t->bk;
- t->bk->fd = t->fd;
- }
- delete t;
- return res;
- }
-
-
- void <T>DLList::del_rear()
- {
- if (h == 0)
- error("del_rear of empty list");
- <T>DLListNode* t = h->bk;
- if (h->fd == h)
- h = 0;
- else
- {
- t->fd->bk = t->bk;
- t->bk->fd = t->fd;
- }
- delete t;
- }
-