2016-07-05 11:01:22 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-02 20:23:39 +08:00
|
|
|
#include "data_structures/concurrent/common.hpp"
|
2016-07-31 04:20:21 +08:00
|
|
|
#include "data_structures/concurrent/skiplist.hpp"
|
2016-07-05 11:01:22 +08:00
|
|
|
|
2016-07-31 04:20:21 +08:00
|
|
|
using std::pair;
|
|
|
|
|
2016-07-31 20:56:13 +08:00
|
|
|
template <typename K, typename T>
|
|
|
|
class ConcurrentMap
|
|
|
|
{
|
2016-08-25 22:29:45 +08:00
|
|
|
typedef Item<K, T> item_t;
|
|
|
|
typedef SkipList<item_t> list;
|
|
|
|
typedef typename SkipList<item_t>::Iterator list_it;
|
|
|
|
typedef typename SkipList<item_t>::ConstIterator list_it_con;
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
public:
|
|
|
|
ConcurrentMap() {}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
class Accessor : public AccessorBase<item_t>
|
|
|
|
{
|
|
|
|
friend class ConcurrentMap;
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
using AccessorBase<item_t>::AccessorBase;
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
private:
|
|
|
|
using AccessorBase<item_t>::accessor;
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
public:
|
|
|
|
std::pair<list_it, bool> insert(const K &key, const T &data)
|
|
|
|
{
|
|
|
|
return accessor.insert(item_t(key, data));
|
|
|
|
}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
std::pair<list_it, bool> insert(const K &key, T &&data)
|
|
|
|
{
|
2016-08-30 07:45:07 +08:00
|
|
|
return accessor.insert(item_t(key, std::move(data)));
|
2016-08-25 22:29:45 +08:00
|
|
|
}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
std::pair<list_it, bool> insert(K &&key, T &&data)
|
|
|
|
{
|
|
|
|
return accessor.insert(
|
|
|
|
item_t(std::forward<K>(key), std::forward<T>(data)));
|
|
|
|
}
|
|
|
|
|
2016-08-30 07:45:07 +08:00
|
|
|
template <class... Args1, class... Args2>
|
|
|
|
std::pair<list_it, bool> emplace(const K &key,
|
|
|
|
std::tuple<Args1...> first_args,
|
|
|
|
std::tuple<Args2...> second_args)
|
|
|
|
{
|
|
|
|
return accessor.emplace(
|
|
|
|
key, std::piecewise_construct,
|
|
|
|
std::forward<std::tuple<Args1...>>(first_args),
|
|
|
|
std::forward<std::tuple<Args2...>>(second_args));
|
|
|
|
}
|
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
list_it_con find(const K &key) const { return accessor.find(key); }
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
list_it find(const K &key) { return accessor.find(key); }
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
list_it_con find_or_larger(const T &item) const
|
|
|
|
{
|
|
|
|
return accessor.find_or_larger(item);
|
|
|
|
}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
list_it find_or_larger(const T &item)
|
|
|
|
{
|
|
|
|
return accessor.find_or_larger(item);
|
|
|
|
}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
bool contains(const K &key) const
|
|
|
|
{
|
|
|
|
return this->find(key) != this->end();
|
|
|
|
}
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
bool remove(const K &key) { return accessor.remove(key); }
|
|
|
|
};
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
Accessor access() { return Accessor(&skiplist); }
|
2016-07-31 04:20:21 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
const Accessor access() const { return Accessor(&skiplist); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
list skiplist;
|
2016-07-05 11:01:22 +08:00
|
|
|
};
|