21 #ifndef OST_DB_PAGED_ARRAY_HH
22 #define OST_DB_PAGED_ARRAY_HH
33 namespace ost{
namespace db{
35 template <
typename T, u
int64_t P>
38 typedef std::vector<T> Page;
46 return pages_[this->GetPage(i)][this->GetOffset(i)];
51 return pages_[this->GetPage(i)][this->GetOffset(i)];
57 pages_.push_back(Page());
58 pages_.back().reserve(P);
60 if (pages_.back().size()==P) {
61 pages_.push_back(Page());
62 pages_.back().reserve(P);
64 pages_.back().push_back(t);
71 return static_cast<uint64_t>((pages_.size()-1)*P+pages_.back().size());
77 return pages_.empty();
89 if(to <= from)
return;
91 if(from >= current_size)
return;
92 if(to > current_size) to = current_size;
96 uint64_t num_elements_to_shift = current_size - to;
97 for(
uint64_t i = 0; i < num_elements_to_shift; ++i) {
98 (*this)[from + i] = (*this)[to + i];
101 uint64_t num_elements_deleted = to - from;
102 uint64_t new_size = current_size - num_elements_deleted;
103 uint64_t new_last_element_idx = new_size - 1;
104 uint64_t new_last_page_idx = this->GetPage(new_last_element_idx);
105 pages_.resize(new_last_page_idx + 1);
106 uint64_t offset = this->GetOffset(new_last_element_idx);
107 pages_.back().resize(offset + 1);
111 void Write(std::ofstream& out_stream,
bool treat_as_pod=
true)
const
115 out_stream.write(reinterpret_cast<char*>(&s),
sizeof(
uint64_t));
116 for (
typename std::vector<Page>::const_iterator i=pages_.begin(),
117 e=pages_.end(); i!=e; ++i) {
118 out_stream.write(reinterpret_cast<const char*>(&i->front()),
119 i->size()*
sizeof(T));
122 throw ost::Error(
"Cannot write non POD paged array!");
125 void Read(std::ifstream& in_stream,
bool treat_as_pod=
true)
129 in_stream.read(reinterpret_cast<char*>(&s),
sizeof(
uint64_t));
131 pages_.resize(num_pages);
132 for (
uint64_t i=0; i<num_pages; ++i) {
137 in_stream.read(reinterpret_cast<char*>(&p.front()),
141 throw ost::Error(
"Cannot load non POD paged array!");
147 template <
typename DS>
154 return pages_ == rhs.pages_;
163 std::vector<Page> pages_;
void push_back(const T &t)
bool operator!=(const PagedArray &rhs) const
void Write(std::ofstream &out_stream, bool treat_as_pod=true) const
T & operator[](uint64_t i)
unsigned __int64 uint64_t
void Read(std::ifstream &in_stream, bool treat_as_pod=true)
void ClearRange(uint64_t from, uint64_t to)
bool operator==(const PagedArray &rhs) const
const T & operator[](uint64_t i) const