diff --git a/main.cpp b/main.cpp index 529d945..e7136a0 100644 --- a/main.cpp +++ b/main.cpp @@ -244,7 +244,6 @@ int main() std::forward_list> vec = { { "a", "b" },{ "a", "b" } }; std::unordered_map m; - std::array ar = {"d", "v"}; m["test"] = "asd"; LuaVal t441 = vec; std::cout << t441.dumps() << std::endl; diff --git a/smallfolk.cpp b/smallfolk.cpp index dca6cb4..8c908af 100644 --- a/smallfolk.cpp +++ b/smallfolk.cpp @@ -61,6 +61,11 @@ std::string LuaVal::tostring() const } size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const +{ + return LuaValHash(v); +} + +size_t LuaValHash(LuaVal const & v) { switch (v.tag) { @@ -73,7 +78,7 @@ size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const case TNUMBER: return std::hash()(v.d); case TTABLE: - return std::hash()(v.tbl_ptr); + return std::hash()(v.tbl_ptr); } return std::hash()(v.tostring()); } diff --git a/smallfolk.h b/smallfolk.h index e5b9641..38779d3 100644 --- a/smallfolk.h +++ b/smallfolk.h @@ -35,6 +35,20 @@ enum LuaTypeTag TBOOL, }; +class LuaVal; +size_t LuaValHash(LuaVal const & v); + +namespace std { + template <> + struct hash { + public: + size_t operator()(LuaVal const & v) const + { + return LuaValHash(v); + }; + }; +} + class LuaVal { public: @@ -52,7 +66,7 @@ class LuaVal size_t operator()(LuaVal const & v) const; }; - typedef std::unordered_map LuaTable; + typedef std::unordered_map LuaTable; typedef std::unique_ptr TblPtr; // circular reference memleak if insert self to self LuaVal(const LuaTypeTag tag) : tag(tag), tbl_ptr(tag == TTABLE ? new LuaTable() : nullptr), d(0), b(false) {} @@ -63,7 +77,6 @@ class LuaVal LuaVal(const std::string & s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {} LuaVal(const char * s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {} LuaVal(const bool b) : tag(TBOOL), tbl_ptr(nullptr), d(0), b(b) {} - LuaVal(LuaTable const & luatable) : tag(TTABLE), tbl_ptr(new LuaTable(luatable)), d(0), b(false) {} LuaVal(LuaVal const & val) : tag(val.tag), tbl_ptr(val.tag == TTABLE ? val.tbl_ptr ? new LuaTable(*val.tbl_ptr) : new LuaTable() : nullptr), s(val.s), d(val.d), b(val.b) {} LuaVal(LuaVal && val) noexcept : tag(std::move(val.tag)), tbl_ptr(std::move(val.tbl_ptr)), s(std::move(val.s)), d(std::move(val.d)), b(std::move(val.b)) {} LuaVal(std::initializer_list const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false) @@ -122,9 +135,8 @@ class LuaVal { InitializeMap(l); } - LuaVal(std::unordered_map const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false) + LuaVal(std::unordered_map const & l) : tag(TTABLE), tbl_ptr(new LuaTable(l)), d(0), b(false) { - InitializeMap(l); } template LuaVal(std::unordered_map const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false) { @@ -271,6 +283,8 @@ class LuaVal tbl[std::move(k)] = std::move(v); } } + + friend size_t LuaValHash(LuaVal const & v); LuaTypeTag tag; TblPtr tbl_ptr;