31 #ifndef __TASMANIAN_SPARSE_GRID_INDEX_SETS_HPP
32 #define __TASMANIAN_SPARSE_GRID_INDEX_SETS_HPP
77 std::vector<T>
spltVector2D(std::vector<T>
const &x,
size_t stride,
int ibegin,
int iend){
78 size_t sbegin(ibegin), send(iend);
79 size_t num_strips = x.size() / stride;
80 size_t new_stride = send - sbegin;
81 std::vector<T> result(num_strips * new_stride);
83 auto ir = result.begin();
84 for(
size_t i=0; i<num_strips; i++){
85 std::copy_n(ix + sbegin, new_stride, ir);
86 std::advance(ix, stride);
87 std::advance(ir, new_stride);
109 template<
typename IntTypeA,
typename IntTypeB>
110 Data2D(IntTypeA new_stride, IntTypeB new_num_strips) : stride(static_cast<size_t>(new_stride)), num_strips(static_cast<size_t>(new_num_strips)),
111 vec(Utils::
size_mult(stride, num_strips)){}
113 template<
typename IntTypeA,
typename IntTypeB>
114 Data2D(IntTypeA new_stride, IntTypeB new_num_strips, T val) : stride(static_cast<size_t>(new_stride)),
115 num_strips(static_cast<size_t>(new_num_strips)),
116 vec(Utils::
size_mult(stride, num_strips), val){}
118 template<
typename IntTypeA,
typename IntTypeB>
119 Data2D(IntTypeA new_stride, IntTypeB new_num_strips, std::vector<T> &&
data) : stride(static_cast<size_t>(new_stride)), num_strips(static_cast<size_t>(new_num_strips)),
120 vec(std::forward<std::vector<T>>(
data)){}
125 template<
bool iomode, IO::IOPad pad>
126 void writeVector(std::ostream &os)
const{ IO::writeVector<iomode, pad, T>(vec, os); }
129 bool empty()
const{
return (num_strips == 0); }
135 result.num_strips = num_strips;
143 T
const*
getStrip(
int i)
const{
return &(vec[i*stride]); }
153 T*
data(){
return vec.data(); }
155 T
const*
data()
const{
return vec.data(); }
160 vec = std::vector<double>();
164 inline typename std::vector<T>
release(){
return std::move(vec); }
167 inline typename std::vector<T>::iterator
begin(){
return vec.begin(); }
169 inline typename std::vector<T>::const_iterator
begin()
const{
return vec.cbegin(); }
171 inline typename std::vector<T>::iterator
end(){
return vec.end(); }
173 inline typename std::vector<T>::const_iterator
end()
const{
return vec.cend(); }
176 inline typename std::vector<T>::reverse_iterator
rbegin(){
return vec.rbegin(); }
179 void appendStrip(
typename std::vector<T>::const_iterator
const &x){
180 vec.insert(vec.end(), x, x + stride);
191 vec.insert(vec.begin() +
static_cast<size_t>(pos) * stride, x.begin(), x.end());
197 vec.insert(vec.end(), other.vec.begin(), other.vec.end());
198 num_strips += other.num_strips;
202 size_t stride, num_strips;
214 template<
typename iomode,
typename DataType,
typename IndexStr
ide,
typename IndexNumStrips>
240 num_dimensions(cnum_dimensions), cache_num_indexes((int)(new_indexes.size() / cnum_dimensions)),
241 indexes(std::move(new_indexes)){}
246 num_dimensions((size_t) IO::
readNumber<iomode, int>(is)),
247 cache_num_indexes(IO::
readNumber<iomode, int>(is)),
248 indexes(IO::
readVector<iomode, int>(is, Utils::
size_mult(num_dimensions, cache_num_indexes)))
257 template<
bool useAscii>
void write(std::ostream &os)
const;
260 inline bool empty()
const{
return indexes.empty(); }
277 inline std::vector<int>::const_iterator
begin()
const{
return indexes.cbegin(); }
279 inline std::vector<int>::const_iterator
end()
const{
return indexes.cend(); }
281 inline size_t totalSize()
const{
return indexes.size(); }
284 inline std::vector<int>
release(){
return std::move(indexes); }
291 inline bool missing(
const std::vector<int> &p)
const{
return (
getSlot(p.data()) == -1); }
294 inline const int *
getIndex(
int i)
const{
return &(indexes[
static_cast<size_t>(i) * num_dimensions]); }
298 return std::vector<int>(&indexes[
static_cast<size_t>(i) * num_dimensions], &indexes[
static_cast<size_t>(i) * num_dimensions] + num_dimensions);
313 int getMaxIndex()
const{
return (
empty()) ? 0 : *std::max_element(indexes.begin(), indexes.end()); }
316 size_t num_dimensions;
317 int cache_num_indexes;
318 std::vector<int> indexes;
339 StorageSet(
int cnum_outputs,
int cnum_values, std::vector<double> &&vals) :
340 num_outputs(cnum_outputs), num_values(cnum_values), values(std::move(vals)){}
342 template<
typename iomode>
StorageSet(std::istream &is, iomode) :
343 num_outputs((size_t) IO::
readNumber<iomode, int>(is)),
344 num_values((size_t) IO::
readNumber<iomode, int>(is)),
345 values((IO::
readFlag<iomode>(is)) ? IO::
readVector<iomode, double>(is, Utils::
size_mult(num_outputs, num_values)) : std::vector<double>())
356 template<
bool useAscii>
void write(std::ostream &os)
const;
359 void resize(
int cnum_outputs,
int cnum_values){
360 num_outputs = (size_t) cnum_outputs;
361 num_values = (size_t) cnum_values;
362 values = std::vector<double>();
369 double const*
getValues(
int i)
const{
return &(values[i*num_outputs]); }
371 double*
getValues(
int i){
return &(values[i*num_outputs]); }
374 void setValues(
const double vals[]){ values = std::vector<double>(vals, vals + num_outputs * num_values); }
377 num_values = vals.size() / num_outputs;
378 values = std::move(vals);
383 return {iend - ibegin, (int) num_values,
spltVector2D(values, num_outputs, ibegin, iend)};
387 inline std::vector<double>::const_iterator
begin()
const{
return values.cbegin(); }
389 inline std::vector<double>::const_iterator
end()
const{
return values.cend(); }
392 inline std::vector<double>
release(){
return std::move(values); }
407 size_t num_outputs, num_values;
408 std::vector<double> values;
Generic 2D data structure divided into contiguous strips of fixed length (similar to a matrix).
Definition: tsgIndexSets.hpp:104
bool empty() const
Returns true if the number of strips is zero.
Definition: tsgIndexSets.hpp:129
Data2D()
Default constructor makes an empty data-structure.
Definition: tsgIndexSets.hpp:107
std::vector< T >::iterator getIStrip(int i)
Return iterator set at the i-th strip.
Definition: tsgIndexSets.hpp:145
T const * data() const
Returns a const reference to the internal data.
Definition: tsgIndexSets.hpp:155
Data2D(IntTypeA new_stride, IntTypeB new_num_strips, T val)
Create data-structure with given stride and number of strips and initializes with val.
Definition: tsgIndexSets.hpp:114
Data2D(IntTypeA new_stride, IntTypeB new_num_strips)
Create data-structure with given stride and number of strips.
Definition: tsgIndexSets.hpp:110
std::vector< T > release()
Moves the data vector out of the class, this method invalidates the object.
Definition: tsgIndexSets.hpp:164
std::vector< T >::const_iterator end() const
Returns a const iterator to the end of the internal data.
Definition: tsgIndexSets.hpp:173
std::vector< T >::reverse_iterator rbegin()
Returns a reverse iterator to the end of the internal data.
Definition: tsgIndexSets.hpp:176
void writeVector(std::ostream &os) const
Write the internal vector to a stream.
Definition: tsgIndexSets.hpp:126
std::vector< T >::iterator begin()
Returns an iterator to the beginning of the internal data.
Definition: tsgIndexSets.hpp:167
void appendStrip(typename std::vector< T >::const_iterator const &x)
Uses std::vector::insert to append the data.
Definition: tsgIndexSets.hpp:179
Data2D< T > splitData(int ibegin, int iend) const
Get the data between ibegin and iend of each strip.
Definition: tsgIndexSets.hpp:132
size_t getTotalEntries() const
Returns the total number of entries, stride times number of trips.
Definition: tsgIndexSets.hpp:151
void append(Data2D< T > const &other)
Uses std::vector::insert to append all the data from the other to this.
Definition: tsgIndexSets.hpp:196
T * data()
Returns a reference to the internal data.
Definition: tsgIndexSets.hpp:153
void clear()
Clear all used data.
Definition: tsgIndexSets.hpp:157
std::vector< T >::const_iterator begin() const
Returns a const iterator to the beginning of the internal data.
Definition: tsgIndexSets.hpp:169
~Data2D()=default
Default destructor.
std::vector< T >::iterator end()
Returns an iterator to the end of the internal data.
Definition: tsgIndexSets.hpp:171
int getNumStrips() const
Returns the number of strips.
Definition: tsgIndexSets.hpp:149
void appendStrip(int pos, const std::vector< T > &x)
Uses std::vector::insert to append a strip x to the existing data at position pos,...
Definition: tsgIndexSets.hpp:190
Data2D(IntTypeA new_stride, IntTypeB new_num_strips, std::vector< T > &&data)
Create data-structure with given stride and number of strips and moves data into the internal vector.
Definition: tsgIndexSets.hpp:119
void appendStrip(const std::vector< T > &x)
Uses std::vector::insert to append x, assumes x.size() is one stride.
Definition: tsgIndexSets.hpp:185
size_t getStride() const
Returns the stride.
Definition: tsgIndexSets.hpp:147
T * getStrip(int i)
Returns a reference to the i-th strip.
Definition: tsgIndexSets.hpp:141
T const * getStrip(int i) const
Returns a const reference to the i-th strip.
Definition: tsgIndexSets.hpp:143
Class that stores multi-indexes in sorted (lexicographical) order.
Definition: tsgIndexSets.hpp:234
MultiIndexSet(Data2D< int > const &data)
Copy a collection of unsorted indexes into a sorted multi-index set, sorts during the copy.
MultiIndexSet(size_t cnum_dimensions, std::vector< int > &&new_indexes)
Constructor, makes a set by moving out of the vector, the vector must be already sorted.
Definition: tsgIndexSets.hpp:239
MultiIndexSet(std::istream &is, iomode)
Read from stream constructor.
Definition: tsgIndexSets.hpp:245
size_t getNumDimensions() const
Returns the number of dimensions.
Definition: tsgIndexSets.hpp:263
int getSlot(const int *p) const
Returns the slot containing index p, returns -1 if not found.
int getSlot(const std::vector< int > &p) const
Returns the slot containing index p, returns -1 if not found.
Definition: tsgIndexSets.hpp:289
int getMaxIndex() const
Returns the maximum single index in the set.
Definition: tsgIndexSets.hpp:313
std::vector< int > release()
Moves the index vector out of the class, this method invalidates the object.
Definition: tsgIndexSets.hpp:284
bool missing(const std::vector< int > &p) const
Returns true if p is missing from the set, false otherwise.
Definition: tsgIndexSets.hpp:291
const int * getIndex(int i) const
Returns the i-th index of the set, useful to loop over all indexes or to cross reference with values.
Definition: tsgIndexSets.hpp:294
size_t totalSize() const
Returns the number of dimensions times the number of indexes.
Definition: tsgIndexSets.hpp:281
int getNumIndexes() const
Returns the number of indexes.
Definition: tsgIndexSets.hpp:265
MultiIndexSet & operator+=(MultiIndexSet const &addition)
If empty, copy addition, otherwise merge the indexes of addition into this set, i....
Definition: tsgIndexSets.hpp:270
~MultiIndexSet()=default
Default destructor.
MultiIndexSet()
Default constructor, makes an empty set.
Definition: tsgIndexSets.hpp:237
bool empty() const
Returns true if there are no multi-indexes in the set, false otherwise.
Definition: tsgIndexSets.hpp:260
std::vector< int > copyIndex(int i) const
Returns a copy of the i-th index of the set.
Definition: tsgIndexSets.hpp:297
std::vector< int >::const_iterator begin() const
Returns a const iterator to the beginning of the internal data.
Definition: tsgIndexSets.hpp:277
std::vector< int >::const_iterator end() const
Returns a const iterator to the end of the internal data.
Definition: tsgIndexSets.hpp:279
void addSortedIndexes(std::vector< int > const &addition)
Add more indexes to a non-empty set, addition must be sorted and the set must be initialized.
MultiIndexSet operator-(const MultiIndexSet &substract) const
Return a new multi-index set that holds the indexed present in this set, but missing in substract.
void write(std::ostream &os) const
Write the set to ASCII or binary stream, use with std::ofstream and std::ifstream.
void removeIndex(const std::vector< int > &p)
Removes p from the set (if exists).
Class that stores values, i.e., model outputs, the order of the values is in sync with the order of s...
Definition: tsgIndexSets.hpp:334
StorageSet splitValues(int ibegin, int iend) const
Return a StorageSet with values between ibegin and iend.
Definition: tsgIndexSets.hpp:382
size_t getNumOutputs() const
Returns the number of outputs.
Definition: tsgIndexSets.hpp:366
void write(std::ostream &os) const
Write the set to ASCII or binary stream, use with std::ofstream and std::ifstream.
std::vector< double >::const_iterator end() const
Returns a const iterator to the end of the internal data.
Definition: tsgIndexSets.hpp:389
StorageSet(int cnum_outputs, int cnum_values, std::vector< double > &&vals)
Move constructor from a known vector.
Definition: tsgIndexSets.hpp:339
double const * getValues(int i) const
Returns const reference to the i-th value.
Definition: tsgIndexSets.hpp:369
void setValues(std::vector< double > &&vals)
Replace the existing values with vals using move semantics, the size of vals must be num_outputs time...
Definition: tsgIndexSets.hpp:376
void setValues(const double vals[])
Replace the existing values with a copy of vals, the size must be at least num_outputs times num_valu...
Definition: tsgIndexSets.hpp:374
void addValues(const MultiIndexSet &old_set, const MultiIndexSet &new_set, const double new_vals[])
Add more values to the set, the old_set and new_set are the associated multi-index sets required to m...
void resize(int cnum_outputs, int cnum_values)
Clear the existing values and assigns new dimensions, does not allocate memory for the new values.
Definition: tsgIndexSets.hpp:359
~StorageSet()=default
Default destructor.
StorageSet(std::istream &is, iomode)
Read constructor.
Definition: tsgIndexSets.hpp:342
std::vector< double >::const_iterator begin() const
Returns a const iterator to the beginning of the internal data.
Definition: tsgIndexSets.hpp:387
double * getValues(int i)
Returns reference to the i-th value.
Definition: tsgIndexSets.hpp:371
std::vector< double > release()
Moves the values vector out of the class, this method invalidates the object.
Definition: tsgIndexSets.hpp:392
StorageSet()
Default constructor, makes an empty set.
Definition: tsgIndexSets.hpp:337
Val readNumber(std::istream &is)
Read a single number, used to read ints (and potentially cast to size_t) or read a double.
Definition: tsgIOHelpers.hpp:393
bool readFlag(std::istream &os)
Read a flag, ascii uses 0 and 1, binary uses characters y and n (counter intuitive,...
Definition: tsgIOHelpers.hpp:321
Data2D< DataType > readData2D(std::istream &is, IndexStride stride, IndexNumStrips num_strips)
Read the Data2D structure from the stream, assumes the given number of strips and stride.
Definition: tsgIndexSets.hpp:215
void readVector(std::istream &is, std::vector< VecType > &x)
Read the vector from the stream, the size must already be set.
Definition: tsgIOHelpers.hpp:359
std::vector< T > spltVector2D(std::vector< T > const &x, size_t stride, int ibegin, int iend)
Take a vector logically organized into stips and strides and extract a sub-strip from every strip.
Definition: tsgIndexSets.hpp:77
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition: tsgUtils.hpp:82
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Templates to simply file I/O.