// 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 #include #include ".SLList.h" void SLList::error(const char* msg) { (*lib_error_handler)("SLList", msg); } SLList::SLList(SLList& a) { if (a.last == 0) last = 0; else { SLListNode* p = a.last->tl; SLListNode* h = new SLListNode(p->hd); last = h; for (;;) { if (p == a.last) { last->tl = h; return; } p = p->tl; SLListNode* n = new SLListNode(p->hd); last->tl = n; last = n; } } } SLList& SLList::operator = (SLList& a) { if (last != a.last) { clear(); if (a.last != 0) { SLListNode* p = a.last->tl; SLListNode* h = new SLListNode(p->hd); last = h; for (;;) { if (p == a.last) { last->tl = h; break; } p = p->tl; SLListNode* n = new SLListNode(p->hd); last->tl = n; last = n; } } } return *this; } void SLList::clear() { if (last == 0) return; SLListNode* p = last->tl; last->tl = 0; last = 0; while (p != 0) { SLListNode* nxt = p->tl; delete(p); p = nxt; } } Pix SLList::prepend( item) { SLListNode* t = new SLListNode(item); if (last == 0) t->tl = last = t; else { t->tl = last->tl; last->tl = t; } return Pix(t); } Pix SLList::prepend(SLListNode* t) { if (t == 0) return 0; if (last == 0) t->tl = last = t; else { t->tl = last->tl; last->tl = t; } return Pix(t); } Pix SLList::append( item) { SLListNode* t = new SLListNode(item); if (last == 0) t->tl = last = t; else { t->tl = last->tl; last->tl = t; last = t; } return Pix(t); } Pix SLList::append(SLListNode* t) { if (t == 0) return 0; if (last == 0) t->tl = last = t; else { t->tl = last->tl; last->tl = t; last = t; } return Pix(t); } void SLList::join(SLList& b) { SLListNode* t = b.last; b.last = 0; if (last == 0) last = t; else if (t != 0) { SLListNode* f = last->tl; last->tl = t->tl; t->tl = f; last = t; } } Pix SLList::ins_after(Pix p, item) { SLListNode* u = (SLListNode*)p; SLListNode* t = new SLListNode(item); if (last == 0) t->tl = last = t; else if (u == 0) // ins_after 0 means prepend { t->tl = last->tl; last->tl = t; } else { t->tl = u->tl; u->tl = t; if (u == last) last = t; } return Pix(t); } void SLList::del_after(Pix p) { SLListNode* u = (SLListNode*)p; if (last == 0 || u == last) error("cannot del_after last"); if (u == 0) u = last; // del_after 0 means delete first SLListNode* t = u->tl; if (u == t) last = 0; else { u->tl = t->tl; if (last == t) last = u; } delete t; } int SLList::owns(Pix p) { SLListNode* t = last; if (t != 0 && p != 0) { do { if (Pix(t) == p) return 1; t = t->tl; } while (t != last); } return 0; } SLList::remove_front() { if (last == 0) error("remove_front of empty list"); SLListNode* t = last->tl; res = t->hd; if (t == last) last = 0; else last->tl = t->tl; delete t; return res; } int SLList::remove_front(& x) { if (last == 0) return 0; else { SLListNode* t = last->tl; x = t->hd; if (t == last) last = 0; else last->tl = t->tl; delete t; return 1; } } void SLList::del_front() { if (last == 0) error("del_front of empty list"); SLListNode* t = last->tl; if (t == last) last = 0; else last->tl = t->tl; delete t; } int SLList::OK() { int v = 1; if (last != 0) { SLListNode* t = last; long count = MAXLONG; // Lots of chances to find last! do { count--; t = t->tl; } while (count > 0 && t != last); v &= count > 0; } if (!v) error("invariant failure"); return v; }