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