Skip to content

Commit

Permalink
pass segment size at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Feb 11, 2024
1 parent 878e99d commit 7db82ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
15 changes: 8 additions & 7 deletions include/flatmemory/details/byte_stream_segmented.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

namespace flatmemory
{
template<NumBytes N = 1000000> // 1MB
class ByteStreamSegmented
{
private:
NumBytes m_num_bytes_per_segment;
std::vector<uint8_t*> m_segments;

size_t cur_segment_id;
Expand All @@ -46,17 +46,18 @@ namespace flatmemory
/// @brief Allocate a block of size N and update tracking variables.
void increase_capacity() {
if (cur_segment_id == (m_segments.size() - 1)) {
m_segments.push_back(new uint8_t[N]);
m_segments.push_back(new uint8_t[m_num_bytes_per_segment]);
cur_segment_pos = 0;
capacity += N;
capacity += m_num_bytes_per_segment;
}
++cur_segment_id;
assert(cur_segment_id < m_segments.size());
}

public:
ByteStreamSegmented()
: cur_segment_id(-1)
explicit ByteStreamSegmented(NumBytes n = 1000000)
: m_num_bytes_per_segment(n)
, cur_segment_id(-1)
, cur_segment_pos(0)
, size(0)
, capacity(0)
Expand All @@ -81,8 +82,8 @@ namespace flatmemory
/// and otherwise, push_back a new segment first.
uint8_t* write(const uint8_t* data, size_t amount) {
assert(data);
assert(amount <= N);
if (amount > (N - cur_segment_pos)) {
assert(amount <= m_num_bytes_per_segment);
if (amount > (m_num_bytes_per_segment - cur_segment_pos)) {
increase_capacity();
}
uint8_t* result_data = &m_segments[cur_segment_id][cur_segment_pos];
Expand Down
6 changes: 3 additions & 3 deletions include/flatmemory/details/containers/unordered_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ namespace flatmemory {
*
* We use it for states.
*/
template<typename T, NumBytes N = 1000000, typename Hash = std::hash<ConstView<T>>, typename Equal = std::equal_to<ConstView<T>>, typename Allocator = std::allocator<ConstView<T>>>
template<typename T, typename Hash = std::hash<ConstView<T>>, typename Equal = std::equal_to<ConstView<T>>, typename Allocator = std::allocator<ConstView<T>>>
class UnorderedSet
{
private:
// Persistent storage 1MiB blocks
ByteStreamSegmented<N> m_storage;
ByteStreamSegmented m_storage;

// Data to be accessed
std::unordered_set<ConstView<T>, Hash, Equal, Allocator> m_data;

public:
UnorderedSet() = default;
explicit UnorderedSet(NumBytes n = 1000000) : m_storage(ByteStreamSegmented(n)) { }
// Move only
UnorderedSet(const UnorderedSet& other) = delete;
UnorderedSet& operator=(const UnorderedSet& other) = delete;
Expand Down
19 changes: 11 additions & 8 deletions include/flatmemory/details/containers/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ namespace flatmemory {
*
* We use it to store ground actions
*/
template<typename T, NumBytes N = 1000000>
template<typename T>
class VariableSizedTypeVector
{
private:
// Persistent storage 1MiB blocks
ByteStreamSegmented<N> m_storage;
ByteStreamSegmented m_storage;

// Data to be accessed
std::vector<View<T>> m_data;

public:
VariableSizedTypeVector() = default;
explicit VariableSizedTypeVector(NumBytes n = 1000000)
: m_storage(ByteStreamSegmented(n)) { }
// Move only
VariableSizedTypeVector(const VariableSizedTypeVector& other) = delete;
VariableSizedTypeVector& operator=(const VariableSizedTypeVector& other) = delete;
Expand All @@ -44,12 +45,12 @@ class VariableSizedTypeVector
*/

[[nodiscard]] View<T> operator[](size_t pos) {
assert(pos <= static_cast<int>(size()));
assert(pos <= size());
return m_data[pos];
}

[[nodiscard]] ConstView<T> operator[](size_t pos) const {
assert(pos <= static_cast<int>(size()));
assert(pos <= size());
return m_data[pos];
}

Expand Down Expand Up @@ -102,20 +103,22 @@ class VariableSizedTypeVector
*
* We use it to store SearchNodes.
*/
template<typename T, NumBytes N = 1000000>
template<typename T>
class FixedSizedTypeVector
{
private:
// Persistent storage 1MiB blocks
ByteStreamSegmented<N> m_storage;
ByteStreamSegmented m_storage;

// Data to be accessed
std::vector<View<T>> m_data;

const Builder<T> m_default_builder;

public:
FixedSizedTypeVector(Builder<T>&& default_builder) : m_default_builder(std::move(default_builder)) {
FixedSizedTypeVector(Builder<T>&& default_builder, NumBytes n = 1000000)
: m_storage(ByteStreamSegmented(n))
, 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()?");
}
Expand Down

0 comments on commit 7db82ea

Please sign in to comment.