| /* | |
| * Copyright 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #pragma once | |
| #include <ftl/initializer_list.h> | |
| #include <ftl/small_vector.h> | |
| #include <algorithm> | |
| #include <functional> | |
| #include <iterator> | |
| #include <type_traits> | |
| #include <utility> | |
| namespace android::ftl { | |
| template <typename> | |
| struct is_small_set; | |
| // Container with unique, unordered elements. Unlike std::unordered_set, elements are | |
| // stored in contiguous storage for cache efficiency. The set is allocated statically until its | |
| // size exceeds N, at which point elements are relocated to dynamic memory. Lookup is done via | |
| // linear scan. | |
| // | |
| // SmallSet<T, 0> unconditionally allocates on the heap. | |
| // | |
| // Example usage: | |
| // | |
| // ftl::SmallSet<int, 3> set; | |
| // assert(set.empty()); | |
| // assert(!set.dynamic()); | |
| // | |
| // set = {123, -1, 42}; | |
| // assert(set.size() == 3u); | |
| // assert(!set.dynamic()); | |
| // | |
| // assert(set.contains(123)); | |
| // assert(set.find(42) != set.end()); | |
| // | |
| // set.insert(0); | |
| // assert(set.dynamic()); | |
| // | |
| // assert(set == SmallSet{-1, 0, 42, 123}); | |
| // | |
| template <typename T, std::size_t N, typename KeyEqual = std::equal_to<>> | |
| class SmallSet final { | |
| using Set = SmallVector<T, N>; | |
| template <typename, std::size_t, typename> | |
| friend class SmallSet; | |
| template <typename I, typename Traits = std::iterator_traits<I>> | |
| using is_input_iterator = | |
| std::conjunction<std::is_base_of<std::input_iterator_tag, typename Traits::iterator_category>, | |
| std::negation<std::is_constructible<T, I>>>; | |
| public: | |
| using key_type = T; | |
| using value_type = T; | |
| using key_equal = KeyEqual; | |
| using size_type = typename Set::size_type; | |
| using difference_type = typename Set::difference_type; | |
| using const_reference = typename Set::const_reference; | |
| using reference = const_reference; | |
| using const_iterator = typename Set::const_iterator; | |
| using iterator = const_iterator; | |
| using const_pointer = typename Set::const_pointer; | |
| using pointer = const_pointer; | |
| // Creates an empty set. | |
| SmallSet() = default; | |
| // Constructs at most N elements in place by forwarding per-element constructor arguments. | |
| // The template arguments T and N are inferred using the deduction guide defined below. | |
| // The syntax for listing elements is as follows: | |
| // | |
| // ftl::SmallSet set = ftl::init::list<std::string>("abc")("123456", 3u)(3u, '?'); | |
| // static_assert(std::is_same_v<decltype(set), ftl::SmallSet<std::string, 3>>); | |
| // | |
| template <typename U, std::size_t... Sizes, typename... Types> | |
| SmallSet(InitializerList<U, std::index_sequence<Sizes...>, Types...>&& list) | |
| : set_(std::move(list)) { | |
| deduplicate(); | |
| } | |
| // Copies at most N elements from an array. | |
| template <typename U, std::size_t M> | |
| explicit SmallSet(U (&array)[M]) : set_(array) { | |
| deduplicate(); | |
| } | |
| // Copies at most N elements from the range [first, last). | |
| template <typename Iterator, typename = std::enable_if_t<is_input_iterator<Iterator>{}>> | |
| SmallSet(Iterator first, Iterator last) : set_(first, last) { | |
| deduplicate(); | |
| } | |
| // Constructs at most N elements. | |
| // | |
| // ftl::SmallSet set = {1, 2, 3}; | |
| // static_assert(std::is_same_v<decltype(set), ftl::SmallSet<int, 3>>); | |
| // | |
| template <typename E, typename... Es, | |
| typename = std::enable_if_t< | |
| std::is_constructible_v<value_type, E> && | |
| !is_small_set<std::remove_cvref_t<E>>{}>> | |
| SmallSet(E&& element, Es&&... elements) | |
| : set_(std::forward<E>(element), std::forward<Es>(elements)...) { | |
| deduplicate(); | |
| } | |
| // Copies or moves elements from a convertible set. | |
| template <typename U, std::size_t M, typename E> | |
| SmallSet(SmallSet<U, M, E> other) : set_(std::move(other.set_)) {} | |
| static constexpr size_type static_capacity() { return N; } | |
| size_type max_size() const { return set_.max_size(); } | |
| size_type size() const { return set_.size(); } | |
| bool empty() const { return set_.empty(); } | |
| // Returns whether the set is backed by static or dynamic storage. | |
| bool dynamic() const { | |
| if constexpr (static_capacity() > 0) { | |
| return set_.dynamic(); | |
| } else { | |
| return true; | |
| } | |
| } | |
| iterator begin() const { return cbegin(); } | |
| const_iterator cbegin() const { return set_.cbegin(); } | |
| iterator end() const { return cend(); } | |
| const_iterator cend() const { return set_.cend(); } | |
| // Returns whether an element exists in the set. | |
| template <typename K = key_type> | |
| bool contains(const K& key) const { | |
| return find(key) != end(); | |
| } | |
| // Returns an iterator to an existing element for the given key, or the end() iterator otherwise. | |
| template <typename K = key_type> | |
| const_iterator find(const K& key) const { | |
| return find(key, cbegin()); | |
| } | |
| // Inserts an element unless it exists. Returns an iterator to the inserted or existing element, | |
| // and whether the element was inserted. | |
| // | |
| // On insert, if the set reaches its static or dynamic capacity, then all iterators are | |
| // invalidated. Otherwise, only the end() iterator is invalidated. | |
| // | |
| std::pair<iterator, bool> insert(const value_type& value) { | |
| if (const auto it = find(value); it != end()) { | |
| return {it, false}; | |
| } | |
| decltype(auto) ref_or_it = set_.emplace_back(value); | |
| if constexpr (static_capacity() > 0) { | |
| return {&ref_or_it, true}; | |
| } else { | |
| return {ref_or_it, true}; | |
| } | |
| } | |
| std::pair<iterator, bool> insert(value_type&& value) { | |
| if (const auto it = find(value); it != end()) { | |
| return {it, false}; | |
| } | |
| decltype(auto) ref_or_it = set_.emplace_back(std::move(value)); | |
| if constexpr (static_capacity() > 0) { | |
| return {&ref_or_it, true}; | |
| } else { | |
| return {ref_or_it, true}; | |
| } | |
| } | |
| // Inserts elements from the range [first, last). | |
| // | |
| // On insert, if the set reaches its static or dynamic capacity, then all iterators are | |
| // invalidated. Otherwise, only the end() iterator is invalidated. | |
| // | |
| template <typename Iterator, typename = std::enable_if_t<is_input_iterator<Iterator>{}>> | |
| void insert(Iterator first, Iterator last) { | |
| for (; first != last; ++first) { | |
| insert(*first); | |
| } | |
| } | |
| // Removes an element if it exists, and returns whether it did. | |
| // | |
| // The last() and end() iterators, as well as those to the erased element, are invalidated. | |
| // | |
| template <typename K = key_type> | |
| bool erase(const K& key) { | |
| const auto it = find(key); | |
| if (it == end()) return false; | |
| erase(it); | |
| return true; | |
| } | |
| // Removes an element. | |
| // | |
| // The last() and end() iterators, as well as those to the erased element, are invalidated. | |
| // | |
| void erase(const_iterator it) { | |
| set_.unstable_erase(set_.begin() + (it - set_.cbegin())); | |
| } | |
| // Removes all elements. | |
| // | |
| // All iterators are invalidated. | |
| // | |
| void clear() { set_.clear(); } | |
| private: | |
| template <typename K = key_type> | |
| const_iterator find(const K& key, const_iterator first) const { | |
| return std::find_if(first, cend(), | |
| [&key](const auto& elem) { return KeyEqual{}(elem, key); }); | |
| } | |
| template <typename K = key_type> | |
| bool erase(const K& key, const_iterator first) { | |
| const auto it = find(key, first); | |
| if (it == end()) return false; | |
| erase(it); | |
| return true; | |
| } | |
| void deduplicate() { | |
| for (auto it = begin(); it != end();) { | |
| if (const auto& value = *it; ++it != end()) { | |
| while (erase(value, it)); | |
| } | |
| } | |
| } | |
| Set set_; | |
| }; | |
| // Deduction guide for array constructor. | |
| template <typename T, std::size_t N> | |
| SmallSet(T (&)[N]) -> SmallSet<std::remove_cv_t<T>, N>; | |
| // Deduction guide for variadic constructor. | |
| template <typename T, typename... Us, typename V = std::decay_t<T>, | |
| typename = std::enable_if_t<(std::is_constructible_v<V, Us> && ...)>> | |
| SmallSet(T&&, Us&&...) -> SmallSet<V, 1 + sizeof...(Us)>; | |
| // Deduction guide for in-place constructor. | |
| template <typename T, std::size_t... Sizes, typename... Types> | |
| SmallSet(InitializerList<T, std::index_sequence<Sizes...>, Types...>&&) | |
| -> SmallSet<T, sizeof...(Sizes)>; | |
| // Returns whether two sets contain equal elements. | |
| template <typename T, std::size_t N, typename U, std::size_t M, typename E> | |
| bool operator==(const SmallSet<T, N, E>& lhs, const SmallSet<U, M, E>& rhs) { | |
| if (lhs.size() != rhs.size()) return false; | |
| for (const auto& elem : lhs) { | |
| if (!rhs.contains(elem)) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| template <typename> | |
| struct is_small_set : std::false_type {}; | |
| template <typename T, std::size_t N, typename E> | |
| struct is_small_set<SmallSet<T, N, E>> : std::true_type {}; | |
| } // namespace android::ftl | |