Guest User

Untitled

a guest
Nov 22nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. template <typename K>
  2. unsigned long hash(K const & key) {
  3. // TODO
  4. unsigned long l = 0;
  5. for ( int i = 0 ; i < key.length; i++ )
  6. l += ( stoi(k.at[i]) * ( i+1) );
  7.  
  8. return l;
  9. } // cat // tac -> collision!
  10.  
  11.  
  12.  
  13. // Implement
  14. template <typename K, typename V>
  15. class SimpleHashMap {
  16. public:
  17.  
  18. std::mutex m;
  19.  
  20. std::vector<std::pair<unsigned long,V> > v;
  21.  
  22. bool get(K & key,V & value) {
  23. auto hashKey = hash(key);
  24. // TODO <--
  25. // lock_guard with condition variable return true after write
  26. {
  27. std::lock_guard<std::mutex> l(m);
  28.  
  29. for ( const auto& it : v )
  30. {
  31. if ( it->first == hashKey)
  32. {
  33. value = it->second;
  34. return true;
  35. }
  36. }
  37.  
  38. }
  39. //value = ""; // <--
  40. return false;
  41.  
  42. }
  43.  
  44.  
  45. void put(K & key, V & value) {
  46. // TODO <--
  47. auto hashKey = hash(key);
  48.  
  49. //lock & hold cv
  50. {
  51. std::lock_guard<Std::mutex> l(m);
  52.  
  53. for ( auto it = v.begin(); it != v.end(); it++ )
  54. {
  55.  
  56. if ( (it->first) == hashKey )
  57. {
  58.  
  59. v.erase(it);
  60. break;
  61. }
  62.  
  63. }
  64. v.push_back(std::make_pair(hash(key),value));
  65.  
  66. }
  67. //set cv to true
  68. }
  69.  
  70. };
Add Comment
Please, Sign In to add comment