Skip to content

Instantly share code, notes, and snippets.

@seelabs
Created January 17, 2022 22:15
Show Gist options
  • Save seelabs/c91655d1a3516f854b17145faac7db25 to your computer and use it in GitHub Desktop.
Save seelabs/c91655d1a3516f854b17145faac7db25 to your computer and use it in GitHub Desktop.
#include <boost/intrusive/unordered_set.hpp>
#include <vector>
using B = boost::intrusive::unordered_set_base_hook<
boost::intrusive::link_mode<boost::intrusive::normal_link>>;
struct element : B
{
element(int i) : value_{i}
{
}
friend bool
operator==(element const& lhs, element const& rhs)
{
return lhs.value_ == rhs.value_;
}
friend std::size_t
hash_value(element const& v)
{
return std::size_t(v.value_);
}
int value_;
};
template <bool CacheBegin>
void
testIt()
{
using T = typename boost::intrusive::make_unordered_set<element,
boost::intrusive::constant_time_size<true>, void, void,
boost::intrusive::cache_begin<CacheBegin>>::type;
using bucket_type = T::bucket_type;
using bucket_traits = T::bucket_traits;
std::vector<bucket_type> buckets;
buckets.resize(3);
T c(bucket_traits(&buckets[0], buckets.size()));
// If we add elements before rehashing, we're OK
// Hoever, if the container is empty, we hit the assert.
if (0)
{
std::vector<element> v;
v.emplace_back(0);
c.insert(v.begin(), v.end());
}
{
std::vector<bucket_type> tmp;
std::swap(tmp, buckets);
buckets.resize(7);
c.rehash(bucket_traits(&buckets[0], buckets.size()));
}
}
int
main()
{
testIt<false>();
testIt<true>();
// When the "CacheBegin" parameter is set to true, we hit an assert:
// clang-format off
// main: /home/swd/projs/packages/vcpkg/installed/x64-linux/include/boost/intrusive/hashtable.hpp:1264: void boost::intrusive::bucket_hash_equal_t<ValueTraits, VoidOrKeyOfValue, VoidOrKeyHash, VoidOrKeyEqual, BucketTraits, true>::priv_insertion_update_cache(std::size_t) [with ValueTraits = boost::intrusive::bhtraits<element, boost::intrusive::slist_node_traits<void*>, boost::intrusive::normal_link, boost::intrusive::dft_tag, 4>; VoidOrKeyOfValue = void; VoidOrKeyHash = void; VoidOrKeyEqual = void; BucketTraits = boost::intrusive::bucket_traits_impl<boost::intrusive::get_slist_impl<boost::intrusive::slist_node_traits<void*> >::type>; std::size_t = long unsigned int]: Assertion `insertion_bucket < this->bucket_hash_type::priv_bucket_count()' failed.
// clang-format on
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment