Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
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() : 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
201private:
202 size_t stride, num_strips;
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() : 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)){}
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 {}
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
315private:
316 size_t num_dimensions;
317 int cache_num_indexes;
318 std::vector<int> indexes;
319};
320
334class StorageSet{
335public:
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
406private:
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
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68