1 /* $XConsortium: PointerTable.C /main/1 1996/07/29 17:01:40 cde-hp $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
5 #ifndef PointerTable_DEF_INCLUDED
6 #define PointerTable_DEF_INCLUDED 1
11 namespace SP_NAMESPACE {
14 template<class P, class K, class HF, class KF>
15 PointerTable<P, K, HF, KF>::PointerTable()
16 : used_(0), usedLimit_(0), null_(0)
20 template<class P, class K, class HF, class KF>
21 void PointerTable<P, K, HF, KF>::clear()
28 template<class P, class K, class HF, class KF>
29 P PointerTable<P, K, HF, KF>::insert(P p, Boolean replace)
32 if (vec_.size() == 0) {
35 h = startIndex(KF::key(*p));
38 for (h = startIndex(KF::key(*p)); vec_[h] != 0 ; h = nextIndex(h))
39 if (KF::key(*vec_[h]) == KF::key(*p)) {
48 if (used_ >= usedLimit_) {
49 if (vec_.size() > size_t(-1)/2) {
50 if (usedLimit_ == vec_.size() - 1)
51 abort(); // FIXME throw an exception
53 usedLimit_ = vec_.size() - 1;
57 Vector<P> oldVec(vec_.size()*2, P(0));
59 usedLimit_ = vec_.size() / 2;
60 for (size_t i = 0; i < oldVec.size(); i++)
63 for (j = startIndex(KF::key(*oldVec[i]));
69 for (h = startIndex(KF::key(*p)); vec_[h] != 0; h = nextIndex(h))
79 template<class P, class K, class HF, class KF>
80 const P &PointerTable<P, K, HF, KF>::lookup(const K &k) const
83 for (size_t i = startIndex(k); vec_[i] != 0; i = nextIndex(i))
84 if (KF::key(*vec_[i]) == k)
90 template<class P, class K, class HF, class KF>
91 P PointerTable<P, K, HF, KF>::remove(const K &k)
94 for (size_t i = startIndex(k); vec_[i] != 0; i = nextIndex(i))
95 if (KF::key(*vec_[i]) == k) {
105 r = startIndex(KF::key(*vec_[i]));
106 } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
108 } while (vec_[i] != 0);
116 template<class P, class K, class HF, class KF>
117 void PointerTable<P, K, HF, KF>::swap(PointerTable<P, K, HF, KF> &to)
120 size_t tem = to.used_;
124 to.usedLimit_ = usedLimit_;
128 template<class P, class K, class HF, class KF>
129 PointerTableIter<P, K, HF, KF>::PointerTableIter(const PointerTable<P, K, HF, KF> &table)
130 : tablePtr_(&table), i_(0)
134 template<class P, class K, class HF, class KF>
135 const P &PointerTableIter<P, K, HF, KF>::next()
137 for (; i_ < tablePtr_->vec_.size(); i_++)
138 if (tablePtr_->vec_[i_] != 0)
139 return tablePtr_->vec_[i_++];
140 return tablePtr_->null_;
147 #endif /* not PointerTable_DEF_INCLUDED */