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