// 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 the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
// A CHSet is implemented as an array (tab) of buckets, each of which
// contains a pointer to a list of <T>CHNodes. Each node contains a
// pointer to the next node in the list, and a pointer to the <T>.
// The end of the list is marked by a next node pointer which is odd
// when considered as an integer (least significant bit = 1). The
// assumption is that CHNodes will all begin on even addresses. If
// the odd pointer is right-shifted by one bit, it becomes the index
// within the tab array of the next bucket (that is, bucket i has
// next bucket pointer 2*(i+1)+1).
// The bucket pointers are initialized by the constructor and
// used to support the next(Pix&) method.
// This implementation is not portable to machines with different
// pointer and integer sizes, or on which CHNodes might be aligned on
// odd byte boundaries, but allows the same pointer to be used for
// chaining within a bucket and to the next bucket.
static inline int goodCHptr(<T>CHNode* t)
return ((((unsigned)t) & 1) == 0);
static inline <T>CHNode* index_to_CHptr(int i)
return (<T>CHNode*)((i << 1) + 1);
static inline int CHptr_to_index(<T>CHNode* t)
return ( ((unsigned) t) >> 1);
<T>CHSet::<T>CHSet(unsigned int sz)
tab = (<T>CHNode**)(new <T>CHNodePtr[size = sz]);
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
<T>CHSet::<T>CHSet(<T>CHSet& a)
tab = (<T>CHNode**)(new <T>CHNodePtr[size = a.size]);
for (unsigned int i = 0; i < size; ++i) tab[i] = index_to_CHptr(i+1);
for (Pix p = a.first(); p; a.next(p)) add(a(p));
Pix <T>CHSet::seek(<T&> key)
unsigned int h = <T>HASH(key) % size;
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
Pix <T>CHSet::add(<T&> item)
unsigned int h = <T>HASH(item) % size;
for (<T>CHNode* t = tab[h]; goodCHptr(t); t = t->tl)
t = new <T>CHNode(item, tab[h]);
void <T>CHSet::del(<T&> key)
unsigned int h = <T>HASH(key) % size;
for (unsigned int i = 0; i < size; ++i)
tab[i] = index_to_CHptr(i+1);
for (unsigned int i = 0; i < size; ++i) if (goodCHptr(tab[i])) return Pix(tab[i]);
void <T>CHSet::next(Pix& p)
<T>CHNode* t = ((<T>CHNode*)p)->tl;
for (unsigned int i = CHptr_to_index(t); i < size; ++i)
int <T>CHSet::operator == (<T>CHSet& b)
for (unsigned int i = 0; i < size; ++i)
for (p = tab[i]; goodCHptr(p); p = p->tl)
for (i = 0; i < b.size; ++i)
for (p = b.tab[i]; goodCHptr(p); p = p->tl)
int <T>CHSet::operator <= (<T>CHSet& b)
for (unsigned int i = 0; i < size; ++i)
for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl)
void <T>CHSet::operator |= (<T>CHSet& b)
if (&b == this || b.count == 0)
for (unsigned int i = 0; i < b.size; ++i)
for (<T>CHNode* p = b.tab[i]; goodCHptr(p); p = p->tl)
void <T>CHSet::operator &= (<T>CHSet& b)
for (unsigned int i = 0; i < size; ++i)
void <T>CHSet::operator -= (<T>CHSet& b)
for (unsigned int i = 0; i < size; ++i)
for (unsigned int i = 0; i < size; ++i)
for (<T>CHNode* p = tab[i]; goodCHptr(p); p = p->tl) ++n;
v &= CHptr_to_index(p) == i + 1;
if (!v) error("invariant failure");