HashMap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = abs( hash( key ) ) % bucketCount;
  58. if( !buckets[ index ] )
  59. buckets[ index ] = new Array< MapEntry<K, V>>();
  60. for( auto iterator = buckets[ index ]->begin(); 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 = abs( hash( key ) ) % bucketCount;
  73. if( !buckets[ index ] )
  74. return;
  75. int listIndex = 0;
  76. for( auto bucket : *buckets[ index ] )
  77. {
  78. if( bucket.getKey() == key )
  79. {
  80. bucket.remove( listIndex );
  81. return;
  82. }
  83. listIndex++;
  84. }
  85. }
  86. V get( K key ) const
  87. {
  88. int index = abs( hash( key ) ) % bucketCount;
  89. if( !buckets[ index ] )
  90. throw "element not found";
  91. for( auto iterator : *buckets[ index ] )
  92. {
  93. if( iterator.getKey() == key )
  94. return iterator.getValue();
  95. }
  96. throw "element not found";
  97. }
  98. V safeGet( K key, V fallback ) const
  99. {
  100. int index = abs( hash( key ) ) % bucketCount;
  101. if( !buckets[ index ] )
  102. return fallback;
  103. for( auto bucket : *buckets[ index ] )
  104. {
  105. if( bucket.getKey() == key )
  106. return bucket.getValue();
  107. }
  108. return fallback;
  109. }
  110. bool has( K key ) const
  111. {
  112. int index = abs( hash( key ) ) % bucketCount;
  113. if( !buckets[ index ] )
  114. return 0;
  115. for( auto bucket : buckets[ index ] )
  116. {
  117. if( bucket.getKey() == key )
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. };
  123. }