cc_hdict.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: cc_hdict.h /main/5 1996/08/21 15:49:02 drk $ */
  24. #ifndef _cc_hdict_h
  25. #define _cc_hdict_h 1
  26. #include "dti_cc/types.h"
  27. #include "dti_cc/cc_pvect.h"
  28. #include "dti_cc/CC_Slist.h"
  29. #include <iostream>
  30. using namespace std;
  31. template <class K, class V>
  32. class kv_pair {
  33. public:
  34. static CC_Boolean f_needRemove;
  35. kv_pair(K* k, V* v = 0): f_key(k), f_value(v) {};
  36. ~kv_pair();
  37. unsigned int operator==(const kv_pair<K, V>&);
  38. #ifdef DEBUG
  39. ostream& print(ostream&);
  40. friend ostream& operator<<(ostream& out, kv_pair<K, V>& kv) {
  41. return kv.print(out);
  42. }
  43. #endif
  44. K* f_key;
  45. V* f_value;
  46. };
  47. #define DEFAULT_BUCKET_NUM 30
  48. template <class K, class V> class hashTableIterator;
  49. template <class K, class V> class hashTable
  50. {
  51. //template <class K, class V>
  52. //friend class hashTableIterator;
  53. friend class hashTableIterator<K, V>;
  54. protected:
  55. unsigned (*f_hash_func_ptr)(const K&);
  56. pointer_vector<CC_TPtrSlist<kv_pair<K, V> > > f_buckets;
  57. size_t f_items;
  58. protected:
  59. kv_pair<K, V>* _find(const K* k) const;
  60. public:
  61. hashTable(const hashTable <K,V> &);
  62. hashTable(unsigned (*)(const K&),
  63. size_t init_bucket_num = DEFAULT_BUCKET_NUM
  64. );
  65. ~hashTable();
  66. void clearAndDestroy();
  67. size_t entries() { return f_items; };
  68. CC_Boolean contains(const K*) const;
  69. V* findValue(const K*) const;
  70. K* findKeyAndValue(const K*, V*&) const;
  71. void insertKeyAndValue(K*, V*);
  72. K* remove(const K*);
  73. #ifdef DEBUG
  74. ostream& print(ostream& out);
  75. friend ostream& operator<<(ostream& out, hashTable<K, V>& ht) {
  76. return ht.print(out);
  77. };
  78. #endif
  79. };
  80. template <class K, class V>
  81. class hashTableIterator
  82. {
  83. protected:
  84. size_t f_bucket_num;
  85. size_t f_pos;
  86. kv_pair<K, V>* f_rec;
  87. hashTable<K, V>& f_hashTable;
  88. CC_Boolean _findNonEmptyBucket();
  89. CC_Boolean _findNextRecord();
  90. public:
  91. hashTableIterator(hashTable<K, V>&);
  92. ~hashTableIterator();
  93. CC_Boolean operator++();
  94. K* key();
  95. V* value() const;
  96. };
  97. #ifdef EXPAND_TEMPLATES
  98. #include "cc_hdict.C"
  99. #endif
  100. #endif