• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) const19 std::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 
GetKeyByStringId(StringId stringId) const28 StringKey StringHashMap::GetKeyByStringId(StringId stringId) const
29 {
30     return orderedKey_[stringId - CUSTOM_STRID_START]; // 3: index_ start from 3
31 }
32 
InsertStrAndGetStringId(const std::string & str)33 StringId StringHashMap::InsertStrAndGetStringId(const std::string &str)
34 {
35     StringKey key = GenerateStringKey(str);
36     auto it = indexMap_.find(key);
37     if (it != indexMap_.end()) {
38         return it->second;
39     } else {  // NOLINT(readability-else-after-return)
40         index_++;
41         hashmap_.emplace(key, str);
42         orderedKey_.emplace_back(key);
43         indexMap_.emplace(key, index_);
44         return index_;
45     }
46 }
47 
GenerateStringKey(const std::string & str) const48 StringKey StringHashMap::GenerateStringKey(const std::string &str) const
49 {
50     return std::hash<std::string>{} (str);
51 }
52 }  // namespace rawheap_translate
53