• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "kernel_snapshot_trie.h"
17 
18 namespace OHOS {
19 namespace HiviewDFX {
20 
KernelSnapshotTrie()21 KernelSnapshotTrie::KernelSnapshotTrie() : root_(new TrieNode())
22 {
23 }
24 
Insert(const std::string & key,SnapshotSection type)25 bool KernelSnapshotTrie::Insert(const std::string& key, SnapshotSection type)
26 {
27     if (key.empty()) {
28         return false;
29     }
30 
31     TrieNode* currentNode = root_.get();
32     for (char ch : key) {
33         if (currentNode->children.find(ch) == currentNode->children.end()) {
34             currentNode->children[ch] = std::unique_ptr<TrieNode>(new TrieNode());
35         }
36         currentNode = currentNode->children[ch].get();
37     }
38 
39     currentNode->isEnd = true;
40     currentNode->sectionType = type;
41     return true;
42 }
43 
MatchPrefix(const std::string & key,SnapshotSection & type) const44 bool KernelSnapshotTrie::MatchPrefix(const std::string& key, SnapshotSection& type) const
45 {
46     TrieNode* currentNode = root_.get();
47     for (char ch : key) {
48         auto it = currentNode->children.find(ch);
49         if (it == currentNode->children.end()) {
50             return false;
51         }
52         currentNode = it->second.get();
53         if (currentNode->isEnd) {
54             type = currentNode->sectionType;
55             return true;
56         }
57     }
58     return false;
59 }
60 } // namespace HiviewDFX
61 } // namespace OHOS
62