File: paged_array.hh

package info (click to toggle)
openstructure 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,240 kB
  • sloc: cpp: 188,571; python: 36,686; ansic: 34,298; fortran: 3,275; sh: 312; xml: 146; makefile: 29
file content (174 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2020 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//------------------------------------------------------------------------------


#ifndef OST_PAGED_ARRAY_HH
#define OST_PAGED_ARRAY_HH

/*
  Author: Marco Biasini, Gabriel Studer
 */

#include <vector>
#include <fstream>
#include <ost/stdint.hh>
#include <ost/message.hh>

namespace ost{


/// \brief Vector style container that splits content in pages, suited for large 
///        amounts of data. Comes with serialization functionality.
template <typename T, uint64_t P>
class PagedArray  {
private:
  typedef std::vector<T> Page;
public:
  typedef T value_type;
  
  PagedArray() {}
  
  T& operator[](uint64_t i) 
  { 
    return pages_[this->GetPage(i)][this->GetOffset(i)]; 
  }
  
  const T& operator[](uint64_t i) const
  { 
    return pages_[this->GetPage(i)][this->GetOffset(i)]; 
  }  
  
  void push_back(const T& t)
  { 
    if (pages_.empty()) {
      pages_.push_back(Page());
      pages_.back().reserve(P);
    }
    if (pages_.back().size()==P) {
      pages_.push_back(Page());
      pages_.back().reserve(P);
    }
    pages_.back().push_back(t);
  }
  uint64_t size() const 
  {
    if (pages_.empty()) {
      return 0;
    } else {
      return static_cast<uint64_t>((pages_.size()-1)*P+pages_.back().size());  
    }
  }

  bool empty() const 
  {
    return pages_.empty();
  }

  void clear() 
  {
    pages_.clear();
  }
  
  // clears everything in [from, to[ and shifts every datapoint after the specified
  // range at the location defined by from
  void ClearRange(uint64_t from, uint64_t to) 
  {
    if(to <= from) return; // invalid range
    uint64_t current_size = this->size();
    if(from >= current_size) return; // nothing to delete
    if(to > current_size) to = current_size; // from is in the valid range, but
                                             // to is too large. Let's just kill
                                             // [from, this->size()[

    uint64_t num_elements_to_shift = current_size - to;
    for(uint64_t i = 0; i < num_elements_to_shift; ++i) {
      (*this)[from + i] = (*this)[to + i];
    }

    uint64_t num_elements_deleted = to - from;
    uint64_t new_size = current_size - num_elements_deleted;
    uint64_t new_last_element_idx = new_size - 1;
    uint64_t new_last_page_idx = this->GetPage(new_last_element_idx);
    pages_.resize(new_last_page_idx + 1);
    uint64_t offset = this->GetOffset(new_last_element_idx);
    pages_.back().resize(offset + 1);
  }
  

  void Write(std::ofstream& out_stream, bool treat_as_pod=true) const
  {
    if (treat_as_pod) {
      uint64_t s=this->size();
      out_stream.write(reinterpret_cast<char*>(&s), sizeof(uint64_t));
      for (typename std::vector<Page>::const_iterator i=pages_.begin(), 
           e=pages_.end(); i!=e; ++i) {
        out_stream.write(reinterpret_cast<const char*>(&i->front()), 
                         i->size()*sizeof(T));        
      }
    } else {
      throw ost::Error("Cannot write non POD paged array!");
    }
  }
  void Read(std::ifstream& in_stream, bool treat_as_pod=true)
  {
    if (treat_as_pod) {
      uint64_t s;
      in_stream.read(reinterpret_cast<char*>(&s), sizeof(uint64_t));
      uint64_t num_pages=s/P+1;
      pages_.resize(num_pages);
      for (uint64_t i=0; i<num_pages; ++i) {
        uint64_t left=std::min(P, s-i*P);
        Page& p=pages_[i];
        p.reserve(P);
        p.resize(left);
        in_stream.read(reinterpret_cast<char*>(&p.front()), 
                       p.size()*sizeof(T));
        if(!in_stream.good()) {
          throw ost::Error("Failed to read data!");
        }
      }
    } else {
      throw ost::Error("Cannot load non POD paged array!");
    }
  }

  // portable serialization
  // (cleanly element by element with fixed-width base-types)
  template <typename DS>
  void Serialize(DS& ds) {
    // assumes that T is not a fundamental type!
    ds & pages_;
  }

  bool operator==(const PagedArray& rhs) const {
    return pages_ == rhs.pages_;
  }
  bool operator!=(const PagedArray& rhs) const {
    return !this->operator==(rhs);
  }
  
private:
  uint64_t GetPage(uint64_t i) const { return i/P; }
  uint64_t GetOffset(uint64_t i) const { return i % P; }
  std::vector<Page> pages_;
};

} // ns

#endif