hash.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #include <pj++/types.hpp>
  22. #include <pj++/pool.hpp>
  23. #include <pj/hash.h>
  24. //
  25. // Hash table.
  26. //
  27. class Pj_Hash_Table : public Pj_Object
  28. {
  29. public:
  30. //
  31. // Hash table iterator.
  32. //
  33. class iterator
  34. {
  35. public:
  36. iterator()
  37. {
  38. }
  39. explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i)
  40. : ht_(h), it_(i)
  41. {
  42. }
  43. iterator(const iterator &rhs)
  44. : ht_(rhs.ht_), it_(rhs.it_)
  45. {
  46. }
  47. void operator++()
  48. {
  49. it_ = pj_hash_next(ht_, it_);
  50. }
  51. bool operator==(const iterator &rhs)
  52. {
  53. return ht_ == rhs.ht_ && it_ == rhs.it_;
  54. }
  55. iterator & operator=(const iterator &rhs)
  56. {
  57. ht_=rhs.ht_; it_=rhs.it_;
  58. return *this;
  59. }
  60. private:
  61. pj_hash_table_t *ht_;
  62. pj_hash_iterator_t it_val_;
  63. pj_hash_iterator_t *it_;
  64. friend class Pj_Hash_Table;
  65. };
  66. //
  67. // Construct hash table.
  68. //
  69. Pj_Hash_Table(Pj_Pool *pool, unsigned size)
  70. {
  71. table_ = pj_hash_create(pool->pool_(), size);
  72. }
  73. //
  74. // Destroy hash table.
  75. //
  76. ~Pj_Hash_Table()
  77. {
  78. }
  79. //
  80. // Calculate hash value.
  81. //
  82. static pj_uint32_t calc( pj_uint32_t initial_hval,
  83. const void *key,
  84. unsigned keylen = PJ_HASH_KEY_STRING)
  85. {
  86. return pj_hash_calc(initial_hval, key, keylen);
  87. }
  88. //
  89. // Return pjlib compatible hash table object.
  90. //
  91. pj_hash_table_t *pj_hash_table_t_()
  92. {
  93. return table_;
  94. }
  95. //
  96. // Get the value associated with the specified key.
  97. //
  98. void *get(const void *key, unsigned keylen = PJ_HASH_KEY_STRING)
  99. {
  100. return pj_hash_get(table_, key, keylen);
  101. }
  102. //
  103. // Associate a value with a key.
  104. // Set the value to NULL to delete the key from the hash table.
  105. //
  106. void set(Pj_Pool *pool,
  107. const void *key,
  108. void *value,
  109. unsigned keylen = PJ_HASH_KEY_STRING)
  110. {
  111. pj_hash_set(pool->pool_(), table_, key, keylen, value);
  112. }
  113. //
  114. // Get number of items in the hash table.
  115. //
  116. unsigned count()
  117. {
  118. return pj_hash_count(table_);
  119. }
  120. //
  121. // Iterate hash table.
  122. //
  123. iterator begin()
  124. {
  125. iterator it(table_, NULL);
  126. it.it_ = pj_hash_first(table_, &it.it_val_);
  127. return it;
  128. }
  129. //
  130. // End of items.
  131. //
  132. iterator end()
  133. {
  134. return iterator(table_, NULL);
  135. }
  136. private:
  137. pj_hash_table_t *table_;
  138. };
  139. #endif /* __PJPP_HASH_HPP__ */