// Copyright (c) 2023, QuantStack and Mamba Contributors // // Distributed under the terms of the BSD 3-Clause License. // // The full license is in the file LICENSE, distributed with this software. #ifndef MAMBA_UTILFLAT_SET_HPP #define MAMBA_UTILFLAT_SET_HPP #include #include #include #include #include "mamba/util/deprecation.hpp" #include "mamba/util/tuple_hash.hpp" namespace mamba::util { struct sorted_unique_t { explicit sorted_unique_t() = default; }; inline constexpr sorted_unique_t sorted_unique{}; /** * A sorted vector behaving like a set. * * Like, ``std::set``, uniqueness is determined by using the equivalence relation. * In imprecise terms, two objects ``a`` and ``b`` are considered equivalent if neither * compares less than the other: ``!comp(a, b) && !comp(b, a)`` */ template , typename Allocator = std::allocator> class MAMBA_DEPRECATED_CXX23 flat_set : private std::vector { public: using Base = std::vector; using typename Base::allocator_type; using typename Base::const_iterator; using typename Base::const_reverse_iterator; using typename Base::size_type; using typename Base::value_type; using key_compare = Compare; using value_compare = Compare; using Base::cbegin; using Base::cend; using Base::crbegin; using Base::crend; using Base::clear; using Base::empty; using Base::reserve; using Base::size; flat_set() = default; flat_set( std::initializer_list il, key_compare compare = key_compare(), const allocator_type& alloc = allocator_type() ); template flat_set( InputIterator first, InputIterator last, key_compare compare = key_compare(), const allocator_type& alloc = Allocator() ); template flat_set( sorted_unique_t, InputIterator first, InputIterator last, key_compare compare = key_compare(), const allocator_type& alloc = Allocator() ); flat_set(const flat_set&) = default; flat_set(flat_set&&) = default; explicit flat_set(std::vector&& other, key_compare compare = key_compare()); explicit flat_set(const std::vector& other, key_compare compare = key_compare()); auto operator=(const flat_set&) -> flat_set& = default; auto operator=(flat_set&&) -> flat_set& = default; auto key_comp() const -> const key_compare&; auto front() const noexcept -> const value_type&; auto back() const noexcept -> const value_type&; auto operator[](size_type pos) const -> const value_type&; auto at(size_type pos) const -> const value_type&; auto begin() const noexcept -> const_iterator; auto end() const noexcept -> const_iterator; auto rbegin() const noexcept -> const_reverse_iterator; auto rend() const noexcept -> const_reverse_iterator; /** Insert an element in the set. * * Like std::vector and unlike std::set, inserting an element invalidates iterators. */ auto insert(value_type&& value) -> std::pair; auto insert(const value_type& value) -> std::pair; template void insert(InputIterator first, InputIterator last); auto erase(const_iterator pos) -> const_iterator; auto erase(const_iterator first, const_iterator last) -> const_iterator; auto erase(const value_type& value) -> size_type; template auto contains(const T& value) const -> bool; private: key_compare m_compare; auto key_eq(const value_type& a, const value_type& b) const -> bool; template auto insert_impl(U&& value) -> std::pair; void sort_and_remove_duplicates(); template friend auto operator==(const flat_set& lhs, const flat_set& rhs) -> bool; template friend auto set_union(const flat_set&, const flat_set&) -> flat_set; template friend auto set_intersection(const flat_set&, const flat_set&) -> flat_set; template friend auto set_difference(const flat_set&, const flat_set&) -> flat_set; template friend auto set_symmetric_difference(const flat_set&, const flat_set&) -> flat_set; }; template , class Allocator = std::allocator> flat_set(std::initializer_list, Compare = Compare(), Allocator = Allocator()) -> flat_set; template < class InputIt, class Comp = std::less::value_type>, class Alloc = std::allocator::value_type>> flat_set(InputIt, InputIt, Comp = Comp(), Alloc = Alloc()) -> flat_set::value_type, Comp, Alloc>; template , class Allocator = std::allocator> flat_set(std::vector&&, Compare compare = Compare()) -> flat_set; template , class Allocator = std::allocator> flat_set(const std::vector&, Compare compare = Compare()) -> flat_set; template auto operator==(const flat_set& lhs, const flat_set& rhs) -> bool; template auto operator!=(const flat_set& lhs, const flat_set& rhs) -> bool; template auto set_is_disjoint_of( const flat_set& lhs, const flat_set& rhs ) -> bool; template auto is_subset_of( const flat_set& lhs, const flat_set& rhs ) -> bool; template auto is_strict_subset_of( const flat_set& lhs, const flat_set& rhs ) -> bool; template auto is_superset_of( const flat_set& lhs, const flat_set& rhs ) -> bool; template auto is_strict_superset_of( const flat_set& lhs, const flat_set& rhs ) -> bool; template auto set_union( // const flat_set& lhs, const flat_set& rhs ) -> flat_set; template auto set_intersection( const flat_set& lhs, const flat_set& rhs ) -> flat_set; template auto set_difference( const flat_set& lhs, const flat_set& rhs ) -> flat_set; template auto set_symmetric_difference( const flat_set& lhs, const flat_set& rhs ) -> flat_set; /******************************* * vector_set Implementation * *******************************/ template flat_set::flat_set( std::initializer_list il, key_compare compare, const allocator_type& alloc ) : Base(std::move(il), alloc) , m_compare(std::move(compare)) { sort_and_remove_duplicates(); } template template flat_set::flat_set( InputIterator first, InputIterator last, key_compare compare, const allocator_type& alloc ) : Base(first, last, alloc) , m_compare(std::move(compare)) { sort_and_remove_duplicates(); } template template flat_set::flat_set( sorted_unique_t, InputIterator first, InputIterator last, key_compare compare, const allocator_type& alloc ) : Base(first, last, alloc) , m_compare(std::move(compare)) { } template flat_set::flat_set(std::vector&& other, C compare) : Base(std::move(other)) , m_compare(std::move(compare)) { sort_and_remove_duplicates(); } template flat_set::flat_set(const std::vector& other, C compare) : Base(std::move(other)) , m_compare(std::move(compare)) { sort_and_remove_duplicates(); } template auto flat_set::key_comp() const -> const key_compare& { return m_compare; } template auto flat_set::front() const noexcept -> const value_type& { return Base::front(); } template auto flat_set::back() const noexcept -> const value_type& { return Base::back(); } template auto flat_set::operator[](size_type pos) const -> const value_type& { return Base::operator[](pos); } template auto flat_set::at(size_type pos) const -> const value_type& { return Base::at(pos); } template auto flat_set::begin() const noexcept -> const_iterator { return Base::begin(); } template auto flat_set::end() const noexcept -> const_iterator { return Base::end(); } template auto flat_set::rbegin() const noexcept -> const_reverse_iterator { return Base::rbegin(); } template auto flat_set::rend() const noexcept -> const_reverse_iterator { return Base::rend(); } template auto flat_set::insert(const value_type& value) -> std::pair { return insert_impl(value); } template auto flat_set::insert(value_type&& value) -> std::pair { return insert_impl(std::move(value)); } template auto flat_set::key_eq(const value_type& a, const value_type& b) const -> bool { return !m_compare(a, b) && !m_compare(b, a); } template void flat_set::sort_and_remove_duplicates() { std::sort(Base::begin(), Base::end(), m_compare); auto is_eq = [this](const value_type& a, const value_type& b) { return key_eq(a, b); }; Base::erase(std::unique(Base::begin(), Base::end(), is_eq), Base::end()); } template template void flat_set::insert(InputIterator first, InputIterator last) { Base::insert(Base::end(), first, last); sort_and_remove_duplicates(); } template template auto flat_set::insert_impl(U&& value) -> std::pair { auto it = std::lower_bound(begin(), end(), value, m_compare); if ((it != end()) && (key_eq(*it, value))) { return { it, false }; } it = Base::insert(it, std::forward(value)); return { it, true }; } template auto flat_set::erase(const_iterator pos) -> const_iterator { // No need to sort or remove duplicates again return Base::erase(pos); } template auto flat_set::erase(const_iterator first, const_iterator last) -> const_iterator { // No need to sort or remove duplicates again return Base::erase(first, last); } template auto flat_set::erase(const value_type& value) -> size_type { auto it = std::lower_bound(begin(), end(), value, m_compare); if ((it == end()) || (!(key_eq(*it, value)))) { return 0; } erase(it); return 1; } template template auto flat_set::contains(const T& value) const -> bool { return std::binary_search(begin(), end(), value); } namespace detail { /** * Check if two sorted range have an empty intersection. * * Edited from https://en.cppreference.com/w/cpp/algorithm/set_intersection * Distributed under the terms of the Copyright/CC-BY-SA License. * The full license can be found at the address * https://en.cppreference.com/w/Cppreference:Copyright/CC-BY-SA */ template auto set_disjoint(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) -> bool { while (first1 != last1 && first2 != last2) { if (comp(*first1, *first2)) { ++first1; } else { if (!comp(*first2, *first1)) { return false; // *first1 and *first2 are equivalent. } ++first2; } } return true; } } template auto operator==(const flat_set& lhs, const flat_set& rhs) -> bool { auto is_eq = [&lhs](const auto& a, const auto& b) { return lhs.key_eq(a, b); }; return std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), is_eq); } template auto operator!=(const flat_set& lhs, const flat_set& rhs) -> bool { return !(lhs == rhs); } template auto set_is_disjoint_of(const flat_set& lhs, const flat_set& rhs) -> bool { return detail::set_disjoint(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), lhs.key_comp()); } template auto set_is_subset_of(const flat_set& lhs, const flat_set& rhs) -> bool { return (lhs.size() <= rhs.size()) // For perf && std::includes(rhs.cbegin(), rhs.cend(), lhs.cbegin(), lhs.cend(), lhs.key_comp()); } template auto set_is_strict_subset_of(const flat_set& lhs, const flat_set& rhs) -> bool { return (lhs.size() < rhs.size()) && set_is_subset_of(lhs, rhs); } template auto set_is_superset_of(const flat_set& lhs, const flat_set& rhs) -> bool { return set_is_subset_of(rhs, lhs); } template auto set_is_strict_superset_of(const flat_set& lhs, const flat_set& rhs) -> bool { return set_is_strict_subset_of(rhs, lhs); } template auto set_union(const flat_set& lhs, const flat_set& rhs) -> flat_set { auto out = flat_set(); out.reserve(std::max(lhs.size(), rhs.size())); // lower bound std::set_union( lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), std::back_inserter(static_cast::Base&>(out)), lhs.m_compare ); return out; } template auto set_intersection(const flat_set& lhs, const flat_set& rhs) -> flat_set { auto out = flat_set(); std::set_intersection( lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), std::back_inserter(static_cast::Base&>(out)), lhs.m_compare ); return out; } template auto set_difference(const flat_set& lhs, const flat_set& rhs) -> flat_set { auto out = flat_set(); std::set_difference( lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), std::back_inserter(static_cast::Base&>(out)), lhs.m_compare ); return out; } template auto set_symmetric_difference(const flat_set& lhs, const flat_set& rhs) -> flat_set { auto out = flat_set(); std::set_symmetric_difference( lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend(), std::back_inserter(static_cast::Base&>(out)), lhs.m_compare ); return out; } } template struct std::hash> { auto operator()(const mamba::util::flat_set& set) const -> std::size_t { return mamba::util::hash_range(set); } }; #endif