diff --git a/src/Stmt.cc b/src/Stmt.cc index ee441ba213..f176febd7a 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -710,7 +710,7 @@ void SwitchStmt::Init() t->Append(e->GetType()); comp_hash = new CompositeHash(std::move(t)); - case_label_value_map.SetDeleteFunc(int_del_func); + case_label_hash_map.SetDeleteFunc(int_del_func); } SwitchStmt::SwitchStmt(ExprPtr index, case_list* arg_cases) @@ -855,12 +855,13 @@ bool SwitchStmt::AddCaseLabelValueMapping(const Val* v, int idx) type_name(e->GetType()->Tag())); } - int* label_idx = case_label_value_map.Lookup(hk.get()); + int* label_idx = case_label_hash_map.Lookup(hk.get()); if ( label_idx ) return false; - case_label_value_map.Insert(hk.get(), new int(idx)); + case_label_value_map[v] = idx; + case_label_hash_map.Insert(hk.get(), new int(idx)); return true; } @@ -884,7 +885,7 @@ std::pair SwitchStmt::FindCaseLabelMatch(const Val* v) const ID* label_id = nullptr; // Find matching expression cases. - if ( case_label_value_map.Length() ) + if ( case_label_hash_map.Length() ) { auto hk = comp_hash->MakeHashKey(*v, true); @@ -897,7 +898,7 @@ std::pair SwitchStmt::FindCaseLabelMatch(const Val* v) const return std::make_pair(-1, nullptr); } - if ( auto i = case_label_value_map.Lookup(hk.get()) ) + if ( auto i = case_label_hash_map.Lookup(hk.get()) ) label_idx = *i; } diff --git a/src/Stmt.h b/src/Stmt.h index a6c6b60621..1031cd7b5f 100644 --- a/src/Stmt.h +++ b/src/Stmt.h @@ -183,15 +183,24 @@ public: bool NoFlowAfter(bool ignore_break) const override; protected: + friend class ZAMCompiler; + + int DefaultCaseIndex() const { return default_case_idx; } + const auto& ValueMap() const { return case_label_value_map; } + const std::vector>* TypeMap() const + { return &case_label_type_list; } + const CompositeHash* CompHash() const { return comp_hash; } + ValPtr DoExec(Frame* f, Val* v, StmtFlowType& flow) override; bool IsPure() const override; // Initialize composite hash and case label map. void Init(); - // Adds an entry in case_label_value_map for the given value to associate it - // with the given index in the cases list. If the entry already exists, - // returns false, else returns true. + // Adds entries in case_label_value_map and case_label_hash_map + // for the given value to associate it with the given index in + // the cases list. If the entry already exists, returns false, + // else returns true. bool AddCaseLabelValueMapping(const Val* v, int idx); // Adds an entry in case_label_type_map for the given type (w/ ID) to @@ -208,7 +217,8 @@ protected: case_list* cases; int default_case_idx; CompositeHash* comp_hash; - PDict case_label_value_map; + std::unordered_map case_label_value_map; + PDict case_label_hash_map; std::vector> case_label_type_list; };