Doxygen 1.9.1
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2 (development)
tsgIndexSets.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Miroslav Stoyanov
3  *
4  * This file is part of
5  * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
12  * and the following disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND IMPLIED.
25  * THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY PATENT,
26  * COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE.
27  * THE USER ASSUMES RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING FROM OR ARISING OUT OF,
28  * IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
29  */
30 
31 #ifndef __TASMANIAN_SPARSE_GRID_INDEX_SETS_HPP
32 #define __TASMANIAN_SPARSE_GRID_INDEX_SETS_HPP
33 
34 #include "tsgIOHelpers.hpp"
35 
63 namespace TasGrid{
64 
76 template<typename T>
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);
82  auto ix = x.begin();
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);
88  }
89  return result;
90 }
91 
103 template<typename T>
104 class Data2D{
105 public:
107  Data2D() : stride(0), num_strips(0){}
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)){}
122  ~Data2D() = default;
123 
125  template<bool iomode, IO::IOPad pad>
126  void writeVector(std::ostream &os) const{ IO::writeVector<iomode, pad, T>(vec, os); }
127 
129  bool empty() const{ return (num_strips == 0); }
130 
132  Data2D<T> splitData(int ibegin, int iend) const{
133  if (stride == 0) return Data2D<T>(); // empty data object
134  Data2D<T> result(iend - ibegin, 0);
135  result.num_strips = num_strips;
136  result.vec = spltVector2D(vec, stride, ibegin, iend);
137  return result;
138  }
139 
141  T* getStrip(int i){ return &(vec[i*stride]); }
143  T const* getStrip(int i) const{ return &(vec[i*stride]); }
145  typename std::vector<T>::iterator getIStrip(int i){ return vec.begin() + Utils::size_mult(stride, i); }
147  size_t getStride() const{ return stride; }
149  int getNumStrips() const{ return (int) num_strips; }
151  size_t getTotalEntries() const{ return vec.size(); }
153  T* data(){ return vec.data(); }
155  T const* data() const{ return vec.data(); }
157  void clear(){
158  stride = 0;
159  num_strips = 0;
160  vec = std::vector<double>();
161  }
162 
164  inline typename std::vector<T> release(){ return std::move(vec); }
165 
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(); }
174 
176  inline typename std::vector<T>::reverse_iterator rbegin(){ return vec.rbegin(); }
177 
179  void appendStrip(typename std::vector<T>::const_iterator const &x){
180  vec.insert(vec.end(), x, x + stride);
181  num_strips++;
182  }
183 
185  void appendStrip(const std::vector<T> &x){
186  appendStrip(x.begin());
187  }
188 
190  void appendStrip(int pos, const std::vector<T> &x){
191  vec.insert(vec.begin() + static_cast<size_t>(pos) * stride, x.begin(), x.end());
192  num_strips++;
193  }
194 
196  void append(Data2D<T> const &other) {
197  vec.insert(vec.end(), other.vec.begin(), other.vec.end());
198  num_strips += other.num_strips;
199  }
200 
201 private:
202  size_t stride, num_strips;
203  std::vector<T> vec;
204 };
205 
206 namespace IO{
214  template<typename iomode, typename DataType, typename IndexStride, typename IndexNumStrips>
215  Data2D<DataType> readData2D(std::istream &is, IndexStride stride, IndexNumStrips num_strips){
216  return Data2D<DataType>(stride, num_strips, readVector<iomode, DataType>(is, Utils::size_mult(stride, num_strips)));
217  }
218 }
219 
235 public:
237  MultiIndexSet() : num_dimensions(0), cache_num_indexes(0){}
239  MultiIndexSet(size_t cnum_dimensions, std::vector<int> &&new_indexes) :
240  num_dimensions(cnum_dimensions), cache_num_indexes((int)(new_indexes.size() / cnum_dimensions)),
241  indexes(std::move(new_indexes)){}
245  template<typename iomode> MultiIndexSet(std::istream &is, iomode) :
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)))
249  {}
251  ~MultiIndexSet() = default;
252 
254 
257  template<bool useAscii> void write(std::ostream &os) const;
258 
260  inline bool empty() const{ return indexes.empty(); }
261 
263  inline size_t getNumDimensions() const{ return num_dimensions; }
265  inline int getNumIndexes() const{ return cache_num_indexes; }
266 
268  void addSortedIndexes(std::vector<int> const &addition);
270  inline MultiIndexSet& operator += (MultiIndexSet const &addition){
271  num_dimensions = addition.getNumDimensions();
272  addSortedIndexes(addition.indexes);
273  return *this;
274  }
275 
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(); }
282 
284  inline std::vector<int> release(){ return std::move(indexes); }
285 
287  int getSlot(const int *p) const;
289  inline int getSlot(const std::vector<int> &p) const{ return getSlot(p.data()); }
291  inline bool missing(const std::vector<int> &p) const{ return (getSlot(p.data()) == -1); }
292 
294  inline const int *getIndex(int i) const{ return &(indexes[static_cast<size_t>(i) * num_dimensions]); }
295 
297  inline std::vector<int> copyIndex(int i) const{
298  return std::vector<int>(&indexes[static_cast<size_t>(i) * num_dimensions], &indexes[static_cast<size_t>(i) * num_dimensions] + num_dimensions);
299  }
300 
307  MultiIndexSet operator -(const MultiIndexSet &substract) const;
308 
310  void removeIndex(const std::vector<int> &p);
311 
313  int getMaxIndex() const{ return (empty()) ? 0 : *std::max_element(indexes.begin(), indexes.end()); }
314 
315 private:
316  size_t num_dimensions;
317  int cache_num_indexes;
318  std::vector<int> indexes;
319 };
320 
335 public:
337  StorageSet() : num_outputs(0), num_values(0){}
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>())
346  {}
348  ~StorageSet() = default;
349 
356  template<bool useAscii> void write(std::ostream &os) const;
357 
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>();
363  }
364 
366  size_t getNumOutputs() const{ return num_outputs; }
367 
369  double const* getValues(int i) const{ return &(values[i*num_outputs]); }
371  double* getValues(int i){ return &(values[i*num_outputs]); }
372 
374  void setValues(const double vals[]){ values = std::vector<double>(vals, vals + num_outputs * num_values); }
376  void setValues(std::vector<double> &&vals){
377  num_values = vals.size() / num_outputs;
378  values = std::move(vals); // move assignment
379  }
380 
382  StorageSet splitValues(int ibegin, int iend) const{
383  return {iend - ibegin, (int) num_values, spltVector2D(values, num_outputs, ibegin, iend)};
384  }
385 
387  inline std::vector<double>::const_iterator begin() const{ return values.cbegin(); }
389  inline std::vector<double>::const_iterator end() const{ return values.cend(); }
390 
392  inline std::vector<double> release(){ return std::move(values); }
393 
404  void addValues(const MultiIndexSet &old_set, const MultiIndexSet &new_set, const double new_vals[]);
405 
406 private:
407  size_t num_outputs, num_values; // kept as size_t to avoid conversions in products, but each one is small individually
408  std::vector<double> values;
409 };
410 
411 }
412 
413 #endif
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.