将字符串转义成一个对应的固定数字。
#pragma once
#include <unordered_map>
#include <mutex>
class dictionary {
private:
std::unordered_map<std::string, int> string_map;
std::unordered_map<const char*, int> read_map;
std::unordered_map<const char*, int> write_map;
std::unordered_map<int, std::string> bi_string_map;
std::mutex _Mutex;
int _id = 0;
public:
int operator[](const char* _s) {
auto it = read_map.find(_s);
if (it == read_map.end()) {
std::lock_guard<std::mutex> _lock_guard(_Mutex);
if (string_map.find(_s) == string_map.end()) {
string_map[_s] = _id++;
}
write_map.insert({ _s, string_map[_s] });
return it->second;
}
return it->second;
}
void sync() {
for (auto& _p : write_map) {
read_map[_p.first] = _p.second;
}
write_map.clear();
}
};
// 定义全局变量 提供使用字典
namespace dict {
extern dictionary attributeDict;
}