📄 hashmap.hh
字号:
#ifndef CLICK_HASHMAP_HH#define CLICK_HASHMAP_HH#include <click/hashcode.hh>CLICK_DECLSclass 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 K, class V = void> class _HashMap_const_iterator;template <class K, class V = void> class _HashMap_iterator;template <class K, class V = void> class HashMap;template <class K, class V>class HashMap { public: typedef K key_type; typedef V mapped_type; struct Pair; HashMap(); explicit HashMap(const V &, HashMap_ArenaFactory * = 0); HashMap(const HashMap<K, V> &); ~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<K, V> &); // iteration typedef _HashMap_const_iterator<K, V> const_iterator; typedef _HashMap_iterator<K, V> 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<K, V> &operator=(const HashMap<K, V> &); 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<K, V> &); void resize0(size_t); size_t bucket(const K &) const; friend class _HashMap_iterator<K, V>; friend class _HashMap_const_iterator<K, V>;};template <class K, class V>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<K, V>::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<K, V> *_hm; typename HashMap<K, V>::Elt *_elt; size_t _bucket; _HashMap_const_iterator(const HashMap<K, V> *m, bool begin); friend class HashMap<K, V>; friend class _HashMap_iterator<K, V>;};template <class K, class V>class _HashMap_iterator : public _HashMap_const_iterator<K, V> { public: typedef _HashMap_const_iterator<K, V> inherited; typedef typename HashMap<K, V>::Pair Pair; Pair *pair() const { return const_cast<Pair *>(inherited::pair()); } V &value() const { return const_cast<V &>(inherited::value()); } private: _HashMap_iterator(HashMap<K, V> *m, bool begin) : inherited(m, begin) { } friend class HashMap<K, V>;};template <class K, class V>inline typename HashMap<K, V>::const_iteratorHashMap<K, V>::begin() const{ return const_iterator(this, true);}template <class K, class V>inline typename HashMap<K, V>::iteratorHashMap<K, V>::begin(){ return iterator(this, true);}template <class K, class V>inline typename HashMap<K, V>::const_iteratorHashMap<K, V>::end() const{ return const_iterator(this, false);}template <class K, class V>inline typename HashMap<K, V>::iteratorHashMap<K, V>::end(){ return iterator(this, false);}template <class K, class V>inline V *HashMap<K, V>::findp(const K &key) const{ Pair *p = find_pair(key); return (p ? &p->value : 0);}template <class K, class V>inline const V &HashMap<K, V>::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 <class K, class V>inline const V &HashMap<K, V>::find(const K &key) const{ return find(key, _default_value);}template <class K, class V>inline const V &HashMap<K, V>::operator[](const K &key) const{ return find(key);}template <class K, class V>inline_HashMap_const_iterator<K, V>::operator unspecified_bool_type() const{ return live() ? &_HashMap_const_iterator::live : 0;}template <class K>class HashMap<K, void *> { public: typedef K key_type; typedef void *mapped_type; struct Pair; HashMap(); explicit HashMap(void *, HashMap_ArenaFactory * = 0); HashMap(const HashMap<K, void *> &); ~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<K, void *> &); // iterators typedef _HashMap_const_iterator<K, void *> const_iterator; typedef _HashMap_iterator<K, void *> 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<K, void *> &operator=(const HashMap<K, void *> &); struct Pair { K key; void *value; }; enum { MAX_NBUCKETS = 32767, DEFAULT_INITIAL_NBUCKETS = 127, DEFAULT_RESIZE_THRESHOLD = 2 }; private:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -