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><U>AVLAssoc_h
- #define _<T><U>AVLAssoc_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
-
- #ifndef _<U>_typedefs
- #define _<U>_typedefs 1
- typedef void (*<U>Procedure)(<U&>);
- typedef <U> (*<U>Mapper)(<U&>);
- typedef <U> (*<U>Combiner)(<U&>, <U&>);
- typedef int (*<U>Predicate)(<U&>);
- typedef int (*<U>Comparator)(<U&>, <U&>);
- #endif
-
-
- #ifndef _<T><U>AVLAssocNode
- #define _<T><U>AVLAssocNode
-
- struct <T><U>AVLAssocNode
- {
- <T><U>AVLAssocNode* lt;
- <T><U>AVLAssocNode* rt;
- <T> key;
- <U> cont;
- char stat;
- <T><U>AVLAssocNode(<T&> k,
- <T><U>AVLAssocNode* l=0,
- <T><U>AVLAssocNode* r=0);
- <T><U>AVLAssocNode(<T&> k, <U&> c,
- <T><U>AVLAssocNode* l=0,
- <T><U>AVLAssocNode* r=0);
- ~<T><U>AVLAssocNode();
- };
-
-
- inline <T><U>AVLAssocNode::<T><U>AVLAssocNode(<T&> k,
- <T><U>AVLAssocNode* l=0,
- <T><U>AVLAssocNode* r=0)
- {
- key = k;
- lt = l;
- rt = r;
- stat = 0;
- }
-
- inline <T><U>AVLAssocNode::<T><U>AVLAssocNode(<T&> k, <U&> c,
- <T><U>AVLAssocNode* l=0,
- <T><U>AVLAssocNode* r=0)
- {
- key = k;
- cont = c;
- lt = l;
- rt = r;
- stat = 0;
- }
-
- inline <T><U>AVLAssocNode::~<T><U>AVLAssocNode() {}
-
- typedef <T><U>AVLAssocNode* <T><U>AVLAssocNodePtr;
-
- #endif
-
- class <T><U>AVLAssoc;
- class <T><U>AVLAssocTrav;
-
- class <T><U>AVLAssoc
- {
- friend class <T><U>AVLAssocTrav;
-
- <T><U>AVLAssocNode* root;
- int cnt;
-
- <T><U>AVLAssoc(<T><U>AVLAssocNode* p, int l);
- <T><U>AVLAssocNode* leftmost();
- <T><U>AVLAssocNode* rightmost();
- <T><U>AVLAssocNode* pred(<T><U>AVLAssocNode* t);
- <T><U>AVLAssocNode* succ(<T><U>AVLAssocNode* t);
- void _kill(<T><U>AVLAssocNode* t);
- void _add(<T><U>AVLAssocNode*& t);
- void _del(<T><U>AVLAssocNode* p, <T><U>AVLAssocNode*& t);
-
- public:
-
- static <T>Comparator key_key_comparison_function;
- int key_key_cmp(<T&>a, <T&> b);
-
- <T><U>AVLAssoc();
- <T><U>AVLAssoc(<T><U>AVLAssoc& a);
- ~<T><U>AVLAssoc();
-
- <T><U>AVLAssoc& operator = (<T><U>AVLAssoc& a);
-
- int count();
- int empty();
- void clear();
-
- <U>& operator [] (<T&> k);
- int contains(<T&> key);
- int del(<T&> key);
-
- void apply(<U>Procedure f);
-
- void error(const char* msg);
- };
-
- class <T><U>AVLAssocTrav
- {
- <T><U>AVLAssoc* h;
- <T><U>AVLAssocNode* current;
-
- public:
- <T><U>AVLAssocTrav(<T><U>AVLAssoc& l, int dir = 1);
- ~<T><U>AVLAssocTrav();
-
- int null();
- int valid();
- const void* operator void* ();
- int operator ! ();
- void advance(int dir = 1);
- void reset(int dir = 1);
- void reset(<T><U>AVLAssoc& l, int dir = 1);
- <U>& get();
- const <T>& key();
- };
-
-
- inline int <T><U>AVLAssoc::key_key_cmp(<U&>a, <U&> b)
- {
- #ifdef <T>COMPARISONFUNCTION
- return <T>COMPARISONFUNCTION(a, b);
- #else
- return (*<T><U>AVLAssoc::key_key_comparison_function)(a, b);
- #endif
- }
-
- inline <T><U>AVLAssoc::<T><U>AVLAssoc()
- {
- root = 0;
- cnt = 0;
- }
-
-
- inline <T><U>AVLAssoc::<T><U>AVLAssoc(<T><U>AVLAssocNode* p, int l)
- {
- root = p;
- cnt = l;
- }
-
- inline <T><U>AVLAssoc::~<T><U>AVLAssoc()
- {
- _kill(root);
- }
-
- inline int <T><U>AVLAssoc::count()
- {
- return cnt;
- }
-
- inline int <T><U>AVLAssoc::empty()
- {
- return cnt == 0;
- }
-
-
- inline void <T><U>AVLAssocTrav::reset(int dir = 1)
- {
- if (dir < 0)
- current = h->rightmost();
- else
- current = h->leftmost();
- }
-
-
- inline void <T><U>AVLAssocTrav::advance(int dir = 1)
- {
- if (current != 0)
- {
- if (dir < 0)
- current = h->pred(current);
- else
- current = h->succ(current);
- }
- }
-
- inline <T><U>AVLAssocTrav::<T><U>AVLAssocTrav(<T><U>AVLAssoc& a, int dir = 1)
- {
- h = &a;
- reset(dir);
- }
-
- inline void <T><U>AVLAssocTrav::reset(<T><U>AVLAssoc& a, int dir = 1)
- {
- h = &a;
- reset(dir);
- }
-
- inline <T><U>AVLAssocTrav::~<T><U>AVLAssocTrav() {}
-
- inline int <T><U>AVLAssocTrav::null()
- {
- return current == 0;
- }
-
- inline int <T><U>AVLAssocTrav::valid()
- {
- return current != 0;
- }
-
- inline const void* <T><U>AVLAssocTrav::operator void* ()
- {
- return (current == 0)? 0 : this;
- }
-
- inline int <T><U>AVLAssocTrav::operator ! ()
- {
- return (current == 0);
- }
-
-
- inline <U>& <T><U>AVLAssocTrav::get()
- {
- if (current == 0)
- h->error("get of null traverser");
- return current->cont;
- }
-
- inline const <T>& <T><U>AVLAssocTrav::key()
- {
- if (current == 0)
- h->error("get of null traverser");
- return current->key;
- }
-
- inline void <T><U>AVLAssoc::clear()
- {
- _kill(root);
- cnt = 0;
- root = 0;
- }
-
- inline void <T><U>AVLAssoc::apply(<U>Procedure f)
- {
- for (<T><U>AVLAssocNode* t = leftmost(); t != 0; t = succ(t))
- (*f)(t->cont);
- }
-
-
- extern void default_<T><U>AVLAssoc_error_handler(char*);
- extern one_arg_error_handler_t <T><U>AVLAssoc_error_handler;
-
- extern one_arg_error_handler_t
- set_<T><U>AVLAssoc_error_handler(one_arg_error_handler_t f);
-
-
- #endif
-