2018-03-06 14:25:36 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-03-06 14:25:36 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-03-06 17:27:52 +01:00
|
|
|
|
2018-03-06 14:25:36 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2019-11-26 18:53:50 +01:00
|
|
|
#include <limits>
|
2018-03-07 08:47:33 +01:00
|
|
|
#include <map>
|
2018-03-07 13:46:58 +01:00
|
|
|
#include <tuple>
|
2020-08-05 16:18:33 +02:00
|
|
|
#include <shared_mutex>
|
2018-03-06 14:25:36 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
template <class ValueT>
|
|
|
|
class Enumerator {
|
|
|
|
public:
|
|
|
|
using Key = int32;
|
2018-03-07 00:10:54 +01:00
|
|
|
|
2020-05-23 21:27:24 +02:00
|
|
|
std::map<ValueT, int32> get_map() const {
|
|
|
|
return map_;
|
|
|
|
}
|
|
|
|
|
2021-01-12 01:37:17 +01:00
|
|
|
void clear() {
|
|
|
|
map_.clear();
|
|
|
|
arr_.clear();
|
|
|
|
next_id = 1;
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:18:33 +02:00
|
|
|
void lock_access_mutex() const {
|
|
|
|
access_mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_access_mutex() const {
|
|
|
|
access_mutex.unlock();
|
|
|
|
}
|
2020-05-23 21:27:24 +02:00
|
|
|
|
2020-08-05 16:18:33 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return true if the key is new
|
|
|
|
*/
|
|
|
|
Key next() {
|
|
|
|
auto id = next_id++;
|
|
|
|
|
|
|
|
return id;
|
2020-05-23 21:27:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:18:33 +02:00
|
|
|
void erase_unsafe(Key key_y) {
|
|
|
|
auto find_val = arr_.find(key_y);
|
|
|
|
if (find_val != arr_.end()) {
|
|
|
|
// Erase this
|
|
|
|
map_.erase(find_val->second);
|
|
|
|
// TODO: Not sure about erasing this, instead
|
|
|
|
arr_.erase(key_y);
|
|
|
|
}
|
2020-05-23 21:27:24 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 00:10:54 +01:00
|
|
|
Key add(ValueT v) {
|
2020-08-05 16:18:33 +02:00
|
|
|
std::lock_guard<std::shared_timed_mutex> writerLock(access_mutex);
|
|
|
|
|
|
|
|
return add_internal(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
Key add_internal(ValueT v) {
|
|
|
|
auto id = next();
|
2018-03-07 08:47:33 +01:00
|
|
|
bool was_inserted;
|
|
|
|
decltype(map_.begin()) it;
|
2020-08-05 16:18:33 +02:00
|
|
|
std::tie(it, was_inserted) = map_.emplace(std::move(v), id);
|
|
|
|
if (was_inserted) {
|
|
|
|
arr_[id] = it->first;
|
2018-03-07 08:47:33 +01:00
|
|
|
}
|
|
|
|
return it->second;
|
2018-03-06 14:25:36 +01:00
|
|
|
}
|
2018-03-07 00:10:54 +01:00
|
|
|
|
2018-03-06 14:25:36 +01:00
|
|
|
const ValueT &get(Key key) const {
|
2020-08-05 16:18:33 +02:00
|
|
|
std::shared_lock<std::shared_timed_mutex> readerLock(access_mutex);
|
|
|
|
|
|
|
|
return get_internal(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueT &get_internal(Key key) const {
|
|
|
|
return arr_.at(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Key,ValueT> add_and_get(ValueT v) {
|
|
|
|
std::lock_guard<std::shared_timed_mutex> writerLock(access_mutex);
|
|
|
|
|
|
|
|
auto remote_key = add_internal(v);
|
|
|
|
auto &stored_info = get_internal(remote_key);
|
|
|
|
return std::make_pair(remote_key, stored_info);
|
2018-03-06 14:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-08-05 16:18:33 +02:00
|
|
|
mutable int32 next_id = 1;
|
2018-03-07 08:47:33 +01:00
|
|
|
std::map<ValueT, int32> map_;
|
2020-08-05 16:18:33 +02:00
|
|
|
std::unordered_map<int32, ValueT> arr_;
|
|
|
|
mutable std::shared_timed_mutex access_mutex;
|
2018-03-06 14:25:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|