HashMap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <functional>
  3. #include "Array.h"
  4. namespace Framework
  5. {
  6. template<typename K, typename V>
  7. class MapEntry
  8. {
  9. private:
  10. K key;
  11. V value;
  12. public:
  13. MapEntry()
  14. {}
  15. MapEntry( K key, V value )
  16. : key( key ),
  17. value( value )
  18. {}
  19. K getKey()
  20. {
  21. return key;
  22. }
  23. V getValue()
  24. {
  25. return value;
  26. }
  27. template<typename K2, typename V2>
  28. friend class HashMap;
  29. };
  30. template<typename K, typename V>
  31. class HashMap : public virtual ReferenceCounter
  32. {
  33. private:
  34. Array< MapEntry<K, V> > **buckets;
  35. int bucketCount;
  36. std::function<int( K )> hash;
  37. public:
  38. HashMap( int bucketCount, std::function<int( K )> hash )
  39. : ReferenceCounter(),
  40. buckets( new Array< MapEntry<K, V> > *[ bucketCount ] ),
  41. bucketCount( bucketCount ),
  42. hash( hash )
  43. {
  44. memset( buckets, 0, sizeof( Array< MapEntry<K, V> > * ) * bucketCount );
  45. }
  46. ~HashMap()
  47. {
  48. for( int i = 0; i < bucketCount; i++ )
  49. {
  50. if( buckets[ i ] )
  51. buckets[ i ]->release();
  52. }
  53. delete[] buckets;
  54. }
  55. void put( K key, V value )
  56. {
  57. int index = hash( key ) % bucketCount;
  58. if( !buckets[ index ] )
  59. buckets[ index ] = new Array< MapEntry<K, V>>();
  60. for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
  61. {
  62. if( iterator._.getKey() == key )
  63. {
  64. iterator.set( MapEntry<K, V>( key, value ) );
  65. return;
  66. }
  67. }
  68. buckets[ index ]->add( MapEntry<K, V>( key, value ) );
  69. }
  70. void remove( K key )
  71. {
  72. int index = hash( key ) % bucketCount;
  73. if( !buckets[ index ] )
  74. return;
  75. int listIndex = 0;
  76. for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++, listIndex++ )
  77. {
  78. if( iterator._.getKey() == key )
  79. {
  80. buckets[ index ]->remove( listIndex );
  81. return;
  82. }
  83. }
  84. }
  85. V get( K key ) const
  86. {
  87. int index = hash( key ) % bucketCount;
  88. if( !buckets[ index ] )
  89. throw "element not found";
  90. for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
  91. {
  92. if( iterator._.getKey() == key )
  93. return iterator._.getValue();
  94. }
  95. throw "element not found";
  96. }
  97. V safeGet( K key, V fallback ) const
  98. {
  99. int index = hash( key ) % bucketCount;
  100. if( !buckets[ index ] )
  101. return fallback;
  102. for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
  103. {
  104. if( iterator._.getKey() == key )
  105. return iterator._.getValue();
  106. }
  107. return fallback;
  108. }
  109. bool has( K key ) const
  110. {
  111. int index = hash( key ) % bucketCount;
  112. if( !buckets[ index ] )
  113. return 0;
  114. for( auto iterator = buckets[ index ]->getIterator(); iterator; iterator++ )
  115. {
  116. if( iterator._.getKey() == key )
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. };
  122. }