From f3370213f1c47c5b7edb37b4a3e4e53b35e65630 Mon Sep 17 00:00:00 2001 From: Dominik Drexler Date: Tue, 5 Mar 2024 23:20:30 +0100 Subject: [PATCH] with prints in case of further errors --- .../details/byte_buffer_segmented.hpp | 2 +- .../details/containers/unordered_set.hpp | 41 +++++++++++++++---- .../flatmemory/details/containers/vector.hpp | 16 ++++---- include/flatmemory/details/types/bitset.hpp | 2 +- include/flatmemory/details/types/tuple.hpp | 29 ++++++++++++- include/flatmemory/flatmemory.hpp | 19 ++++----- tests/unit/types/tuple.cpp | 38 ++++++++++++++++- 7 files changed, 117 insertions(+), 30 deletions(-) diff --git a/include/flatmemory/details/byte_buffer_segmented.hpp b/include/flatmemory/details/byte_buffer_segmented.hpp index 1657661..4a5b601 100644 --- a/include/flatmemory/details/byte_buffer_segmented.hpp +++ b/include/flatmemory/details/byte_buffer_segmented.hpp @@ -91,7 +91,7 @@ namespace flatmemory memcpy(result_data, data, amount); cur_segment_pos += amount; m_size += amount; - last_written += amount; + last_written = amount; return result_data; } diff --git a/include/flatmemory/details/containers/unordered_set.hpp b/include/flatmemory/details/containers/unordered_set.hpp index f586689..57eddba 100644 --- a/include/flatmemory/details/containers/unordered_set.hpp +++ b/include/flatmemory/details/containers/unordered_set.hpp @@ -7,18 +7,34 @@ #include "../view.hpp" #include "../view_const.hpp" #include "../type_traits.hpp" +#include "../byte_buffer_utils.hpp" #include +#include namespace flatmemory { +template +struct CustomHash { + size_t operator()(const T& element) const { + return element.hash(); + } +}; + +template +struct CustomEqual { + bool operator()(const T& left_element, const T& right_element) const { + return left_element == right_element; + } +}; + /** * UnorderedSet behaves like std::unordered_set * but without the functionality to erase elements * since m_storage would keep growing. */ -template>, typename Equal = std::equal_to>, typename Allocator = std::allocator>> +template class UnorderedSet { private: @@ -26,13 +42,13 @@ class UnorderedSet ByteBufferSegmented m_storage; // Data to be accessed - std::unordered_set, Hash, Equal, Allocator> m_data; + std::unordered_set, CustomHash>, CustomEqual>> m_data; - using iterator = std::unordered_set, Hash, Equal, Allocator>::iterator; - using const_iterator = std::unordered_set, Hash, Equal, Allocator>::const_iterator; + using iterator = std::unordered_set>::iterator; + using const_iterator = std::unordered_set>::const_iterator; public: - explicit UnorderedSet(NumBytes n = 1000000) + explicit UnorderedSet(NumBytes n = 1000000) : m_storage(ByteBufferSegmented(n)) { } // Move only UnorderedSet(const UnorderedSet& other) = delete; @@ -86,16 +102,25 @@ class UnorderedSet [[nodiscard]] ConstView insert(const ConstView& view) { + std::cout << "unordered_set written: "; + print(view.buffer(), view.buffer_size()); const uint8_t* data = view.buffer(); - size_t amount = view.get_buffer_size(); + size_t amount = view.buffer_size(); const uint8_t* new_data = m_storage.write(data, amount); auto result_view = ConstView(new_data); + std::cout << "data: " << data << " result_view_ptr: " << &result_view << std::endl; auto it = m_data.find(result_view); + if (it != m_data.end()) { // not unique, mark the storage as free again - m_storage.undo_last_write(); + //m_storage.undo_last_write(); + std::cout << "not unique!" << std::endl; + auto obtained_view = *it; + std::cout << "unordered_set obtained: "; + print(obtained_view.buffer(), obtained_view.buffer_size()); return *it; } + auto result = m_data.insert(result_view); return *result.first; } @@ -103,7 +128,7 @@ class UnorderedSet [[nodiscard]] ConstView insert(const View& view) { const uint8_t* data = view.buffer(); - size_t amount = view.get_buffer_size(); + size_t amount = view.buffer_size(); const uint8_t* new_data = m_storage.write(data, amount); auto result_view = ConstView(new_data); auto it = m_data.find(result_view); diff --git a/include/flatmemory/details/containers/vector.hpp b/include/flatmemory/details/containers/vector.hpp index e50f634..3dc6081 100644 --- a/include/flatmemory/details/containers/vector.hpp +++ b/include/flatmemory/details/containers/vector.hpp @@ -17,7 +17,7 @@ namespace flatmemory { /** * VariableSizedTypeVector can handle different sized objects - * but does not support resize since the exact + * but does not support resize since the exact * amount of needed bytes is not known in advance. */ template @@ -34,7 +34,7 @@ class VariableSizedTypeVector using const_iterator = std::vector>::const_iterator; public: - explicit VariableSizedTypeVector(NumBytes n = 1000000) + explicit VariableSizedTypeVector(NumBytes n = 1000000) : m_storage(ByteBufferSegmented(n)) { } // Move only VariableSizedTypeVector(const VariableSizedTypeVector& other) = delete; @@ -56,9 +56,9 @@ class VariableSizedTypeVector return m_data[pos]; } - [[nodiscard]] View back() { + [[nodiscard]] View back() { assert(!m_data.empty()); - return m_data.back(); + return m_data.back(); } [[nodiscard]] ConstView back() const { @@ -99,11 +99,11 @@ class VariableSizedTypeVector } void push_back(const View& view) { - m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + m_data.push_back(View(m_storage.write(view.buffer(), view.buffer_size()))); } void push_back(const ConstView& view) { - m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + m_data.push_back(View(m_storage.write(view.buffer(), view.buffer_size()))); } }; @@ -127,9 +127,9 @@ class FixedSizedTypeVector using const_iterator = std::vector>::const_iterator; public: - FixedSizedTypeVector(Builder&& default_builder, NumBytes n = 1000000) + FixedSizedTypeVector(Builder&& default_builder, NumBytes n = 1000000) : m_storage(ByteBufferSegmented(n)) - , m_default_builder(std::move(default_builder)) { + , m_default_builder(std::move(default_builder)) { if (m_default_builder.buffer().data() == nullptr) { throw std::runtime_error("Builder is not fully initialized! Did you forget to call finish()?"); } diff --git a/include/flatmemory/details/types/bitset.hpp b/include/flatmemory/details/types/bitset.hpp index 3f01e35..43e15b8 100644 --- a/include/flatmemory/details/types/bitset.hpp +++ b/include/flatmemory/details/types/bitset.hpp @@ -368,7 +368,7 @@ namespace flatmemory const auto length = static_cast(last_relevant_index + 1) * sizeof(Block); // Compute a hash value up to and including this block - int64_t hash[2]; + int64_t hash[2] = {0, 0}; MurmurHash3_x64_128(blocks.data(), length, seed, hash); return static_cast(hash[0] + 0x9e3779b9 + (hash[1] << 6) + (hash[1] >> 2)); } diff --git a/include/flatmemory/details/types/tuple.hpp b/include/flatmemory/details/types/tuple.hpp index 9089dd9..9b32926 100644 --- a/include/flatmemory/details/types/tuple.hpp +++ b/include/flatmemory/details/types/tuple.hpp @@ -211,6 +211,7 @@ namespace flatmemory } else { // write offset m_buffer.write(element_data.position, buffer_size); + std::cout << "offset: " << buffer_size << std::endl; m_buffer.write_padding(element_data.end, element_data.padding); // write data @@ -321,6 +322,7 @@ namespace flatmemory * Operators */ [[nodiscard]] bool operator==(const View& other) const { + std::cout << "operator==" << std::endl; if (this != &other) { if (buffer_size() != other.buffer_size()) return false; return std::memcmp(m_buf, other.m_buf, buffer_size()) == 0; @@ -436,6 +438,7 @@ namespace flatmemory */ [[nodiscard]] bool operator==(const ConstView& other) const { + std::cout << "operator==" << std::endl; if (this != &other) { if (m_buf != other.m_buf) { if (buffer_size() != other.buffer_size()) return false; @@ -466,6 +469,7 @@ namespace flatmemory assert(test_correct_alignment>(m_buf + Layout>::layout_data.element_datas[I].position)); return read_value>(m_buf + Layout>::layout_data.element_datas[I].position); } else { + std::cout << "get: " << read_value(m_buf + Layout>::layout_data.element_datas[I].position) << std::endl; return element_view_type(m_buf + read_value(m_buf + Layout>::layout_data.element_datas[I].position)); } } @@ -503,12 +507,33 @@ namespace flatmemory namespace std { - // Inject hash into the std namespace + // Inject comparison and hash into the std namespace + template + struct equal_to>> + { + bool operator()(const flatmemory::View>& view_left, const flatmemory::View>& view_right) const + { + std::cout << "equal_to" << std::endl; + return view_left == view_right; + } + }; + + template + struct equal_to>> + { + bool operator()(const flatmemory::ConstView>& view_left, const flatmemory::ConstView>& view_right) const + { + std::cout << "equal_to" << std::endl; + return view_left == view_right; + } + }; + template struct hash>> { std::size_t operator()(const flatmemory::View> &tuple) const { + std::cout << "hash" << std::endl; return tuple.hash(); } }; @@ -518,6 +543,7 @@ namespace std { std::size_t operator()(const flatmemory::ConstView> &tuple) const { + std::cout << "hash" << std::endl; return tuple.hash(); } }; @@ -527,6 +553,7 @@ namespace std { std::size_t operator()(const flatmemory::Builder> &tuple) const { + std::cout << "hash" << std::endl; return tuple.hash(); } }; diff --git a/include/flatmemory/flatmemory.hpp b/include/flatmemory/flatmemory.hpp index a259aed..014fb03 100644 --- a/include/flatmemory/flatmemory.hpp +++ b/include/flatmemory/flatmemory.hpp @@ -19,14 +19,6 @@ #define FLATMEMORY_FLATMEMORY_HPP_ -/** - * Containers -*/ - -#include "details/containers/unordered_set.hpp" -#include "details/containers/vector.hpp" - - /** * Types */ @@ -34,6 +26,13 @@ #include "details/types/bitset.hpp" #include "details/types/trivial.hpp" #include "details/types/tuple.hpp" -#include "details/types/vector.hpp" +#include "details/types/vector.hpp" + +/** + * Containers +*/ + +#include "details/containers/unordered_set.hpp" +#include "details/containers/vector.hpp" -#endif +#endif diff --git a/tests/unit/types/tuple.cpp b/tests/unit/types/tuple.cpp index ed8a364..948ce5d 100644 --- a/tests/unit/types/tuple.cpp +++ b/tests/unit/types/tuple.cpp @@ -20,6 +20,7 @@ #include #include +#include namespace flatmemory::tests @@ -193,7 +194,7 @@ namespace flatmemory::tests auto view1 = View(builder1.buffer().data()); auto view2 = View(builder2.buffer().data()); auto view3 = View(builder3.buffer().data()); - + EXPECT_TRUE((view1 == view2)); EXPECT_EQ(view1.hash(), view2.hash()); @@ -217,4 +218,39 @@ namespace flatmemory::tests EXPECT_FALSE((const_view2 == const_view3)); EXPECT_NE(const_view2.hash(), const_view3.hash()); } + + + TEST(FlatmemoryTests, TypesTupleStateTest) { + using BitsetLayout = Bitset; + using TupleLayout = Tuple; + using UnorderedSet = UnorderedSet; + + Layout().print(); + std::cout << std::endl; + Layout().print(); + std::cout << std::endl; + + auto vec = UnorderedSet(); + + std::random_device rd; // Obtain a random number from hardware + std::mt19937 eng(rd()); // Seed the generator + + auto builder = Builder(); + + for (size_t i = 0; i < 2; ++i) { + std::cout << std::endl << std::endl; + size_t i2 = eng() % 2; + + //i2 = 2; + builder.get<1>().get_blocks().resize(i2); + builder.finish(); + auto const_view = ConstView(builder.buffer().data()); + auto view = vec.insert(const_view); + + std::cout << "unordered_set returned: "; + print(view.buffer(), view.buffer_size()); + + EXPECT_EQ(view.get<1>().get_blocks().size(), i2); + } + } }