1
/* $Id: hash.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJPP_HASH_HPP__
21
#define __PJPP_HASH_HPP__
23
#include <pj++/types.hpp>
24
#include <pj++/pool.hpp>
30
class Pj_Hash_Table : public Pj_Object
34
// Hash table iterator.
42
explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i)
46
iterator(const iterator &rhs)
47
: ht_(rhs.ht_), it_(rhs.it_)
52
it_ = pj_hash_next(ht_, it_);
54
bool operator==(const iterator &rhs)
56
return ht_ == rhs.ht_ && it_ == rhs.it_;
58
iterator & operator=(const iterator &rhs)
60
ht_=rhs.ht_; it_=rhs.it_;
65
pj_hash_iterator_t it_val_;
66
pj_hash_iterator_t *it_;
68
friend class Pj_Hash_Table;
72
// Construct hash table.
74
Pj_Hash_Table(Pj_Pool *pool, unsigned size)
76
table_ = pj_hash_create(pool->pool_(), size);
80
// Destroy hash table.
87
// Calculate hash value.
89
static pj_uint32_t calc( pj_uint32_t initial_hval,
91
unsigned keylen = PJ_HASH_KEY_STRING)
93
return pj_hash_calc(initial_hval, key, keylen);
97
// Return pjlib compatible hash table object.
99
pj_hash_table_t *pj_hash_table_t_()
105
// Get the value associated with the specified key.
107
void *get(const void *key, unsigned keylen = PJ_HASH_KEY_STRING)
109
return pj_hash_get(table_, key, keylen);
113
// Associate a value with a key.
114
// Set the value to NULL to delete the key from the hash table.
116
void set(Pj_Pool *pool,
119
unsigned keylen = PJ_HASH_KEY_STRING)
121
pj_hash_set(pool->pool_(), table_, key, keylen, value);
125
// Get number of items in the hash table.
129
return pj_hash_count(table_);
133
// Iterate hash table.
137
iterator it(table_, NULL);
138
it.it_ = pj_hash_first(table_, &it.it_val_);
147
return iterator(table_, NULL);
151
pj_hash_table_t *table_;
155
#endif /* __PJPP_HASH_HPP__ */