new hashmap

This commit is contained in:
blueloveTH 2022-12-05 04:57:13 +08:00
parent ee6286250d
commit 3c6e69476f
7 changed files with 1800 additions and 11 deletions

View File

@ -8,10 +8,8 @@
#include <sstream> #include <sstream>
#include <regex> #include <regex>
#include <unordered_map>
#include <memory> #include <memory>
#include <variant> #include <variant>
#include <functional>
#include <stack> #include <stack>
#include <cmath> #include <cmath>
#include <stdexcept> #include <stdexcept>

View File

@ -47,7 +47,7 @@ struct CodeObject {
// for goto use // for goto use
// note: some opcodes moves the bytecode, such as listcomp // note: some opcodes moves the bytecode, such as listcomp
// goto/label should be put at toplevel statements // goto/label should be put at toplevel statements
std::unordered_map<_Str, int> co_labels; emhash8::HashMap<_Str, int> co_labels;
void addLabel(const _Str& label){ void addLabel(const _Str& label){
if(co_labels.find(label) != co_labels.end()){ if(co_labels.find(label) != co_labels.end()){

View File

@ -29,7 +29,7 @@ public:
bool isCompilingClass = false; bool isCompilingClass = false;
VM* vm; VM* vm;
std::unordered_map<_TokenType, GrammarRule> rules; emhash8::HashMap<_TokenType, GrammarRule> rules;
_Code getCode() { _Code getCode() {
return codes.top(); return codes.top();

1789
src/hash_table8.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
#include <fstream> #include <fstream>
#include <functional>
#include "pocketpy.h" #include "pocketpy.h"

View File

@ -41,8 +41,8 @@ constexpr _TokenType TK(const char* const token) {
const _TokenType __KW_BEGIN = TK("class"); const _TokenType __KW_BEGIN = TK("class");
const _TokenType __KW_END = TK("raise"); const _TokenType __KW_END = TK("raise");
const std::unordered_map<std::string_view, _TokenType> __KW_MAP = [](){ const emhash8::HashMap<std::string_view, _TokenType> __KW_MAP = [](){
std::unordered_map<std::string_view, _TokenType> map; emhash8::HashMap<std::string_view, _TokenType> map;
for(int k=__KW_BEGIN; k<=__KW_END; k++) map[__TOKENS[k]] = k; for(int k=__KW_BEGIN; k<=__KW_END; k++) map[__TOKENS[k]] = k;
return map; return map;
}(); }();

View File

@ -33,25 +33,26 @@ public:
}; };
class PyVarDict: public std::unordered_map<_Str, PyVar> { #include "hash_table8.hpp"
class PyVarDict: public emhash8::HashMap<_Str, PyVar> {
PyVar& at(const _Str&) = delete; PyVar& at(const _Str&) = delete;
public: public:
PyVar& operator[](const _Str& key) { PyVar& operator[](const _Str& key) {
return std::unordered_map<_Str, PyVar>::operator[](key); return emhash8::HashMap<_Str, PyVar>::operator[](key);
} }
const PyVar& operator[](const _Str& key) const { const PyVar& operator[](const _Str& key) const {
auto it = find(key); auto it = find(key);
if (it == end()){ if (it == end()){
auto msg = "std::unordered_map key not found, '" + key.str() + "'"; auto msg = "map key not found, '" + key.str() + "'";
throw std::out_of_range(msg); throw std::out_of_range(msg);
} }
return it->second; return it->second;
} }
// define constructors the same as std::unordered_map using emhash8::HashMap<_Str, PyVar>::HashMap;
using std::unordered_map<_Str, PyVar>::unordered_map;
}; };