summaryrefslogtreecommitdiff
path: root/pjlib/include/pj++/hash.hpp
blob: 26f480105f6be48d809e3d8156344479818c679b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* $Id$
 *
 */
#ifndef __PJPP_HASH_H__
#define __PJPP_HASH_H__

#include <pj++/types.hpp>
#include <pj/hash.h>

class PJ_Hash_Table
{
public:
    class iterator
    {
    public:
	iterator() {}
	explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) : ht_(h), it_(i) {}
	iterator(const iterator &rhs) : ht_(rhs.ht_), it_(rhs.it_) {}
	void operator++() { it_ = pj_hash_next(ht_, it_); }
	bool operator==(const iterator &rhs) { return ht_ == rhs.ht_ && it_ == rhs.it_; }
	iterator & operator=(const iterator &rhs) { ht_=rhs.ht_; it_=rhs.it_; return *this; }
    private:
	pj_hash_table_t *ht_;
	pj_hash_iterator_t it_val_;
	pj_hash_iterator_t *it_;

	friend class PJ_Hash_Table;
    };

    static PJ_Hash_Table *create(PJ_Pool *pool, unsigned size)
    {
	return (PJ_Hash_Table*) pj_hash_create(pool->pool_(), size);
    }

    static pj_uint32_t calc(pj_uint32_t initial_hval, const void *key, unsigned keylen)
    {
	return pj_hash_calc(initial_hval, key, keylen);
    }

    pj_hash_table_t *hash_table_()
    {
	return (pj_hash_table_t*)this;
    }

    void *get(const void *key, unsigned keylen)
    {
	return pj_hash_get(this->hash_table_(), key, keylen);
    }

    void set(PJ_Pool *pool, const void *key, unsigned keylen, void *value)
    {
	pj_hash_set(pool->pool_(), this->hash_table_(), key, keylen, value);
    }

    unsigned count()
    {
	return pj_hash_count(this->hash_table_());
    }

    iterator begin()
    {
	iterator it(this->hash_table_(), NULL);
	it.it_ = pj_hash_first(this->hash_table_(), &it.it_val_);
	return it;
    }

    iterator end()
    {
	return iterator(this->hash_table_(), NULL);
    }
};

#endif	/* __PJPP_HASH_H__ */