Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // hashmap.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file describes the hash_map template class.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 11/06/1997 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef _HASHMAP_H_
  19. #define _HASHMAP_H_
  20. #include <hashtbl.h>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // CLASS
  24. //
  25. // hash_map<Key, Value, Hasher, KeyMatch>
  26. //
  27. // DESCRIPTION
  28. //
  29. // Extends hash_table (q.v.) to implement a hash map.
  30. //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. template <
  33. class Key,
  34. class Value,
  35. class Hasher = hash_util::Hasher<Key>,
  36. class KeyMatch = std::equal_to<Key>
  37. >
  38. class hash_map
  39. : public hash_table<
  40. Key,
  41. Hasher,
  42. std::pair<const Key, Value>,
  43. ExtractFirst< std::pair<const Key, Value> >,
  44. KeyMatch
  45. >
  46. {
  47. public:
  48. typedef typename table_type::const_iterator const_iterator;
  49. typedef Value referent_type;
  50. //////////
  51. // The hash map can support a non-const iterator since you can change
  52. // Value without effecting the hash value.
  53. //////////
  54. class iterator : public const_iterator
  55. {
  56. public:
  57. iterator(SList* _first, SList* _end)
  58. : const_iterator(_first, _end)
  59. {
  60. }
  61. value_type& operator*() const
  62. {
  63. return node->value;
  64. }
  65. value_type* operator->() const
  66. {
  67. return &**this;
  68. }
  69. };
  70. hash_map(size_t size = 16)
  71. : table_type(size) { }
  72. // non-const version of hash_table::begin
  73. iterator begin()
  74. {
  75. return iterator(table, table + buckets);
  76. }
  77. // non-const version of hash_table::find
  78. value_type* find(const key_type& key)
  79. {
  80. return const_cast<value_type*>(table_type::find(key));
  81. }
  82. // Duplicates implementation hidden in base class.
  83. const value_type* find(const key_type& key) const
  84. {
  85. return const_cast<value_type*>(table_type::find(key));
  86. }
  87. // Allows the map to be indexed like an array.
  88. referent_type& operator[](const key_type& key)
  89. {
  90. // Compute the hash value once.
  91. size_t hv = hasher(key);
  92. // See if the key exists.
  93. const value_type* v = search_bucket(table[hv & mask], key);
  94. if (!v)
  95. {
  96. reserve_space();
  97. // The key wasn't found, so create a new node using
  98. // a default referent.
  99. Node* node = new Node(value_type(key, referent_type()));
  100. add_entry();
  101. table[hv & mask].push_front(node);
  102. v = &node->value;
  103. }
  104. return const_cast<referent_type&>(v->second);
  105. }
  106. };
  107. #endif // _HASHMAP_H_