Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- };
Add Comment
Please, Sign In to add comment