diff --git a/README.md b/README.md index 7938c05..f07b1fd 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,12 @@ EXPECT_EQ(view.size(), 2); EXPECT_EQ(view[0][0], 5); EXPECT_EQ(view[1][0], 6); EXPECT_EQ(view[1][1], 7); + +// 5. Insert into a set either using builder or view +auto unordered_set = UnorderedSet(); +auto view1 = unordered_set.insert(builder); +auto view2 = unordered_set.insert(view); +EXPECT_EQ(view1, view2); ``` diff --git a/include/flatmemory/details/containers/unordered_set.hpp b/include/flatmemory/details/containers/unordered_set.hpp index 2c44681..f586689 100644 --- a/include/flatmemory/details/containers/unordered_set.hpp +++ b/include/flatmemory/details/containers/unordered_set.hpp @@ -4,6 +4,7 @@ #include "../byte_buffer_segmented.hpp" #include "../builder.hpp" +#include "../view.hpp" #include "../view_const.hpp" #include "../type_traits.hpp" @@ -84,6 +85,38 @@ class UnorderedSet } + [[nodiscard]] ConstView insert(const ConstView& view) { + const uint8_t* data = view.buffer(); + size_t amount = view.get_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); + if (it != m_data.end()) { + // not unique, mark the storage as free again + m_storage.undo_last_write(); + return *it; + } + auto result = m_data.insert(result_view); + return *result.first; + } + + + [[nodiscard]] ConstView insert(const View& view) { + const uint8_t* data = view.buffer(); + size_t amount = view.get_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); + if (it != m_data.end()) { + // not unique, mark the storage as free again + m_storage.undo_last_write(); + return *it; + } + auto result = m_data.insert(result_view); + return *result.first; + } + + /** * Lookup */ diff --git a/include/flatmemory/details/containers/vector.hpp b/include/flatmemory/details/containers/vector.hpp index 81469cf..e50f634 100644 --- a/include/flatmemory/details/containers/vector.hpp +++ b/include/flatmemory/details/containers/vector.hpp @@ -97,6 +97,14 @@ class VariableSizedTypeVector void push_back(const Builder& builder) { m_data.push_back(View(m_storage.write(builder.buffer().data(), builder.buffer().size()))); } + + void push_back(const View& view) { + m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + } + + void push_back(const ConstView& view) { + m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + } }; /** @@ -190,6 +198,14 @@ class FixedSizedTypeVector m_data.push_back(View(m_storage.write(builder.get_data(), builder.size()))); } + void push_back(const View& view) { + m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + } + + void push_back(const ConstView& view) { + m_data.push_back(View(m_storage.write(view.buffer(), view.get_buffer_size()))); + } + void resize(size_t count) { const uint8_t* default_data = m_default_builder.buffer().data(); size_t default_size = m_default_builder.buffer().size(); diff --git a/include/flatmemory/details/types/bitset.hpp b/include/flatmemory/details/types/bitset.hpp index 1698599..3e91b67 100644 --- a/include/flatmemory/details/types/bitset.hpp +++ b/include/flatmemory/details/types/bitset.hpp @@ -385,6 +385,9 @@ namespace flatmemory /** * Getters */ + [[nodiscard]] uint8_t* buffer() { return m_buf; } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } + [[nodiscard]] size_t buffer_size() const { assert(m_buf); @@ -489,6 +492,8 @@ namespace flatmemory * Getters */ + [[nodiscard]] uint8_t* buffer() { return m_buf; } + [[nodiscard]] size_t buffer_size() const { assert(m_buf); diff --git a/include/flatmemory/details/types/trivial.hpp b/include/flatmemory/details/types/trivial.hpp index 6286e4d..f0ff501 100644 --- a/include/flatmemory/details/types/trivial.hpp +++ b/include/flatmemory/details/types/trivial.hpp @@ -130,6 +130,9 @@ namespace flatmemory assert(m_buf); return sizeof(T); } + + [[nodiscard]] uint8_t* buffer() { return m_buf; } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } }; @@ -173,6 +176,8 @@ namespace flatmemory assert(m_buf); return sizeof(T); } + + [[nodiscard]] uint8_t* buffer() { return m_buf; } }; } diff --git a/include/flatmemory/details/types/tuple.hpp b/include/flatmemory/details/types/tuple.hpp index f65774d..ec7b3a8 100644 --- a/include/flatmemory/details/types/tuple.hpp +++ b/include/flatmemory/details/types/tuple.hpp @@ -332,17 +332,6 @@ namespace flatmemory } - /** - * Capacity - */ - - [[nodiscard]] size_t buffer_size() const { - assert(m_buf); - assert(test_correct_alignment(m_buf + Layout>::layout_data.buffer_size_position)); - return read_value(m_buf + Layout>::layout_data.buffer_size_position); - } - - /** * Lookup */ @@ -376,6 +365,26 @@ namespace flatmemory } } + [[nodiscard]] uint8_t* buffer() { return m_buf; } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } + + [[nodiscard]] size_t buffer_size() const { + assert(m_buf); + assert(test_correct_alignment(m_buf + Layout>::layout_data.buffer_size_position)); + return read_value(m_buf + Layout>::layout_data.buffer_size_position); + } + + + /** + * Capacity + */ + [[nodiscard]] size_t size() const { return Layout>::size; } + + + /** + * Hashing + */ + [[nodiscard]] size_t hash() const { size_t seed = Layout>::size; int64_t hash[2]; @@ -455,16 +464,26 @@ namespace flatmemory } } - - /** - * Capacity - */ [[nodiscard]] size_t buffer_size() const { assert(m_buf); assert(test_correct_alignment(m_buf + Layout>::layout_data.buffer_size_position)); return read_value(m_buf + Layout>::layout_data.buffer_size_position); } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } + + + /** + * Capacity + */ + + [[nodiscard]] size_t size() const { return Layout>::size; } + + + /** + * Hashing + */ + [[nodiscard]] size_t hash() const { size_t seed = Layout>::size; int64_t hash[2]; diff --git a/include/flatmemory/details/types/vector.hpp b/include/flatmemory/details/types/vector.hpp index 714d2d2..68b7a0b 100644 --- a/include/flatmemory/details/types/vector.hpp +++ b/include/flatmemory/details/types/vector.hpp @@ -351,6 +351,9 @@ namespace flatmemory [[nodiscard]] T_* data() { return reinterpret_cast(m_buf + Layout>::vector_data_position); } [[nodiscard]] const T_* data() const { return reinterpret_cast(m_buf + Layout>::vector_data_position); } + [[nodiscard]] uint8_t* buffer() { return m_buf; } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } + /** * Iterators @@ -594,6 +597,8 @@ namespace flatmemory [[nodiscard]] const T_* data() const { return reinterpret_cast(m_buf + Layout>::vector_data_position); } + [[nodiscard]] const uint8_t* buffer() const { return m_buf; } + /** * Iterators