npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
NtupleBuffer.hh
1 #ifndef NPSTAT_NTUPLEBUFFER_HH_
2 #define NPSTAT_NTUPLEBUFFER_HH_
3 
4 //=========================================================================
5 // NtupleBuffer.hh
6 //
7 // Buffer for data exchange between ArchivedNtuple and underlying archive.
8 // Application code should never use this header directly.
9 //
10 // Author: I. Volobouev
11 //
12 // November 2010
13 //=========================================================================
14 
15 #include <vector>
16 #include <cassert>
17 #include <stdexcept>
18 
19 #include "geners/ClassId.hh"
20 #include "geners/Int2Type.hh"
21 
22 namespace npstat {
23  template <typename T>
25  {
26  public:
27  typedef T value_type;
28 
29  inline NtupleBuffer()
30  : firstRow_(0), maxrows_(0), ncols_(0),
31  writeColumnByColumn_(false) {}
32 
33  inline NtupleBuffer(const unsigned long maxrows,
34  const unsigned long ncols,
35  const bool writeColumnByColumn)
36  : firstRow_(0),
37  maxrows_(maxrows),
38  ncols_(ncols),
39  writeColumnByColumn_(writeColumnByColumn)
40  {
41  if (maxrows_ == 0)
42  ++maxrows_;
43  if (!ncols_) throw std::invalid_argument(
44  "In npstat::NtupleBuffer constructor: "
45  "at least one column is required");
46  data_.reserve(maxrows_*ncols_);
47  if (writeColumnByColumn)
48  {
49  columnOffsets_.reserve(ncols_);
50  for (unsigned long i=0; i<ncols_; ++i)
51  columnOffsets_.push_back(0LL);
52  }
53  }
54 
55  inline unsigned long nColumns() const {return ncols_;}
56  inline unsigned long maxrows() const {return maxrows_;}
57  inline unsigned long firstRow() const {return firstRow_;}
58  inline unsigned long itemsBuffered() const {return data_.size();}
59  inline bool writeByColumn() const {return writeColumnByColumn_;}
60  inline bool isFull() const {return data_.size() >= maxrows_*ncols_;}
61  inline const std::vector<long long>& columnOffsets() const
62  {return columnOffsets_;}
63 
64  inline unsigned long nRows() const
65  {return firstRow_ + data_.size()/ncols_;}
66  inline bool rowInRange(const unsigned long row) const
67  {return row >= firstRow_ && row - firstRow_ < data_.size()/ncols_;}
68 
69  inline T operator()(const unsigned long row,
70  const unsigned long c) const
71  {
72  return data_[(row-firstRow_)*ncols_ + c];
73  }
74  inline T at(const unsigned long row, const unsigned long c) const
75  {
76  if (row < firstRow_)
77  throw std::out_of_range("In npstat::NtupleBuffer::at: "
78  "row number is out of range");
79  if (c >= ncols_)
80  throw std::out_of_range("In npstat::NtupleBuffer::at: "
81  "column number is out of range");
82  return data_.at((row-firstRow_)*ncols_ + c);
83  }
84 
85  inline bool fill(const T* values, const unsigned long lenValues)
86  {
87  if (lenValues != ncols_)
88  throw std::invalid_argument("In npstat::NtupleBuffer::fill: "
89  "incompatible data size");
90  assert(values);
91  for (unsigned long i=0; i<lenValues; ++i)
92  data_.push_back(values[i]);
93  return data_.size() < maxrows_*ncols_;
94  }
95 
96  inline void clear()
97  {
98  firstRow_ += (data_.size()/ncols_);
99  data_.clear();
100  }
101 
102  inline bool rowContents(const unsigned long absRow, T* buf,
103  const unsigned long lenBuf) const
104  {
105  if (absRow < firstRow_)
106  return false;
107  const unsigned long row = absRow - firstRow_;
108  if (row >= data_.size()/ncols_)
109  return false;
110  if (lenBuf < ncols_)
111  throw std::invalid_argument(
112  "In npstat::NtupleBuffer::rowContents:"
113  " provided buffer is too small");
114  assert(buf);
115  const T* local = &data_[0] + row*ncols_;
116  for (unsigned long i=0; i<ncols_; ++i)
117  buf[i] = local[i];
118  return true;
119  }
120 
121  inline bool columnContents(const unsigned long col, T* buf,
122  const unsigned long lenBuf) const
123  {
124  if (col >= ncols_)
125  throw std::out_of_range(
126  "In npstat::NtupleBuffer::columnContents:"
127  " column number is out of range");
128  const unsigned long localRows = data_.size()/ncols_;
129  if (localRows)
130  {
131  if (lenBuf < localRows)
132  throw std::invalid_argument(
133  "In npstat::NtupleBuffer::columnContents:"
134  " provided buffer is too small");
135  assert(buf);
136  const T* local = &data_[0] + col;
137  for (unsigned long i=0; i<localRows; ++i)
138  buf[i] = local[ncols_*i];
139  }
140  return true;
141  }
142 
143  inline bool operator==(const NtupleBuffer& r) const
144  {
145  return firstRow_ == r.firstRow_ &&
146  maxrows_ == r.maxrows_ &&
147  ncols_ == r.ncols_ &&
148  writeColumnByColumn_ == r.writeColumnByColumn_ &&
149  data_ == r.data_;
150  }
151  inline bool operator!=(const NtupleBuffer& r) const
152  {return !(*this == r);}
153 
154  inline void setFirstRow(const unsigned long off) {firstRow_ = off;}
155 
156  // Methods needed for I/O
157  gs::ClassId classId() const {return gs::ClassId(*this);}
158  bool write(std::ostream&) const;
159 
160  static const char* classname();
161  static inline unsigned version() {return 1;}
162  static void restore(const gs::ClassId& id, std::istream& in,
163  NtupleBuffer* buf);
164  static void readColumn(const gs::ClassId& id, std::istream& in,
165  unsigned long column, long long offset,
166  T* buffer, unsigned long bufferLength);
167  private:
168  bool writeTransposed(std::ostream&, gs::Int2Type<true>) const;
169  bool writeTransposed(std::ostream&, gs::Int2Type<false>) const;
170  void readTransposed(std::istream&, unsigned long, gs::Int2Type<false>);
171  void readTransposed(std::istream&, unsigned long, gs::Int2Type<true>);
172 
173  static bool readColumnWOffset(
174  std::istream& in, long long offset,
175  T* buffer, unsigned long nrows, gs::Int2Type<false>);
176 
177  static bool readColumnWOffset(
178  std::istream& in, long long offset,
179  T* buffer, unsigned long nrows, gs::Int2Type<true>);
180 
181  std::vector<T> data_;
182  mutable std::vector<long long> columnOffsets_;
183  unsigned long firstRow_;
184  unsigned long maxrows_;
185  unsigned long ncols_;
186  bool writeColumnByColumn_;
187  };
188 }
189 
190 #include "npstat/stat/NtupleBuffer.icc"
191 
192 #endif // NPSTAT_NTUPLEBUFFER_HH_
Definition: NtupleBuffer.hh:25
Definition: AbsArrayProjector.hh:14