Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * * Copyright (c) 2021 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #ifndef LSL_OrderedMap_h #define LSL_OrderedMap_h #include "BTree.h" namespace lsl { template<typename K, typename T, class C=std::less<K>> struct TRIVIAL_ABI OrderedMap { using key_type = K; using mapped_type = T; using value_type = std::pair<const key_type,mapped_type>; using key_compare = C; using difference_type = std::ptrdiff_t; using reference = value_type&; using pointer = value_type*; using size_type = std::size_t; private: // So this is gross. The issue is that the value_types can't be copied because of the const key_type. // std::map handles that by constructing in place and never moving the pairs, but that does not work // for a B+Tree. Instead we use an internal type so we can move things around as necessary, and just // cast it to the value_type before hand it to users so they don't mutate the key and invalidate the // the tree. using internal_value_type = std::pair<key_type,mapped_type>; public: struct value_compare { bool operator()(const value_type& lhs, const value_type& rhs) const { return _comp(lhs.first, rhs.first); } private: friend struct OrderedMap; value_compare( key_compare Comp ) : _comp(Comp) {} key_compare _comp; }; struct const_iterator { using iterator_category = std::bidirectional_iterator_tag; using value_type = std::pair<const key_type,mapped_type>; using difference_type = std::ptrdiff_t; using pointer = value_type*; using reference = value_type&; const_iterator(const const_iterator& other) : _i(other._i) {} const_iterator(const_iterator&& other) : _i(other._i) { swap(other); } const_iterator& operator=(const const_iterator& other) { auto tmp = other; swap(tmp); return *this; } const_iterator& operator=(const_iterator&& other) { swap(other); return *this; } reference operator*() { return *((pointer)&*_i); } pointer operator->() { // So this is gross. std::map avoid this by never moving return (pointer)&*_i; } const_iterator& operator++() { ++_i; return *this; } const_iterator operator++(int) { auto tmp = *this; ++*this; return tmp; } const_iterator& operator--() { --_i; return *this; } const_iterator operator--(int) const { auto result = *this; --*this; return result; } std::strong_ordering operator<=>(const const_iterator& other) const = default; const_iterator(typename BTree<internal_value_type, value_compare,false>::const_iterator I) : _i(I) {} friend void swap(const_iterator& x, const_iterator& y) { x.swap(y); } private: friend struct OrderedMap; void swap(const_iterator& other) { using std::swap; if (this == &other) { return; } swap(_i, other._i); } typename BTree<internal_value_type,value_compare,false>::const_iterator _i; }; using iterator = const_iterator; mapped_type& operator[]( const key_type& key ) { auto i = find(key); if (i == end()) { auto j = insert({key, mapped_type()}); i = j.first; } return i->second; } const_iterator cbegin() const { return _btree.begin(); } const_iterator cend() const { return _btree.end();} const_iterator begin() const { return cbegin(); } const_iterator end() const { return cend(); } iterator begin() { return std::as_const(*this).begin(); } iterator end() { return std::as_const(*this).end(); } iterator insert(const_iterator hint, const value_type& key) { return _btree.insert(hint._i, key).first; } iterator insert(const_iterator hint, value_type&& key) { return _btree.insert(hint._i, std::move(key)).first; } std::pair<iterator,bool> insert(const value_type& key) { return _btree.insert(key); } std::pair<iterator,bool> insert(value_type&& key) { return _btree.insert(std::move(key)); } const_iterator find(const key_type& key) const { return _btree.find({key, mapped_type()}); } iterator find(const key_type& key) { return iterator(std::as_const(*this).find(key)); } const_iterator lower_bound(const key_type& key) const { return _btree.lower_bound({key, mapped_type()}); } iterator lower_bound(const key_type& key) { return iterator(std::as_const(*this).lower_bound(key)); } iterator erase(iterator i) { return _btree.erase(i._i); } size_type erase(const key_type& key) { return _btree.erase({key, mapped_type()}); } size_type size() const { return _btree.size(); } bool empty() const { return _btree.empty(); } void clear() { return _btree.clear(); } size_type count(const key_type& key) const { return _btree.count({key, mapped_type()}); } OrderedMap() = delete; // OrderedMap(const OrderedMap&); PRIVATE explicit OrderedMap(key_compare comp, Allocator& allocator) : _btree(value_compare(comp), allocator) {} explicit OrderedMap(Allocator& allocator) : OrderedMap(key_compare(), allocator) {} OrderedMap(OrderedMap&& other) { swap(other); } OrderedMap& operator=(const OrderedMap& other) { auto tmp = other; swap(tmp); return *this; } OrderedMap& operator=(OrderedMap&& other) { swap(other); return *this; } friend void swap(OrderedMap& x, OrderedMap& y) { x.swap(y); } private: OrderedMap(const OrderedMap& other) : _btree(other._btree) {} void swap(OrderedMap& other) { using std::swap; if (this == &other) { return; } swap(_btree, other._btree); } value_compare value_comp() const { return value_compare(); } BTree<internal_value_type,value_compare,false> _btree; }; template<typename K, typename T, class C=std::less<K>> struct TRIVIAL_ABI OrderedMultiMap { using key_type = K; using mapped_type = T; using value_type = std::pair<const key_type,mapped_type>; using key_compare = C; using difference_type = std::ptrdiff_t; using reference = value_type&; using pointer = value_type*; using size_type = std::size_t; private: // So this is gross. The issue is that the value_types can't be copied because of the const key_type. // std::map handles that by constructing in place and never moving the pairs, but that does not work // for a B+Tree. Instead we use an internal type so we can move things around as necessary, and just // cast it to the value_type before hand it to users so they don't mutate the key and invalidate the // the tree. using internal_value_type = std::pair<key_type,mapped_type>; public: struct value_compare { bool operator()(const value_type& lhs, const value_type& rhs) const { return _comp(lhs.first, rhs.first); } private: friend struct OrderedMultiMap; value_compare( key_compare Comp ) : _comp(Comp) {} key_compare _comp; }; struct const_iterator { using iterator_category = std::bidirectional_iterator_tag; using value_type = std::pair<const key_type,mapped_type>; using difference_type = std::ptrdiff_t; using pointer = value_type*; using reference = value_type&; const_iterator(const const_iterator& other) : _i(other._i) {} const_iterator(const_iterator&& other) : _i(other._i) { swap(other); } const_iterator& operator=(const const_iterator& other) { auto tmp = other; swap(tmp); return *this; } const_iterator& operator=(const_iterator&& other) { swap(other); return *this; } reference operator*() { return *((pointer)&*_i); } pointer operator->() { // So this is gross. std::map avoid this by never moving return (pointer)&*_i; } const_iterator& operator++() { ++_i; return *this; } const_iterator operator++(int) { auto tmp = *this; ++*this; return tmp; } const_iterator& operator--() { --_i; return *this; } const_iterator operator--(int) const { auto result = *this; --*this; return result; } std::strong_ordering operator<=>(const const_iterator& other) const = default; const_iterator(typename BTree<internal_value_type, value_compare,true>::const_iterator I) : _i(I) {} friend void swap(const_iterator& x, const_iterator& y) { x.swap(y); } private: friend struct OrderedMultiMap; void swap(const_iterator& other) { using std::swap; if (this == &other) { return; } swap(_i, other._i); } typename BTree<internal_value_type,value_compare,true>::const_iterator _i; }; using iterator = const_iterator; const_iterator cbegin() const { return _btree.begin(); } const_iterator cend() const { return _btree.end();} const_iterator begin() const { return cbegin(); } const_iterator end() const { return cend(); } iterator begin() { return std::as_const(*this).begin(); } iterator end() { return std::as_const(*this).end(); } iterator insert(const_iterator hint, const value_type& key) { return _btree.insert(hint._i, key).first; } iterator insert(const_iterator hint, value_type&& key) { return _btree.insert(hint._i, std::move(key)).first; } iterator insert(const value_type& key) { return _btree.insert(key).first; } iterator insert(value_type&& key) { return _btree.insert(std::move(key)).first; } const_iterator find(const key_type& key) const { return _btree.find({key, mapped_type()}); } iterator find(const key_type& key) { return iterator(std::as_const(*this).find(key)); } const_iterator lower_bound(const key_type& key) const { return _btree.lower_bound({key, mapped_type()}); } iterator lower_bound(const key_type& key) { return iterator(std::as_const(*this).lower_bound(key)); } iterator erase(iterator i) { return _btree.erase(i._i); } size_type erase(const key_type& key) { return _btree.erase({key, mapped_type()}); } size_type size() const { return _btree.size(); } bool empty() const { return _btree.empty(); } void clear() { return _btree.clear(); } size_type count(const key_type& key) const { return _btree.count({key, mapped_type()}); } OrderedMultiMap() = delete; //OrderedMultiMap(const OrderedMultiMap&); PRIVATE explicit OrderedMultiMap(key_compare comp, Allocator& allocator) : _btree(value_compare(comp), allocator) {} explicit OrderedMultiMap(Allocator& allocator) : OrderedMultiMap(key_compare(), allocator) {} OrderedMultiMap(OrderedMultiMap&& other) { swap(other); } OrderedMultiMap& operator=(const OrderedMultiMap& other) { auto tmp = other; swap(tmp); return *this; } OrderedMultiMap& operator=(OrderedMultiMap&& other) { swap(other); return *this; } friend void swap(OrderedMultiMap& x, OrderedMultiMap& y) { x.swap(y); } private: OrderedMultiMap(const OrderedMultiMap& other) : _btree(other._btree) {} void swap(OrderedMultiMap& other) { using std::swap; if (this == &other) { return; } swap(_btree, other._btree); } value_compare value_comp() const { return value_compare(); } BTree<internal_value_type,value_compare,true> _btree; }; }; #endif /* LSL_OrderedMap_h */ |