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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UNIQUE_VALUED_MAP_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UNIQUE_VALUED_MAP_H 18 19 #include <functional> 20 #include <optional> 21 #include <unordered_map> 22 23 namespace OHOS::Ace { 24 template<typename Key, typename Value, typename VHash = std::hash<Value>> 25 class UniqueValuedMap { 26 private: 27 std::unordered_map<Key, Value> keyToValue; 28 std::unordered_map<Value, Key, VHash> valueToKey; 29 30 public: Put(const Key & key,const Value & value)31 void Put(const Key& key, const Value& value) 32 { 33 // Remove previous mapping for this value (if exists) 34 auto valueIt = valueToKey.find(value); 35 if (valueIt != valueToKey.end()) { 36 keyToValue.erase(valueIt->second); 37 valueToKey.erase(valueIt); 38 } 39 40 // Remove previous mapping for this key (if exists) 41 auto keyIt = keyToValue.find(key); 42 if (keyIt != keyToValue.end()) { 43 valueToKey.erase(keyIt->second); 44 keyToValue.erase(keyIt); 45 } 46 47 // Add new mappings 48 keyToValue[key] = value; 49 valueToKey[value] = key; 50 } 51 Get(const Key & key)52 std::optional<Value> Get(const Key& key) const 53 { 54 auto it = keyToValue.find(key); 55 if (it == keyToValue.end()) { 56 return std::nullopt; 57 } 58 return it->second; 59 } 60 GetKey(const Value & value)61 std::optional<Key> GetKey(const Value& value) const 62 { 63 auto it = valueToKey.find(value); 64 if (it == valueToKey.end()) { 65 return std::nullopt; 66 } 67 return it->second; 68 } 69 Remove(const Key & key)70 void Remove(const Key& key) 71 { 72 auto it = keyToValue.find(key); 73 if (it != keyToValue.end()) { 74 valueToKey.erase(it->second); 75 keyToValue.erase(it); 76 } 77 } 78 RemoveValue(const Value & value)79 void RemoveValue(const Value& value) 80 { 81 auto it = valueToKey.find(value); 82 if (it != valueToKey.end()) { 83 keyToValue.erase(it->second); 84 valueToKey.erase(it); 85 } 86 } 87 ContainsKey(const Key & key)88 bool ContainsKey(const Key& key) const 89 { 90 return keyToValue.find(key) != keyToValue.end(); 91 } 92 ContainsValue(const Value & value)93 bool ContainsValue(const Value& value) const 94 { 95 return valueToKey.find(value) != valueToKey.end(); 96 } 97 Size()98 size_t Size() const 99 { 100 return keyToValue.size(); 101 } 102 Clear()103 void Clear() 104 { 105 keyToValue.clear(); 106 valueToKey.clear(); 107 } 108 RemoveIf(std::function<bool (const Key &,const Value &)> && pred)109 void RemoveIf(std::function<bool(const Key&, const Value&)>&& pred) 110 { 111 if (!pred) { 112 return; 113 } 114 for (auto it = keyToValue.begin(); it != keyToValue.end();) { 115 if (pred(it->first, it->second)) { 116 valueToKey.erase(it->second); 117 it = keyToValue.erase(it); 118 } else { 119 ++it; 120 } 121 } 122 } 123 }; 124 } // namespace OHOS::Ace 125 126 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UNIQUE_VALUED_MAP_H 127