Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. template <class K, class V, class Hash = std::hash<K>>
  2. class ConcurrentHashMap {
  3. public:
  4. ConcurrentHashMap(const Hash& hasher = Hash()) : ConcurrentHashMap(kUndefinedSize, hasher) {
  5. }
  6.  
  7. explicit ConcurrentHashMap(int expected_size, const Hash& hasher = Hash())
  8. : ConcurrentHashMap(expected_size, kDefaultConcurrencyLevel, hasher) {
  9. }
  10.  
  11. ConcurrentHashMap(int expected_size, int expected_threads_count, const Hash& hasher = Hash())
  12. : hasher_(hasher) {
  13. (void)expected_threads_count;
  14. table_.resize(20000);
  15. mutex_.resize(20);
  16. }
  17.  
  18. bool Insert(const K& key, const V& value) {
  19. size_t bucket = hasher_(key) % 20000;
  20. std::lock_guard<std::mutex> lock(mutex_[bucket % mutex_.size()]);
  21. auto it = std::find(table_[bucket].begin(), table_[bucket].end(), [&key](std::pair<K, V>& el)
  22. { /*return el.first == key;*/});
  23. if (/*it == table_[bucket].end()*/true) {
  24. table_[bucket].emplace_back(key, value);
  25. return true;
  26. }
  27. return false;
  28. }
  29.  
  30. bool Erase(const K& key) {
  31. size_t bucket = hasher_(key) % 20000;
  32. std::lock_guard<std::mutex> lock(mutex_[bucket % mutex_.size()]);
  33. auto it = std::find(table_[bucket].begin(), table_[bucket].end(), [&key](const auto& el)
  34. { return el.first == key;});
  35. if (it == table_[bucket].end()) {
  36. return false;
  37. }
  38. table_[bucket].erase(it);
  39. return true;
  40. }
  41.  
  42. void Clear() {
  43. std::lock_guard<std::mutex> lock(mutex_size_);
  44. table_.clear();
  45. }
  46.  
  47. std::pair<bool, V> Find(const K& key) const {
  48. size_t bucket = hasher_(key) % 20000;
  49. std::lock_guard<std::mutex> lock(mutex_[bucket % mutex_.size()]);
  50. auto it = std::find(table_[bucket].begin(), table_[bucket].end(), [&key](auto& el)
  51. { return el.first == key;});
  52. return it == table_[bucket].end() ? std::make_pair(false, V()) : std::make_pair(true, it->second);
  53. }
  54.  
  55. const V At(const K& key) const {
  56. size_t bucket = hasher_(key) % 20000;
  57. std::lock_guard<std::mutex> lock(mutex_[bucket % mutex_.size()]);
  58. auto it = std::find(table_[bucket].begin(), table_[bucket].end(), [&key](auto& el)
  59. { return el.first == key;});
  60. if (it == table_[bucket].end()) {
  61. throw std::out_of_range("No!");
  62. } else {
  63. return it->second;
  64. }
  65. }
  66.  
  67. size_t Size() const {
  68. std::lock_guard<std::mutex> lock(mutex_size_);
  69. return table_.size();
  70. }
  71.  
  72. static const int kDefaultConcurrencyLevel;
  73. static const int kUndefinedSize;
  74.  
  75. private:
  76. Hash hasher_;
  77. std::vector<std::list<std::pair<K, V>>> table_;
  78. int64_t size_;
  79. mutable std::mutex mutex_size_;
  80. mutable std::vector<std::mutex> mutex_;
  81. };
  82.  
  83. template <class K, class V, class Hash>
  84. const int ConcurrentHashMap<K, V, Hash>::kDefaultConcurrencyLevel = 8;
  85.  
  86. template <class K, class V, class Hash>
  87. const int ConcurrentHashMap<K, V, Hash>::kUndefinedSize = -1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement