diff --git a/include/boost/unordered/unordered_node_set.hpp b/include/boost/unordered/unordered_node_set.hpp new file mode 100644 index 00000000..67edcd5a --- /dev/null +++ b/include/boost/unordered/unordered_node_set.hpp @@ -0,0 +1,631 @@ +// Copyright (C) 2022 Christian Mazakas +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_UNORDERED_UNORDERED_NODE_SET_HPP_INCLUDED +#define BOOST_UNORDERED_UNORDERED_NODE_SET_HPP_INCLUDED + +#include +#if defined(BOOST_HAS_PRAGMA_ONCE) +#pragma once +#endif + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace boost { + namespace unordered { + +#if defined(BOOST_MSVC) +#pragma warning(push) +#pragma warning(disable : 4714) /* marked as __forceinline not inlined */ +#endif + + namespace detail { + template struct node_set_types + { + using key_type = Key; + using init_type = Key; + using value_type = Key; + + using storage_type = Key*; + using moved_type = storage_type; + + static Key const& extract(value_type const& key) { return key; } + static Key const& extract(storage_type k) { return *k; } + static storage_type&& move(storage_type& x) { return std::move(x); } + + template + static void construct(A&, storage_type* p, moved_type&& x) + { + *p = x; + x = nullptr; + } + + template + static void construct(A& al, storage_type* p, Args&&... args) + { + *p = boost::to_address(boost::allocator_allocate(al, 1)); + try { + boost::allocator_construct(al, *p, std::forward(args)...); + } catch (...) { + boost::allocator_deallocate(al, + boost::pointer_traits< + typename boost::allocator_pointer::type>::pointer_to(**p), + 1); + throw; + } + } + + template static void destroy(A& al, storage_type* p) noexcept + { + if (*p) { + boost::allocator_destroy(al, *p); + boost::allocator_deallocate(al, + boost::pointer_traits< + typename boost::allocator_pointer::type>::pointer_to(**p), + 1); + } + } + }; + } // namespace detail + + template + class unordered_node_set + { + using set_types = detail::node_set_types; + + using table_type = detail::foa::table::type>; + + table_type table_; + + template + typename unordered_node_set::size_type friend erase_if( + unordered_node_set& set, Pred pred); + + public: + using key_type = Key; + using value_type = typename set_types::value_type; + using init_type = typename set_types::init_type; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using hasher = Hash; + using key_equal = KeyEqual; + using allocator_type = Allocator; + using reference = value_type&; + using const_reference = value_type const&; + using pointer = typename boost::allocator_pointer::type; + using const_pointer = + typename boost::allocator_const_pointer::type; + using iterator = boost::indirect_iterator; + using const_iterator = + boost::indirect_iterator; + + unordered_node_set() : unordered_node_set(0) {} + + explicit unordered_node_set(size_type n, hasher const& h = hasher(), + key_equal const& pred = key_equal(), + allocator_type const& a = allocator_type()) + : table_(n, h, pred, a) + { + } + + unordered_node_set(size_type n, allocator_type const& a) + : unordered_node_set(n, hasher(), key_equal(), a) + { + } + + unordered_node_set(size_type n, hasher const& h, allocator_type const& a) + : unordered_node_set(n, h, key_equal(), a) + { + } + + template + unordered_node_set( + InputIterator f, InputIterator l, allocator_type const& a) + : unordered_node_set(f, l, size_type(0), hasher(), key_equal(), a) + { + } + + explicit unordered_node_set(allocator_type const& a) + : unordered_node_set(0, a) + { + } + + template + unordered_node_set(Iterator first, Iterator last, size_type n = 0, + hasher const& h = hasher(), key_equal const& pred = key_equal(), + allocator_type const& a = allocator_type()) + : unordered_node_set(n, h, pred, a) + { + this->insert(first, last); + } + + template + unordered_node_set( + InputIt first, InputIt last, size_type n, allocator_type const& a) + : unordered_node_set(first, last, n, hasher(), key_equal(), a) + { + } + + template + unordered_node_set(Iterator first, Iterator last, size_type n, + hasher const& h, allocator_type const& a) + : unordered_node_set(first, last, n, h, key_equal(), a) + { + } + + unordered_node_set(unordered_node_set const& other) : table_(other.table_) + { + } + + unordered_node_set( + unordered_node_set const& other, allocator_type const& a) + : table_(other.table_, a) + { + } + + unordered_node_set(unordered_node_set&& other) + noexcept(std::is_nothrow_move_constructible::value&& + std::is_nothrow_move_constructible::value&& + std::is_nothrow_move_constructible::value) + : table_(std::move(other.table_)) + { + } + + unordered_node_set(unordered_node_set&& other, allocator_type const& al) + : table_(std::move(other.table_), al) + { + } + + unordered_node_set(std::initializer_list ilist, + size_type n = 0, hasher const& h = hasher(), + key_equal const& pred = key_equal(), + allocator_type const& a = allocator_type()) + : unordered_node_set(ilist.begin(), ilist.end(), n, h, pred, a) + { + } + + unordered_node_set( + std::initializer_list il, allocator_type const& a) + : unordered_node_set(il, size_type(0), hasher(), key_equal(), a) + { + } + + unordered_node_set(std::initializer_list init, size_type n, + allocator_type const& a) + : unordered_node_set(init, n, hasher(), key_equal(), a) + { + } + + unordered_node_set(std::initializer_list init, size_type n, + hasher const& h, allocator_type const& a) + : unordered_node_set(init, n, h, key_equal(), a) + { + } + + ~unordered_node_set() = default; + + unordered_node_set& operator=(unordered_node_set const& other) + { + table_ = other.table_; + return *this; + } + + unordered_node_set& operator=(unordered_node_set&& other) noexcept( + noexcept(std::declval() = std::declval())) + { + table_ = std::move(other.table_); + return *this; + } + + allocator_type get_allocator() const noexcept + { + return table_.get_allocator(); + } + + /// Iterators + /// + + iterator begin() noexcept { return table_.begin(); } + const_iterator begin() const noexcept { return table_.begin(); } + const_iterator cbegin() const noexcept { return table_.cbegin(); } + + iterator end() noexcept { return table_.end(); } + const_iterator end() const noexcept { return table_.end(); } + const_iterator cend() const noexcept { return table_.cend(); } + + /// Capacity + /// + + BOOST_ATTRIBUTE_NODISCARD bool empty() const noexcept + { + return table_.empty(); + } + + size_type size() const noexcept { return table_.size(); } + + size_type max_size() const noexcept { return table_.max_size(); } + + /// Modifiers + /// + + void clear() noexcept { table_.clear(); } + + BOOST_FORCEINLINE std::pair insert( + value_type const& value) + { + return table_.insert(value); + } + + BOOST_FORCEINLINE std::pair insert(value_type&& value) + { + return table_.insert(std::move(value)); + } + + BOOST_FORCEINLINE iterator insert(const_iterator, value_type const& value) + { + return table_.insert(value).first; + } + + BOOST_FORCEINLINE iterator insert(const_iterator, value_type&& value) + { + return table_.insert(std::move(value)).first; + } + + template + void insert(InputIterator first, InputIterator last) + { + for (auto pos = first; pos != last; ++pos) { + table_.emplace(*pos); + } + } + + void insert(std::initializer_list ilist) + { + this->insert(ilist.begin(), ilist.end()); + } + + template + BOOST_FORCEINLINE std::pair emplace(Args&&... args) + { + return table_.emplace(std::forward(args)...); + } + + template + BOOST_FORCEINLINE iterator emplace_hint(const_iterator, Args&&... args) + { + return table_.emplace(std::forward(args)...).first; + } + + BOOST_FORCEINLINE void erase(const_iterator pos) + { + return table_.erase(pos); + } + iterator erase(const_iterator first, const_iterator last) + { + while (first != last) { + this->erase(first++); + } + return iterator{detail::foa::const_iterator_cast_tag{}, last}; + } + + BOOST_FORCEINLINE size_type erase(key_type const& key) + { + return table_.erase(key); + } + + template + BOOST_FORCEINLINE typename std::enable_if< + detail::transparent_non_iterable::value, + size_type>::type + erase(K const& key) + { + return table_.erase(key); + } + + void swap(unordered_node_set& rhs) noexcept( + noexcept(std::declval().swap(std::declval()))) + { + table_.swap(rhs.table_); + } + + template + void merge(unordered_node_set& source) + { + table_.merge(source.table_); + } + + template + void merge(unordered_node_set&& source) + { + table_.merge(std::move(source.table_)); + } + + /// Lookup + /// + + BOOST_FORCEINLINE size_type count(key_type const& key) const + { + auto pos = table_.find(key); + return pos != table_.end() ? 1 : 0; + } + + template + BOOST_FORCEINLINE typename std::enable_if< + detail::are_transparent::value, size_type>::type + count(K const& key) const + { + auto pos = table_.find(key); + return pos != table_.end() ? 1 : 0; + } + + BOOST_FORCEINLINE iterator find(key_type const& key) + { + return table_.find(key); + } + + BOOST_FORCEINLINE const_iterator find(key_type const& key) const + { + return table_.find(key); + } + + template + BOOST_FORCEINLINE typename std::enable_if< + boost::unordered::detail::are_transparent::value, + iterator>::type + find(K const& key) + { + return table_.find(key); + } + + template + BOOST_FORCEINLINE typename std::enable_if< + boost::unordered::detail::are_transparent::value, + const_iterator>::type + find(K const& key) const + { + return table_.find(key); + } + + BOOST_FORCEINLINE bool contains(key_type const& key) const + { + return this->find(key) != this->end(); + } + + template + BOOST_FORCEINLINE typename std::enable_if< + boost::unordered::detail::are_transparent::value, + bool>::type + contains(K const& key) const + { + return this->find(key) != this->end(); + } + + std::pair equal_range(key_type const& key) + { + auto pos = table_.find(key); + if (pos == table_.end()) { + return {pos, pos}; + } + + auto next = pos; + ++next; + return {pos, next}; + } + + std::pair equal_range( + key_type const& key) const + { + auto pos = table_.find(key); + if (pos == table_.end()) { + return {pos, pos}; + } + + auto next = pos; + ++next; + return {pos, next}; + } + + template + typename std::enable_if< + detail::are_transparent::value, + std::pair >::type + equal_range(K const& key) + { + auto pos = table_.find(key); + if (pos == table_.end()) { + return {pos, pos}; + } + + auto next = pos; + ++next; + return {pos, next}; + } + + template + typename std::enable_if< + detail::are_transparent::value, + std::pair >::type + equal_range(K const& key) const + { + auto pos = table_.find(key); + if (pos == table_.end()) { + return {pos, pos}; + } + + auto next = pos; + ++next; + return {pos, next}; + } + + /// Hash Policy + /// + + size_type bucket_count() const noexcept { return table_.capacity(); } + + float load_factor() const noexcept { return table_.load_factor(); } + + float max_load_factor() const noexcept + { + return table_.max_load_factor(); + } + + void max_load_factor(float) {} + + size_type max_load() const noexcept { return table_.max_load(); } + + void rehash(size_type n) { table_.rehash(n); } + + void reserve(size_type n) { table_.reserve(n); } + + /// Observers + /// + + hasher hash_function() const { return table_.hash_function(); } + + key_equal key_eq() const { return table_.key_eq(); } + }; + + template + bool operator==( + unordered_node_set const& lhs, + unordered_node_set const& rhs) + { + if (&lhs == &rhs) { + return true; + } + + return (lhs.size() == rhs.size()) && ([&] { + for (auto const& key : lhs) { + auto pos = rhs.find(key); + if ((pos == rhs.end()) || (key != *pos)) { + return false; + } + } + return true; + })(); + } + + template + bool operator!=( + unordered_node_set const& lhs, + unordered_node_set const& rhs) + { + return !(lhs == rhs); + } + + template + void swap(unordered_node_set& lhs, + unordered_node_set& rhs) + noexcept(noexcept(lhs.swap(rhs))) + { + lhs.swap(rhs); + } + + template + typename unordered_node_set::size_type + erase_if(unordered_node_set& set, Pred pred) + { + return erase_if(set.table_, pred); + } + +#if defined(BOOST_MSVC) +#pragma warning(pop) /* C4714 */ +#endif + +#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES + template ::value_type>, + class Pred = + std::equal_to::value_type>, + class Allocator = std::allocator< + typename std::iterator_traits::value_type>, + class = boost::enable_if_t >, + class = boost::enable_if_t >, + class = boost::enable_if_t >, + class = boost::enable_if_t > > + unordered_node_set(InputIterator, InputIterator, + std::size_t = boost::unordered::detail::foa::default_bucket_count, + Hash = Hash(), Pred = Pred(), Allocator = Allocator()) + -> unordered_node_set< + typename std::iterator_traits::value_type, Hash, Pred, + Allocator>; + + template , + class Pred = std::equal_to, class Allocator = std::allocator, + class = boost::enable_if_t >, + class = boost::enable_if_t >, + class = boost::enable_if_t > > + unordered_node_set(std::initializer_list, + std::size_t = boost::unordered::detail::foa::default_bucket_count, + Hash = Hash(), Pred = Pred(), Allocator = Allocator()) + -> unordered_node_set; + + template >, + class = boost::enable_if_t > > + unordered_node_set(InputIterator, InputIterator, std::size_t, Allocator) + -> unordered_node_set< + typename std::iterator_traits::value_type, + boost::hash::value_type>, + std::equal_to::value_type>, + Allocator>; + + template >, + class = boost::enable_if_t >, + class = boost::enable_if_t > > + unordered_node_set( + InputIterator, InputIterator, std::size_t, Hash, Allocator) + -> unordered_node_set< + typename std::iterator_traits::value_type, Hash, + std::equal_to::value_type>, + Allocator>; + + template > > + unordered_node_set(std::initializer_list, std::size_t, Allocator) + -> unordered_node_set, std::equal_to, Allocator>; + + template >, + class = boost::enable_if_t > > + unordered_node_set(std::initializer_list, std::size_t, Hash, Allocator) + -> unordered_node_set, Allocator>; + + template >, + class = boost::enable_if_t > > + unordered_node_set(InputIterator, InputIterator, Allocator) + -> unordered_node_set< + typename std::iterator_traits::value_type, + boost::hash::value_type>, + std::equal_to::value_type>, + Allocator>; + + template > > + unordered_node_set(std::initializer_list, Allocator) + -> unordered_node_set, std::equal_to, Allocator>; +#endif + + } // namespace unordered +} // namespace boost + +#endif diff --git a/include/boost/unordered/unordered_node_set_fwd.hpp b/include/boost/unordered/unordered_node_set_fwd.hpp new file mode 100644 index 00000000..bdd7fd0b --- /dev/null +++ b/include/boost/unordered/unordered_node_set_fwd.hpp @@ -0,0 +1,49 @@ + +// Copyright (C) 2023 Christian Mazakas +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_UNORDERED_NODE_SET_FWD_HPP_INCLUDED +#define BOOST_UNORDERED_NODE_SET_FWD_HPP_INCLUDED + +#include +#if defined(BOOST_HAS_PRAGMA_ONCE) +#pragma once +#endif + +#include +#include +#include +#include + +namespace boost { + namespace unordered { + template , + class KeyEqual = std::equal_to, + class Allocator = std::allocator > + class unordered_node_set; + + template + bool operator==( + unordered_node_set const& lhs, + unordered_node_set const& rhs); + + template + bool operator!=( + unordered_node_set const& lhs, + unordered_node_set const& rhs); + + template + void swap(unordered_node_set& lhs, + unordered_node_set& rhs) + noexcept(noexcept(lhs.swap(rhs))); + } // namespace unordered + + using boost::unordered::unordered_node_set; + + using boost::unordered::swap; + using boost::unordered::operator==; + using boost::unordered::operator!=; +} // namespace boost + +#endif