1 #ifndef NPSTAT_NTUPLEBUFFER_HH_
2 #define NPSTAT_NTUPLEBUFFER_HH_
19 #include "geners/ClassId.hh"
20 #include "geners/Int2Type.hh"
30 : firstRow_(0), maxrows_(0), ncols_(0),
31 writeColumnByColumn_(
false) {}
34 const unsigned long ncols,
35 const bool writeColumnByColumn)
39 writeColumnByColumn_(writeColumnByColumn)
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)
49 columnOffsets_.reserve(ncols_);
50 for (
unsigned long i=0; i<ncols_; ++i)
51 columnOffsets_.push_back(0LL);
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_;}
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_;}
69 inline T operator()(
const unsigned long row,
70 const unsigned long c)
const
72 return data_[(row-firstRow_)*ncols_ + c];
74 inline T at(
const unsigned long row,
const unsigned long c)
const
77 throw std::out_of_range(
"In npstat::NtupleBuffer::at: "
78 "row number is out of range");
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);
85 inline bool fill(
const T* values,
const unsigned long lenValues)
87 if (lenValues != ncols_)
88 throw std::invalid_argument(
"In npstat::NtupleBuffer::fill: "
89 "incompatible data size");
91 for (
unsigned long i=0; i<lenValues; ++i)
92 data_.push_back(values[i]);
93 return data_.size() < maxrows_*ncols_;
98 firstRow_ += (data_.size()/ncols_);
102 inline bool rowContents(
const unsigned long absRow, T* buf,
103 const unsigned long lenBuf)
const
105 if (absRow < firstRow_)
107 const unsigned long row = absRow - firstRow_;
108 if (row >= data_.size()/ncols_)
111 throw std::invalid_argument(
112 "In npstat::NtupleBuffer::rowContents:"
113 " provided buffer is too small");
115 const T* local = &data_[0] + row*ncols_;
116 for (
unsigned long i=0; i<ncols_; ++i)
121 inline bool columnContents(
const unsigned long col, T* buf,
122 const unsigned long lenBuf)
const
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_;
131 if (lenBuf < localRows)
132 throw std::invalid_argument(
133 "In npstat::NtupleBuffer::columnContents:"
134 " provided buffer is too small");
136 const T* local = &data_[0] + col;
137 for (
unsigned long i=0; i<localRows; ++i)
138 buf[i] = local[ncols_*i];
145 return firstRow_ == r.firstRow_ &&
146 maxrows_ == r.maxrows_ &&
147 ncols_ == r.ncols_ &&
148 writeColumnByColumn_ == r.writeColumnByColumn_ &&
152 {
return !(*
this == r);}
154 inline void setFirstRow(
const unsigned long off) {firstRow_ = off;}
157 gs::ClassId classId()
const {
return gs::ClassId(*
this);}
158 bool write(std::ostream&)
const;
160 static const char* classname();
161 static inline unsigned version() {
return 1;}
162 static void restore(
const gs::ClassId&
id, std::istream& in,
164 static void readColumn(
const gs::ClassId&
id, std::istream& in,
165 unsigned long column,
long long offset,
166 T* buffer,
unsigned long bufferLength);
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>);
173 static bool readColumnWOffset(
174 std::istream& in,
long long offset,
175 T* buffer,
unsigned long nrows, gs::Int2Type<false>);
177 static bool readColumnWOffset(
178 std::istream& in,
long long offset,
179 T* buffer,
unsigned long nrows, gs::Int2Type<true>);
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_;
190 #include "npstat/stat/NtupleBuffer.icc"
Definition: NtupleBuffer.hh:25
Definition: AbsArrayProjector.hh:14