OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_CONTAINERS_FLAT_MAP_H_ |
| 6 #define BASE_CONTAINERS_FLAT_MAP_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 #include <functional> |
| 10 #include <map> |
| 11 #include <utility> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/containers/flat_sorted_container_base.h" |
| 15 #include "base/logging.h" |
| 16 |
| 17 namespace base { |
| 18 |
| 19 namespace internal { |
| 20 |
| 21 template <typename Key, typename T, class Compare> |
| 22 struct map_compare : private Compare { |
| 23 using key_type = Key; |
| 24 using value_type = std::pair<key_type, T>; |
| 25 |
| 26 using Compare::operator(); |
| 27 |
| 28 bool operator()(const value_type& lhs, const key_type& rhs) { |
| 29 return operator()(lhs.first, rhs); |
| 30 } |
| 31 |
| 32 bool operator()(const key_type& lhs, const value_type& rhs) { |
| 33 return operator()(lhs, rhs.first); |
| 34 } |
| 35 |
| 36 bool operator()(const value_type& lhs, const value_type& rhs) { |
| 37 return operator()(lhs.first, rhs.first); |
| 38 } |
| 39 |
| 40 template <typename Lhs, typename Rhs> |
| 41 bool equal(const Lhs& lhs, const Rhs& rhs) { |
| 42 return !operator()(lhs, rhs) && !operator()(rhs, lhs); |
| 43 } |
| 44 |
| 45 const key_type& key_from_value(const value_type& value) { |
| 46 return value.first; |
| 47 } |
| 48 |
| 49 key_type& key_from_value(value_type& value) { // NOLINT |
| 50 return value.first; |
| 51 } |
| 52 }; |
| 53 |
| 54 } // namespace internal |
| 55 |
| 56 // std::vector is not particulary friendly with const value type, |
| 57 // so, unlike std::map, we use non const Key |
| 58 template <typename Key, |
| 59 typename T, |
| 60 class Compare = std::less<Key>, |
| 61 class UnderlyingType = std::vector<std::pair<Key, T>>> |
| 62 class flat_map : public internal::flat_sorted_container_base< |
| 63 internal::map_compare<Key, T, Compare>, |
| 64 UnderlyingType> { |
| 65 using base_type = internal::flat_sorted_container_base< |
| 66 internal::map_compare<Key, T, Compare>, |
| 67 UnderlyingType>; |
| 68 |
| 69 public: |
| 70 // typedefs------------------------------------------------------------------ |
| 71 |
| 72 // ours |
| 73 using std_map = std::map<Key, T, Compare>; |
| 74 using mapped_type = T; |
| 75 using key_type = typename base_type::key_type; |
| 76 |
| 77 // ctors--------------------------------------------------------------------- |
| 78 using base_type::base_type; |
| 79 |
| 80 // methods------------------------------------------------------------------- |
| 81 |
| 82 mapped_type& at(const key_type& key) { |
| 83 return const_cast<T&>(static_cast<const flat_map&>(*this).at(key)); |
| 84 } |
| 85 |
| 86 const mapped_type& at(const key_type& key) const { |
| 87 auto pos = this->find(key); |
| 88 CHECK(pos != this->end()); |
| 89 return pos->second; |
| 90 } |
| 91 |
| 92 mapped_type& operator[](key_type key) { |
| 93 auto pos = this->lower_bound(key); |
| 94 if (pos != this->end() && this->key_value_comp().equal(*pos, key)) { |
| 95 return pos->second; |
| 96 } |
| 97 auto guard = this->unsafe_access(); |
| 98 T& res = guard->emplace(pos, std::move(key), T())->second; |
| 99 guard.release(); |
| 100 return res; |
| 101 } |
| 102 }; |
| 103 |
| 104 } // namespace base |
| 105 |
| 106 #endif // BASE_CONTAINERS_FLAT_MAP_H_ |
OLD | NEW |