• 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 
21 template<class T1, class T2, class T3>
22 class DoubleMap {
23 public:
DoubleMap(T3 invalidValue)24     DoubleMap(T3 invalidValue)
25     {
26         invalidValue_ = invalidValue;
27     }
SetInvalidRet(T3 invalidValue)28     void SetInvalidRet(T3 invalidValue)
29     {
30         invalidValue_ = invalidValue;
31     }
Insert(T1 t1,T2 t2,T3 t3)32     void Insert(T1 t1, T2 t2, T3 t3)
33     {
34         auto streamIdHookidMap = internalMap_.find(t1);
35         if (streamIdHookidMap != internalMap_.end()) {
36             auto hookId = (*streamIdHookidMap).second.find(t2);
37             if (hookId == (*streamIdHookidMap).second.end()) {
38                 (*streamIdHookidMap).second.insert(std::make_pair(t2, t3));
39             } else {
40                 (*streamIdHookidMap).second.at(t2) = t3;
41             }
42         } else {
43             std::map<T2, T3> mm = {
44                 {t2, t3}
45             };
46             internalMap_.insert(std::make_pair(t1, mm));
47         }
48     }
Find(T1 t1,T2 t2)49     T3 Find(T1 t1, T2 t2)
50     {
51         auto streamIdHookidMap = internalMap_.find(t1);
52         if (streamIdHookidMap != internalMap_.end()) {
53             auto hookId = (*streamIdHookidMap).second.find(t2);
54             if (hookId == (*streamIdHookidMap).second.end()) {
55                 return invalidValue_;
56             } else {
57                 return hookId->second;
58             }
59         } else {
60             return invalidValue_;
61         }
62     }
Find(T1 t1)63     const std::map<T2, T3>* Find(T1 t1) const
64     {
65         auto streamIdHookidMap = internalMap_.find(t1);
66         if (streamIdHookidMap != internalMap_.end()) {
67             return &streamIdHookidMap->second;
68         }
69         return nullptr;
70     }
Erase(T1 t1)71     void Erase(T1 t1)
72     {
73         auto streamIdHookidMap = internalMap_.find(t1);
74         if (streamIdHookidMap != internalMap_.end()) {
75             internalMap_.erase(streamIdHookidMap);
76         }
77     }
Erase(T1 t1,T2 t2)78     void Erase(T1 t1, T2 t2)
79     {
80         auto streamIdHookidMap = internalMap_.find(t1);
81         if (streamIdHookidMap != internalMap_.end()) {
82             auto hookId = (*streamIdHookidMap).second.find(t2);
83             if (hookId != (*streamIdHookidMap).second.end()) {
84                 (*streamIdHookidMap).second.erase(hookId);
85             }
86         }
87     }
Empty()88     bool Empty()
89     {
90         return internalMap_.size() == 0 ? true : false;
91     }
Clear()92     void Clear()
93     {
94         internalMap_.clear();
95     }
96 
97 private:
98     std::map<T1, std::map<T2, T3>> internalMap_;
99     T3 invalidValue_;
100 };
101 
102 #endif // DOUBLEMAP_H
103