16d528ed9Sopenharmony_ci// Copyright 2017 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#ifndef BASE_CONTAINERS_FLAT_SET_H_ 66d528ed9Sopenharmony_ci#define BASE_CONTAINERS_FLAT_SET_H_ 76d528ed9Sopenharmony_ci 86d528ed9Sopenharmony_ci#include <functional> 96d528ed9Sopenharmony_ci 106d528ed9Sopenharmony_ci#include "base/containers/flat_tree.h" 116d528ed9Sopenharmony_ci#include "base/template_util.h" 126d528ed9Sopenharmony_ci 136d528ed9Sopenharmony_cinamespace base { 146d528ed9Sopenharmony_ci 156d528ed9Sopenharmony_ci// flat_set is a container with a std::set-like interface that stores its 166d528ed9Sopenharmony_ci// contents in a sorted vector. 176d528ed9Sopenharmony_ci// 186d528ed9Sopenharmony_ci// Please see //base/containers/README.md for an overview of which container 196d528ed9Sopenharmony_ci// to select. 206d528ed9Sopenharmony_ci// 216d528ed9Sopenharmony_ci// PROS 226d528ed9Sopenharmony_ci// 236d528ed9Sopenharmony_ci// - Good memory locality. 246d528ed9Sopenharmony_ci// - Low overhead, especially for smaller sets. 256d528ed9Sopenharmony_ci// - Performance is good for more workloads than you might expect (see 266d528ed9Sopenharmony_ci// overview link above). 276d528ed9Sopenharmony_ci// - Supports C++14 set interface. 286d528ed9Sopenharmony_ci// 296d528ed9Sopenharmony_ci// CONS 306d528ed9Sopenharmony_ci// 316d528ed9Sopenharmony_ci// - Inserts and removals are O(n). 326d528ed9Sopenharmony_ci// 336d528ed9Sopenharmony_ci// IMPORTANT NOTES 346d528ed9Sopenharmony_ci// 356d528ed9Sopenharmony_ci// - Iterators are invalidated across mutations. 366d528ed9Sopenharmony_ci// - If possible, construct a flat_set in one operation by inserting into 376d528ed9Sopenharmony_ci// a std::vector and moving that vector into the flat_set constructor. 386d528ed9Sopenharmony_ci// - For multiple removals use base::EraseIf() which is O(n) rather than 396d528ed9Sopenharmony_ci// O(n * removed_items). 406d528ed9Sopenharmony_ci// 416d528ed9Sopenharmony_ci// QUICK REFERENCE 426d528ed9Sopenharmony_ci// 436d528ed9Sopenharmony_ci// Most of the core functionality is inherited from flat_tree. Please see 446d528ed9Sopenharmony_ci// flat_tree.h for more details for most of these functions. As a quick 456d528ed9Sopenharmony_ci// reference, the functions available are: 466d528ed9Sopenharmony_ci// 476d528ed9Sopenharmony_ci// Constructors (inputs need not be sorted): 486d528ed9Sopenharmony_ci// flat_set(InputIterator first, InputIterator last, 496d528ed9Sopenharmony_ci// FlatContainerDupes = KEEP_FIRST_OF_DUPES, 506d528ed9Sopenharmony_ci// const Compare& compare = Compare()); 516d528ed9Sopenharmony_ci// flat_set(const flat_set&); 526d528ed9Sopenharmony_ci// flat_set(flat_set&&); 536d528ed9Sopenharmony_ci// flat_set(std::vector<Key>, 546d528ed9Sopenharmony_ci// FlatContainerDupes = KEEP_FIRST_OF_DUPES, 556d528ed9Sopenharmony_ci// const Compare& compare = Compare()); // Re-use storage. 566d528ed9Sopenharmony_ci// flat_set(std::initializer_list<value_type> ilist, 576d528ed9Sopenharmony_ci// FlatContainerDupes = KEEP_FIRST_OF_DUPES, 586d528ed9Sopenharmony_ci// const Compare& comp = Compare()); 596d528ed9Sopenharmony_ci// 606d528ed9Sopenharmony_ci// Assignment functions: 616d528ed9Sopenharmony_ci// flat_set& operator=(const flat_set&); 626d528ed9Sopenharmony_ci// flat_set& operator=(flat_set&&); 636d528ed9Sopenharmony_ci// flat_set& operator=(initializer_list<Key>); 646d528ed9Sopenharmony_ci// 656d528ed9Sopenharmony_ci// Memory management functions: 666d528ed9Sopenharmony_ci// void reserve(size_t); 676d528ed9Sopenharmony_ci// size_t capacity() const; 686d528ed9Sopenharmony_ci// void shrink_to_fit(); 696d528ed9Sopenharmony_ci// 706d528ed9Sopenharmony_ci// Size management functions: 716d528ed9Sopenharmony_ci// void clear(); 726d528ed9Sopenharmony_ci// size_t size() const; 736d528ed9Sopenharmony_ci// size_t max_size() const; 746d528ed9Sopenharmony_ci// bool empty() const; 756d528ed9Sopenharmony_ci// 766d528ed9Sopenharmony_ci// Iterator functions: 776d528ed9Sopenharmony_ci// iterator begin(); 786d528ed9Sopenharmony_ci// const_iterator begin() const; 796d528ed9Sopenharmony_ci// const_iterator cbegin() const; 806d528ed9Sopenharmony_ci// iterator end(); 816d528ed9Sopenharmony_ci// const_iterator end() const; 826d528ed9Sopenharmony_ci// const_iterator cend() const; 836d528ed9Sopenharmony_ci// reverse_iterator rbegin(); 846d528ed9Sopenharmony_ci// const reverse_iterator rbegin() const; 856d528ed9Sopenharmony_ci// const_reverse_iterator crbegin() const; 866d528ed9Sopenharmony_ci// reverse_iterator rend(); 876d528ed9Sopenharmony_ci// const_reverse_iterator rend() const; 886d528ed9Sopenharmony_ci// const_reverse_iterator crend() const; 896d528ed9Sopenharmony_ci// 906d528ed9Sopenharmony_ci// Insert and accessor functions: 916d528ed9Sopenharmony_ci// pair<iterator, bool> insert(const key_type&); 926d528ed9Sopenharmony_ci// pair<iterator, bool> insert(key_type&&); 936d528ed9Sopenharmony_ci// void insert(InputIterator first, InputIterator last, 946d528ed9Sopenharmony_ci// FlatContainerDupes = KEEP_FIRST_OF_DUPES); 956d528ed9Sopenharmony_ci// iterator insert(const_iterator hint, const key_type&); 966d528ed9Sopenharmony_ci// iterator insert(const_iterator hint, key_type&&); 976d528ed9Sopenharmony_ci// pair<iterator, bool> emplace(Args&&...); 986d528ed9Sopenharmony_ci// iterator emplace_hint(const_iterator, Args&&...); 996d528ed9Sopenharmony_ci// 1006d528ed9Sopenharmony_ci// Erase functions: 1016d528ed9Sopenharmony_ci// iterator erase(iterator); 1026d528ed9Sopenharmony_ci// iterator erase(const_iterator); 1036d528ed9Sopenharmony_ci// iterator erase(const_iterator first, const_iterator& last); 1046d528ed9Sopenharmony_ci// template <typename K> size_t erase(const K& key); 1056d528ed9Sopenharmony_ci// 1066d528ed9Sopenharmony_ci// Comparators (see std::set documentation). 1076d528ed9Sopenharmony_ci// key_compare key_comp() const; 1086d528ed9Sopenharmony_ci// value_compare value_comp() const; 1096d528ed9Sopenharmony_ci// 1106d528ed9Sopenharmony_ci// Search functions: 1116d528ed9Sopenharmony_ci// template <typename K> size_t count(const K&) const; 1126d528ed9Sopenharmony_ci// template <typename K> iterator find(const K&); 1136d528ed9Sopenharmony_ci// template <typename K> const_iterator find(const K&) const; 1146d528ed9Sopenharmony_ci// template <typename K> bool contains(const K&) const; 1156d528ed9Sopenharmony_ci// template <typename K> pair<iterator, iterator> equal_range(K&); 1166d528ed9Sopenharmony_ci// template <typename K> iterator lower_bound(const K&); 1176d528ed9Sopenharmony_ci// template <typename K> const_iterator lower_bound(const K&) const; 1186d528ed9Sopenharmony_ci// template <typename K> iterator upper_bound(const K&); 1196d528ed9Sopenharmony_ci// template <typename K> const_iterator upper_bound(const K&) const; 1206d528ed9Sopenharmony_ci// 1216d528ed9Sopenharmony_ci// General functions: 1226d528ed9Sopenharmony_ci// void swap(flat_set&&); 1236d528ed9Sopenharmony_ci// 1246d528ed9Sopenharmony_ci// Non-member operators: 1256d528ed9Sopenharmony_ci// bool operator==(const flat_set&, const flat_set); 1266d528ed9Sopenharmony_ci// bool operator!=(const flat_set&, const flat_set); 1276d528ed9Sopenharmony_ci// bool operator<(const flat_set&, const flat_set); 1286d528ed9Sopenharmony_ci// bool operator>(const flat_set&, const flat_set); 1296d528ed9Sopenharmony_ci// bool operator>=(const flat_set&, const flat_set); 1306d528ed9Sopenharmony_ci// bool operator<=(const flat_set&, const flat_set); 1316d528ed9Sopenharmony_ci// 1326d528ed9Sopenharmony_citemplate <class Key, class Compare = std::less<>> 1336d528ed9Sopenharmony_ciusing flat_set = typename ::base::internal::flat_tree< 1346d528ed9Sopenharmony_ci Key, 1356d528ed9Sopenharmony_ci Key, 1366d528ed9Sopenharmony_ci ::base::internal::GetKeyFromValueIdentity<Key>, 1376d528ed9Sopenharmony_ci Compare>; 1386d528ed9Sopenharmony_ci 1396d528ed9Sopenharmony_ci} // namespace base 1406d528ed9Sopenharmony_ci 1416d528ed9Sopenharmony_ci#endif // BASE_CONTAINERS_FLAT_SET_H_