algorithm-engineering/src/container.cpp

30 lines
832 B
C++
Raw Normal View History

2025-07-28 22:25:26 +02:00
#include "container.hpp"
#include <cstddef>
#include <span>
2025-09-28 14:58:27 +02:00
#include <iostream>
2025-07-28 22:25:26 +02:00
namespace ae {
container::container(std::span<const element_type> data) {
2025-09-28 14:58:27 +02:00
// Reserve enough space for the chunks to avoid relocation while building the structure
container::Directory dir(data.size() / container::chunk_size + 1);
2025-07-28 22:25:26 +02:00
2025-09-28 14:58:27 +02:00
auto entry = 0;
2025-07-28 22:25:26 +02:00
for (auto first = data.begin(); first < data.end();) {
2025-09-28 14:58:27 +02:00
const auto last = (data.end() - first) < container::chunk_size ? data.end() : first + container::chunk_size;
Chunk chunk;
// This could be improved by just pointing dir[entry] to first, removing the copy process.
std::copy(first, last, chunk.begin());
dir[entry++] = chunk;
2025-07-28 22:25:26 +02:00
first = last;
}
2025-09-28 14:58:27 +02:00
this->data = dir;
this->size_ = data.size();
2025-07-28 22:25:26 +02:00
}
2025-09-28 14:58:27 +02:00
uint32_t container::size() { return this->size_; }
2025-07-28 22:25:26 +02:00
} // namespace ae