Changeset 36 for pjproject/main/pjlib/include/pj++/hash.hpp
- Timestamp:
- Nov 9, 2005 3:37:19 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/main/pjlib/include/pj++/hash.hpp
r29 r36 1 1 /* $Id$ 2 *3 2 */ 4 3 #ifndef __PJPP_HASH_H__ … … 6 5 7 6 #include <pj++/types.hpp> 7 #include <pj++/pool.hpp> 8 8 #include <pj/hash.h> 9 9 10 class PJ_Hash_Table 10 // 11 // Hash table. 12 // 13 class Pj_Hash_Table : public Pj_Object 11 14 { 12 15 public: 16 // 17 // Hash table iterator. 18 // 13 19 class iterator 14 20 { 15 21 public: 16 iterator() {} 17 explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) : ht_(h), it_(i) {} 18 iterator(const iterator &rhs) : ht_(rhs.ht_), it_(rhs.it_) {} 19 void operator++() { it_ = pj_hash_next(ht_, it_); } 20 bool operator==(const iterator &rhs) { return ht_ == rhs.ht_ && it_ == rhs.it_; } 21 iterator & operator=(const iterator &rhs) { ht_=rhs.ht_; it_=rhs.it_; return *this; } 22 iterator() 23 { 24 } 25 explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) 26 : ht_(h), it_(i) 27 { 28 } 29 iterator(const iterator &rhs) 30 : ht_(rhs.ht_), it_(rhs.it_) 31 { 32 } 33 void operator++() 34 { 35 it_ = pj_hash_next(ht_, it_); 36 } 37 bool operator==(const iterator &rhs) 38 { 39 return ht_ == rhs.ht_ && it_ == rhs.it_; 40 } 41 iterator & operator=(const iterator &rhs) 42 { 43 ht_=rhs.ht_; it_=rhs.it_; 44 return *this; 45 } 22 46 private: 23 47 pj_hash_table_t *ht_; … … 25 49 pj_hash_iterator_t *it_; 26 50 27 friend class P J_Hash_Table;51 friend class Pj_Hash_Table; 28 52 }; 29 53 30 static PJ_Hash_Table *create(PJ_Pool *pool, unsigned size) 54 // 55 // Construct hash table. 56 // 57 Pj_Hash_Table(Pj_Pool *pool, unsigned size) 31 58 { 32 return (PJ_Hash_Table*)pj_hash_create(pool->pool_(), size);59 table_ = pj_hash_create(pool->pool_(), size); 33 60 } 34 61 35 static pj_uint32_t calc(pj_uint32_t initial_hval, const void *key, unsigned keylen) 62 // 63 // Destroy hash table. 64 // 65 ~Pj_Hash_Table() 66 { 67 } 68 69 // 70 // Calculate hash value. 71 // 72 static pj_uint32_t calc( pj_uint32_t initial_hval, 73 const void *key, 74 unsigned keylen = PJ_HASH_KEY_STRING) 36 75 { 37 76 return pj_hash_calc(initial_hval, key, keylen); 38 77 } 39 78 40 pj_hash_table_t *hash_table_() 79 // 80 // Return pjlib compatible hash table object. 81 // 82 pj_hash_table_t *pj_hash_table_t_() 41 83 { 42 return (pj_hash_table_t*)this;84 return table_; 43 85 } 44 86 45 void *get(const void *key, unsigned keylen) 87 // 88 // Get the value associated with the specified key. 89 // 90 void *get(const void *key, unsigned keylen = PJ_HASH_KEY_STRING) 46 91 { 47 return pj_hash_get(t his->hash_table_(), key, keylen);92 return pj_hash_get(table_, key, keylen); 48 93 } 49 94 50 void set(PJ_Pool *pool, const void *key, unsigned keylen, void *value) 95 // 96 // Associate a value with a key. 97 // Set the value to NULL to delete the key from the hash table. 98 // 99 void set(Pj_Pool *pool, 100 const void *key, 101 void *value, 102 unsigned keylen = PJ_HASH_KEY_STRING) 51 103 { 52 pj_hash_set(pool->pool_(), t his->hash_table_(), key, keylen, value);104 pj_hash_set(pool->pool_(), table_, key, keylen, value); 53 105 } 54 106 107 // 108 // Get number of items in the hash table. 109 // 55 110 unsigned count() 56 111 { 57 return pj_hash_count(t his->hash_table_());112 return pj_hash_count(table_); 58 113 } 59 114 115 // 116 // Iterate hash table. 117 // 60 118 iterator begin() 61 119 { 62 iterator it(t his->hash_table_(), NULL);63 it.it_ = pj_hash_first(t his->hash_table_(), &it.it_val_);120 iterator it(table_, NULL); 121 it.it_ = pj_hash_first(table_, &it.it_val_); 64 122 return it; 65 123 } 66 124 125 // 126 // End of items. 127 // 67 128 iterator end() 68 129 { 69 return iterator(t his->hash_table_(), NULL);130 return iterator(table_, NULL); 70 131 } 132 133 private: 134 pj_hash_table_t *table_; 71 135 }; 72 136 137 73 138 #endif /* __PJPP_HASH_H__ */ 139
Note: See TracChangeset
for help on using the changeset viewer.