1 // Copyright 2020 The Chromium Authors 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_FIXED_FLAT_SET_H_ 6 #define BASE_CONTAINERS_FIXED_FLAT_SET_H_ 7 8 #include <array> 9 #include <functional> 10 #include <type_traits> 11 12 #include "base/check.h" 13 #include "base/containers/flat_set.h" 14 #include "base/containers/flat_tree.h" 15 16 namespace base { 17 18 // fixed_flat_set is a immutable container with a std::set-like interface that 19 // stores its contents in a sorted std::array. 20 // 21 // It is a special case of base::flat_set, and mostly useful as a look-up table. 22 // 23 // Please see //base/containers/README.md for an overview of which container 24 // to select. 25 // 26 // QUICK REFERENCE 27 // 28 // Most of the core functionality is inherited from flat_tree. Please see 29 // flat_tree.h for more details for most of these functions. As a quick 30 // reference, the functions available are: 31 // 32 // Constructors (inputs need to be sorted): 33 // fixed_flat_set(const fixed_flat_set&); 34 // fixed_flat_set(sorted_unique_t, 35 // const container_type& items, 36 // const Compare& compare = Compare()); 37 // 38 // Size management functions: 39 // size_t size() const; 40 // size_t max_size() const; 41 // bool empty() const; 42 // 43 // Iterator functions: 44 // const_iterator begin() const; 45 // const_iterator cbegin() const; 46 // const_iterator end() const; 47 // const_iterator cend() const; 48 // const reverse_iterator rbegin() const; 49 // const_reverse_iterator crbegin() const; 50 // const_reverse_iterator rend() const; 51 // const_reverse_iterator crend() const; 52 // 53 // Comparators (see std::set documentation). 54 // key_compare key_comp() const; 55 // value_compare value_comp() const; 56 // 57 // Search functions: 58 // template <typename K> size_t count(const K&) const; 59 // template <typename K> const_iterator find(const K&) const; 60 // template <typename K> bool contains(const K&) const; 61 // template <typename K> 62 // pair<const_iterator, const_iterator> equal_range(K&) const; 63 // template <typename K> const_iterator lower_bound(const K&) const; 64 // template <typename K> const_iterator upper_bound(const K&) const; 65 // 66 // Non-member operators: 67 // bool operator==(const fixed_flat_set&, const fixed_flat_set&); 68 // bool operator!=(const fixed_flat_set&, const fixed_flat_set&); 69 // bool operator<(const fixed_flat_set&, const fixed_flat_set&); 70 // bool operator>(const fixed_flat_set&, const fixed_flat_set&); 71 // bool operator>=(const fixed_flat_set&, const fixed_flat_set&); 72 // bool operator<=(const fixed_flat_set&, const fixed_flat_set&); 73 // 74 template <class Key, size_t N, class Compare = std::less<>> 75 using fixed_flat_set = base::flat_set<Key, Compare, std::array<const Key, N>>; 76 77 // Utility function to simplify constructing a fixed_flat_set from a fixed list 78 // of keys. Requires that the passed in `data` contains unique keys. 79 // 80 // Example usage: 81 // constexpr auto kIntSet = base::MakeFixedFlatSet<int>({1, 2, 3, 4}); 82 // 83 // Data needs not to be sorted: 84 // constexpr auto kStringSet = base::MakeFixedFlatSet<base::StringPiece>( 85 // {"foo", "bar", "baz", "qux"}); 86 // 87 // Note: Wrapping `Key` in `std::common_type_t` below requires callers to 88 // explicitly specify `Key`, which is desired here. 89 template <class Key, size_t N, class Compare = std::less<>> 90 constexpr fixed_flat_set<Key, N, Compare> MakeFixedFlatSet( 91 std::common_type_t<Key>(&&data)[N], 92 const Compare& comp = Compare()) { 93 internal::InsertionSort(data, data + N, comp); 94 CHECK(internal::is_sorted_and_unique(data, comp)); 95 // Specify the value_type explicitly to ensure that the returned array has 96 // immutable keys. 97 return fixed_flat_set<Key, N, Compare>( 98 sorted_unique, internal::ToArray<const Key>(data), comp); 99 } 100 101 } // namespace base 102 103 #endif // BASE_CONTAINERS_FIXED_FLAT_SET_H_ 104