Skip to content

Commit

Permalink
added more api to insert view and constview to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Feb 13, 2024
1 parent 7e10104 commit 4b99b97
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TwoDimVecUint16>();
auto view1 = unordered_set.insert(builder);
auto view2 = unordered_set.insert(view);
EXPECT_EQ(view1, view2);
```
Expand Down
33 changes: 33 additions & 0 deletions include/flatmemory/details/containers/unordered_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "../byte_buffer_segmented.hpp"
#include "../builder.hpp"
#include "../view.hpp"
#include "../view_const.hpp"
#include "../type_traits.hpp"

Expand Down Expand Up @@ -84,6 +85,38 @@ class UnorderedSet
}


[[nodiscard]] ConstView<T> insert(const ConstView<T>& 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<T>(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<T> insert(const View<T>& 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<T>(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
*/
Expand Down
16 changes: 16 additions & 0 deletions include/flatmemory/details/containers/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class VariableSizedTypeVector
void push_back(const Builder<T>& builder) {
m_data.push_back(View<T>(m_storage.write(builder.buffer().data(), builder.buffer().size())));
}

void push_back(const View<T>& view) {
m_data.push_back(View<T>(m_storage.write(view.buffer(), view.get_buffer_size())));
}

void push_back(const ConstView<T>& view) {
m_data.push_back(View<T>(m_storage.write(view.buffer(), view.get_buffer_size())));
}
};

/**
Expand Down Expand Up @@ -190,6 +198,14 @@ class FixedSizedTypeVector
m_data.push_back(View<T>(m_storage.write(builder.get_data(), builder.size())));
}

void push_back(const View<T>& view) {
m_data.push_back(View<T>(m_storage.write(view.buffer(), view.get_buffer_size())));
}

void push_back(const ConstView<T>& view) {
m_data.push_back(View<T>(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();
Expand Down
5 changes: 5 additions & 0 deletions include/flatmemory/details/types/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -489,6 +492,8 @@ namespace flatmemory
* Getters
*/

[[nodiscard]] uint8_t* buffer() { return m_buf; }

[[nodiscard]] size_t buffer_size() const
{
assert(m_buf);
Expand Down
5 changes: 5 additions & 0 deletions include/flatmemory/details/types/trivial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};


Expand Down Expand Up @@ -173,6 +176,8 @@ namespace flatmemory
assert(m_buf);
return sizeof(T);
}

[[nodiscard]] uint8_t* buffer() { return m_buf; }
};
}

Expand Down
49 changes: 34 additions & 15 deletions include/flatmemory/details/types/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,6 @@ namespace flatmemory
}


/**
* Capacity
*/

[[nodiscard]] size_t buffer_size() const {
assert(m_buf);
assert(test_correct_alignment<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position));
return read_value<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position);
}


/**
* Lookup
*/
Expand Down Expand Up @@ -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<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position));
return read_value<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position);
}


/**
* Capacity
*/
[[nodiscard]] size_t size() const { return Layout<Tuple<Ts...>>::size; }


/**
* Hashing
*/

[[nodiscard]] size_t hash() const {
size_t seed = Layout<Tuple<Ts...>>::size;
int64_t hash[2];
Expand Down Expand Up @@ -455,16 +464,26 @@ namespace flatmemory
}
}


/**
* Capacity
*/
[[nodiscard]] size_t buffer_size() const {
assert(m_buf);
assert(test_correct_alignment<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position));
return read_value<buffer_size_type>(m_buf + Layout<Tuple<Ts...>>::layout_data.buffer_size_position);
}

[[nodiscard]] const uint8_t* buffer() const { return m_buf; }


/**
* Capacity
*/

[[nodiscard]] size_t size() const { return Layout<Tuple<Ts...>>::size; }


/**
* Hashing
*/

[[nodiscard]] size_t hash() const {
size_t seed = Layout<Tuple<Ts...>>::size;
int64_t hash[2];
Expand Down
5 changes: 5 additions & 0 deletions include/flatmemory/details/types/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ namespace flatmemory
[[nodiscard]] T_* data() { return reinterpret_cast<T_*>(m_buf + Layout<Vector<T>>::vector_data_position); }
[[nodiscard]] const T_* data() const { return reinterpret_cast<const T_*>(m_buf + Layout<Vector<T>>::vector_data_position); }

[[nodiscard]] uint8_t* buffer() { return m_buf; }
[[nodiscard]] const uint8_t* buffer() const { return m_buf; }


/**
* Iterators
Expand Down Expand Up @@ -594,6 +597,8 @@ namespace flatmemory

[[nodiscard]] const T_* data() const { return reinterpret_cast<const T_*>(m_buf + Layout<Vector<T>>::vector_data_position); }

[[nodiscard]] const uint8_t* buffer() const { return m_buf; }


/**
* Iterators
Expand Down

0 comments on commit 4b99b97

Please sign in to comment.