flat_set.h 5.0 KB

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