Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #ifndef __PJPP_HASH_HPP__ |
| 21 | #define __PJPP_HASH_HPP__ |
| 22 | |
| 23 | #include <pj++/types.hpp> |
| 24 | #include <pj++/pool.hpp> |
| 25 | #include <pj/hash.h> |
| 26 | |
| 27 | // |
| 28 | // Hash table. |
| 29 | // |
| 30 | class Pj_Hash_Table : public Pj_Object |
| 31 | { |
| 32 | public: |
| 33 | // |
| 34 | // Hash table iterator. |
| 35 | // |
| 36 | class iterator |
| 37 | { |
| 38 | public: |
| 39 | iterator() |
| 40 | { |
| 41 | } |
| 42 | explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) |
| 43 | : ht_(h), it_(i) |
| 44 | { |
| 45 | } |
| 46 | iterator(const iterator &rhs) |
| 47 | : ht_(rhs.ht_), it_(rhs.it_) |
| 48 | { |
| 49 | } |
| 50 | void operator++() |
| 51 | { |
| 52 | it_ = pj_hash_next(ht_, it_); |
| 53 | } |
| 54 | bool operator==(const iterator &rhs) |
| 55 | { |
| 56 | return ht_ == rhs.ht_ && it_ == rhs.it_; |
| 57 | } |
| 58 | iterator & operator=(const iterator &rhs) |
| 59 | { |
| 60 | ht_=rhs.ht_; it_=rhs.it_; |
| 61 | return *this; |
| 62 | } |
| 63 | private: |
| 64 | pj_hash_table_t *ht_; |
| 65 | pj_hash_iterator_t it_val_; |
| 66 | pj_hash_iterator_t *it_; |
| 67 | |
| 68 | friend class Pj_Hash_Table; |
| 69 | }; |
| 70 | |
| 71 | // |
| 72 | // Construct hash table. |
| 73 | // |
| 74 | Pj_Hash_Table(Pj_Pool *pool, unsigned size) |
| 75 | { |
| 76 | table_ = pj_hash_create(pool->pool_(), size); |
| 77 | } |
| 78 | |
| 79 | // |
| 80 | // Destroy hash table. |
| 81 | // |
| 82 | ~Pj_Hash_Table() |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | // |
| 87 | // Calculate hash value. |
| 88 | // |
| 89 | static pj_uint32_t calc( pj_uint32_t initial_hval, |
| 90 | const void *key, |
| 91 | unsigned keylen = PJ_HASH_KEY_STRING) |
| 92 | { |
| 93 | return pj_hash_calc(initial_hval, key, keylen); |
| 94 | } |
| 95 | |
| 96 | // |
| 97 | // Return pjlib compatible hash table object. |
| 98 | // |
| 99 | pj_hash_table_t *pj_hash_table_t_() |
| 100 | { |
| 101 | return table_; |
| 102 | } |
| 103 | |
| 104 | // |
| 105 | // Get the value associated with the specified key. |
| 106 | // |
| 107 | void *get(const void *key, unsigned keylen = PJ_HASH_KEY_STRING) |
| 108 | { |
| 109 | return pj_hash_get(table_, key, keylen); |
| 110 | } |
| 111 | |
| 112 | // |
| 113 | // Associate a value with a key. |
| 114 | // Set the value to NULL to delete the key from the hash table. |
| 115 | // |
| 116 | void set(Pj_Pool *pool, |
| 117 | const void *key, |
| 118 | void *value, |
| 119 | unsigned keylen = PJ_HASH_KEY_STRING) |
| 120 | { |
| 121 | pj_hash_set(pool->pool_(), table_, key, keylen, value); |
| 122 | } |
| 123 | |
| 124 | // |
| 125 | // Get number of items in the hash table. |
| 126 | // |
| 127 | unsigned count() |
| 128 | { |
| 129 | return pj_hash_count(table_); |
| 130 | } |
| 131 | |
| 132 | // |
| 133 | // Iterate hash table. |
| 134 | // |
| 135 | iterator begin() |
| 136 | { |
| 137 | iterator it(table_, NULL); |
| 138 | it.it_ = pj_hash_first(table_, &it.it_val_); |
| 139 | return it; |
| 140 | } |
| 141 | |
| 142 | // |
| 143 | // End of items. |
| 144 | // |
| 145 | iterator end() |
| 146 | { |
| 147 | return iterator(table_, NULL); |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | pj_hash_table_t *table_; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | #endif /* __PJPP_HASH_HPP__ */ |
| 156 | |