mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 15:08:45 +00:00
42 lines
760 B
C++
42 lines
760 B
C++
#pragma once
|
|
|
|
#include <endian.h>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
|
|
namespace utils {
|
|
|
|
using IdType = std::size_t;
|
|
|
|
enum class ReferenceType { Reference, UniqueReference };
|
|
|
|
template<typename T>
|
|
class Storage {
|
|
public:
|
|
Storage() = default;
|
|
|
|
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
|