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><U>CHashAssoc.h"
-
- // error handling
-
-
- void default_<T><U>CHashAssoc_error_handler(char* msg)
- {
- cerr << "Fatal <T><U>CHashAssoc error. " << msg << "\n";
- exit(1);
- }
-
- one_arg_error_handler_t <T><U>CHashAssoc_error_handler = default_<T><U>CHashAssoc_error_handler;
-
- one_arg_error_handler_t set_<T><U>CHashAssoc_error_handler(one_arg_error_handler_t f)
- {
- one_arg_error_handler_t old = <T><U>CHashAssoc_error_handler;
- <T><U>CHashAssoc_error_handler = f;
- return old;
- }
-
- void <T><U>CHashAssoc::error(const char* msg)
- {
- (*<T><U>CHashAssoc_error_handler)(msg);
- }
-
- <T><U>CHashAssoc::<T><U>CHashAssoc(int sz)
- {
- tab = (<T><U>SLListNode**)(new <T><U>SLListNodePtr[size = sz]);
- for (int i = 0; i < size; ++i) tab[i] = 0;
- cnt = 0;
- }
-
- <T><U>CHashAssoc::<T><U>CHashAssoc(<T><U>CHashAssoc& a)
- {
- tab = (<T><U>SLListNode**)(new <T><U>SLListNodePtr[size = a.size]);
- for (int i = 0; i < size; ++i) tab[i] = 0;
- cnt = 0;
- for (<T><U>CHashAssocTrav p(a); p; p.advance())
- (*this)[p.key()] = p.get();
- }
-
- <T><U>CHashAssoc& <T><U>CHashAssoc::operator = (<T><U>CHashAssoc& a)
- {
- if (a.tab != tab)
- {
- clear();
- delete tab;
- tab = (<T><U>SLListNode**)(new <T><U>SLListNodePtr[size = a.size]);
- for (int i = 0; i < size; ++i) tab[i] = 0;
- cnt = 0;
- for (<T><U>CHashAssocTrav p(a); p; p.advance())
- (*this)[p.key()] = p.get();
- }
- return *this;
- }
-
-
- <U>& <T><U>CHashAssoc::operator [](<T&> key)
- {
- int h = key_hash(key) % size;
-
- for (<T><U>SLListNode* t = tab[h]; t != 0; t = t->tl)
- if (key_key_eq(t->key, key))
- return t->cont;
- t = new <T><U>SLListNode(key);
- t->tl = tab[h];
- tab[h] = t;
- ++cnt;
- return t->cont;
- }
-
- int <T><U>CHashAssoc::contains(<T&> key)
- {
- int h = key_hash(key) % size;
-
- for (<T><U>SLListNode* t = tab[h]; t != 0; t = t->tl)
- if (key_key_eq(t->key, key))
- return 1;
- return 0;
- }
-
- int <T><U>CHashAssoc::del(<T&> key)
- {
- int h = key_hash(key) % size;
-
- <T><U>SLListNode* t = tab[h];
- <T><U>SLListNode* trail = t;
- while (t != 0)
- {
- if (key_key_eq(t->key, key))
- {
- if (trail == t)
- tab[h] = t->tl;
- else
- trail->tl = t->tl;
- delete t;
- --cnt;
- return 1;
- }
- trail = t;
- t = t->tl;
- }
- return 0;
- }
-
- void <T><U>CHashAssoc::apply(<U>Procedure f)
- {
- for (int i = 0; i < size; ++i)
- {
- for (<T><U>SLListNode* p = tab[i]; p != 0; p = p->tl)
- (*f)(p->cont);
- }
- }
-
- void <T><U>CHashAssoc::clear()
- {
- for (int i = 0; i < size; ++i)
- {
- <T><U>SLListNode* p = tab[i];
- tab[i] = 0;
- while (p != 0)
- {
- <T><U>SLListNode* nxt = p->tl;
- delete(p);
- p = nxt;
- }
- }
- cnt = 0;
- }
-
- void <T><U>CHashAssocTrav::reset()
- {
- for (int i = 0; i < h->size; ++i)
- {
- if (h->tab[i] != 0)
- {
- pos = i;
- p = h->tab[i];
- return;
- }
- }
- pos = -1;
- p = 0;
- }
-
- void <T><U>CHashAssocTrav::advance()
- {
- if (p->tl != 0)
- p = p->tl;
- else
- {
- for (int i = pos + 1; i < h->size; ++i)
- {
- if (h->tab[i] != 0)
- {
- pos = i;
- p = h->tab[i];
- return;
- }
- }
- pos = -1;
- p = 0;
- }
- }
-
-
-
-