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>BSTAssoc_h
- #define _<T><U>BSTAssoc_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>BSTAssocNode
- #define _<T><U>BSTAssocNode
-
- struct <T><U>BSTAssocNode
- {
- <T><U>BSTAssocNode* lt;
- <T><U>BSTAssocNode* rt;
- <T> key;
- <U> cont;
- <T><U>BSTAssocNode(<T&> k,
- <T><U>BSTAssocNode* l=0,
- <T><U>BSTAssocNode* r=0);
- <T><U>BSTAssocNode(<T&> k, <U&> c,
- <T><U>BSTAssocNode* l=0,
- <T><U>BSTAssocNode* r=0);
- ~<T><U>BSTAssocNode();
- };
-
-
- inline <T><U>BSTAssocNode::<T><U>BSTAssocNode(<T&> k,
- <T><U>BSTAssocNode* l=0,
- <T><U>BSTAssocNode* r=0)
- {
- key = k;
- lt = l;
- rt = r;
- }
-
- inline <T><U>BSTAssocNode::<T><U>BSTAssocNode(<T&> k, <U&> c,
- <T><U>BSTAssocNode* l=0,
- <T><U>BSTAssocNode* r=0)
- {
- key = k;
- cont = c;
- lt = l;
- rt = r;
- }
-
- inline <T><U>BSTAssocNode::~<T><U>BSTAssocNode() {}
-
- typedef <T><U>BSTAssocNode* <T><U>BSTAssocNodePtr;
-
- #endif
-
- class <T><U>BSTAssoc;
- class <T><U>BSTAssocTrav;
-
- class <T><U>BSTAssoc
- {
- friend class <T><U>BSTAssocTrav;
-
- <T><U>BSTAssocNode* root;
- int cnt;
-
- void _apply(<U>Procedure f, <T><U>BSTAssocNode* t);
- void _kill(<T><U>BSTAssocNode* t);
- <T><U>BSTAssocNode* _copy(<T><U>BSTAssocNode* t);
-
- public:
- static <T>Comparator key_key_comparison_function;
- int key_key_cmp(<T&>a, <T&> b);
-
-
- <T><U>BSTAssoc();
- <T><U>BSTAssoc(<T><U>BSTAssoc& a);
- ~<T><U>BSTAssoc();
-
- <T><U>BSTAssoc& operator = (<T><U>BSTAssoc& 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 balance();
- void error(const char* msg);
- };
-
- class <T><U>BSTAssocTrav
- {
- <T><U>BSTAssoc* h;
- <T><U>BSTAssocNode** stk;
- int sp;
-
- public:
- <T><U>BSTAssocTrav(<T><U>BSTAssoc& l);
- ~<T><U>BSTAssocTrav();
-
- int null();
- int valid();
- const void* operator void* ();
- int operator ! ();
- void advance();
- void reset();
- void reset(<T><U>BSTAssoc& l);
- <U>& get();
- const <T>& key();
- };
-
- inline int <T><U>BSTAssoc::key_key_cmp(<U&>a, <U&> b)
- {
- #ifdef <T>COMPARISONFUNCTION
- return <T>COMPARISONFUNCTION(a, b);
- #else
- return (*<T><U>BSTAssoc::key_key_comparison_function)(a, b);
- #endif
- }
-
-
-
- inline <T><U>BSTAssoc::<T><U>BSTAssoc()
- {
- root = 0;
- cnt = 0;
- }
-
- inline <T><U>BSTAssoc::<T><U>BSTAssoc(<T><U>BSTAssoc& a)
- {
- cnt = a.cnt;
- root = _copy(a.root);
- }
-
- inline <T><U>BSTAssoc& <T><U>BSTAssoc::operator = (<T><U>BSTAssoc& a)
- {
- if (a.root != root)
- {
- _kill(root);
- cnt = a.cnt;
- root = _copy(a.root);
- }
- return *this;
- }
-
- inline <T><U>BSTAssoc::~<T><U>BSTAssoc()
- {
- _kill(root);
- }
-
- inline int <T><U>BSTAssoc::count()
- {
- return cnt;
- }
-
- inline int <T><U>BSTAssoc::empty()
- {
- return cnt == 0;
- }
-
- inline <T><U>BSTAssocTrav::<T><U>BSTAssocTrav(<T><U>BSTAssoc& a)
- {
- h = &a;
- stk = 0;
- reset();
- }
-
- inline void <T><U>BSTAssocTrav::reset(<T><U>BSTAssoc& a)
- {
- h = &a;
- reset();
- }
-
- inline <T><U>BSTAssocTrav::~<T><U>BSTAssocTrav()
- {
- delete stk;
- }
-
- inline int <T><U>BSTAssocTrav::null()
- {
- return sp < 0;
- }
-
- inline int <T><U>BSTAssocTrav::valid()
- {
- return sp >= 0;
- }
-
- inline const void* <T><U>BSTAssocTrav::operator void* ()
- {
- return (sp < 0)? 0 : this;
- }
-
- inline int <T><U>BSTAssocTrav::operator ! ()
- {
- return (sp < 0);
- }
-
-
- inline <U>& <T><U>BSTAssocTrav::get()
- {
- if (sp < 0)
- h->error("get of null traverser");
- return stk[sp]->cont;
- }
-
- inline const <U>& <T><U>BSTAssocTrav::key()
- {
- if (sp < 0)
- h->error("get of null traverser");
- return stk[sp]->key;
- }
-
- inline void <T><U>BSTAssoc::clear()
- {
- _kill(root);
- cnt = 0;
- root = 0;
- }
-
- inline void <T><U>BSTAssoc::apply(<U>Procedure f)
- {
- _apply(f, root);
- }
-
- extern void default_<T><U>BSTAssoc_error_handler(char*);
- extern one_arg_error_handler_t <T><U>BSTAssoc_error_handler;
-
- extern one_arg_error_handler_t
- set_<T><U>BSTAssoc_error_handler(one_arg_error_handler_t f);
-
-
- #endif
-