xref: /aosp_15_r20/external/libchrome/base/containers/flat_set.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_CONTAINERS_FLAT_SET_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_CONTAINERS_FLAT_SET_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <functional>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/containers/flat_tree.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/template_util.h"
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker namespace base {
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker // flat_set is a container with a std::set-like interface that stores its
16*635a8641SAndroid Build Coastguard Worker // contents in a sorted vector.
17*635a8641SAndroid Build Coastguard Worker //
18*635a8641SAndroid Build Coastguard Worker // Please see //base/containers/README.md for an overview of which container
19*635a8641SAndroid Build Coastguard Worker // to select.
20*635a8641SAndroid Build Coastguard Worker //
21*635a8641SAndroid Build Coastguard Worker // PROS
22*635a8641SAndroid Build Coastguard Worker //
23*635a8641SAndroid Build Coastguard Worker //  - Good memory locality.
24*635a8641SAndroid Build Coastguard Worker //  - Low overhead, especially for smaller sets.
25*635a8641SAndroid Build Coastguard Worker //  - Performance is good for more workloads than you might expect (see
26*635a8641SAndroid Build Coastguard Worker //    overview link above).
27*635a8641SAndroid Build Coastguard Worker //  - Supports C++14 set interface.
28*635a8641SAndroid Build Coastguard Worker //
29*635a8641SAndroid Build Coastguard Worker // CONS
30*635a8641SAndroid Build Coastguard Worker //
31*635a8641SAndroid Build Coastguard Worker //  - Inserts and removals are O(n).
32*635a8641SAndroid Build Coastguard Worker //
33*635a8641SAndroid Build Coastguard Worker // IMPORTANT NOTES
34*635a8641SAndroid Build Coastguard Worker //
35*635a8641SAndroid Build Coastguard Worker //  - Iterators are invalidated across mutations.
36*635a8641SAndroid Build Coastguard Worker //  - If possible, construct a flat_set in one operation by inserting into
37*635a8641SAndroid Build Coastguard Worker //    a std::vector and moving that vector into the flat_set constructor.
38*635a8641SAndroid Build Coastguard Worker //  - For multiple removals use base::EraseIf() which is O(n) rather than
39*635a8641SAndroid Build Coastguard Worker //    O(n * removed_items).
40*635a8641SAndroid Build Coastguard Worker //
41*635a8641SAndroid Build Coastguard Worker // QUICK REFERENCE
42*635a8641SAndroid Build Coastguard Worker //
43*635a8641SAndroid Build Coastguard Worker // Most of the core functionality is inherited from flat_tree. Please see
44*635a8641SAndroid Build Coastguard Worker // flat_tree.h for more details for most of these functions. As a quick
45*635a8641SAndroid Build Coastguard Worker // reference, the functions available are:
46*635a8641SAndroid Build Coastguard Worker //
47*635a8641SAndroid Build Coastguard Worker // Constructors (inputs need not be sorted):
48*635a8641SAndroid Build Coastguard Worker //   flat_set(InputIterator first, InputIterator last,
49*635a8641SAndroid Build Coastguard Worker //            FlatContainerDupes = KEEP_FIRST_OF_DUPES,
50*635a8641SAndroid Build Coastguard Worker //            const Compare& compare = Compare());
51*635a8641SAndroid Build Coastguard Worker //   flat_set(const flat_set&);
52*635a8641SAndroid Build Coastguard Worker //   flat_set(flat_set&&);
53*635a8641SAndroid Build Coastguard Worker //   flat_set(std::vector<Key>,
54*635a8641SAndroid Build Coastguard Worker //            FlatContainerDupes = KEEP_FIRST_OF_DUPES,
55*635a8641SAndroid Build Coastguard Worker //            const Compare& compare = Compare());  // Re-use storage.
56*635a8641SAndroid Build Coastguard Worker //   flat_set(std::initializer_list<value_type> ilist,
57*635a8641SAndroid Build Coastguard Worker //            FlatContainerDupes = KEEP_FIRST_OF_DUPES,
58*635a8641SAndroid Build Coastguard Worker //            const Compare& comp = Compare());
59*635a8641SAndroid Build Coastguard Worker //
60*635a8641SAndroid Build Coastguard Worker // Assignment functions:
61*635a8641SAndroid Build Coastguard Worker //   flat_set& operator=(const flat_set&);
62*635a8641SAndroid Build Coastguard Worker //   flat_set& operator=(flat_set&&);
63*635a8641SAndroid Build Coastguard Worker //   flat_set& operator=(initializer_list<Key>);
64*635a8641SAndroid Build Coastguard Worker //
65*635a8641SAndroid Build Coastguard Worker // Memory management functions:
66*635a8641SAndroid Build Coastguard Worker //   void   reserve(size_t);
67*635a8641SAndroid Build Coastguard Worker //   size_t capacity() const;
68*635a8641SAndroid Build Coastguard Worker //   void   shrink_to_fit();
69*635a8641SAndroid Build Coastguard Worker //
70*635a8641SAndroid Build Coastguard Worker // Size management functions:
71*635a8641SAndroid Build Coastguard Worker //   void   clear();
72*635a8641SAndroid Build Coastguard Worker //   size_t size() const;
73*635a8641SAndroid Build Coastguard Worker //   size_t max_size() const;
74*635a8641SAndroid Build Coastguard Worker //   bool   empty() const;
75*635a8641SAndroid Build Coastguard Worker //
76*635a8641SAndroid Build Coastguard Worker // Iterator functions:
77*635a8641SAndroid Build Coastguard Worker //   iterator               begin();
78*635a8641SAndroid Build Coastguard Worker //   const_iterator         begin() const;
79*635a8641SAndroid Build Coastguard Worker //   const_iterator         cbegin() const;
80*635a8641SAndroid Build Coastguard Worker //   iterator               end();
81*635a8641SAndroid Build Coastguard Worker //   const_iterator         end() const;
82*635a8641SAndroid Build Coastguard Worker //   const_iterator         cend() const;
83*635a8641SAndroid Build Coastguard Worker //   reverse_iterator       rbegin();
84*635a8641SAndroid Build Coastguard Worker //   const reverse_iterator rbegin() const;
85*635a8641SAndroid Build Coastguard Worker //   const_reverse_iterator crbegin() const;
86*635a8641SAndroid Build Coastguard Worker //   reverse_iterator       rend();
87*635a8641SAndroid Build Coastguard Worker //   const_reverse_iterator rend() const;
88*635a8641SAndroid Build Coastguard Worker //   const_reverse_iterator crend() const;
89*635a8641SAndroid Build Coastguard Worker //
90*635a8641SAndroid Build Coastguard Worker // Insert and accessor functions:
91*635a8641SAndroid Build Coastguard Worker //   pair<iterator, bool> insert(const key_type&);
92*635a8641SAndroid Build Coastguard Worker //   pair<iterator, bool> insert(key_type&&);
93*635a8641SAndroid Build Coastguard Worker //   void                 insert(InputIterator first, InputIterator last,
94*635a8641SAndroid Build Coastguard Worker //                               FlatContainerDupes = KEEP_FIRST_OF_DUPES);
95*635a8641SAndroid Build Coastguard Worker //   iterator             insert(const_iterator hint, const key_type&);
96*635a8641SAndroid Build Coastguard Worker //   iterator             insert(const_iterator hint, key_type&&);
97*635a8641SAndroid Build Coastguard Worker //   pair<iterator, bool> emplace(Args&&...);
98*635a8641SAndroid Build Coastguard Worker //   iterator             emplace_hint(const_iterator, Args&&...);
99*635a8641SAndroid Build Coastguard Worker //
100*635a8641SAndroid Build Coastguard Worker // Erase functions:
101*635a8641SAndroid Build Coastguard Worker //   iterator erase(iterator);
102*635a8641SAndroid Build Coastguard Worker //   iterator erase(const_iterator);
103*635a8641SAndroid Build Coastguard Worker //   iterator erase(const_iterator first, const_iterator& last);
104*635a8641SAndroid Build Coastguard Worker //   template <typename K> size_t erase(const K& key);
105*635a8641SAndroid Build Coastguard Worker //
106*635a8641SAndroid Build Coastguard Worker // Comparators (see std::set documentation).
107*635a8641SAndroid Build Coastguard Worker //   key_compare   key_comp() const;
108*635a8641SAndroid Build Coastguard Worker //   value_compare value_comp() const;
109*635a8641SAndroid Build Coastguard Worker //
110*635a8641SAndroid Build Coastguard Worker // Search functions:
111*635a8641SAndroid Build Coastguard Worker //   template <typename K> size_t                   count(const K&) const;
112*635a8641SAndroid Build Coastguard Worker //   template <typename K> iterator                 find(const K&);
113*635a8641SAndroid Build Coastguard Worker //   template <typename K> const_iterator           find(const K&) const;
114*635a8641SAndroid Build Coastguard Worker //   template <typename K> pair<iterator, iterator> equal_range(K&);
115*635a8641SAndroid Build Coastguard Worker //   template <typename K> iterator                 lower_bound(const K&);
116*635a8641SAndroid Build Coastguard Worker //   template <typename K> const_iterator           lower_bound(const K&) const;
117*635a8641SAndroid Build Coastguard Worker //   template <typename K> iterator                 upper_bound(const K&);
118*635a8641SAndroid Build Coastguard Worker //   template <typename K> const_iterator           upper_bound(const K&) const;
119*635a8641SAndroid Build Coastguard Worker //
120*635a8641SAndroid Build Coastguard Worker // General functions:
121*635a8641SAndroid Build Coastguard Worker //   void swap(flat_set&&);
122*635a8641SAndroid Build Coastguard Worker //
123*635a8641SAndroid Build Coastguard Worker // Non-member operators:
124*635a8641SAndroid Build Coastguard Worker //   bool operator==(const flat_set&, const flat_set);
125*635a8641SAndroid Build Coastguard Worker //   bool operator!=(const flat_set&, const flat_set);
126*635a8641SAndroid Build Coastguard Worker //   bool operator<(const flat_set&, const flat_set);
127*635a8641SAndroid Build Coastguard Worker //   bool operator>(const flat_set&, const flat_set);
128*635a8641SAndroid Build Coastguard Worker //   bool operator>=(const flat_set&, const flat_set);
129*635a8641SAndroid Build Coastguard Worker //   bool operator<=(const flat_set&, const flat_set);
130*635a8641SAndroid Build Coastguard Worker //
131*635a8641SAndroid Build Coastguard Worker template <class Key, class Compare = std::less<>>
132*635a8641SAndroid Build Coastguard Worker using flat_set = typename ::base::internal::flat_tree<
133*635a8641SAndroid Build Coastguard Worker     Key,
134*635a8641SAndroid Build Coastguard Worker     Key,
135*635a8641SAndroid Build Coastguard Worker     ::base::internal::GetKeyFromValueIdentity<Key>,
136*635a8641SAndroid Build Coastguard Worker     Compare>;
137*635a8641SAndroid Build Coastguard Worker 
138*635a8641SAndroid Build Coastguard Worker }  // namespace base
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker #endif  // BASE_CONTAINERS_FLAT_SET_H_
141