Skip to content

Commit

Permalink
some work on moving operator== and operator!= from intrusive to non i…
Browse files Browse the repository at this point in the history
…ntrusive
  • Loading branch information
drexlerd committed Aug 20, 2024
1 parent 2acbbe1 commit 6d480b2
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 297 deletions.
9 changes: 0 additions & 9 deletions include/flatmemory/details/byte_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ class ByteBuffer
return sizeof(value);
}

/// @brief Writes a pointer to the stream
template<typename T>
size_t write(size_t pos, const T* pointer)
{
uintptr_t address = reinterpret_cast<uintptr_t>(pointer);
write(pos, reinterpret_cast<const uint8_t*>(&address), sizeof(address));
return sizeof(address);
}

size_t write_padding(size_t pos, size_t amount)
{
if (amount > 0)
Expand Down
1 change: 1 addition & 0 deletions include/flatmemory/details/byte_buffer_segmented.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace flatmemory
{

class ByteBufferSegmented
{
private:
Expand Down
130 changes: 35 additions & 95 deletions include/flatmemory/details/types/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "flatmemory/details/layout.hpp"
#include "flatmemory/details/layout_utils.hpp"
#include "flatmemory/details/operator.hpp"
#include "flatmemory/details/types/formatter.hpp"
#include "flatmemory/details/types/vector.hpp"
#include "flatmemory/details/view.hpp"
#include "flatmemory/details/view_const.hpp"
Expand All @@ -43,7 +44,7 @@ namespace flatmemory
* The optional tag can be used to disallow operations with bitsets with other non-default tags.
*/

template<IsUnsignedIntegral Block, typename Tag = void>
template<IsUnsignedIntegral Block, typename Tag>
struct Bitset : public NonTrivialType
{
/// @brief Non-trivial copy-constructor
Expand Down Expand Up @@ -111,120 +112,47 @@ concept HasBlockType = std::is_same_v<typename T::BlockType, Block>;
template<typename T, typename Tag>
concept HasCompatibleTagType = std::is_same_v<typename T::TagType, Tag> || std::is_same_v<Tag, void> || std::is_same_v<typename T::TagType, void>;

/// @brief Concept for user define bitsets based on the STL
template<typename T>
concept IsUserDefinedBitset = requires(T a, const T b)
struct is_bitset_builder_helper : std::false_type
{
/* Common */

requires IsIntegral<typename T::BlockType>;

/* Non const version*/

{
a.get_default_bit_value()
} -> std::same_as<bool&>;
{
a.get_blocks()
} -> std::same_as<std::vector<typename T::BlockType>&>;

/* Const version */

{
b.get_default_bit_value()
} -> std::same_as<bool>;
{
b.get_blocks()
} -> std::same_as<const std::vector<typename T::BlockType>&>;
};

/// @brief Concept for Builder
template<typename T>
concept IsBitsetBuilder = requires(T a, const T b)
struct is_bitset_nonconst_view_helper : std::false_type
{
/* Common */

requires IsIntegral<typename T::BlockType>;
typename T::TagType;

/* Non const version*/

{
a.get_default_bit_value()
} -> std::same_as<bool&>;
{
a.get_blocks()
} -> std::same_as<Builder<Vector<typename T::BlockType>>&>;

/* Const version */

{
b.get_default_bit_value()
} -> std::same_as<bool>;
{
b.get_blocks()
} -> std::same_as<const Builder<Vector<typename T::BlockType>>&>;
};

/// @brief Concept for View
template<typename T>
concept IsBitsetView = requires(T a, const T b)
struct is_bitset_const_view_helper : std::false_type
{
/* Common */

requires IsIntegral<typename T::BlockType>;
typename T::TagType;

/* Non const version*/

{
a.get_default_bit_value()
} -> std::same_as<bool&>;
{
a.get_blocks()
} -> std::same_as<View<Vector<typename T::BlockType>>>;

/* Const version */

{
b.get_default_bit_value()
} -> std::same_as<bool>;
{
b.get_blocks()
} -> std::same_as<ConstView<Vector<typename T::BlockType>>>;
};

/// @brief Concept for ConstView
template<typename T>
concept IsBitsetConstView = requires(T a, const T b)
template<typename Block, typename Tag>
struct is_bitset_builder_helper<Builder<Bitset<Block, Tag>>> : std::true_type
{
/* Common */
};

requires IsIntegral<typename T::BlockType>;
typename T::TagType;
template<typename Block, typename Tag>
struct is_bitset_nonconst_view_helper<View<Bitset<Block, Tag>>> : std::true_type
{
};

/* Non const version*/
template<typename Block, typename Tag>
struct is_bitset_const_view_helper<ConstView<Bitset<Block, Tag>>> : std::true_type
{
};

{
a.get_default_bit_value()
} -> std::same_as<bool>;
{
a.get_blocks()
} -> std::same_as<ConstView<Vector<typename T::BlockType>>>;
template<typename T>
concept IsBitsetBuilder = is_bitset_builder_helper<T>::value;

/* Const version */
template<typename T>
concept IsBitsetView = is_bitset_nonconst_view_helper<T>::value;

{
b.get_default_bit_value()
} -> std::same_as<bool>;
{
b.get_blocks()
} -> std::same_as<ConstView<Vector<typename T::BlockType>>>;
};
template<typename T>
concept IsBitsetConstView = is_bitset_const_view_helper<T>::value;

/// @brief Concept for Bitset
template<typename T>
concept IsBitset = IsUserDefinedBitset<T> || IsBitsetBuilder<T> || IsBitsetView<T> || IsBitsetConstView<T>;
concept IsBitset = IsBitsetBuilder<T> || IsBitsetView<T> || IsBitsetConstView<T>;

/**
* Operator
Expand Down Expand Up @@ -1801,6 +1729,18 @@ static_assert(!HaveCompatibleTagType<Builder<Bitset<uint64_t, Tag1>>, Builder<Bi

static_assert(HaveCompatibleTagType<Builder<Bitset<uint64_t, Tag1>>, View<Bitset<uint64_t, Tag1>>>);
static_assert(!HaveCompatibleTagType<Builder<Bitset<uint64_t, Tag1>>, View<Bitset<uint64_t, Tag2>>>);

/**
* Pretty printing
*/

template<flatmemory::IsUnsignedIntegral Block, typename Tag>
std::ostream& operator<<(std::ostream& out, ConstView<Bitset<Block, Tag>> element)
{
auto formatter = Formatter(0, 4);
formatter.write(element, out);
return out;
}
}

template<flatmemory::IsUnsignedIntegral Block, typename Tag>
Expand Down
60 changes: 60 additions & 0 deletions include/flatmemory/details/types/formatter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 Dominik Drexler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef FLATMEMORY_TYPES_FORMATTER_HPP_
#define FLATMEMORY_TYPES_FORMATTER_HPP_

#include "flatmemory/details/concepts.hpp"
#include "flatmemory/details/view_const.hpp"

#include <cstddef>
#include <ostream>

namespace flatmemory
{

template<IsUnsignedIntegral Block, typename Tag = void>
struct Bitset;

template<IsTriviallyCopyableOrNonTrivialType... Ts>
struct Tuple;

template<IsTriviallyCopyableOrNonTrivialType T>
struct Vector;

class Formatter
{
private:
size_t m_indent;
size_t m_add_indent;

public:
Formatter(size_t indent = 0, size_t add_indent = 0) : m_indent(indent), m_add_indent(add_indent) {}

template<IsUnsignedIntegral Block, typename Tag>
void write(ConstView<Bitset<Block, Tag>> element, std::ostream& out);

template<IsTriviallyCopyableOrNonTrivialType... Ts>
void write(ConstView<Tuple<Ts...>> element, std::ostream& out);

template<IsTriviallyCopyableOrNonTrivialType T>
void write(ConstView<Vector<T>> element, std::ostream& out);
};

}

#endif
42 changes: 42 additions & 0 deletions include/flatmemory/details/types/formatter_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2024 Dominik Drexler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef FLATMEMORY_TYPES_FORMATTER_IMPL_HPP_
#define FLATMEMORY_TYPES_FORMATTER_IMPL_HPP_

#include "flatmemory/details/types/formatter.hpp"

namespace flatmemory
{

template<IsUnsignedIntegral Block, typename Tag>
void Formatter::write(ConstView<Bitset<Block, Tag>> element, std::ostream& out)
{
}

template<IsTriviallyCopyableOrNonTrivialType... Ts>
void Formatter::write(ConstView<Tuple<Ts...>> element, std::ostream& out)
{
}

template<IsTriviallyCopyableOrNonTrivialType T>
void Formatter::write(ConstView<Vector<T>> element, std::ostream& out)
{
}
}

#endif
26 changes: 21 additions & 5 deletions include/flatmemory/details/types/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "flatmemory/details/concepts.hpp"
#include "flatmemory/details/layout.hpp"
#include "flatmemory/details/layout_utils.hpp"
#include "flatmemory/details/types/formatter.hpp"
#include "flatmemory/details/view.hpp"
#include "flatmemory/details/view_const.hpp"

Expand Down Expand Up @@ -467,8 +468,8 @@ size_t Builder<Tuple<Ts...>>::finish_iterative_impl(std::index_sequence<Is...>,
}
else
{
/* Write the data pos at the offset pos. */
out.write(pos + element_data.position, static_cast<offset_type>(data_pos));
/* Write the distance between written data pos and offset pos at the offset pos. */
out.write(pos + element_data.position, static_cast<offset_type>(data_pos - element_data.position));
out.write_padding(pos + element_data.end, element_data.padding);

/* Write the data at offset */
Expand Down Expand Up @@ -686,7 +687,8 @@ decltype(auto) View<Tuple<Ts...>>::get()
}
else
{
return element_view_type<I>(m_buf + read_value<offset_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position));
const auto offset_pos = m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position;
return element_view_type<I>(offset_pos + read_value<offset_type>(offset_pos));
}
}

Expand All @@ -704,7 +706,8 @@ decltype(auto) View<Tuple<Ts...>>::get() const
}
else
{
return const_element_view_type<I>(m_buf + read_value<offset_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position));
const auto offset_pos = m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position;
return const_element_view_type<I>(offset_pos + read_value<offset_type>(offset_pos));
}
}

Expand Down Expand Up @@ -828,7 +831,8 @@ decltype(auto) ConstView<Tuple<Ts...>>::get() const
}
else
{
return const_element_view_type<I>(m_buf + read_value<offset_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position));
const auto offset_pos = m_buf + Layout<Tuple<Ts...>>::layout_data.element_datas[I].position;
return const_element_view_type<I>(offset_pos + read_value<offset_type>(offset_pos));
}
}

Expand Down Expand Up @@ -861,6 +865,18 @@ size_t ConstView<Tuple<Ts...>>::hash() const
return static_cast<std::size_t>(hash[0] + 0x9e3779b9 + (hash[1] << 6) + (hash[1] >> 2));
}

/**
* Pretty printing
*/

template<IsTriviallyCopyableOrNonTrivialType... Ts>
std::ostream& operator<<(std::ostream& out, ConstView<Tuple<Ts...>> element)
{
auto formatter = Formatter(0, 4);
formatter.write(element, out);
return out;
}

}

template<typename... Ts>
Expand Down
Loading

0 comments on commit 6d480b2

Please sign in to comment.