mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 13:07:13 +00:00
going to change symbol table structure
This commit is contained in:
parent
9c25cb1c6f
commit
782a48c4ff
11 changed files with 535 additions and 354 deletions
36
include/utils.hpp
Normal file
36
include/utils.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <endian.h>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace utils {
|
||||
|
||||
using IdType = std::size_t;
|
||||
|
||||
template<typename T>
|
||||
class Storage {
|
||||
public:
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue