Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 19 | #include <ftl/initializer_list.h> |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 20 | #include <ftl/optional.h> |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 21 | #include <ftl/small_vector.h> |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 22 | |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 24 | #include <functional> |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 25 | #include <type_traits> |
| 26 | #include <utility> |
| 27 | |
| 28 | namespace android::ftl { |
| 29 | |
| 30 | // Associative container with unique, unordered keys. Unlike std::unordered_map, key-value pairs are |
| 31 | // stored in contiguous storage for cache efficiency. The map is allocated statically until its size |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 32 | // exceeds N, at which point mappings are relocated to dynamic memory. The try_emplace operation has |
| 33 | // a non-standard analogue try_replace that destructively emplaces. The API also defines an in-place |
| 34 | // counterpart to insert_or_assign: emplace_or_replace. Lookup is done not via a subscript operator, |
| 35 | // but immutable getters that can optionally transform the value. |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 36 | // |
| 37 | // SmallMap<K, V, 0> unconditionally allocates on the heap. |
| 38 | // |
| 39 | // Example usage: |
| 40 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 41 | // ftl::SmallMap<int, std::string, 3> map; |
| 42 | // assert(map.empty()); |
| 43 | // assert(!map.dynamic()); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 44 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 45 | // map = ftl::init::map<int, std::string>(123, "abc")(-1)(42, 3u, '?'); |
| 46 | // assert(map.size() == 3u); |
| 47 | // assert(!map.dynamic()); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 48 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 49 | // assert(map.contains(123)); |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 50 | // assert(map.get(42).transform([](const std::string& s) { return s.size(); }) == 3u); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 51 | // |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 52 | // const auto opt = map.get(-1); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 53 | // assert(opt); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 54 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 55 | // std::string& ref = *opt; |
| 56 | // assert(ref.empty()); |
| 57 | // ref = "xyz"; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 58 | // |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 59 | // map.emplace_or_replace(0, "vanilla", 2u, 3u); |
| 60 | // assert(map.dynamic()); |
| 61 | // |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 62 | // assert(map == SmallMap(ftl::init::map(-1, "xyz"sv)(0, "nil"sv)(42, "???"sv)(123, "abc"sv))); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 63 | // |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 64 | template <typename K, typename V, std::size_t N, typename KeyEqual = std::equal_to<K>> |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 65 | class SmallMap final { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 66 | using Map = SmallVector<std::pair<const K, V>, N>; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 67 | |
Dominik Laskowski | 206996f | 2022-01-20 13:21:57 -0800 | [diff] [blame] | 68 | template <typename, typename, std::size_t, typename> |
| 69 | friend class SmallMap; |
| 70 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 71 | public: |
| 72 | using key_type = K; |
| 73 | using mapped_type = V; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 74 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 75 | using value_type = typename Map::value_type; |
| 76 | using size_type = typename Map::size_type; |
| 77 | using difference_type = typename Map::difference_type; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 78 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 79 | using reference = typename Map::reference; |
| 80 | using iterator = typename Map::iterator; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 81 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 82 | using const_reference = typename Map::const_reference; |
| 83 | using const_iterator = typename Map::const_iterator; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 84 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 85 | // Creates an empty map. |
| 86 | SmallMap() = default; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 87 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 88 | // Constructs at most N key-value pairs in place by forwarding per-pair constructor arguments. |
| 89 | // The template arguments K, V, and N are inferred using the deduction guide defined below. |
| 90 | // The syntax for listing pairs is as follows: |
| 91 | // |
| 92 | // ftl::SmallMap map = ftl::init::map<int, std::string>(123, "abc")(-1)(42, 3u, '?'); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 93 | // static_assert(std::is_same_v<decltype(map), ftl::SmallMap<int, std::string, 3>>); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 94 | // |
| 95 | // The types of the key and value are deduced if the first pair contains exactly two arguments: |
| 96 | // |
| 97 | // ftl::SmallMap map = ftl::init::map(0, 'a')(1, 'b')(2, 'c'); |
| 98 | // static_assert(std::is_same_v<decltype(map), ftl::SmallMap<int, char, 3>>); |
| 99 | // |
| 100 | template <typename U, std::size_t... Sizes, typename... Types> |
| 101 | SmallMap(InitializerList<U, std::index_sequence<Sizes...>, Types...>&& list) |
| 102 | : map_(std::move(list)) { |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 103 | deduplicate(); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 104 | } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 105 | |
Dominik Laskowski | 206996f | 2022-01-20 13:21:57 -0800 | [diff] [blame] | 106 | // Copies or moves key-value pairs from a convertible map. |
| 107 | template <typename Q, typename W, std::size_t M, typename E> |
| 108 | SmallMap(SmallMap<Q, W, M, E> other) : map_(std::move(other.map_)) {} |
| 109 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 110 | size_type max_size() const { return map_.max_size(); } |
| 111 | size_type size() const { return map_.size(); } |
| 112 | bool empty() const { return map_.empty(); } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 113 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 114 | // Returns whether the map is backed by static or dynamic storage. |
| 115 | bool dynamic() const { return map_.dynamic(); } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 116 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 117 | iterator begin() { return map_.begin(); } |
| 118 | const_iterator begin() const { return cbegin(); } |
| 119 | const_iterator cbegin() const { return map_.cbegin(); } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 120 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 121 | iterator end() { return map_.end(); } |
| 122 | const_iterator end() const { return cend(); } |
| 123 | const_iterator cend() const { return map_.cend(); } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 124 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 125 | // Returns whether a mapping exists for the given key. |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 126 | bool contains(const key_type& key) const { return get(key).has_value(); } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 127 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 128 | // Returns a reference to the value for the given key, or std::nullopt if the key was not found. |
| 129 | // |
| 130 | // ftl::SmallMap map = ftl::init::map('a', 'A')('b', 'B')('c', 'C'); |
| 131 | // |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 132 | // const auto opt = map.get('c'); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 133 | // assert(opt == 'C'); |
| 134 | // |
| 135 | // char d = 'd'; |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 136 | // const auto ref = map.get('d').value_or(std::ref(d)); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 137 | // ref.get() = 'D'; |
| 138 | // assert(d == 'D'); |
| 139 | // |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 140 | auto get(const key_type& key) const -> Optional<std::reference_wrapper<const mapped_type>> { |
| 141 | for (const auto& [k, v] : *this) { |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 142 | if (KeyEqual{}(k, key)) { |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 143 | return std::cref(v); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 144 | } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 145 | } |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 146 | return {}; |
| 147 | } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 148 | |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 149 | auto get(const key_type& key) -> Optional<std::reference_wrapper<mapped_type>> { |
| 150 | for (auto& [k, v] : *this) { |
| 151 | if (KeyEqual{}(k, key)) { |
| 152 | return std::ref(v); |
| 153 | } |
| 154 | } |
| 155 | return {}; |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 158 | // Returns an iterator to an existing mapping for the given key, or the end() iterator otherwise. |
| 159 | const_iterator find(const key_type& key) const { return const_cast<SmallMap&>(*this).find(key); } |
| 160 | iterator find(const key_type& key) { return find(key, begin()); } |
| 161 | |
| 162 | // Inserts a mapping unless it exists. Returns an iterator to the inserted or existing mapping, |
| 163 | // and whether the mapping was inserted. |
| 164 | // |
| 165 | // On emplace, if the map reaches its static or dynamic capacity, then all iterators are |
| 166 | // invalidated. Otherwise, only the end() iterator is invalidated. |
| 167 | // |
| 168 | template <typename... Args> |
| 169 | std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args) { |
| 170 | if (const auto it = find(key); it != end()) { |
| 171 | return {it, false}; |
| 172 | } |
| 173 | |
| 174 | auto& ref = map_.emplace_back(std::piecewise_construct, std::forward_as_tuple(key), |
| 175 | std::forward_as_tuple(std::forward<Args>(args)...)); |
| 176 | return {&ref, true}; |
| 177 | } |
| 178 | |
| 179 | // Replaces a mapping if it exists, and returns an iterator to it. Returns the end() iterator |
| 180 | // otherwise. |
| 181 | // |
| 182 | // The value is replaced via move constructor, so type V does not need to define copy/move |
| 183 | // assignment, e.g. its data members may be const. |
| 184 | // |
| 185 | // The arguments may directly or indirectly refer to the mapping being replaced. |
| 186 | // |
| 187 | // Iterators to the replaced mapping point to its replacement, and others remain valid. |
| 188 | // |
| 189 | template <typename... Args> |
| 190 | iterator try_replace(const key_type& key, Args&&... args) { |
| 191 | const auto it = find(key); |
| 192 | if (it == end()) return it; |
| 193 | map_.replace(it, std::piecewise_construct, std::forward_as_tuple(key), |
| 194 | std::forward_as_tuple(std::forward<Args>(args)...)); |
| 195 | return it; |
| 196 | } |
| 197 | |
| 198 | // In-place counterpart of std::unordered_map's insert_or_assign. Returns true on emplace, or |
| 199 | // false on replace. |
| 200 | // |
| 201 | // The value is emplaced and replaced via move constructor, so type V does not need to define |
| 202 | // copy/move assignment, e.g. its data members may be const. |
| 203 | // |
| 204 | // On emplace, if the map reaches its static or dynamic capacity, then all iterators are |
| 205 | // invalidated. Otherwise, only the end() iterator is invalidated. On replace, iterators |
| 206 | // to the replaced mapping point to its replacement, and others remain valid. |
| 207 | // |
| 208 | template <typename... Args> |
| 209 | std::pair<iterator, bool> emplace_or_replace(const key_type& key, Args&&... args) { |
| 210 | const auto [it, ok] = try_emplace(key, std::forward<Args>(args)...); |
| 211 | if (ok) return {it, ok}; |
| 212 | map_.replace(it, std::piecewise_construct, std::forward_as_tuple(key), |
| 213 | std::forward_as_tuple(std::forward<Args>(args)...)); |
| 214 | return {it, ok}; |
| 215 | } |
| 216 | |
| 217 | // Removes a mapping if it exists, and returns whether it did. |
| 218 | // |
| 219 | // The last() and end() iterators, as well as those to the erased mapping, are invalidated. |
| 220 | // |
| 221 | bool erase(const key_type& key) { return erase(key, begin()); } |
| 222 | |
Dominik Laskowski | 44828ce | 2021-09-13 11:00:22 -0700 | [diff] [blame] | 223 | // Removes all mappings. |
| 224 | // |
| 225 | // All iterators are invalidated. |
| 226 | // |
| 227 | void clear() { map_.clear(); } |
| 228 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 229 | private: |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 230 | iterator find(const key_type& key, iterator first) { |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 231 | return std::find_if(first, end(), |
| 232 | [&key](const auto& pair) { return KeyEqual{}(pair.first, key); }); |
Dominik Laskowski | dda9bba | 2021-02-03 18:56:00 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool erase(const key_type& key, iterator first) { |
| 236 | const auto it = find(key, first); |
| 237 | if (it == end()) return false; |
| 238 | map_.unstable_erase(it); |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | void deduplicate() { |
| 243 | for (auto it = begin(); it != end();) { |
| 244 | if (const auto key = it->first; ++it != end()) { |
| 245 | while (erase(key, it)); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 250 | Map map_; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | // Deduction guide for in-place constructor. |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 254 | template <typename K, typename V, typename E, std::size_t... Sizes, typename... Types> |
| 255 | SmallMap(InitializerList<KeyValue<K, V, E>, std::index_sequence<Sizes...>, Types...>&&) |
| 256 | -> SmallMap<K, V, sizeof...(Sizes), E>; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 257 | |
| 258 | // Returns whether the key-value pairs of two maps are equal. |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 259 | template <typename K, typename V, std::size_t N, typename Q, typename W, std::size_t M, typename E> |
| 260 | bool operator==(const SmallMap<K, V, N, E>& lhs, const SmallMap<Q, W, M, E>& rhs) { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 261 | if (lhs.size() != rhs.size()) return false; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 262 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 263 | for (const auto& [k, v] : lhs) { |
| 264 | const auto& lv = v; |
Dominik Laskowski | 54494bd | 2022-08-02 13:37:14 -0700 | [diff] [blame] | 265 | if (!rhs.get(k).transform([&lv](const W& rv) { return lv == rv; }).value_or(false)) { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 266 | return false; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 267 | } |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 268 | } |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 269 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 270 | return true; |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // TODO: Remove in C++20. |
Dominik Laskowski | 1874896 | 2021-09-26 18:47:58 -0700 | [diff] [blame] | 274 | template <typename K, typename V, std::size_t N, typename Q, typename W, std::size_t M, typename E> |
| 275 | inline bool operator!=(const SmallMap<K, V, N, E>& lhs, const SmallMap<Q, W, M, E>& rhs) { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 276 | return !(lhs == rhs); |
Dominik Laskowski | c4b9146 | 2020-11-02 13:37:25 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 279 | } // namespace android::ftl |