• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef SRC_TRACE_BASE_DOUBLEMAP_H
17 #define SRC_TRACE_BASE_DOUBLEMAP_H
18 
19 #include <map>
20 #include "base_map.h"
21 
22 template <class T1, class T2, class T3>
23 class DoubleMap {
24 public:
DoubleMap(T3 invalidValue)25     DoubleMap(T3 invalidValue)
26     {
27         invalidValue_ = invalidValue;
28     }
SetInvalidRet(T3 invalidValue)29     void SetInvalidRet(T3 invalidValue)
30     {
31         invalidValue_ = invalidValue;
32     }
Insert(T1 t1,T2 t2,T3 t3)33     void Insert(T1 t1, T2 t2, T3 t3)
34     {
35         auto streamIdHookidMap = internalMap_.find(t1);
36         if (streamIdHookidMap != internalMap_.end()) {
37             auto hookId = (*streamIdHookidMap).second.find(t2);
38             if (hookId == (*streamIdHookidMap).second.end()) {
39                 (*streamIdHookidMap).second.emplace(std::make_pair(t2, t3));
40             } else {
41                 (*streamIdHookidMap).second.at(t2) = t3;
42             }
43         } else {
44             std::map<T2, T3> mm = {{t2, t3}};
45             internalMap_.emplace(std::make_pair(t1, mm));
46         }
47     }
Find(T1 t1,T2 t2)48     T3 Find(T1 t1, T2 t2)
49     {
50         auto streamIdHookidMap = internalMap_.find(t1);
51         if (streamIdHookidMap != internalMap_.end()) {
52             auto hookId = (*streamIdHookidMap).second.find(t2);
53             if (hookId == (*streamIdHookidMap).second.end()) {
54                 return invalidValue_;
55             } else {
56                 return hookId->second;
57             }
58         } else {
59             return invalidValue_;
60         }
61     }
Find(T1 t1)62     const std::map<T2, T3>* Find(T1 t1) const
63     {
64         auto streamIdHookidMap = internalMap_.find(t1);
65         if (streamIdHookidMap != internalMap_.end()) {
66             return &streamIdHookidMap->second;
67         }
68         return nullptr;
69     }
70     MAP_ERASE_WITH_SINGLE_PARME(T1)
DOUBLE_MAP_ERASE_WITH_DOUBLE_PARME(T1,T2)71     DOUBLE_MAP_ERASE_WITH_DOUBLE_PARME(T1, T2)
72     bool Empty()
73     {
74         return internalMap_.size() == 0 ? true : false;
75     }
Clear()76     void Clear()
77     {
78         internalMap_.clear();
79     }
80 
81 private:
82     std::map<T1, std::map<T2, T3>> internalMap_;
83     T3 invalidValue_;
84 };
85 
86 #endif // DOUBLEMAP_H
87