// 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. */ #ifdef __GNUG__ #pragma implementation #endif #include ".CHBag.h" // The nodes are linked together serially via a version // of a trick used in some vtables: odd pointers are // actually links to the next table entry. // Not terrible, but not wonderful either static inline int goodCHptr(CHNode* t) { return ((((unsigned)t) & 1) == 0); } static inline CHNode* index_to_CHptr(int i) { return (CHNode*)((i << 1) + 1); } static inline int CHptr_to_index(CHNode* t) { return ( ((unsigned) t) >> 1); } CHBag::CHBag(unsigned int sz) { tab = (CHNode**)(new CHNodePtr[size = sz]); for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1); count = 0; } CHBag::CHBag(CHBag& a) { tab = (CHNode**)(new CHNodePtr[size = a.size]); for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1); count = 0; for (Pix p = a.first(); p; a.next(p)) add(a(p)); } Pix CHBag::seek( key, Pix i) { CHNode* p = (CHNode*)i; if (p == 0 || !EQ(p->hd, key)) { unsigned int h = HASH(key) % size; for (CHNode* t = tab[h]; goodCHptr(t); t = t->tl) if (EQ(key, t->hd)) return Pix(t); } else { for (p = p->tl; goodCHptr(p); p = p->tl) if (EQ(p->hd, key)) return Pix(p); } return 0; } int CHBag::nof( key) { int n = 0; unsigned int h = HASH(key) % size; for (CHNode* t = tab[h]; goodCHptr(t); t = t->tl) if (EQ(key, t->hd)) ++n; return n; } Pix CHBag::add( item) { unsigned int h = HASH(item) % size; CHNode* t = new CHNode(item); t->tl = tab[h]; tab[h] = t; ++count; return Pix(t); } void CHBag::del( key) { unsigned int h = HASH(key) % size; CHNode* t = tab[h]; CHNode* trail = t; while (goodCHptr(t)) { if (EQ(key, t->hd)) { if (trail == t) tab[h] = t->tl; else trail->tl = t->tl; delete t; --count; return; } trail = t; t = t->tl; } } void CHBag::remove( key) { unsigned int h = HASH(key) % size; CHNode* t = tab[h]; CHNode* trail = t; while (goodCHptr(t)) { if (EQ(key, t->hd)) { --count; if (trail == t) { tab[h] = t->tl; delete t; t = trail = tab[h]; } else { trail->tl = t->tl; delete t; t = trail->tl; } } else { trail = t; t = t->tl; } } } void CHBag::clear() { for (unsigned int i = 0; i < size; ++i) { CHNode* p = tab[i]; tab[i] = index_to_CHptr(i+1); while (goodCHptr(p)) { CHNode* nxt = p->tl; delete(p); p = nxt; } } count = 0; } Pix CHBag::first() { for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]); return 0; } void CHBag::next(Pix& p) { if (p == 0) return; CHNode* t = ((CHNode*)p)->tl; if (goodCHptr(t)) p = Pix(t); else { for (unsigned int i = CHptr_to_index(t); i < size; ++i) { if (goodCHptr(tab[i])) { p = Pix(tab[i]); return; } } p = 0; } } int CHBag::OK() { int v = tab != 0; int n = 0; for (unsigned int i = 0; i < size; ++i) { for (CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n; v &= CHptr_to_index(p) == i + 1; } v &= count == n; if (!v) error("invariant failure"); return v; }