1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ecmascript/dfx/hprof/rawheap_translate/string_hashmap.h" 17 18 namespace rawheap_translate { GetStringByKey(StringKey key) const19std::string StringHashMap::GetStringByKey(StringKey key) const 20 { 21 auto it = hashmap_.find(key); 22 if (it != hashmap_.end()) { 23 return it->second; 24 } 25 return ""; 26 } 27 InsertStrAndGetStringId(const std::string & str)28StringId StringHashMap::InsertStrAndGetStringId(const std::string &str) 29 { 30 StringKey key = GenerateStringKey(str); 31 auto it = indexMap_.find(key); 32 if (it != indexMap_.end()) { 33 return it->second; 34 } else { // NOLINT(readability-else-after-return) 35 index_++; 36 hashmap_.emplace(key, str); 37 orderedKey_.emplace_back(key); 38 indexMap_.emplace(key, index_); 39 return index_; 40 } 41 } 42 GenerateStringKey(const std::string & str) const43StringKey StringHashMap::GenerateStringKey(const std::string &str) const 44 { 45 return std::hash<std::string>{} (str); 46 } 47 } // namespace rawheap_translate 48