Skip to content

Commit

Permalink
ensure block type compatibility everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed May 18, 2024
1 parent 91bd482 commit 6cf49be
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
57 changes: 57 additions & 0 deletions include/flatmemory/details/types/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ using block_type_extractor_t = typename block_type_extractor<T>::type;
template<typename T1, typename T2>
concept SameBlockType = std::is_same_v<block_type_extractor_t<T1>, block_type_extractor_t<T2>>;

// Concept to check the block type
template<typename T, typename Block>
concept HasBlockType = std::is_same_v<block_type_extractor_t<T>, Block>;

/**
* Operator
*/
Expand Down Expand Up @@ -576,6 +580,7 @@ class View<Bitset<Block>>
*/

template<IsBitset Other>
requires HasBlockType<Other, Block>
[[nodiscard]] bool operator==(const Other& other) const
{
assert(m_buf);
Expand All @@ -584,12 +589,14 @@ class View<Bitset<Block>>
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
[[nodiscard]] bool operator!=(const Other& other) const
{
return !(*this == other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool is_superseteq(const Other& other) const
{
assert(m_buf);
Expand All @@ -598,6 +605,7 @@ class View<Bitset<Block>>
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool are_disjoint(const Other& other) const
{
assert(m_buf);
Expand Down Expand Up @@ -716,26 +724,30 @@ class ConstView<Bitset<Block>>
*/

template<IsBitset Other>
requires HasBlockType<Other, Block>
[[nodiscard]] bool operator==(const Other& other) const
{
assert(m_buf);
return BitsetOperator::are_equal(*this, other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
[[nodiscard]] bool operator!=(const Other& other) const
{
return !(*this == other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool is_superseteq(const Other& other) const
{
assert(m_buf);
return BitsetOperator::is_superseteq(*this, other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool are_disjoint(const Other& other) const
{
assert(m_buf);
Expand Down Expand Up @@ -856,6 +868,19 @@ class Builder<Bitset<Block>> : public IBuilder<Builder<Bitset<Block>>>
}
}

template<IsBitset T>
requires HasBlockType<T, Block>
void init_from_view(const T& other)
{
m_default_bit_value = other.get_default_bit_value();
const auto& other_blocks = other.get_blocks();
m_blocks.resize(other_blocks.size());
for (size_t i = 0; i < other_blocks.size(); ++i)
{
m_blocks[i] = other_blocks[i];
}
}

public:
Builder() : Builder(0) {}

Expand All @@ -873,35 +898,64 @@ class Builder<Bitset<Block>> : public IBuilder<Builder<Bitset<Block>>>
Builder(Builder&& other) noexcept = default;
Builder& operator=(Builder&& other) noexcept = default;

/**
* Conversion constructors
*/

Builder(const View<Bitset<Block>>& other) { init_from_view(other); }

Builder(const ConstView<Bitset<Block>>& other) { init_from_view(other); }

/**
* Conversion assignments
*/

Builder& operator=(const View<Bitset<Block>>& other)
{
init_from_view(other);
return *this;
}

Builder& operator=(const ConstView<Bitset<Block>>& other)
{
init_from_view(other);
return *this;
}

/**
* Operators
*/

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool operator<(const Other& other) const
{
return BitsetOperator::less(*this, other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool operator==(const Other& other) const
{
return BitsetOperator::are_equal(*this, other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
[[nodiscard]] bool operator!=(const Other& other) const
{
return !(*this == other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool is_superseteq(const Other& other) const
{
return BitsetOperator::is_superseteq(*this, other);
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
bool are_disjoint(const Other& other) const
{
return BitsetOperator::are_disjoint(*this, other);
Expand Down Expand Up @@ -968,6 +1022,7 @@ class Builder<Bitset<Block>> : public IBuilder<Builder<Bitset<Block>>>
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
Builder& operator|=(const Other& other)
{
// Fetch data
Expand Down Expand Up @@ -996,6 +1051,7 @@ class Builder<Bitset<Block>> : public IBuilder<Builder<Bitset<Block>>>
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
Builder& operator&=(const Other& other)
{
// Fetch data
Expand Down Expand Up @@ -1025,6 +1081,7 @@ class Builder<Bitset<Block>> : public IBuilder<Builder<Bitset<Block>>>
}

template<IsBitset Other>
requires HasBlockType<Other, Block>
Builder& operator-=(const Other& other)
{
// Fetch data
Expand Down
47 changes: 46 additions & 1 deletion tests/unit/types/bitset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,58 @@ TEST(FlatmemoryTests, TypesBitsetConstructorCopyTest)
size_t num_bits = 2;
auto builder = Builder<Bitset<uint64_t>>(num_bits);
builder.set(1);
builder.finish();
EXPECT_FALSE(builder.get(0));
EXPECT_TRUE(builder.get(1));

// Test builder
// Test Builder
auto copy_builder = Builder<Bitset<uint64_t>>(builder);
EXPECT_FALSE(copy_builder.get(0));
EXPECT_TRUE(copy_builder.get(1));

// Test View
auto view = View<Bitset<uint64_t>>(builder.buffer().data());
auto copy_builder_view = Builder<Bitset<uint64_t>>(view);
EXPECT_FALSE(copy_builder_view.get(0));
EXPECT_TRUE(copy_builder_view.get(1));

// Test ConstView
auto const_view = ConstView<Bitset<uint64_t>>(builder.buffer().data());
auto copy_builder_const_view = Builder<Bitset<uint64_t>>(const_view);
EXPECT_FALSE(copy_builder_const_view.get(0));
EXPECT_TRUE(copy_builder_const_view.get(1));
}

TEST(FlatmemoryTests, TypesBitsetAssignmentTest)
{
// Test size constructor
size_t num_bits = 2;
auto builder = Builder<Bitset<uint64_t>>(num_bits);
builder.set(1);
builder.finish();
EXPECT_FALSE(builder.get(0));
EXPECT_TRUE(builder.get(1));

// Test Builder
auto tmp_builder = Builder<Bitset<uint64_t>>();
tmp_builder = builder;
EXPECT_FALSE(tmp_builder.get(0));
EXPECT_TRUE(tmp_builder.get(1));

// Test View
auto view = View<Bitset<uint64_t>>(builder.buffer().data());
auto tmp_builder_view = Builder<Bitset<uint64_t>>();
tmp_builder_view = view;
EXPECT_FALSE(tmp_builder_view.get(0));
EXPECT_TRUE(tmp_builder_view.get(1));

// Test ConstView
builder.finish();
auto const_view = ConstView<Bitset<uint64_t>>(builder.buffer().data());
auto tmp_builder_const_view = Builder<Bitset<uint64_t>>();
tmp_builder_const_view = const_view;
EXPECT_FALSE(tmp_builder_const_view.get(0));
EXPECT_TRUE(tmp_builder_const_view.get(1));
}

TEST(FlatmemoryTests, TypesBitsetTest)
Expand Down

0 comments on commit 6cf49be

Please sign in to comment.