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 "<T>OSet.h"
-
-
- // error handling
-
-
- void default_<T>OSet_error_handler(char* msg)
- {
- cerr << "Fatal <T>OSet error. " << msg << "\n";
- exit(1);
- }
-
- one_arg_error_handler_t <T>OSet_error_handler = default_<T>OSet_error_handler;
-
- one_arg_error_handler_t set_<T>OSet_error_handler(one_arg_error_handler_t f)
- {
- one_arg_error_handler_t old = <T>OSet_error_handler;
- <T>OSet_error_handler = f;
- return old;
- }
-
- void <T>OSet::error(const char* msg)
- {
- (*<T>OSet_error_handler)(msg);
- }
-
- <T>SLListNode* copy<T>SLListNodes(<T>SLListNode* a)
- {
- if (a == 0)
- return a;
- else
- {
- <T>SLListNode* h = new <T>SLListNode(a->hd);
- <T>SLListNode* trail = h;
- for(a = a->tl; a != 0; a = a->tl)
- {
- <T>SLListNode* n = new <T>SLListNode(a->hd);
- trail->tl = n;
- trail = n;
- }
- return h;
- }
- }
-
-
- void <T>OSet::clear()
- {
- <T>SLListNode* p = P;
- P = 0;
- while (p != 0)
- {
- <T>SLListNode* nxt = p->tl;
- delete(p);
- p = nxt;
- }
- }
-
-
- void <T>OSet::apply(<T>Procedure f)
- {
- for(<T>SLListNode* p = P; p != 0; p = p->tl)
- (*f)((p->hd));
- }
-
-
- <T> <T>OSet::reduce(<T>Combiner f, <T&> base)
- {
- <T> r = base;
- for(<T>SLListNode* p = P; p != 0; p = p->tl)
- r = (*f)(r, (p->hd));
- return r;
- }
-
- int <T>OSet::contains(<T&> targ)
- {
- <T>SLListNode* p = P;
- for (;;)
- {
- if (p == 0)
- return 0;
- int cmp = COMPARISON_FUNCTION(p->hd, targ);
- if (cmp == 0)
- return 1;
- else if (cmp > 0)
- return 0;
- else
- p = p->tl;
- }
- }
-
-
- void <T>OSetTrav::advance_to(<T&> targ)
- {
- <T>SLListNode* p = P;
- for (;;)
- {
- if (p == 0)
- {
- P = 0;
- return;
- }
- int cmp = COMPARISON_FUNCTION(p->hd, targ);
- if (cmp == 0)
- {
- P = p;
- return;
- }
- else if (cmp > 0)
- {
- P = 0;
- return;
- }
- else
- p = p->tl;
- }
- }
-
-
-
- void <T>OSet::del(<T&> targ)
- {
- <T>SLListNode* h = P;
- if (h == 0)
- return;
- int cmp = COMPARISON_FUNCTION(h->hd, targ);
- if (cmp == 0)
- {
- <T>SLListNode* nxt = h->tl;
- delete(h);
- P = nxt;
- return;
- }
-
- <T>SLListNode* trail = h;
- <T>SLListNode* p = h->tl;
- for (;;)
- {
- if (p == 0)
- return;
- cmp = COMPARISON_FUNCTION(p->hd, targ);
- if (cmp == 0)
- {
- <T>SLListNode* nxt = p->tl;
- delete(p);
- trail->tl = nxt;
- return;
- }
- else if (cmp > 0)
- return;
- else
- {
- trail = p;
- p = p->tl;
- }
- }
- }
-
-
- void <T>OSet::add(<T&> item)
- {
- <T>SLListNode* a = P;
- if (a == 0)
- {
- P = new <T>SLListNode(item);
- return;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, item);
- if (cmp == 0)
- return;
- else if (cmp > 0)
- {
- P = new <T>SLListNode(item);
- P->tl = a;
- return;
- }
- else
- {
- <T>SLListNode* h = a;
- <T>SLListNode* trail = h;
- a = a->tl;
- for (;;)
- {
- if (a == 0)
- {
- <T>SLListNode* n = new <T>SLListNode(item);
- trail->tl = n;
- return;
- }
- cmp = COMPARISON_FUNCTION(a->hd, item);
- if (cmp == 0)
- return;
- else if (cmp < 0)
- {
- trail = a;
- a = a->tl;
- }
- else
- {
- <T>SLListNode* n = new <T>SLListNode(item);
- n->tl = a;
- trail->tl = n;
- return;
- }
- }
- }
- }
-
- int operator == (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- for (;;)
- {
- if (a == 0)
- return b == 0;
- else if (b == 0 || COMPARISON_FUNCTION(a->hd, b->hd) != 0)
- return 0;
- else
- {
- a = a->tl;
- b = b->tl;
- }
- }
- }
-
- int operator <= (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- for (;;)
- {
- if (a == 0)
- return 1;
- else if (b == 0)
- return 0;
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- return 0;
- else
- b = b->tl;
- }
- }
-
- int operator < (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- int one_diff = 0;
- for (;;)
- {
- if (a == 0)
- return one_diff;
- else if (b == 0)
- return 0;
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- return 0;
- else
- {
- one_diff = 1;
- b = b->tl;
- }
- }
- }
-
-
-
- <T>OSet operator | (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- if (a == 0)
- return <T>OSet(y);
- else if (b == 0)
- return <T>OSet(x);
-
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- <T>SLListNode* h = new <T>SLListNode;
- if (cmp <= 0)
- {
- h->hd = a->hd;
- a = a->tl;
- }
- else
- {
- h->hd = b->hd;
- b = b->tl;
- }
-
- <T>SLListNode* r = h;
-
- for(;;)
- {
- if (a == 0)
- {
- r->tl = copy<T>SLListNodes(b);
- return <T>OSet(h);
- }
- else if (b == 0)
- {
- r->tl = copy<T>SLListNodes(a);
- return <T>OSet(h);
- }
- cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- a = a->tl;
- else if (cmp < 0)
- {
- <T>SLListNode* n = new <T>SLListNode(a->hd);
- r->tl = n;
- r = n;
- a = a->tl;
- }
- else
- {
- <T>SLListNode* n = new <T>SLListNode(b->hd);
- r->tl = n;
- r = n;
- b = b->tl;
- }
- }
- }
-
- void <T>OSet::destructive_union(<T>OSet& y)
- {
- <T>SLListNode* a = P;
- <T>SLListNode* b = y.P;
- y.P = 0;
- if (b == 0)
- return;
- if (a == 0)
- {
- P = b;
- return;
- }
-
- <T>SLListNode* r;
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- r = b;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r = a;
- a = a->tl;
- }
- else
- {
- r = b;
- b = b->tl;
- }
- P = r;
- for(;;)
- {
- if (a == 0)
- {
- r->tl = b;
- return;
- }
- else if (b == 0)
- {
- r->tl = a;
- return;
- }
- cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- r->tl = b;
- r = b;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r->tl = a;
- r = a;
- a = a->tl;
- }
- else
- {
- r->tl = b;
- r = b;
- b = b->tl;
- }
- }
- }
-
- <T>OSet& <T>OSet::operator |=(<T>OSet& y)
- {
- <T>SLListNode* a = P;
- <T>SLListNode* b = y.P;
- if (b == 0)
- return *this;
- else if (a == 0)
- {
- P = copy<T>SLListNodes(b);
- return *this;
- }
- <T>SLListNode* r;
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp <= 0)
- {
- r = a;
- a = a->tl;
- }
- else
- {
- r = new <T>SLListNode(b->hd);
- b = b->tl;
- }
- P = r;
-
- for(;;)
- {
- if (a == 0)
- {
- r->tl = copy<T>SLListNodes(b);
- return *this;
- }
- else if (b == 0)
- {
- r->tl = a;
- return *this;
- }
- cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- b = b->tl;
- else if (cmp < 0)
- {
- r->tl = a;
- r = a;
- a = a->tl;
- }
- else
- {
- <T>SLListNode* n = new <T>SLListNode(b->hd);
- r->tl = n;
- r = n;
- b = b->tl;
- }
- }
- }
-
-
- <T>OSet operator & (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- for (;;)
- {
- if (a == 0 || b == 0)
- return <T>OSet();
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- break;
- else if (cmp < 0)
- a = a->tl;
- else
- b = b->tl;
- }
-
- <T>SLListNode* h = new <T>SLListNode(a->hd);
- <T>SLListNode* r = h;
- a = a->tl;
- b = b->tl;
-
- for (;;)
- {
- if (a == 0 || b == 0)
- {
- return <T>OSet(h);
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* n = new <T>SLListNode(a->hd);
- r->tl = n;
- r = n;
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- a = a->tl;
- else
- b = b->tl;
- }
- }
-
- <T>OSet& <T>OSet::operator &= (<T>OSet& y)
- {
- <T>SLListNode* a = P;
- <T>SLListNode* b = y.P;
- for (;;)
- {
- if (a == 0 || b == 0)
- return *this;
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- break;
- else if (cmp < 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- }
- else
- b = b->tl;
- }
- P = a;
- <T>SLListNode* r = a;
- a = a->tl;
- b = b->tl;
-
- for (;;)
- {
- if (a == 0 || b == 0)
- {
- return *this;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- r->tl = a;
- r = a;
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- }
- else
- b = b->tl;
- }
- }
-
- <T>OSet operator - (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- for (;;)
- {
- if (a == 0 || b == 0)
- return <T>OSet();
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- break;
- else
- b = b->tl;
- }
-
- <T>SLListNode* h = new <T>SLListNode(a->hd);
- <T>SLListNode* r = h;
- a = a->tl;
-
- for (;;)
- {
- if (a == 0)
- {
- return <T>OSet(h);
- }
- else if (b == 0)
- {
- r->tl = copy<T>SLListNodes(a);
- return <T>OSet(h);
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- <T>SLListNode* n = new <T>SLListNode(a->hd);
- r->tl = n;
- r = n;
- a = a->tl;
- }
- else
- b = b->tl;
- }
- }
-
- <T>OSet operator ^ (<T>OSet& x, <T>OSet& y)
- {
- <T>SLListNode* a = x.P;
- <T>SLListNode* b = y.P;
- <T>SLListNode* h;
- for (;;)
- {
- if (a == 0)
- return <T>OSet(y);
- else if (b == 0)
- return <T>OSet(a);
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- h = new <T>SLListNode(a->hd);
- a = a->tl;
- break;
- }
- else
- {
- h = new <T>SLListNode(b->hd);
- b = b->tl;
- break;
- }
- }
-
- <T>SLListNode* r = h;
-
- for (;;)
- {
- if (a == 0)
- {
- r->tl = copy<T>SLListNodes(b);
- return <T>OSet(h);
- }
- else if (b == 0)
- {
- r->tl = copy<T>SLListNodes(a);
- return <T>OSet(h);
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- <T>SLListNode* n = new <T>SLListNode(a->hd);
- r->tl = n;
- r = n;
- a = a->tl;
- }
- else
- {
- <T>SLListNode* n = new <T>SLListNode(b->hd);
- r->tl = n;
- r = n;
- b = b->tl;
- }
- }
- }
-
- <T>OSet& <T>OSet::operator -=(<T>OSet& y)
- {
- <T>SLListNode* a = P;
- <T>SLListNode* b = y.P;
- <T>SLListNode* r;
- for (;;)
- {
- if (a == 0 || b == 0)
- {
- P = a;
- return *this;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r = a;
- a = a->tl;
- break;
- }
- else
- b = b->tl;
- }
- P = r;
-
- for (;;)
- {
- if (a == 0 || b == 0)
- {
- r->tl = a;
- return *this;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r->tl = a;
- r = a;
- a = a->tl;
- }
- else
- b = b->tl;
- }
- }
-
- <T>OSet& <T>OSet::operator ^=(<T>OSet& y)
- {
- <T>SLListNode* a = P;
- <T>SLListNode* b = y.P;
- <T>SLListNode* r;
- for (;;)
- {
- if (a == 0)
- {
- P = copy<T>SLListNodes(b);
- return *this;
- }
- else if (b == 0)
- {
- P = a;
- return *this;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- <T>SLListNode* t = a->tl;
- delete(a);
- a = t;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r = a;
- a = a->tl;
- break;
- }
- else
- {
- r = b;
- b = b->tl;
- break;
- }
- }
- P = r;
-
- for (;;)
- {
- if (a == 0)
- {
- r->tl = copy<T>SLListNodes(b);
- return *this;
- }
- else if (b == 0)
- {
- r->tl = a;
- return *this;
- }
- int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
- if (cmp == 0)
- {
- a = a->tl;
- b = b->tl;
- }
- else if (cmp < 0)
- {
- r->tl = a;
- r = a;
- a = a->tl;
- }
- else
- {
- <T>SLListNode* n = new <T>SLListNode(b->hd);
- r->tl = n;
- r = n;
- b = b->tl;
- }
- }
- }
-