1 // Copyright (c) 1994 James Clark 2 // See the file COPYING for copying permission. 3 #pragma ident "%Z%%M% %I% %E% SMI" 4 5 #ifndef PointerTable_INCLUDED 6 #define PointerTable_INCLUDED 1 7 8 #include "Vector.h" 9 #include "Boolean.h" 10 #include <stddef.h> 11 12 #ifdef SP_NAMESPACE 13 namespace SP_NAMESPACE { 14 #endif 15 16 template<class P, class K, class HF, class KF> class PointerTableIter; 17 18 template<class P, class K, class HF, class KF> 19 class PointerTable { constraints()20 void constraints() { 21 P p(0); 22 const K &key = KF::key(*p); 23 unsigned long n = HF::hash(key); 24 n = 0; // prevent warning 25 } 26 public: 27 PointerTable(); 28 P insert(P, Boolean replace = 0); 29 // Return a reference so that it is possible to do 30 // lookups into a table of smart-pointers from multiple threads. 31 const P &lookup(const K &) const; 32 P remove(const K &); count()33 size_t count() const { return used_; } 34 void clear(); 35 void swap(PointerTable<P, K, HF, KF> &); 36 protected: 37 size_t used_; 38 size_t usedLimit_; 39 Vector<P> vec_; 40 P null_; 41 startIndex(const K & k)42 size_t startIndex(const K &k) const { 43 return size_t(HF::hash(k) & (vec_.size() - 1)); 44 } nextIndex(size_t i)45 size_t nextIndex(size_t i) const { 46 return i == 0 ? vec_.size() - 1 : i - 1; 47 } 48 friend class PointerTableIter<P, K, HF, KF>; 49 }; 50 51 template<class P, class K, class HF, class KF> 52 class PointerTableIter { 53 public: 54 PointerTableIter(const PointerTable<P, K, HF, KF> &); 55 const P &next(); 56 private: 57 const PointerTable<P, K, HF, KF> *tablePtr_; 58 size_t i_; 59 }; 60 61 #ifdef SP_NAMESPACE 62 } 63 #endif 64 65 #endif /* not PointerTable_INCLUDED */ 66 67 #ifdef SP_DEFINE_TEMPLATES 68 #include "PointerTable.cxx" 69 #endif 70