21 #ifndef OST_PAGED_ARRAY_HH
22 #define OST_PAGED_ARRAY_HH
38 template <
typename T, u
int64_t P>
41 typedef std::vector<T> Page;
49 return pages_[this->GetPage(i)][this->GetOffset(i)];
54 return pages_[this->GetPage(i)][this->GetOffset(i)];
60 pages_.push_back(Page());
61 pages_.back().reserve(P);
63 if (pages_.back().size()==P) {
64 pages_.push_back(Page());
65 pages_.back().reserve(P);
67 pages_.back().push_back(t);
74 return static_cast<uint64_t>((pages_.size()-1)*P+pages_.back().size());
80 return pages_.empty();
92 if(to <= from)
return;
94 if(from >= current_size)
return;
95 if(to > current_size) to = current_size;
99 uint64_t num_elements_to_shift = current_size - to;
100 for(
uint64_t i = 0; i < num_elements_to_shift; ++i) {
101 (*this)[from + i] = (*this)[to + i];
104 uint64_t num_elements_deleted = to - from;
105 uint64_t new_size = current_size - num_elements_deleted;
106 uint64_t new_last_element_idx = new_size - 1;
107 uint64_t new_last_page_idx = this->GetPage(new_last_element_idx);
108 pages_.resize(new_last_page_idx + 1);
109 uint64_t offset = this->GetOffset(new_last_element_idx);
110 pages_.back().resize(offset + 1);
114 void Write(std::ofstream& out_stream,
bool treat_as_pod=
true)
const
118 out_stream.write(reinterpret_cast<char*>(&s),
sizeof(
uint64_t));
119 for (
typename std::vector<Page>::const_iterator i=pages_.begin(),
120 e=pages_.end(); i!=e; ++i) {
121 out_stream.write(reinterpret_cast<const char*>(&i->front()),
122 i->size()*
sizeof(T));
125 throw ost::Error(
"Cannot write non POD paged array!");
128 void Read(std::ifstream& in_stream,
bool treat_as_pod=
true)
132 in_stream.read(reinterpret_cast<char*>(&s),
sizeof(
uint64_t));
134 pages_.resize(num_pages);
135 for (
uint64_t i=0; i<num_pages; ++i) {
140 in_stream.read(reinterpret_cast<char*>(&p.front()),
142 if(!in_stream.good()) {
147 throw ost::Error(
"Cannot load non POD paged array!");
153 template <
typename DS>
160 return pages_ == rhs.pages_;
169 std::vector<Page> pages_;
const T & operator[](uint64_t i) const
unsigned __int64 uint64_t
void push_back(const T &t)
void Write(std::ofstream &out_stream, bool treat_as_pod=true) const
bool operator==(const PagedArray &rhs) const
Vector style container that splits content in pages, suited for large amounts of data. Comes with serialization functionality.
bool operator!=(const PagedArray &rhs) const
void Read(std::ifstream &in_stream, bool treat_as_pod=true)
void ClearRange(uint64_t from, uint64_t to)
T & operator[](uint64_t i)