Doxygen 1.17.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.3
Loading...
Searching...
No Matches
tsgIndexSets.hpp
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
49
62
63namespace TasGrid{
64
76template<typename T>
77std::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
103template<typename T>
104class Data2D{
105public:
107 Data2D() = default;
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)){}
121
123 template<bool iomode, IO::IOPad pad>
124 void writeVector(std::ostream &os) const{ IO::writeVector<iomode, pad, T>(vec, os); }
125
127 bool empty() const{ return (num_strips == 0); }
128
130 Data2D<T> splitData(int ibegin, int iend) const{
131 if (stride == 0) return Data2D<T>(); // empty data object
132 Data2D<T> result(iend - ibegin, 0);
133 result.num_strips = num_strips;
134 result.vec = spltVector2D(vec, stride, ibegin, iend);
135 return result;
136 }
137
139 T* getStrip(int i){ return &(vec[i*stride]); }
141 T const* getStrip(int i) const{ return &(vec[i*stride]); }
143 typename std::vector<T>::iterator getIStrip(int i){ return vec.begin() + Utils::size_mult(stride, i); }
145 size_t getStride() const{ return stride; }
147 int getNumStrips() const{ return (int) num_strips; }
149 size_t getTotalEntries() const{ return vec.size(); }
151 T* data(){ return vec.data(); }
153 T const* data() const{ return vec.data(); }
155 size_t totalSize() const{ return vec.size(); }
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
201private:
202 size_t stride = 0, num_strips = 0;
203 std::vector<T> vec;
204};
205
206namespace 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
234class MultiIndexSet{
235public:
237 MultiIndexSet() = default;
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)){}
243 MultiIndexSet(Data2D<int> const &data);
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 {}
250
252
255 template<bool useAscii> void write(std::ostream &os) const;
256
258 inline bool empty() const{ return indexes.empty(); }
259
261 inline size_t getNumDimensions() const{ return num_dimensions; }
263 inline int getNumIndexes() const{ return cache_num_indexes; }
264
266 void addSortedIndexes(std::vector<int> const &addition);
268 inline MultiIndexSet& operator += (MultiIndexSet const &addition){
269 num_dimensions = addition.getNumDimensions();
270 addSortedIndexes(addition.indexes);
271 return *this;
272 }
273
275 inline std::vector<int>::const_iterator begin() const{ return indexes.cbegin(); }
277 inline std::vector<int>::const_iterator end() const{ return indexes.cend(); }
279 int* data(){ return indexes.data(); }
281 int const* data() const{ return indexes.data(); }
283 inline size_t totalSize() const{ return indexes.size(); }
284
286 inline std::vector<int> release(){ return std::move(indexes); }
287
289 int getSlot(const int *p) const;
291 inline int getSlot(const std::vector<int> &p) const{ return getSlot(p.data()); }
293 inline bool missing(const std::vector<int> &p) const{ return (getSlot(p.data()) == -1); }
294
296 inline const int *getIndex(int i) const{ return &(indexes[static_cast<size_t>(i) * num_dimensions]); }
297
299 inline std::vector<int> copyIndex(int i) const{
300 return std::vector<int>(&indexes[static_cast<size_t>(i) * num_dimensions], &indexes[static_cast<size_t>(i) * num_dimensions] + num_dimensions);
301 }
302
309 MultiIndexSet operator -(const MultiIndexSet &substract) const;
310
312 void removeIndex(const std::vector<int> &p);
313
315 int getMaxIndex() const{ return (empty()) ? 0 : *std::max_element(indexes.begin(), indexes.end()); }
316
317private:
318 size_t num_dimensions = 0;
319 int cache_num_indexes = 0;
320 std::vector<int> indexes;
321};
322
336class StorageSet{
337public:
339 StorageSet() = default;
341 StorageSet(int cnum_outputs, int cnum_values, std::vector<double> &&vals) :
342 num_outputs(cnum_outputs), num_values(cnum_values), values(std::move(vals)){}
344 template<typename iomode> StorageSet(std::istream &is, iomode) :
345 num_outputs((size_t) IO::readNumber<iomode, int>(is)),
346 num_values((size_t) IO::readNumber<iomode, int>(is)),
347 values((IO::readFlag<iomode>(is)) ? IO::readVector<iomode, double>(is, Utils::size_mult(num_outputs, num_values)) : std::vector<double>())
348 {}
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
407 double* data(){ return values.data(); }
409 double const* data() const{ return values.data(); }
411 size_t totalSize() const{ return values.size(); }
412
413private:
414 size_t num_outputs = 0, num_values = 0; // kept as size_t to avoid conversions in products, but each one is small individually
415 std::vector<double> values;
416};
417
418}
419
420#endif
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68