#ifndef CLICK_HASHMAP_HH #define CLICK_HASHMAP_HH #include CLICK_DECLS /** @cond never */ class HashMap_Arena; class HashMap_ArenaFactory; // K AND V REQUIREMENTS: // // K::K(const K &) // k1 == k2 // hashcode_t hashcode(const K &) // If hashcode(k1) != hashcode(k2), then k1 != k2. // hashcode() can return any unsigned value. // // V::V() -- only used for default value // V::V(const V &) // V & V::operator=(const V &) template class _HashMap_const_iterator; template class _HashMap_iterator; template class HashMap; template class HashMap { public: typedef K key_type; typedef V mapped_type; struct Pair; HashMap(); explicit HashMap(const V &, HashMap_ArenaFactory * = 0); HashMap(const HashMap &); ~HashMap(); void set_arena(HashMap_ArenaFactory *); size_t size() const { return _n; } bool empty() const { return _n == 0; } size_t nbuckets() const { return _nbuckets; } Pair *find_pair(const K &) const; inline V *findp(const K &) const; inline const V &find(const K &, const V &) const; inline const V &find(const K &) const; inline const V &operator[](const K &) const; Pair *find_pair_force(const K &, const V &); Pair *find_pair_force(const K &k) { return find_pair_force(k, _default_value); } V *findp_force(const K &k, const V &v) { if (Pair *p = find_pair_force(k, v)) return &p->value; else return 0; } V &find_force(const K &k, const V &v) { return *findp_force(k, v); } V *findp_force(const K &k) { return findp_force(k, _default_value); } V &find_force(const K &k) { return *findp_force(k, _default_value); } bool insert(const K &, const V &); bool erase(const K &); bool remove(const K &key) { return erase(key); } void clear(); void swap(HashMap &); // iteration typedef _HashMap_const_iterator const_iterator; typedef _HashMap_iterator iterator; inline const_iterator begin() const; inline iterator begin(); inline const_iterator end() const; inline iterator end(); // dynamic resizing void resize(size_t); bool dynamic_resizing() const { return _capacity < 0x7FFFFFFF; } void set_dynamic_resizing(bool); HashMap &operator=(const HashMap &); struct Pair { K key; V value; }; enum { MAX_NBUCKETS = 4194303, DEFAULT_INITIAL_NBUCKETS = 127, DEFAULT_RESIZE_THRESHOLD = 2 }; private: struct Elt : public Pair { Elt *next; #if defined(__GNUC__) && __GNUC__ < 4 /* Shut up compiler about Pair lacking default constructor */ Elt(const Pair &p) : Pair(p) { } #endif }; Elt **_buckets; size_t _nbuckets; V _default_value; size_t _n; size_t _capacity; HashMap_Arena *_arena; void initialize(HashMap_ArenaFactory *, size_t); void copy_from(const HashMap &); void resize0(size_t); size_t bucket(const K &) const; friend class _HashMap_iterator; friend class _HashMap_const_iterator; }; template class _HashMap_const_iterator { public: bool live() const { return _elt; } typedef bool (_HashMap_const_iterator::*unspecified_bool_type)() const; inline operator unspecified_bool_type() const CLICK_DEPRECATED; void operator++(int); void operator++() { (*this)++; } typedef typename HashMap::Pair Pair; const Pair *pair() const { return _elt; } const K &key() const { return _elt->key; } const V &value() const { return _elt->value; } private: const HashMap *_hm; typename HashMap::Elt *_elt; size_t _bucket; _HashMap_const_iterator(const HashMap *m, bool begin); friend class HashMap; friend class _HashMap_iterator; }; template class _HashMap_iterator : public _HashMap_const_iterator { public: typedef _HashMap_const_iterator inherited; typedef typename HashMap::Pair Pair; Pair *pair() const { return const_cast(inherited::pair()); } V &value() const { return const_cast(inherited::value()); } private: _HashMap_iterator(HashMap *m, bool begin) : inherited(m, begin) { } friend class HashMap; }; template inline typename HashMap::const_iterator HashMap::begin() const { return const_iterator(this, true); } template inline typename HashMap::iterator HashMap::begin() { return iterator(this, true); } template inline typename HashMap::const_iterator HashMap::end() const { return const_iterator(this, false); } template inline typename HashMap::iterator HashMap::end() { return iterator(this, false); } template inline V * HashMap::findp(const K &key) const { Pair *p = find_pair(key); return (p ? &p->value : 0); } template inline const V & HashMap::find(const K &key, const V &default_value) const { Pair *p = find_pair(key); const V *v = (p ? &p->value : &default_value); return *v; } template inline const V & HashMap::find(const K &key) const { return find(key, _default_value); } template inline const V & HashMap::operator[](const K &key) const { return find(key); } template inline _HashMap_const_iterator::operator unspecified_bool_type() const { return live() ? &_HashMap_const_iterator::live : 0; } template class HashMap { public: typedef K key_type; typedef void *mapped_type; struct Pair; HashMap(); explicit HashMap(void *, HashMap_ArenaFactory * = 0); HashMap(const HashMap &); ~HashMap(); void set_arena(HashMap_ArenaFactory *); size_t size() const { return _n; } bool empty() const { return _n == 0; } size_t nbuckets() const { return _nbuckets; } Pair *find_pair(const K &) const; inline void **findp(const K &) const; inline void *find(const K &, void *) const; inline void *find(const K &) const; inline void *operator[](const K &) const; Pair *find_pair_force(const K &, void *); Pair *find_pair_force(const K &k) { return find_pair_force(k, _default_value); } void **findp_force(const K &k, void *v) { if (Pair *p = find_pair_force(k, v)) return &p->value; else return 0; } void *&find_force(const K &k, void *v) { return *findp_force(k, v); } void **findp_force(const K &k) { return findp_force(k, _default_value); } void *&find_force(const K &k) { return *findp_force(k, _default_value); } bool insert(const K &, void *); bool erase(const K &); bool remove(const K &key) { return erase(key); } void clear(); void swap(HashMap &); // iterators typedef _HashMap_const_iterator const_iterator; typedef _HashMap_iterator iterator; inline const_iterator begin() const; inline iterator begin(); inline const_iterator end() const; inline iterator end(); // dynamic resizing void resize(size_t); bool dynamic_resizing() const { return _capacity < 0x7FFFFFFF; } void set_dynamic_resizing(bool); HashMap &operator=(const HashMap &); struct Pair { K key; void *value; }; enum { MAX_NBUCKETS = 32767, DEFAULT_INITIAL_NBUCKETS = 127, DEFAULT_RESIZE_THRESHOLD = 2 }; private: struct Elt : public Pair { Elt *next; #if defined(__GNUC__) && __GNUC__ < 4 /* Shut up compiler about Pair lacking default constructor */ Elt(const Pair &p) : Pair(p) { } #endif }; Elt **_buckets; size_t _nbuckets; void *_default_value; size_t _n; size_t _capacity; HashMap_Arena *_arena; void initialize(HashMap_ArenaFactory *, size_t); void copy_from(const HashMap &); void resize0(size_t); size_t bucket(const K &) const; friend class _HashMap_iterator; friend class _HashMap_const_iterator; }; template class _HashMap_const_iterator { public: bool live() const { return _elt; } typedef bool (_HashMap_const_iterator::*unspecified_bool_type)() const; inline operator unspecified_bool_type() const CLICK_DEPRECATED; void operator++(int); void operator++() { (*this)++; } typedef typename HashMap::Pair Pair; const Pair *pair() const { return _elt; } const K &key() const { return _elt->key; } void *value() const { return _elt->value; } private: const HashMap *_hm; typename HashMap::Elt *_elt; size_t _bucket; _HashMap_const_iterator(const HashMap *, bool begin); template friend class _HashMap_const_iterator; template friend class _HashMap_iterator; template friend class HashMap; }; template class _HashMap_iterator : public _HashMap_const_iterator { public: typedef _HashMap_const_iterator inherited; typedef typename HashMap::Pair Pair; Pair *pair() const { return const_cast(inherited::pair()); } void *&value() const { return this->_elt->value; } private: _HashMap_iterator(HashMap *m, bool begin) : inherited(m, begin) { } template friend class HashMap; }; template inline typename HashMap::const_iterator HashMap::begin() const { return const_iterator(this, true); } template inline typename HashMap::iterator HashMap::begin() { return iterator(this, true); } template inline typename HashMap::const_iterator HashMap::end() const { return const_iterator(this, false); } template inline typename HashMap::iterator HashMap::end() { return iterator(this, false); } template inline void ** HashMap::findp(const K &key) const { Pair *p = find_pair(key); return (p ? &p->value : 0); } template inline void * HashMap::find(const K &key, void *default_value) const { Pair *p = find_pair(key); return (p ? p->value : default_value); } template inline void * HashMap::find(const K &key) const { return find(key, _default_value); } template inline void * HashMap::operator[](const K &key) const { return find(key); } template inline _HashMap_const_iterator::operator unspecified_bool_type() const { return live() ? &_HashMap_const_iterator::live : 0; } template class HashMap : public HashMap { public: typedef K key_type; typedef T *mapped_type; typedef HashMap inherited; struct Pair; HashMap() : inherited() { } explicit HashMap(T *def, HashMap_ArenaFactory *factory = 0) : inherited(def, factory) { } HashMap(const HashMap &o) : inherited(o) { } ~HashMap() { } void set_arena(HashMap_ArenaFactory *af) { inherited::set_arena(af); } // size_t size() const inherited // bool empty() const inherited // size_t nbuckets() const inherited Pair *find_pair(const K &k) const { return reinterpret_cast(inherited::find_pair(k)); } T **findp(const K &k) const { return reinterpret_cast(inherited::findp(k)); } T *find(const K &k, T *v) const { return reinterpret_cast(inherited::find(k, v)); } T *find(const K &k) const { return reinterpret_cast(inherited::find(k)); } T *operator[](const K &k) const { return reinterpret_cast(inherited::operator[](k)); } Pair *find_pair_force(const K &k, T *v) { return reinterpret_cast(inherited::find_pair_force(k, v)); } Pair *find_pair_force(const K &k) { return reinterpret_cast(inherited::find_pair_force(k)); } T **findp_force(const K &k, T *v) { return reinterpret_cast(inherited::findp_force(k, v)); } T *&find_force(const K &k, T *v) { return *reinterpret_cast(inherited::findp_force(k, v)); } T **findp_force(const K &k) { return reinterpret_cast(inherited::findp_force(k)); } T *&find_force(const K &k) { return *reinterpret_cast(inherited::findp_force(k)); } bool insert(const K &k, T *v) { return inherited::insert(k, v); } // bool erase(const K &) inherited // bool remove(const K &) inherited // void clear() inherited void swap(HashMap &o) { inherited::swap(o); } // iteration typedef _HashMap_const_iterator const_iterator; typedef _HashMap_iterator iterator; inline const_iterator begin() const; inline iterator begin(); inline const_iterator end() const; inline iterator end(); // dynamic resizing methods inherited HashMap &operator=(const HashMap &o) { return static_cast &>(inherited::operator=(o)); } struct Pair { K key; T *value; }; }; template class _HashMap_const_iterator { public: typedef _HashMap_const_iterator inherited; bool live() const { return _i.live(); } typedef typename inherited::unspecified_bool_type unspecified_bool_type; inline operator unspecified_bool_type() const CLICK_DEPRECATED; void operator++(int) { _i.operator++(0); } void operator++() { _i.operator++(); } typedef typename HashMap::Pair Pair; const Pair *pair() const { return reinterpret_cast(_i.pair()); } const K &key() const { return _i.key(); } T *value() const { return reinterpret_cast(_i.value()); } private: inherited _i; _HashMap_const_iterator(const HashMap *t, bool begin) : _i(t, begin) { } friend class _HashMap_iterator; template friend class HashMap; }; template class _HashMap_iterator : public _HashMap_const_iterator { public: typedef _HashMap_const_iterator inherited; typedef typename HashMap::Pair Pair; Pair *pair() const { return const_cast(inherited::pair()); } T *&value() const { return pair()->value; } private: _HashMap_iterator(HashMap *t, bool begin) : inherited(t, begin) { } template friend class HashMap; }; template inline typename HashMap::const_iterator HashMap::begin() const { return const_iterator(this, true); } template inline typename HashMap::iterator HashMap::begin() { return iterator(this, true); } template inline typename HashMap::const_iterator HashMap::end() const { return const_iterator(this, false); } template inline typename HashMap::iterator HashMap::end() { return iterator(this, false); } template inline _HashMap_const_iterator::operator unspecified_bool_type() const { return inherited::live() ? &inherited::live : 0; } template inline bool operator==(const _HashMap_const_iterator &a, const _HashMap_const_iterator &b) { return a.pair() == b.pair(); } template inline bool operator!=(const _HashMap_const_iterator &a, const _HashMap_const_iterator &b) { return a.pair() != b.pair(); } /** @endcond */ CLICK_ENDDECLS #include #endif