Created
November 21, 2017 16:31
-
-
Save kundzi/50af2cf84d7bb0fe062d0425c9737f0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template <typename K> | |
| unsigned long hash(K const & key) { | |
| // TODO | |
| unsigned long l = 0; | |
| for ( int i = 0 ; i < key.length; i++ ) | |
| l += ( stoi(k.at[i]) * ( i+1) ); | |
| return l; | |
| } // cat // tac -> collision! | |
| // Implement | |
| template <typename K, typename V> | |
| class SimpleHashMap { | |
| public: | |
| std::mutex m; | |
| std::vector<std::pair<unsigned long,V> > v; | |
| bool get(K & key,V & value) { | |
| auto hashKey = hash(key); | |
| // TODO <-- | |
| // lock_guard with condition variable return true after write | |
| { | |
| std::lock_guard<std::mutex> l(m); | |
| for ( const auto& it : v ) | |
| { | |
| if ( it->first == hashKey) | |
| { | |
| value = it->second; | |
| return true; | |
| } | |
| } | |
| } | |
| //value = ""; // <-- | |
| return false; | |
| } | |
| void put(K & key, V & value) { | |
| // TODO <-- | |
| auto hashKey = hash(key); | |
| //lock & hold cv | |
| { | |
| std::lock_guard<Std::mutex> l(m); | |
| for ( auto it = v.begin(); it != v.end(); it++ ) | |
| { | |
| if ( (it->first) == hashKey ) | |
| { | |
| v.erase(it); | |
| break; | |
| } | |
| } | |
| v.push_back(std::make_pair(hash(key),value)); | |
| } | |
| //set cv to true | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment