initial version of lockfree::HashMap, methods operator[] and put are for now in the interface
This commit is contained in:
parent
44c9585602
commit
7e846b4dc0
35
data_structures/map/hashmap.hpp
Normal file
35
data_structures/map/hashmap.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef MEMGRAPH_DATA_STRUCTURES_LOCKFREE_HASHMAP_HPP
|
||||
#define MEMGRAPH_DATA_STRUCTURES_LOCKFREE_HASHMAP_HPP
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "threading/sync/lockable.hpp"
|
||||
#include "threading/sync/spinlock.hpp"
|
||||
|
||||
namespace lockfree
|
||||
{
|
||||
|
||||
template <class K, class V>
|
||||
class HashMap: Lockable<SpinLock> {
|
||||
public:
|
||||
V operator[](const K& key)
|
||||
{
|
||||
auto guard = acquire();
|
||||
|
||||
return hashmap[key];
|
||||
}
|
||||
|
||||
void put(const K& key, const K& value)
|
||||
{
|
||||
auto quard = acquire();
|
||||
|
||||
hashmap[key] = value;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<K, V> hashmap;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -4,11 +4,10 @@
|
||||
TEST_CASE("Dynamic bitset basic functionality")
|
||||
{
|
||||
DynamicBitset<> db;
|
||||
db.set(2222255555, 1);
|
||||
bool value = db.at(2222255555, 1);
|
||||
db.set(222555, 1);
|
||||
bool value = db.at(222555, 1);
|
||||
REQUIRE(value == true);
|
||||
|
||||
|
||||
db.set(32, 1);
|
||||
value = db.at(32, 1);
|
||||
REQUIRE(value == true);
|
||||
|
10
test/lockfree_hashmap.cpp
Normal file
10
test/lockfree_hashmap.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "catch.hpp"
|
||||
#include "data_structures/map/hashmap.hpp"
|
||||
|
||||
TEST_CASE("Lockfree HashMap basic functionality")
|
||||
{
|
||||
lockfree::HashMap<int, int> hashmap;
|
||||
// hashmap[32] = 10;
|
||||
hashmap.put(32, 10);
|
||||
REQUIRE(hashmap[32] == 10);
|
||||
}
|
Loading…
Reference in New Issue
Block a user