2023-04-17 11:31:00 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
#include <endian.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
namespace utils {
|
|
|
|
|
|
|
|
|
|
using IdType = std::size_t;
|
|
|
|
|
|
2023-04-21 14:27:55 +03:00
|
|
|
enum class ReferenceType { Reference, UniqueReference };
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
template<typename T>
|
|
|
|
|
class Storage {
|
|
|
|
|
public:
|
2023-04-21 14:27:55 +03:00
|
|
|
Storage() = default;
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
IdType GetId(const T& value) {
|
|
|
|
|
IdType id = 0;
|
|
|
|
|
auto value_position = value_to_id_.find(value);
|
|
|
|
|
|
|
|
|
|
if (value_position == value_to_id_.end()) {
|
|
|
|
|
id = id_to_value_.size();
|
|
|
|
|
value_to_id_[value] = id;
|
|
|
|
|
id_to_value_.push_back(value);
|
|
|
|
|
} else {
|
|
|
|
|
id = value_position->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const T& GetValue(IdType id) {
|
|
|
|
|
return id_to_value_[id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<T> id_to_value_;
|
|
|
|
|
std::unordered_map<T, IdType> value_to_id_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace utils
|