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 } Erase(T1 t1)63 void Erase(T1 t1) 64 { 65 auto streamIdHookidMap = internalMap_.find(t1); 66 if (streamIdHookidMap != internalMap_.end()) { 67 internalMap_.erase(streamIdHookidMap); 68 } 69 } Erase(T1 t1,T2 t2)70 void Erase(T1 t1, T2 t2) 71 { 72 auto streamIdHookidMap = internalMap_.find(t1); 73 if (streamIdHookidMap != internalMap_.end()) { 74 auto hookId = (*streamIdHookidMap).second.find(t2); 75 if (hookId != (*streamIdHookidMap).second.end()) { 76 (*streamIdHookidMap).second.erase(hookId); 77 } 78 } 79 } 80 81 private: 82 std::map<T1, std::map<T2, T3>> internalMap_; 83 T3 invalidValue_; 84 }; 85 86 #endif // DOUBLEMAP_H 87