Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 11.6% 50 / 0 / 430
Functions: 5.9% 9 / 2 / 154
Branches: 1.4% 13 / 0 / 906

OMCompiler/SimulationRuntime/cpp/Core/Math/Array.h
Line Branch Exec Source
1 /*
2 * This file belongs to the OpenModelica Run-Time System
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings
5 * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights
6 * reserved.
7 *
8 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
9 * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY
10 * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
11 * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL
12 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
13 *
14 * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License
15 * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs:
16 * http://www.openmodelica.org or https://github.com/OpenModelica/ or
17 * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU
18 * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW
19 * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause.
20 *
21 * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY
23 * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF
24 * OSMC-PL.
25 *
26 */
27
28 #pragma once
29 /** @defgroup math Core.Math
30 * Module for array operations and math functions
31 * @{
32 */
33
34 /**
35 * Operator class to assign simvar memory to a reference array
36 */
37 template<typename T>
38 struct CArray2RefArray
39 {
40 T* operator()(T& val)
41 {
42 return &val;
43 }
44 };
45
46 /**
47 * Operator class to assign simvar memory to a c array
48 * used in getDataCopy methods:
49 * double data[4];
50 * A.getDataCopy(data,4)
51 */
52 template<typename T>
53 struct RefArray2CArray
54 {
55 const T& operator()(const T* val) const
56 {
57 return *val;
58 }
59 };
60 /**
61 * Operator class to copy an c -array to a reference array
62 */
63 template<typename T>
64 struct CopyCArray2RefArray
65 {
66 /**
67 assign value to simvar
68 @param val simvar
69 @param val2 value
70 */
71 T* operator()(T* val,const T& val2)
72 {
73 *val=val2;
74 return val;
75 }
76 };
77
78 /**
79 * Operator class to copy the values of a reference array to a reference array
80 */
81 template<typename T>
82 struct CopyRefArray2RefArray
83 {
84 T* operator()(T* val, const T* val2)
85 {
86 *val = *val2;
87 return val;
88 }
89 };
90
91 /**
92 * Base class for all dynamic and static arrays
93 */
94 template<typename T> class BaseArray
95 {
96 public:
97 2 BaseArray(bool isStatic, bool isRefArray)
98 206 :_isStatic(isStatic)
99 206 ,_isRefArray(isRefArray)
100 {}
101
102 virtual ~BaseArray() {};
103
104 /**
105 * Interface methods for all arrays
106 */
107 virtual const T& operator()(const vector<size_t>& idx) const = 0;
108 virtual T& operator()(const vector<size_t>& idx) = 0;
109 virtual void assign(const T* data) = 0;
110 virtual void assign(const BaseArray<T>& b) = 0;
111 virtual void assign(const T& value) = 0; // fill value
112 virtual std::vector<size_t> getDims() const = 0;
113 virtual int getDim(size_t dim) const = 0; // { (int)getDims()[dim - 1]; }
114 virtual size_t getNumElems() const = 0;
115 virtual size_t getNumDims() const = 0;
116 virtual void setDims(const std::vector<size_t>& v) = 0;
117 virtual void resize(const std::vector<size_t>& dims) = 0;
118 virtual const T* getData() const = 0;
119 virtual T* getData() = 0;
120 virtual void getDataCopy(T data[], size_t n) const = 0;
121 ✗ virtual const T* const* getDataRefs() const
122 {
123 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array getDataRefs call");
124 }
125
126 ✗ virtual T& operator()(size_t i)
127 {
128 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
129 }
130
131 ✗ virtual const T& operator()(size_t i) const
132 {
133 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
134 }
135
136 ✗ virtual T& operator()(size_t i, size_t j)
137 {
138 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
139 }
140
141 ✗ virtual const T& operator()(size_t i, size_t j) const
142 {
143 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
144 }
145
146 ✗ virtual T& operator()(size_t i, size_t j, size_t k)
147 {
148 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
149 }
150
151 ✗ virtual const T& operator()(size_t i, size_t j, size_t k) const
152 {
153 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
154 }
155
156 ✗ virtual T& operator()(size_t i, size_t j, size_t k, size_t l)
157 {
158 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
159 }
160
161 ✗ virtual const T& operator()(size_t i, size_t j, size_t k, size_t l) const
162 {
163 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
164 }
165
166 ✗ virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m)
167 {
168 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
169 }
170
171 ✗ virtual const T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
172 {
173 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
174 }
175
176 ✗ virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n)
177 {
178 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
179 }
180
181 ✗ virtual const T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
182 {
183 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong virtual Array operator call");
184 }
185
186 bool isStatic() const
187 {
188 ✗ return _isStatic;
189 }
190
191 bool isRefArray() const
192 {
193 return _isRefArray;
194 }
195
196 protected:
197 bool _isStatic;
198 bool _isRefArray;
199 };
200
201 /**
202 * Add a const qualifier to force use of const methods
203 */
204 template <typename T>
205 const T& ConstArray(T& arr)
206 {
207 return arr;
208 }
209
210 /**
211 * Wrapper to convert a string array to c_str array
212 */
213 class CStrArray
214 {
215 public:
216 /**
217 * Constructor storing pointers
218 */
219 CStrArray(const BaseArray<string>& stringArray)
220 :_c_str_array(stringArray.getNumElems())
221 {
222 const string *data = stringArray.getData();
223 for(size_t i = 0; i < _c_str_array.size(); i++)
224 _c_str_array[i] = data[i].c_str();
225 }
226
227 /**
228 * Convert to c_str array
229 */
230 operator const char**()
231 {
232 return &_c_str_array[0];
233 }
234
235 /**
236 * Write back to string array and free c strings if allocated
237 */
238 void writeBack(BaseArray<string>& stringArray)
239 {
240 string *data = stringArray.getData();
241 for(size_t i = 0; i < _c_str_array.size(); i++) {
242 data[i] = _c_str_array[i];
243 _ModelicaFreeStringIfAllocated(_c_str_array[i]);
244 }
245 }
246
247 private:
248 vector<const char *> _c_str_array;
249 };
250
251 /**
252 * Base class for array of references to externally stored elements
253 * @param T type of the array
254 * @param nelems number of elements of array
255 */
256 template<typename T, std::size_t nelems>
257 class RefArray : public BaseArray<T>
258 {
259 public:
260 /**
261 * Constuctor for reference array
262 * it uses data from simvars memory
263 */
264 RefArray(T* data)
265 :BaseArray<T>(true, true)
266 {
267 std::transform(data, data + nelems,
268 _ref_array, CArray2RefArray<T>());
269 }
270
271 /**
272 * Constuctor for reference array
273 * intialize array with reference data from simvars memory
274 */
275 RefArray(T* const* ref_data)
276 :BaseArray<T>(true, true)
277 {
278 if (nelems > 0)
279 std::copy(ref_data, ref_data + nelems, _ref_array);
280 }
281
282 /**
283 * Default constuctor for reference array
284 * empty array
285 */
286 RefArray()
287 :BaseArray<T>(true, true)
288 {
289 }
290
291 virtual ~RefArray() {}
292
293 /**
294 * Assigns data to array
295 * @param data new array data
296 * a.assign(data)
297 */
298 virtual void assign(const T* data)
299 {
300 std::transform(_ref_array, _ref_array + nelems, data,
301 _ref_array, CopyCArray2RefArray<T>());
302 }
303
304 /**
305 * Assigns array data to array
306 * @param b any array of type BaseArray
307 * a.assign(b)
308 */
309 virtual void assign(const BaseArray<T>& b)
310 {
311 if(b.isRefArray())
312 std::transform(_ref_array, _ref_array + nelems, b.getDataRefs(),
313 _ref_array, CopyRefArray2RefArray<T>());
314 else
315 std::transform(_ref_array, _ref_array + nelems, b.getData(),
316 _ref_array, CopyCArray2RefArray<T>());
317 }
318
319 /**
320 * Assigns value reference to each array element
321 * @param value new value for each element
322 * a.assign(value)
323 */
324 virtual void assign(const T& value)
325 {
326 for (size_t i = 0; i < nelems; i++)
327 *_ref_array[i] = value;
328 }
329
330 /**
331 * Access to data (read-only)
332 */
333 virtual const T* getData() const
334 {
335 std::transform(_ref_array, _ref_array + nelems, _tmp_data, RefArray2CArray<T>());
336 return _tmp_data;
337 }
338
339 /**
340 * Access to c-array data
341 */
342 virtual T* getData()
343 {
344 throw std::runtime_error("Access data of reference array is not supported");
345 }
346
347 /**
348 * Copies the array data of size n in the data array
349 * data has to be allocated before getDataCopy is called
350 */
351 virtual void getDataCopy(T data[], size_t n) const
352 {
353 assert(n <= nelems);
354 std::transform(_ref_array, _ref_array + n, data, RefArray2CArray<T>());
355 }
356
357 /**
358 * Access to data references (read-only)
359 */
360 virtual const T* const* getDataRefs() const
361 {
362 return _ref_array;
363 }
364
365 /**
366 * Returns number of elements
367 */
368 virtual size_t getNumElems() const
369 {
370 return nelems;
371 }
372
373 virtual void setDims(const std::vector<size_t>& v) { }
374
375 /**
376 * Resize array method
377 * @param dims vector with new dimension sizes
378 * static array could not be resized
379 */
380 virtual void resize(const std::vector<size_t>& dims)
381 {
382 throw std::runtime_error("Resize reference array is not supported");
383 }
384
385 protected:
386 //reference array data
387 T* _ref_array[nelems == 0? 1: nelems];
388 mutable T _tmp_data[nelems == 0? 1: nelems]; // storage for const T* getData()
389 };
390
391 /**
392 * One dimensional static reference array, specializes RefArray
393 * @param T type of the array
394 * @param size dimension of array
395 */
396 template<typename T, std::size_t size>
397 class RefArrayDim1 : public RefArray<T, size>
398 {
399 public:
400 /**
401 * Constuctor for one dimensional reference array
402 * it uses data from simvars memory
403 */
404 RefArrayDim1(T* data) : RefArray<T, size>(data) {}
405
406 /**
407 * Constuctor for one dimensional reference array
408 * intialize array with reference data from simvars memory
409 */
410 RefArrayDim1(T* const* ref_data) : RefArray<T, size>(ref_data) {}
411
412 /**
413 * Default constuctor for one dimensional reference array
414 */
415 RefArrayDim1() : RefArray<T, size>() {}
416
417 virtual ~RefArrayDim1() {}
418
419 /**
420 * Index operator to read array element
421 * @param idx vector of indices
422 */
423 virtual const T& operator()(const vector<size_t>& idx) const
424 {
425 assert(size > (idx[0] - 1));
426 return *(RefArray<T, size>::_ref_array[idx[0]-1]);
427 }
428
429 /**
430 * Index operator to write array element
431 * @param idx vector of indices
432 */
433 virtual T& operator()(const vector<size_t>& idx)
434 {
435 assert(size > (idx[0] - 1));
436 return *(RefArray<T, size>::_ref_array[idx[0]-1]);
437 }
438
439 /**
440 * Index operator to access array element
441 * @param index index
442 */
443 inline virtual T& operator()(size_t index)
444 {
445 assert(size > (index - 1));
446 return *(RefArray<T, size>::_ref_array[index-1]);
447 }
448
449 /**
450 * Return sizes of dimensions
451 */
452 virtual std::vector<size_t> getDims() const
453 {
454 std::vector<size_t> v;
455 v.push_back(size);
456 return v;
457 }
458
459 /**
460 * Return size of one dimension
461 */
462 virtual int getDim(size_t dim) const
463 {
464 return (int)size;
465 }
466
467 /**
468 * Returns number of dimensions
469 */
470 virtual size_t getNumDims() const
471 {
472 return 1;
473 }
474 };
475
476 /**
477 * Two dimensional static reference array, specializes RefArray
478 * @param T type of the array
479 * @param size1 size of dimension one
480 * @param size2 size of dimension two
481 */
482 template<typename T, std::size_t size1, std::size_t size2>
483 class RefArrayDim2 : public RefArray<T, size1*size2>
484 {
485 public:
486 /**
487 * Constuctor for two dimensional reference array
488 * it uses data from simvars memory
489 */
490 RefArrayDim2(T* data) : RefArray<T, size1*size2>(data) {}
491
492 /**
493 * Constuctor for two dimensional reference array
494 * intialize array with reference data from simvars memory
495 */
496 RefArrayDim2(T* const* ref_data) : RefArray<T, size1*size2>(ref_data) {}
497
498 virtual ~RefArrayDim2() {}
499
500 /**
501 * Default constuctor for two dimensional reference array
502 */
503 RefArrayDim2() : RefArray<T, size1*size2>() {}
504
505 /**
506 * Index operator to read array element
507 * @param idx vector of indices
508 */
509 virtual const T& operator()(const vector<size_t>& idx) const
510 {
511 assert((size1*size2) > ((idx[0]-1) + size1*(idx[1]-1)));
512 return *(RefArray<T, size1*size2>::
513 _ref_array[(idx[0]-1) + size1*(idx[1]-1)]);
514 }
515
516 /**
517 * Index operator to write array element
518 * @param idx vector of indices
519 */
520 virtual T& operator()(const vector<size_t>& idx)
521 {
522 assert((size1*size2) > ((idx[0]-1) + size1*(idx[1]-1)));
523 return *(RefArray<T, size1*size2>::
524 _ref_array[(idx[0]-1) + size1*(idx[1]-1)]);
525 }
526
527 /**
528 * Index operator to access array element
529 * @param i index 1
530 * @param j index 2
531 */
532 inline virtual T& operator()(size_t i, size_t j)
533 {
534 assert((size1*size2) > ((i-1) + size1*(j-1)));
535 return *(RefArray<T, size1*size2>::
536 _ref_array[(i-1) + size1*(j-1)]);
537 }
538
539 /**
540 * Return sizes of dimensions
541 */
542 virtual std::vector<size_t> getDims() const
543 {
544 std::vector<size_t> v;
545 v.push_back(size1);
546 v.push_back(size2);
547 return v;
548 }
549
550 /**
551 * Return size of one dimension
552 */
553 virtual int getDim(size_t dim) const
554 {
555 switch (dim) {
556 case 1:
557 return (int)size1;
558 case 2:
559 return (int)size2;
560 default:
561 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
562 }
563 }
564
565 /**
566 * Return sizes of dimensions
567 */
568 virtual size_t getNumDims() const
569 {
570 return 2;
571 }
572 };
573
574 /**
575 * Three dimensional static reference array, specializes RefArray
576 * @param T type of the array
577 * @param size1 size of dimension one
578 * @param size2 size of dimension two
579 * @param size3 size of dimension two
580 */
581 template<typename T, std::size_t size1, std::size_t size2, std::size_t size3>
582 class RefArrayDim3 : public RefArray<T, size1*size2*size3>
583 {
584 public:
585 /**
586 * Constuctor for three dimensional reference array
587 * it uses data from simvars memory
588 */
589 RefArrayDim3(T* data) : RefArray<T, size1*size2*size3>(data) {}
590
591 /**
592 * Constuctor for three dimensional reference array
593 * intialize array with reference data from simvars memory
594 */
595 RefArrayDim3(T* const* ref_data) : RefArray<T, size1*size2*size3>(ref_data) {}
596
597 /**
598 * Default constuctor for three dimensional reference array
599 */
600 RefArrayDim3() : RefArray<T, size1*size2*size3>() {}
601
602 virtual ~RefArrayDim3() {}
603
604 /**
605 * Return sizes of dimensions
606 */
607 virtual std::vector<size_t> getDims() const
608 {
609 std::vector<size_t> v;
610 v.push_back(size1);
611 v.push_back(size2);
612 v.push_back(size3);
613 return v;
614 }
615
616 /**
617 * Return size of one dimension
618 */
619 virtual int getDim(size_t dim) const
620 {
621 switch (dim) {
622 case 1:
623 return (int)size1;
624 case 2:
625 return (int)size2;
626 case 3:
627 return (int)size3;
628 default:
629 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
630 }
631 }
632
633 /**
634 * Index operator to read array element
635 * @param idx vector of indices
636 */
637 virtual const T& operator()(const vector<size_t>& idx) const
638 {
639 assert(size1*size2*size3 > idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1)));
640 return *(RefArray<T, size1*size2*size3>::
641 _ref_array[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1))]);
642 }
643
644 /**
645 * Index operator to write array element
646 * @param idx vector of indices
647 */
648 virtual T& operator()(const vector<size_t>& idx)
649 {
650 assert(size1*size2*size3 > idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1)));
651 return *(RefArray<T, size1*size2*size3>::
652 _ref_array[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1))]);
653 }
654
655 /**
656 * Index operator to access array element
657 * @param i index 1
658 * @param j index 2
659 * @param k index 3
660 */
661 inline virtual T& operator()(size_t i, size_t j, size_t k)
662 {
663 assert(size1*size2*size3 > i-1 + size1*(j-1 + size2*(k-1)));
664 return *(RefArray<T, size1*size2*size3>::
665 _ref_array[i-1 + size1*(j-1 + size2*(k-1))]);
666 }
667
668 /**
669 * Return sizes of dimensions
670 */
671 virtual size_t getNumDims() const
672 {
673 return 3;
674 }
675 };
676
677 /**
678 * Wrap external data with nelems unknown at compile time into array, implements BaseArray interface methods
679 * @param T type of the array elements
680 */
681 template<typename T>
682 class WrapArray : public BaseArray<T>
683 {
684 public:
685 /**
686 * Constuctor for wrapper array storing a pointer
687 */
688 WrapArray(T* data, size_t nelems)
689 204 :BaseArray<T>(true, false)
690 {
691 204 _data = data;
692 204 _nelems = nelems;
693 }
694
695 /**
696 * Constuctor for wrapper array that
697 * holds a pointer to otherarray's data
698 */
699 WrapArray(const WrapArray<T>& otherarray)
700 :BaseArray<T>(true, false)
701 {
702 _data = otherarray._data;
703 _nelems = otherarray.getNumElems();
704 }
705
706 /**
707 * Default constuctor for wrapper array
708 */
709 WrapArray()
710 :BaseArray<T>(true, false)
711 {
712 _data = NULL; // no data assigned yet
713 _nelems = 0;
714 }
715
716 ✗ virtual ~WrapArray() {}
717
718 /**
719 * Index operator to read array element
720 * @param idx vector of indices
721 */
722 ✗ virtual const T& operator()(const vector<size_t>& idx) const
723 {
724 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong WrapArray const operator() call");
725 }
726
727 /**
728 * Index operator to write array element
729 * @param idx vector of indices
730 */
731 ✗ virtual T& operator()(const vector<size_t>& idx)
732 {
733 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong WrapArray operator() call");
734 }
735
736 /**
737 * Return sizes of dimensions
738 */
739 ✗ virtual std::vector<size_t> getDims() const
740 {
741 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong WrapArray getDims call");
742 }
743
744 /**
745 * Return sizes of one dimension
746 */
747 ✗ virtual int getDim(size_t dim) const
748 {
749 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong WrapArray getDim call");
750 }
751
752 /**
753 * Returns number of dimensions
754 */
755 ✗ virtual size_t getNumDims() const
756 {
757 ✗ throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong WrapArray getNumDims call");
758 }
759
760 /**
761 * Resize array method
762 * @param dims vector with new dimension sizes
763 * wrapper array could not be resized
764 */
765 ✗ virtual void resize(const std::vector<size_t>& dims)
766 {
767 ✗ if (dims != this->getDims())
768 ✗ throw std::runtime_error("Cannot resize wrapper array!");
769 ✗ }
770
771 /**
772 * Assigns data to array
773 * @param data new array data
774 * a.assign(data)
775 */
776 ✗ virtual void assign(const T* data)
777 {
778 ✗ if (_nelems > 0) {
779 ✗ if (_data == NULL)
780 ✗ throw std::runtime_error("Cannot assign data to uninitialized WrapArray!");
781 ✗ std::copy(data, data + _nelems, _data);
782 }
783 ✗ }
784
785 /**
786 * Assigns array data to array
787 * @param b any array of type BaseArray
788 * a.assign(b)
789 */
790 ✗ virtual void assign(const BaseArray<T>& b)
791 {
792 ✗ if (_nelems > 0) {
793 ✗ if (_data == NULL)
794 ✗ throw std::runtime_error("Cannot assign to uninitialized WrapArray!");
795 ✗ assert(b.getNumElems() == _nelems);
796 ✗ b.getDataCopy(_data, _nelems);
797 }
798 ✗ }
799
800 /**
801 * Assigns value to each array element
802 * @param value new array value
803 * a.assign(value)
804 */
805 ✗ virtual void assign(const T& value)
806 {
807 ✗ if (_nelems > 0) {
808 ✗ if (_data == NULL)
809 ✗ throw std::runtime_error("Cannot assign value to uninitialized WrapArray!");
810 ✗ std::fill(_data, _data + _nelems, value);
811 }
812 ✗ }
813
814 /**
815 * Access to data
816 */
817 ✗ virtual T* getData()
818 {
819 ✗ return _data;
820 }
821
822 /**
823 * Access to data (read-only)
824 */
825 202 virtual const T* getData() const
826 {
827 202 return _data;
828 }
829
830 /**
831 * Copies the array data of size n in the data array
832 * data has to be allocated before getDataCopy is called
833 */
834 ✗ virtual void getDataCopy(T data[], size_t n) const
835 {
836 ✗ if (n > 0)
837 ✗ std::copy(_data, _data + n, data);
838 ✗ }
839
840 /**
841 * Returns number of elements
842 */
843 ✗ virtual size_t getNumElems() const
844 {
845 ✗ return _nelems;
846 }
847
848 ✗ virtual void setDims(const std::vector<size_t>& v) {}
849
850 protected:
851 T *_data; // array data
852 size_t _nelems; // number of elements
853 };
854
855 /**
856 * Static array, implements BaseArray interface methods
857 * @param T type of the array
858 * @param nelems number of elements of array
859 * @param external indicates if the memory is provided externally
860 */
861 template<typename T, std::size_t nelems, bool external = false>
862 class StatArray : public BaseArray<T>
863 {
864 public:
865 /**
866 * Constuctor for static array
867 * if external it just stores a pointer
868 * else it copies data into array memory
869 */
870 StatArray(T* data)
871 :BaseArray<T>(true, false)
872 {
873 if (external)
874 _data = data;
875 else {
876 _data = _array;
877 if (nelems > 0)
878 std::copy(data, data + nelems, _data);
879 }
880 }
881
882 /**
883 * Constuctor for static array that
884 * copies data from otherarray in array memory
885 * or holds a pointer to otherarray's data
886 */
887 StatArray(const StatArray<T, nelems, true>& otherarray)
888 :BaseArray<T>(true, false)
889 {
890 if (external)
891 _data = otherarray._data;
892 else {
893 _data = _array;
894 otherarray.getDataCopy(_data, nelems);
895 }
896 }
897
898 /**
899 * Constuctor for static array that
900 * copies data from otherarray in array memory
901 * or holds a pointer to otherarray's data
902 */
903 StatArray(const StatArray<T, nelems, false>& otherarray)
904 :BaseArray<T>(true, false)
905 {
906 if (external)
907 _data = otherarray._data;
908 else {
909 _data = _array;
910 _array = otherarray._array;
911 }
912 }
913
914 /**
915 * Constuctor for static array that
916 * lets otherarray copy data into array memory
917 */
918 StatArray(const BaseArray<T>& otherarray)
919 :BaseArray<T>(true, false)
920 {
921 if (external)
922 throw std::runtime_error("Unsupported copy constructor of static array with external storage!");
923 _data = _array;
924 otherarray.getDataCopy(_data, nelems);
925 }
926
927 /**
928 * Default constuctor for static array
929 */
930 StatArray()
931 :BaseArray<T>(true, false)
932 {
933 if (external)
934 _data = NULL; // no data assigned yet
935 else
936 _data = _array;
937 }
938
939 virtual ~StatArray() {}
940
941 /**
942 * Initialize static array with external data.
943 * @param data new array data
944 * a.init(data)
945 */
946 void init(T* data)
947 {
948 if (external)
949 _data = data;
950 else
951 throw std::runtime_error("Invalid init of StatArray with own data!");
952 }
953
954 /**
955 * Resize array method
956 * @param dims vector with new dimension sizes
957 * static array could not be resized
958 */
959 virtual void resize(const std::vector<size_t>& dims)
960 {
961 if (dims != this->getDims())
962 throw std::runtime_error("Cannot resize static array!");
963 }
964
965 /**
966 * Assigns data to array
967 * @param data new array data
968 * a.assign(data)
969 */
970 virtual void assign(const T* data)
971 {
972 if (nelems > 0) {
973 if (_data == NULL)
974 throw std::runtime_error("Cannot assign data to uninitialized StatArray!");
975 std::copy(data, data + nelems, _data);
976 }
977 }
978
979 /**
980 * Assigns array data to array
981 * @param b any array of type BaseArray
982 * a.assign(b)
983 */
984 virtual void assign(const BaseArray<T>& b)
985 {
986 if (nelems > 0) {
987 if (_data == NULL)
988 throw std::runtime_error("Cannot assign to uninitialized StatArray!");
989 assert(b.getNumElems() == nelems);
990 b.getDataCopy(_data, nelems);
991 }
992 }
993
994 /**
995 * Assigns value to each array element
996 * @param value new array value
997 * a.assign(value)
998 */
999 virtual void assign(const T& value)
1000 {
1001 if (nelems > 0) {
1002 if (_data == NULL)
1003 throw std::runtime_error("Cannot assign value to uninitialized StatArray!");
1004 std::fill(_data, _data + nelems, value);
1005 }
1006 }
1007
1008 /**
1009 * Access to data
1010 */
1011 virtual T* getData()
1012 {
1013 return _data;
1014 }
1015
1016 /**
1017 * Access to data (read-only)
1018 */
1019 virtual const T* getData() const
1020 {
1021 return _data;
1022 }
1023
1024 /**
1025 * Copies the array data of size n in the data array
1026 * data has to be allocated before getDataCopy is called
1027 */
1028 virtual void getDataCopy(T data[], size_t n) const
1029 {
1030 if (n > 0)
1031 std::copy(_data, _data + n, data);
1032 }
1033
1034 /**
1035 * Returns number of elements
1036 */
1037 virtual size_t getNumElems() const
1038 {
1039 return nelems;
1040 }
1041
1042 virtual void setDims(const std::vector<size_t>& v) {}
1043
1044 protected:
1045 T _array[external || nelems == 0? 1: nelems]; // static array
1046 T *_data; // array data
1047 };
1048
1049 /**
1050 * One dimensional static array, specializes StatArray
1051 * @param T type of the array
1052 * @param size dimension of array
1053 * @param external indicates if the memory is provided externally
1054 */
1055 template<typename T, std::size_t size, bool external = false>
1056 class StatArrayDim1 : public StatArray<T, size, external>
1057 {
1058 public:
1059 /**
1060 * Constuctor for one dimensional array
1061 * if reference array it uses data from simvars memory
1062 * else it copies data in array memory
1063 */
1064 StatArrayDim1(T* data)
1065 :StatArray<T, size, external>(data) {}
1066
1067 /**
1068 * Constuctor for one dimensional array
1069 * copies data from otherarray in array memory
1070 * or holds a pointer to otherarray's data
1071 */
1072 StatArrayDim1(const StatArrayDim1<T, size, true>& otherarray)
1073 :StatArray<T, size, external>(otherarray)
1074 {
1075 }
1076
1077 /**
1078 * Constuctor for one dimensional array
1079 * copies data from otherarray in array memory
1080 * or holds a pointer to otherarray's data
1081 */
1082 StatArrayDim1(const StatArrayDim1<T, size, false>& otherarray)
1083 :StatArray<T, size, external>(otherarray)
1084 {
1085 }
1086
1087 /**
1088 * Constuctor for one dimensional array that
1089 * lets otherarray copy data into array memory
1090 */
1091 StatArrayDim1(const BaseArray<T>& otherarray)
1092 :StatArray<T, size, external>(otherarray)
1093 {
1094 }
1095
1096 /**
1097 * Constuctor for one dimensional array
1098 * empty array
1099 */
1100 StatArrayDim1()
1101 :StatArray<T, size, external>() {}
1102
1103 virtual ~StatArrayDim1() {}
1104
1105 /**
1106 * Assign static array with external storage to static array.
1107 * a = b
1108 * @param b any array of type StatArrayDim1
1109 */
1110 StatArrayDim1<T, size, external>&
1111 operator=(const StatArrayDim1<T, size, true>& b)
1112 {
1113 this->assign(b);
1114 return *this;
1115 }
1116
1117 /**
1118 * Assign static array with internal storage to static array.
1119 * a = b
1120 * @param b any array of type StatArrayDim1
1121 */
1122 StatArrayDim1<T, size, external>&
1123 operator=(const StatArrayDim1<T, size, false>& b)
1124 {
1125 this->assign(b);
1126 return *this;
1127 }
1128
1129 /**
1130 * Assign array of type base array to one dim static array
1131 * a = b
1132 * @param b any array of type BaseArray
1133 */
1134 StatArrayDim1<T, size, external>& operator=(const BaseArray<T>& b)
1135 {
1136 this->assign(b);
1137 return *this;
1138 }
1139
1140 /**
1141 * Index operator to read array element
1142 * @param idx vector of indices
1143 */
1144 virtual const T& operator()(const vector<size_t>& idx) const
1145 {
1146 return StatArray<T, size, external>::_data[idx[0]-1];
1147 }
1148
1149 /**
1150 * Index operator to write array element
1151 * @param idx vector of indices
1152 */
1153 virtual T& operator()(const vector<size_t>& idx)
1154 {
1155 return StatArray<T, size, external>::_data[idx[0]-1];
1156 }
1157
1158 /**
1159 * Index operator to access array element
1160 * @param index index
1161 */
1162 inline virtual T& operator()(size_t index)
1163 {
1164 return StatArray<T, size, external>::_data[index - 1];
1165 }
1166
1167 /**
1168 * Index operator to read array element
1169 * @param index index
1170 */
1171 inline virtual const T& operator()(size_t index) const
1172 {
1173 return StatArray<T, size, external>::_data[index - 1];
1174 }
1175
1176 /**
1177 * Return sizes of dimensions
1178 */
1179 virtual std::vector<size_t> getDims() const
1180 {
1181 std::vector<size_t> v;
1182 v.push_back(size);
1183 return v;
1184 }
1185
1186 /**
1187 * Return sizes of one dimension
1188 */
1189 virtual int getDim(size_t dim) const
1190 {
1191 return (int)size;
1192 }
1193
1194 /**
1195 * Returns number of dimensions
1196 */
1197 virtual size_t getNumDims() const
1198 {
1199 return 1;
1200 }
1201
1202 // keep the inherited setDims(vector) visible, it is hidden by the overload below
1203 using StatArray<T, size, external>::setDims;
1204 void setDims(size_t size1) { }
1205
1206 typedef const T* const_iterator;
1207 typedef T* iterator;
1208
1209 iterator begin()
1210 {
1211 return StatArray<T, size, external>::_data;
1212 }
1213
1214 iterator end()
1215 {
1216 return StatArray<T, size, external>::_data + size;
1217 }
1218 };
1219
1220 /**
1221 * Two dimensional static array, specializes StatArray
1222 * @param T type of the array
1223 * @param size1 size of dimension one
1224 * @param size2 size of dimension two
1225 * @param external indicates if the memory is provided externally
1226 */
1227 template<typename T, std::size_t size1, std::size_t size2, bool external = false>
1228 class StatArrayDim2 : public StatArray<T, size1*size2, external>
1229 {
1230 public:
1231 /**
1232 * Constuctor for two dimensional array
1233 * if reference array it uses data from simvars memory
1234 * else it copies data in array memory
1235 */
1236 StatArrayDim2(T* data)
1237 :StatArray<T, size1*size2, external>(data) {}
1238
1239 /**
1240 * Constuctor for two dimensional array
1241 * copies data from otherarray in array memory
1242 * or holds a pointer to otherarray's data
1243 */
1244 StatArrayDim2(const StatArrayDim2<T, size1, size2, true>& otherarray)
1245 :StatArray<T, size1*size2, external>(otherarray)
1246 {
1247 }
1248
1249 /**
1250 * Constuctor for two dimensional array
1251 * copies data from otherarray in array memory
1252 * or holds a pointer to otherarray's data
1253 */
1254 StatArrayDim2(const StatArrayDim2<T, size1, size2, false>& otherarray)
1255 :StatArray<T, size1*size2, external>(otherarray)
1256 {
1257 }
1258
1259 /**
1260 * Constuctor for one dimensional array that
1261 * lets otherarray copy data into array memory
1262 */
1263 StatArrayDim2(const BaseArray<T>& otherarray)
1264 :StatArray<T, size1*size2, external>(otherarray)
1265 {
1266 }
1267
1268 /**
1269 * Default constuctor for two dimensional array
1270 */
1271 StatArrayDim2()
1272 :StatArray<T, size1*size2, external>() {}
1273
1274 virtual ~StatArrayDim2(){}
1275
1276 /**
1277 * Assign static array with external storage to static array.
1278 * a = b
1279 * @param b any array of type StatArrayDim2
1280 */
1281 StatArrayDim2<T, size1, size2, external>&
1282 operator=(const StatArrayDim2<T, size1, size2, true>& b)
1283 {
1284 this->assign(b);
1285 return *this;
1286 }
1287
1288 /**
1289 * Assign static array with internal storage to static array.
1290 * a = b
1291 * @param b any array of type StatArrayDim2
1292 */
1293 StatArrayDim2<T, size1, size2, external>&
1294 operator=(const StatArrayDim2<T, size1, size2, false>& b)
1295 {
1296 this->assign(b);
1297 return *this;
1298 }
1299
1300 /**
1301 * Assign array of type base array to two dim static array
1302 * a = b
1303 * @param b any array of type BaseArray
1304 */
1305 StatArrayDim2<T, size1, size2, external>& operator=(const BaseArray<T>& b)
1306 {
1307 this->assign(b);
1308 return *this;
1309 }
1310
1311 /**
1312 * Copies one dimensional array to row i
1313 * @param b array of type StatArrayDim1
1314 * @param i row number
1315 * @param n optional number of rows not needed for static arrays
1316 */
1317 template<bool anybool>
1318 void append(size_t i, const StatArrayDim1<T, size2, anybool>& b, size_t n = 0)
1319 {
1320 const T* data = b.getData();
1321 T *array_data = StatArray<T, size1*size2, external>::getData() + i-1;
1322 for (size_t j = 1; j <= size2; j++) {
1323 //(*this)(i, j) = b(j);
1324 *array_data = *data++;
1325 array_data += size1;
1326 }
1327 }
1328
1329 /**
1330 * Index operator to read array element
1331 * @param idx vector of indices
1332 */
1333 virtual const T& operator()(const vector<size_t>& idx) const
1334 {
1335 return StatArray<T, size1*size2, external>::_data[idx[0]-1 + size1*(idx[1]-1)];
1336 }
1337
1338 /**
1339 * Index operator to write array element
1340 * @param idx vector of indices
1341 */
1342 virtual T& operator()(const vector<size_t>& idx)
1343 {
1344 return StatArray<T, size1*size2, external>::_data[idx[0]-1 + size1*(idx[1]-1)];
1345 }
1346
1347 /**
1348 * Index operator to access array element
1349 * @param i index 1
1350 * @param j index 2
1351 */
1352 inline virtual T& operator()(size_t i, size_t j)
1353 {
1354 return StatArray<T, size1*size2, external>::_data[i-1 + size1*(j-1)];
1355 }
1356
1357 /**
1358 * Index operator to read array element
1359 * @param index index
1360 */
1361 inline virtual const T& operator()(size_t i, size_t j) const
1362 {
1363 return StatArray<T, size1*size2, external>::_data[i-1 + size1*(j-1)];
1364 }
1365
1366 /**
1367 * Return sizes of dimensions
1368 */
1369 virtual std::vector<size_t> getDims() const
1370 {
1371 std::vector<size_t> v;
1372 v.push_back(size1);
1373 v.push_back(size2);
1374 return v;
1375 }
1376
1377 /**
1378 * Return size of one dimension
1379 */
1380 virtual int getDim(size_t dim) const
1381 {
1382 switch (dim) {
1383 case 1:
1384 return (int)size1;
1385 case 2:
1386 return (int)size2;
1387 default:
1388 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
1389 }
1390 }
1391
1392 /**
1393 * Return sizes of dimensions
1394 */
1395 virtual size_t getNumDims() const
1396 {
1397 return 2;
1398 }
1399
1400 // keep the inherited setDims(vector) visible, it is hidden by the overload below
1401 using StatArray<T, size1*size2, external>::setDims;
1402 void setDims(size_t i, size_t j) {}
1403 };
1404
1405 /**
1406 * Three dimensional static array, implements BaseArray interface methods
1407 * @param T type of the array
1408 * @param size1 size of dimension one
1409 * @param size2 size of dimension two
1410 * @param size3 size of dimension two
1411 * @param external indicates if the memory is provided externally
1412 */
1413 template<typename T, std::size_t size1, std::size_t size2, std::size_t size3, bool external = false>
1414 class StatArrayDim3 : public StatArray<T, size1*size2*size3, external>
1415 {
1416 public:
1417 /**
1418 * Constuctor for one dimensional array
1419 * if reference array it uses data from simvars memory
1420 * else it copies data in array memory
1421 */
1422 StatArrayDim3(T* data)
1423 :StatArray<T, size1*size2*size3, external>(data) {}
1424
1425 /**
1426 * Constuctor for three dimensional array
1427 * copies data from otherarray in array memory
1428 * or holds a pointer to otherarray's data
1429 */
1430 StatArrayDim3(const StatArrayDim3<T, size1, size2, size3, true>& otherarray)
1431 :StatArray<T, size1*size2*size3, external>(otherarray)
1432 {
1433 }
1434
1435 /**
1436 * Constuctor for three dimensional array
1437 * copies data from otherarray in array memory
1438 * or holds a pointer to otherarray's data
1439 */
1440 StatArrayDim3(const StatArrayDim3<T, size1, size2, size3, false>& otherarray)
1441 :StatArray<T, size1*size2*size3, external>(otherarray)
1442 {
1443 }
1444
1445 /**
1446 * Constuctor for one dimensional array that
1447 * lets otherarray copy data into array memory
1448 */
1449 StatArrayDim3(const BaseArray<T>& otherarray)
1450 :StatArray<T, size1*size2*size3, external>(otherarray)
1451 {
1452 }
1453
1454 /**
1455 * Default constuctor for three dimensional array
1456 */
1457 StatArrayDim3()
1458 :StatArray<T, size1*size2*size3, external>() {}
1459
1460 virtual ~StatArrayDim3() {}
1461
1462 /**
1463 * Assign static array with external storage to static array.
1464 * a = b
1465 * @param b any array of type StatArrayDim3
1466 */
1467 StatArrayDim3<T, size1, size2, size3, external>&
1468 operator=(const StatArrayDim3<T, size1, size2, size3, true>& b)
1469 {
1470 this->assign(b);
1471 return *this;
1472 }
1473
1474 /**
1475 * Assign static array with internal storage to static array.
1476 * a = b
1477 * @param b any array of type StatArrayDim3
1478 */
1479 StatArrayDim3<T, size1, size2, size3, external>&
1480 operator=(const StatArrayDim3<T, size1, size2, size3, false>& b)
1481 {
1482 this->assign(b);
1483 return *this;
1484 }
1485
1486 /**
1487 * Assign array of type base array to three dim static array
1488 * a = b
1489 * @param b any array of type BaseArray
1490 */
1491 StatArrayDim3<T, size1, size2, size3, external>&
1492 operator=(const BaseArray<T>& b)
1493 {
1494 this->assign(b);
1495 return *this;
1496 }
1497
1498 /**
1499 * Copies two dimensional array to row i
1500 * @param b array of type StatArrayDim2
1501 * @param i row number
1502 * @param n optional number of rows not needed for static arrays
1503 */
1504 template<bool anybool>
1505 void append(size_t i, const StatArrayDim2<T, size2, size3, anybool>& b, size_t n = 0)
1506 {
1507 const T* data = b.getData();
1508 T *array_data = StatArray<T, size1*size2*size3, external>::getData() + i-1;
1509 for (size_t k = 1; k <= size3; k++) {
1510 for (size_t j = 1; j <= size2; j++) {
1511 //(*this)(i, j, k) = b(j, k);
1512 *array_data = *data++;
1513 array_data += size1;
1514 }
1515 }
1516 }
1517
1518 /**
1519 * Return sizes of dimensions
1520 */
1521 virtual std::vector<size_t> getDims() const
1522 {
1523 std::vector<size_t> v;
1524 v.push_back(size1);
1525 v.push_back(size2);
1526 v.push_back(size3);
1527 return v;
1528 }
1529
1530 /**
1531 * Return sizes of one dimension
1532 */
1533 virtual int getDim(size_t dim) const
1534 {
1535 switch (dim) {
1536 case 1:
1537 return (int)size1;
1538 case 2:
1539 return (int)size2;
1540 case 3:
1541 return (int)size3;
1542 default:
1543 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
1544 }
1545 }
1546
1547 /**
1548 * Index operator to read array element
1549 * @param idx vector of indices
1550 */
1551 virtual const T& operator()(const vector<size_t>& idx) const
1552 {
1553 return StatArray<T, size1*size2*size3, external>::
1554 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1))];
1555 }
1556
1557 /**
1558 * Index operator to write array element
1559 * @param idx vector of indices
1560 */
1561 virtual T& operator()(const vector<size_t>& idx)
1562 {
1563 return StatArray<T, size1*size2*size3, external>::
1564 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1))];
1565 }
1566
1567 /**
1568 * Index operator to access array element
1569 * @param i index 1
1570 * @param j index 2
1571 * @param k index 3
1572 */
1573 inline virtual T& operator()(size_t i, size_t j, size_t k)
1574 {
1575 return StatArray<T, size1*size2*size3, external>::
1576 _data[i-1 + size1*(j-1 + size2*(k-1))];
1577 }
1578
1579 /**
1580 * Return sizes of dimensions
1581 */
1582 virtual size_t getNumDims() const
1583 {
1584 return 3;
1585 }
1586
1587 // keep the inherited setDims(vector) visible, it is hidden by the overload below
1588 using StatArray<T, size1*size2*size3, external>::setDims;
1589 void setDims(size_t i, size_t j, size_t k) {}
1590 };
1591
1592 /**
1593 * Three dimensional static array, implements BaseArray interface methods
1594 * @param T type of the array
1595 * @param size1 size of dimension one
1596 * @param size2 size of dimension two
1597 * @param size3 size of dimension three
1598 * @param size4 size of dimension four
1599 * @param external indicates if the memory is provided externally
1600 */
1601 template<typename T, std::size_t size1, std::size_t size2, std::size_t size3, std::size_t size4, bool external = false>
1602 class StatArrayDim4 : public StatArray<T, size1*size2*size3*size4, external>
1603 {
1604 public:
1605 /**
1606 * Constuctor for one dimensional array
1607 * if reference array it uses data from simvars memory
1608 * else it copies data in array memory
1609 */
1610 StatArrayDim4(T* data)
1611 :StatArray<T, size1*size2*size3*size4, external>(data) {}
1612
1613 /**
1614 * Constuctor for three dimensional array
1615 * copies data from otherarray in array memory
1616 * or holds a pointer to otherarray's data
1617 */
1618 StatArrayDim4(const StatArrayDim4<T, size1, size2, size3, size4, true>& otherarray)
1619 :StatArray<T, size1*size2*size3*size4, external>(otherarray)
1620 {
1621 }
1622
1623 /**
1624 * Constuctor for three dimensional array
1625 * copies data from otherarray in array memory
1626 * or holds a pointer to otherarray's data
1627 */
1628 StatArrayDim4(const StatArrayDim4<T, size1, size2, size3, size4, false>& otherarray)
1629 :StatArray<T, size1*size2*size3*size4, external>(otherarray)
1630 {
1631 }
1632
1633 /**
1634 * Constuctor for one dimensional array that
1635 * lets otherarray copy data into array memory
1636 */
1637 StatArrayDim4(const BaseArray<T>& otherarray)
1638 :StatArray<T, size1*size2*size3*size4, external>(otherarray)
1639 {
1640 }
1641
1642 /**
1643 * Default constuctor for three dimensional array
1644 */
1645 StatArrayDim4()
1646 :StatArray<T, size1*size2*size3*size4, external>() {}
1647
1648 virtual ~StatArrayDim4() {}
1649
1650 /**
1651 * Assign static array with external storage to static array.
1652 * a = b
1653 * @param b any array of type StatArrayDim4
1654 */
1655 StatArrayDim4<T, size1, size2, size3, size4, external>&
1656 operator=(const StatArrayDim4<T, size1, size2, size3, size4, true>& b)
1657 {
1658 this->assign(b);
1659 return *this;
1660 }
1661
1662 /**
1663 * Assign static array with internal storage to static array.
1664 * a = b
1665 * @param b any array of type StatArrayDim4
1666 */
1667 StatArrayDim4<T, size1, size2, size3, size4, external>&
1668 operator=(const StatArrayDim4<T, size1, size2, size3, size4, false>& b)
1669 {
1670 this->assign(b);
1671 return *this;
1672 }
1673
1674 /**
1675 * Assign array of type base array to three dim static array
1676 * a = b
1677 * @param b any array of type BaseArray
1678 */
1679 StatArrayDim4<T, size1, size2, size3, size4, external>&
1680 operator=(const BaseArray<T>& b)
1681 {
1682 this->assign(b);
1683 return *this;
1684 }
1685
1686 /**
1687 * Copies two dimensional array to row i
1688 * @param b array of type StatArrayDim3
1689 * @param i row number
1690 * @param n optional number of rows not needed for static arrays
1691 */
1692 template<bool anybool>
1693 void append(size_t i, const StatArrayDim3<T, size2, size3, size4, anybool>& b, size_t n = 0)
1694 {
1695 const T* data = b.getData();
1696 T *array_data = StatArray<T, size1*size2*size3*size4, external>::getData() + i-1;
1697 for (size_t l = 1; l <= size4; l++) {
1698 for (size_t k = 1; k <= size3; k++) {
1699 for (size_t j = 1; j <= size2; j++) {
1700 //(*this)(i, j, k, l) = b(j, k, l);
1701 *array_data = *data++;
1702 array_data += size1;
1703 }
1704 }
1705 }
1706 }
1707
1708 /**
1709 * Return sizes of dimensions
1710 */
1711 virtual std::vector<size_t> getDims() const
1712 {
1713 std::vector<size_t> v;
1714 v.push_back(size1);
1715 v.push_back(size2);
1716 v.push_back(size3);
1717 v.push_back(size4);
1718 return v;
1719 }
1720
1721 /**
1722 * Return sizes of one dimension
1723 */
1724 virtual int getDim(size_t dim) const
1725 {
1726 switch (dim) {
1727 case 1:
1728 return (int)size1;
1729 case 2:
1730 return (int)size2;
1731 case 3:
1732 return (int)size3;
1733 case 4:
1734 return (int)size4;
1735 default:
1736 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
1737 }
1738 }
1739
1740 /**
1741 * Index operator to read array element
1742 * @param idx vector of indices
1743 */
1744 virtual const T& operator()(const vector<size_t>& idx) const
1745 {
1746 return StatArray<T, size1*size2*size3*size4, external>::
1747 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1)))];
1748 }
1749
1750 /**
1751 * Index operator to write array element
1752 * @param idx vector of indices
1753 */
1754 virtual T& operator()(const vector<size_t>& idx)
1755 {
1756 return StatArray<T, size1*size2*size3*size4, external>::
1757 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1)))];
1758 }
1759
1760 /**
1761 * Index operator to access array element
1762 * @param i index 1
1763 * @param j index 2
1764 * @param k index 3
1765 * @param l index 4
1766 */
1767 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l)
1768 {
1769 return StatArray<T, size1*size2*size3*size4, external>::
1770 _data[i-1 + size1*(j-1 + size2*(k-1 + size3*(l-1)))];
1771 }
1772
1773 /**
1774 * Return sizes of dimensions
1775 */
1776 virtual size_t getNumDims() const
1777 {
1778 return 4;
1779 }
1780
1781 // keep the inherited setDims(vector) visible, it is hidden by the overload below
1782 using StatArray<T, size1*size2*size3*size4, external>::setDims;
1783 void setDims(size_t i, size_t j, size_t k, size_t l) {}
1784 };
1785
1786 /**
1787 * Three dimensional static array, implements BaseArray interface methods
1788 * @param T type of the array
1789 * @param size1 size of dimension one
1790 * @param size2 size of dimension two
1791 * @param size3 size of dimension three
1792 * @param size4 size of dimension four
1793 * @param size5 size of dimension five
1794 * @param external indicates if the memory is provided externally
1795 */
1796 template<typename T, std::size_t size1, std::size_t size2, std::size_t size3, std::size_t size4, std::size_t size5, bool external = false>
1797 class StatArrayDim5 : public StatArray<T, size1*size2*size3*size4*size5, external>
1798 {
1799 public:
1800 /**
1801 * Constuctor for one dimensional array
1802 * if reference array it uses data from simvars memory
1803 * else it copies data in array memory
1804 */
1805 StatArrayDim5(T* data)
1806 :StatArray<T, size1*size2*size3*size4*size5, external>(data) {}
1807
1808 /**
1809 * Constuctor for three dimensional array
1810 * copies data from otherarray in array memory
1811 * or holds a pointer to otherarray's data
1812 */
1813 StatArrayDim5(const StatArrayDim5<T, size1, size2, size3, size4, size5, true>& otherarray)
1814 :StatArray<T, size1*size2*size3*size4*size5, external>(otherarray)
1815 {
1816 }
1817
1818 /**
1819 * Constuctor for three dimensional array
1820 * copies data from otherarray in array memory
1821 * or holds a pointer to otherarray's data
1822 */
1823 StatArrayDim5(const StatArrayDim5<T, size1, size2, size3, size4, size5, false>& otherarray)
1824 :StatArray<T, size1*size2*size3*size4*size5, external>(otherarray)
1825 {
1826 }
1827
1828 /**
1829 * Constuctor for one dimensional array that
1830 * lets otherarray copy data into array memory
1831 */
1832 StatArrayDim5(const BaseArray<T>& otherarray)
1833 :StatArray<T, size1*size2*size3*size4*size5, external>(otherarray)
1834 {
1835 }
1836
1837 /**
1838 * Default constuctor for three dimensional array
1839 */
1840 StatArrayDim5()
1841 :StatArray<T, size1*size2*size3*size4*size5, external>() {}
1842
1843 virtual ~StatArrayDim5() {}
1844
1845 /**
1846 * Assign static array with external storage to static array.
1847 * a = b
1848 * @param b any array of type StatArrayDim5
1849 */
1850 StatArrayDim5<T, size1, size2, size3, size4, size5, external>&
1851 operator=(const StatArrayDim5<T, size1, size2, size3, size4, size5, true>& b)
1852 {
1853 this->assign(b);
1854 return *this;
1855 }
1856
1857 /**
1858 * Assign static array with internal storage to static array.
1859 * a = b
1860 * @param b any array of type StatArrayDim5
1861 */
1862 StatArrayDim5<T, size1, size2, size3, size4, size5, external>&
1863 operator=(const StatArrayDim5<T, size1, size2, size3, size4, size5, false>& b)
1864 {
1865 this->assign(b);
1866 return *this;
1867 }
1868
1869 /**
1870 * Assign array of type base array to three dim static array
1871 * a = b
1872 * @param b any array of type BaseArray
1873 */
1874 StatArrayDim5<T, size1, size2, size3, size4, size5, external>&
1875 operator=(const BaseArray<T>& b)
1876 {
1877 this->assign(b);
1878 return *this;
1879 }
1880
1881 /**
1882 * Copies two dimensional array to row i
1883 * @param b array of type StatArrayDim3
1884 * @param i row number
1885 * @param n optional number of rows not needed for static arrays
1886 */
1887 template<bool anybool>
1888 void append(size_t i, const StatArrayDim4<T, size2, size3, size4, size5, anybool>& b, size_t n = 0)
1889 {
1890 const T* data = b.getData();
1891 T *array_data = StatArray<T, size1*size2*size3*size4*size5, external>::getData() + i-1;
1892 for (size_t m = 1; m <= size5; m++) {
1893 for (size_t l = 1; l <= size4; l++) {
1894 for (size_t k = 1; k <= size3; k++) {
1895 for (size_t j = 1; j <= size2; j++) {
1896 //(*this)(i, j, k, l, m) = b(j, k, l, m);
1897 *array_data = *data++;
1898 array_data += size1;
1899 }
1900 }
1901 }
1902 }
1903 }
1904
1905 /**
1906 * Return sizes of dimensions
1907 */
1908 virtual std::vector<size_t> getDims() const
1909 {
1910 std::vector<size_t> v;
1911 v.push_back(size1);
1912 v.push_back(size2);
1913 v.push_back(size3);
1914 v.push_back(size4);
1915 v.push_back(size5);
1916 return v;
1917 }
1918
1919 /**
1920 * Return sizes of one dimension
1921 */
1922 virtual int getDim(size_t dim) const
1923 {
1924 switch (dim) {
1925 case 1:
1926 return (int)size1;
1927 case 2:
1928 return (int)size2;
1929 case 3:
1930 return (int)size3;
1931 case 4:
1932 return (int)size4;
1933 case 5:
1934 return (int)size5;
1935 default:
1936 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
1937 }
1938 }
1939
1940 /**
1941 * Index operator to read array element
1942 * @param idx vector of indices
1943 */
1944 virtual const T& operator()(const vector<size_t>& idx) const
1945 {
1946 return StatArray<T, size1*size2*size3*size4*size5, external>::
1947 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1 + size4*(idx[4]-1))))];
1948 }
1949
1950 /**
1951 * Index operator to write array element
1952 * @param idx vector of indices
1953 */
1954 virtual T& operator()(const vector<size_t>& idx)
1955 {
1956 return StatArray<T, size1*size2*size3*size4*size5, external>::
1957 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1 + size4*(idx[4]-1))))];
1958 }
1959
1960 /**
1961 * Index operator to access array element
1962 * @param i index 1
1963 * @param j index 2
1964 * @param k index 3
1965 * @param l index 4
1966 * @param m index 5
1967 */
1968 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m)
1969 {
1970 return StatArray<T, size1*size2*size3*size4*size5, external>::
1971 _data[i-1 + size1*(j-1 + size2*(k-1 + size3*(l-1 + size4*(m-1))))];
1972 }
1973
1974 /**
1975 * Return sizes of dimensions
1976 */
1977 virtual size_t getNumDims() const
1978 {
1979 return 5;
1980 }
1981
1982 // keep the inherited setDims(vector) visible, it is hidden by the overload below
1983 using StatArray<T, size1*size2*size3*size4*size5, external>::setDims;
1984 void setDims(size_t i, size_t j, size_t k, size_t l, size_t m) {}
1985 };
1986
1987 /**
1988 * Three dimensional static array, implements BaseArray interface methods
1989 * @param T type of the array
1990 * @param size1 size of dimension one
1991 * @param size2 size of dimension two
1992 * @param size3 size of dimension three
1993 * @param size4 size of dimension four
1994 * @param size5 size of dimension five
1995 * @param size6 size of dimension six
1996 * @param external indicates if the memory is provided externally
1997 */
1998 template<typename T, std::size_t size1, std::size_t size2, std::size_t size3, std::size_t size4, std::size_t size5, std::size_t size6, bool external = false>
1999 class StatArrayDim6 : public StatArray<T, size1*size2*size3*size4*size5*size6, external>
2000 {
2001 public:
2002 /**
2003 * Constuctor for one dimensional array
2004 * if reference array it uses data from simvars memory
2005 * else it copies data in array memory
2006 */
2007 StatArrayDim6(T* data)
2008 :StatArray<T, size1*size2*size3*size4*size5*size6, external>(data) {}
2009
2010 /**
2011 * Constuctor for three dimensional array
2012 * copies data from otherarray in array memory
2013 * or holds a pointer to otherarray's data
2014 */
2015 StatArrayDim6(const StatArrayDim6<T, size1, size2, size3, size4, size5, size6, true>& otherarray)
2016 :StatArray<T, size1*size2*size3*size4*size5*size6, external>(otherarray)
2017 {
2018 }
2019
2020 /**
2021 * Constuctor for three dimensional array
2022 * copies data from otherarray in array memory
2023 * or holds a pointer to otherarray's data
2024 */
2025 StatArrayDim6(const StatArrayDim6<T, size1, size2, size3, size4, size5, size6, false>& otherarray)
2026 :StatArray<T, size1*size2*size3*size4*size5*size6, external>(otherarray)
2027 {
2028 }
2029
2030 /**
2031 * Constuctor for one dimensional array that
2032 * lets otherarray copy data into array memory
2033 */
2034 StatArrayDim6(const BaseArray<T>& otherarray)
2035 :StatArray<T, size1*size2*size3*size4*size5*size6, external>(otherarray)
2036 {
2037 }
2038
2039 /**
2040 * Default constuctor for three dimensional array
2041 */
2042 StatArrayDim6()
2043 :StatArray<T, size1*size2*size3*size4*size5*size6, external>() {}
2044
2045 virtual ~StatArrayDim6() {}
2046
2047 /**
2048 * Assign static array with external storage to static array.
2049 * a = b
2050 * @param b any array of type StatArrayDim6
2051 */
2052 StatArrayDim6<T, size1, size2, size3, size4, size5, size6, external>&
2053 operator=(const StatArrayDim6<T, size1, size2, size3, size4, size5, size6, true>& b)
2054 {
2055 this->assign(b);
2056 return *this;
2057 }
2058
2059 /**
2060 * Assign static array with internal storage to static array.
2061 * a = b
2062 * @param b any array of type StatArrayDim6
2063 */
2064 StatArrayDim6<T, size1, size2, size3, size4, size5, size6, external>&
2065 operator=(const StatArrayDim6<T, size1, size2, size3, size4, size5, size6, false>& b)
2066 {
2067 this->assign(b);
2068 return *this;
2069 }
2070
2071 /**
2072 * Assign array of type base array to three dim static array
2073 * a = b
2074 * @param b any array of type BaseArray
2075 */
2076 StatArrayDim6<T, size1, size2, size3, size4, size5, size6, external>&
2077 operator=(const BaseArray<T>& b)
2078 {
2079 this->assign(b);
2080 return *this;
2081 }
2082
2083 /**
2084 * Copies two dimensional array to row i
2085 * @param b array of type StatArrayDim3
2086 * @param i row number
2087 * @param n optional number of rows not needed for static arrays
2088 */
2089 template<bool anybool>
2090 void append(size_t i, const StatArrayDim5<T, size2, size3, size4, size5, size6, anybool>& b, size_t n = 0)
2091 {
2092 const T* data = b.getData();
2093 T *array_data = StatArray<T, size1*size2*size3*size4*size5*size6, external>::getData() + i-1;
2094 for (size_t n = 1; n <= size6; n++) {
2095 for (size_t m = 1; m <= size5; m++) {
2096 for (size_t l = 1; l <= size4; l++) {
2097 for (size_t k = 1; k <= size3; k++) {
2098 for (size_t j = 1; j <= size2; j++) {
2099 //(*this)(i, j, k, l, m, n) = b(j, k, l, m, n);
2100 *array_data = *data++;
2101 array_data += size1;
2102 }
2103 }
2104 }
2105 }
2106 }
2107 }
2108
2109 /**
2110 * Return sizes of dimensions
2111 */
2112 virtual std::vector<size_t> getDims() const
2113 {
2114 std::vector<size_t> v;
2115 v.push_back(size1);
2116 v.push_back(size2);
2117 v.push_back(size3);
2118 v.push_back(size4);
2119 v.push_back(size5);
2120 v.push_back(size6);
2121 return v;
2122 }
2123
2124 /**
2125 * Return sizes of one dimension
2126 */
2127 virtual int getDim(size_t dim) const
2128 {
2129 switch (dim) {
2130 case 1:
2131 return (int)size1;
2132 case 2:
2133 return (int)size2;
2134 case 3:
2135 return (int)size3;
2136 case 4:
2137 return (int)size4;
2138 case 5:
2139 return (int)size5;
2140 case 6:
2141 return (int)size6;
2142 default:
2143 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Wrong getDim");
2144 }
2145 }
2146
2147 /**
2148 * Index operator to read array element
2149 * @param idx vector of indices
2150 */
2151 virtual const T& operator()(const vector<size_t>& idx) const
2152 {
2153 return StatArray<T, size1*size2*size3*size4*size5*size6, external>::
2154 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1 + size4*(idx[4]-1 + size5*(idx[5]-1)))))];
2155 }
2156
2157 /**
2158 * Index operator to write array element
2159 * @param idx vector of indices
2160 */
2161 virtual T& operator()(const vector<size_t>& idx)
2162 {
2163 return StatArray<T, size1*size2*size3*size4*size5*size6, external>::
2164 _data[idx[0]-1 + size1*(idx[1]-1 + size2*(idx[2]-1 + size3*(idx[3]-1 + size4*(idx[4]-1 + size5*(idx[5]-1)))))];
2165 }
2166
2167 /**
2168 * Index operator to access array element
2169 * @param i index 1
2170 * @param j index 2
2171 * @param k index 3
2172 * @param l index 4
2173 * @param m index 5
2174 * @param n index 6
2175 */
2176 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n)
2177 {
2178 return StatArray<T, size1*size2*size3*size4*size5*size6, external>::
2179 _data[i-1 + size1*(j-1 + size2*(k-1 + size3*(l-1 + size4*(m-1 + size5*(n-1)))))];
2180 }
2181
2182 /**
2183 * Return sizes of dimensions
2184 */
2185 virtual size_t getNumDims() const
2186 {
2187 return 6;
2188 }
2189
2190 // keep the inherited setDims(vector) visible, it is hidden by the overload below
2191 using StatArray<T, size1*size2*size3*size4*size5*size6, external>::setDims;
2192 void setDims(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) {}
2193 };
2194
2195 /**
2196 * Dynamically allocated array, implements BaseArray interface methods
2197 * @param T type of the array
2198 * @param ndims number of dimensions of array
2199 */
2200 template<typename T, size_t ndims>
2201 class DynArray : public BaseArray<T>
2202 {
2203 public:
2204 /**
2205 * Constructor for given sizes
2206 */
2207 2 DynArray()
2208 :BaseArray<T>(false,false)
2209 2 ,_dims(ndims)
2210 {
2211 2 _array_data = NULL;
2212 2 _nelems = 0;
2213 1 }
2214
2215 /**
2216 * Copy constructor for DynArray
2217 */
2218 DynArray(const DynArray<T, ndims>& dynarray)
2219 :BaseArray<T>(false,false)
2220 ,_dims(ndims)
2221 {
2222 _array_data = NULL;
2223 _nelems = 0;
2224 assign(dynarray);
2225 }
2226
2227 /**
2228 * Copy constructor for a general BaseArray
2229 */
2230 DynArray(const BaseArray<T>& b)
2231 :BaseArray<T>(false,false)
2232 ,_dims(ndims)
2233 {
2234 _array_data = NULL;
2235 _nelems = 0;
2236 assign(b);
2237 }
2238
2239 2 virtual ~DynArray()
2240 {
2241 2 if (_array_data != NULL)
2242 1 delete [] _array_data;
2243 2 }
2244
2245 1 virtual void assign(const BaseArray<T>& b)
2246 {
2247
1/4
DynArray<int, 1ul>::assign(BaseArray<int> const&):
✗ Branch 2 not taken.
✗ Branch 3 not taken.
DynArray<int, 2ul>::assign(BaseArray<int> const&):
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 resize(b.getDims());
2248 1 b.getDataCopy(_array_data, _nelems);
2249 1 }
2250
2251 ✗ virtual void assign(const T* data)
2252 {
2253 ✗ if (_nelems > 0)
2254 ✗ std::copy(data, data + _nelems, _array_data);
2255 ✗ }
2256
2257 ✗ virtual void assign(const T& value)
2258 {
2259 ✗ if (_nelems > 0)
2260 ✗ std::fill(_array_data, _array_data + _nelems, value);
2261 ✗ }
2262
2263
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 virtual void resize(const std::vector<size_t>& dims)
2264 {
2265
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (dims.size() != ndims)
2266 ✗ throw std::runtime_error("Can't change dimensionality of DynArray");
2267
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (dims != _dims) {
2268 size_t nelems = 0;
2269
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (dims.size() > 0)
2270 1 nelems = std::accumulate(dims.begin(), dims.end(),
2271 1, std::multiplies<size_t>());
2272
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (nelems != _nelems) {
2273
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (_array_data != NULL)
2274 ✗ delete [] _array_data;
2275
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (nelems > 0)
2276
1/4
DynArray<int, 1ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::resize(std::vector<unsigned long, std::allocator<unsigned long> > const&):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 _array_data = new T[nelems];
2277 else
2278 ✗ _array_data = NULL;
2279 1 _nelems = nelems;
2280 }
2281 1 _dims = dims;
2282 }
2283 1 }
2284
2285 ✗ virtual void setDims(const std::vector<size_t>& dims)
2286 {
2287 ✗ resize(dims);
2288 ✗ }
2289
2290 ✗ virtual std::vector<size_t> getDims() const
2291 {
2292 ✗ return _dims;
2293 }
2294
2295 ✗ virtual int getDim(size_t dim) const
2296 {
2297 ✗ return (int)_dims[dim - 1];
2298 }
2299
2300 /**
2301 * access to array data
2302 */
2303 1 virtual T* getData()
2304 {
2305 1 return _array_data;
2306 }
2307
2308 /**
2309 * Copies the array data of size n in the data array
2310 * data has to be allocated before getDataCopy is called
2311 */
2312 1 virtual void getDataCopy(T data[], size_t n) const
2313 {
2314
1/4
DynArray<int, 1ul>::getDataCopy(int*, unsigned long) const:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::getDataCopy(int*, unsigned long) const:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (n > 0)
2315
1/4
DynArray<int, 1ul>::getDataCopy(int*, unsigned long) const:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
DynArray<int, 2ul>::getDataCopy(int*, unsigned long) const:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::copy(_array_data, _array_data + n, data);
2316 1 }
2317
2318 /**
2319 * access to data (read-only)
2320 */
2321 ✗ virtual const T* getData() const
2322 {
2323 ✗ return _array_data;
2324 }
2325
2326 1 virtual size_t getNumElems() const
2327 {
2328 1 return _nelems;
2329 }
2330
2331 ✗ virtual size_t getNumDims() const
2332 {
2333 ✗ return ndims;
2334 }
2335
2336 protected:
2337 T *_array_data;
2338 size_t _nelems;
2339 std::vector<size_t> _dims;
2340 };
2341
2342 /**
2343 * Dynamically allocated one dimensional array, specializes DynArray
2344 * @param T type of the array
2345 */
2346 template<typename T>
2347 class DynArrayDim1 : public DynArray<T, 1>
2348 {
2349 public:
2350 1 DynArrayDim1()
2351
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 :DynArray<T, 1>()
2352 {
2353 }
2354
2355 DynArrayDim1(const DynArrayDim1<T>& dynarray)
2356 :DynArray<T, 1>(dynarray)
2357 {
2358 }
2359
2360 DynArrayDim1(const BaseArray<T>& b)
2361 :DynArray<T, 1>(b)
2362 {
2363 }
2364
2365 DynArrayDim1(size_t size1)
2366 :DynArray<T, 1>()
2367 {
2368 std::vector<size_t> dims;
2369 dims.push_back(size1);
2370 this->resize(dims);
2371 }
2372
2373 DynArrayDim1(size_t size1, const T *data)
2374 :DynArray<T, 1>()
2375 {
2376 std::vector<size_t> dims;
2377 dims.push_back(size1);
2378 this->resize(dims);
2379 if (size1 > 0)
2380 std::copy(data, data + size1, this->_array_data);
2381 }
2382
2383 ✗ virtual ~DynArrayDim1()
2384 {
2385 ✗ }
2386
2387 ✗ virtual const T& operator()(const vector<size_t>& idx) const
2388 {
2389 //return _multi_array[idx[0]-1];
2390 ✗ return this->_array_data[idx[0]-1];
2391 }
2392
2393 ✗ virtual T& operator()(const vector<size_t>& idx)
2394 {
2395 //return _multi_array[idx[0]-1];
2396 ✗ return this->_array_data[idx[0]-1];
2397 }
2398
2399 ✗ inline virtual T& operator()(size_t index)
2400 {
2401 //return _multi_array[index-1];
2402 ✗ return this->_array_data[index-1];
2403 }
2404
2405 ✗ inline virtual const T& operator()(size_t index) const
2406 {
2407 //return _multi_array[index-1];
2408 ✗ return this->_array_data[index-1];
2409 }
2410
2411 DynArrayDim1<T>& operator=(const DynArrayDim1<T>& b)
2412 {
2413 this->assign(b);
2414 return *this;
2415 }
2416
2417 void setDims(size_t size1)
2418 {
2419 std::vector<size_t> dims;
2420 dims.push_back(size1);
2421 this->resize(dims);
2422 }
2423
2424 ✗ virtual void setDims(const std::vector<size_t>& dims)
2425 {
2426 ✗ this->resize(dims);
2427 ✗ }
2428
2429
2430 typedef const T* const_iterator;
2431 typedef T* iterator;
2432
2433 iterator begin()
2434 {
2435 return this->_array_data;
2436 }
2437
2438 iterator end()
2439 {
2440 return this->_array_data + this->_nelems;
2441 }
2442 };
2443
2444 /**
2445 * Dynamically allocated two dimensional array, specializes DynArray
2446 * @param T type of the array
2447 */
2448 template<typename T>
2449 class DynArrayDim2 : public DynArray<T, 2>
2450 {
2451 public:
2452 DynArrayDim2()
2453
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 :DynArray<T, 2>()
2454 {
2455 }
2456
2457 DynArrayDim2(const DynArrayDim2<T>& dynarray)
2458 :DynArray<T, 2>(dynarray)
2459 {
2460 }
2461
2462 DynArrayDim2(const BaseArray<T>& b)
2463 :DynArray<T, 2>(b)
2464 {
2465 }
2466
2467 DynArrayDim2(size_t size1, size_t size2)
2468 :DynArray<T, 2>()
2469 {
2470 std::vector<size_t> dims;
2471 dims.push_back(size1);
2472 dims.push_back(size2);
2473 this->resize(dims);
2474 }
2475
2476 ✗ virtual ~DynArrayDim2() {}
2477
2478 /**
2479 * Copies one dimensional array to row i
2480 * @param b array of type DynArrayDim1
2481 * @param i row number
2482 * @param n number of rows
2483 */
2484 void append(size_t i, const DynArrayDim1<T>& b, size_t n)
2485 {
2486 //if the dynamic array was not allocate before
2487 if(this->_dims[0]==0 )
2488 {
2489 size_t m = b.getDim(1);
2490 if(n > 0 && m > 0)
2491 setDims(n,m);
2492 else
2493 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION, "Could not append array, wrong array dimensions");
2494
2495 }
2496 const T* data = b.getData();
2497 T *array_data = this->_array_data + i-1;
2498 size_t size1 = this->_dims[0];
2499 size_t size2 = this->_dims[1];
2500 for (size_t j = 1; j <= size2; j++) {
2501 //(*this)(i, j) = b(j);
2502 *array_data = *data++;
2503 array_data += size1;
2504 }
2505 }
2506
2507 DynArrayDim2<T>& operator=(const DynArrayDim2<T>& b)
2508 {
2509 this->assign(b);
2510 return *this;
2511 }
2512
2513 ✗ virtual const T& operator()(const vector<size_t>& idx) const
2514 {
2515 //return _multi_array[idx[0]-1][idx[1]-1];
2516 ✗ return this->_array_data[idx[0]-1 + this->_dims[0]*(idx[1]-1)];
2517 }
2518
2519 ✗ virtual T& operator()(const vector<size_t>& idx)
2520 {
2521 //return _multi_array[idx[0]-1][idx[1]-1];
2522 ✗ return this->_array_data[idx[0]-1 + this->_dims[0]*(idx[1]-1)];
2523 }
2524
2525 ✗ inline virtual T& operator()(size_t i, size_t j)
2526 {
2527 //return _multi_array[i-1][j-1];
2528 3 return this->_array_data[i-1 + this->_dims[0]*(j-1)];
2529 }
2530
2531 ✗ inline virtual const T& operator()(size_t i, size_t j) const
2532 {
2533 //return _multi_array[i-1][j-1];
2534 ✗ return this->_array_data[i-1 + this->_dims[0]*(j-1)];
2535 }
2536
2537 ✗ void setDims(size_t size1, size_t size2)
2538 {
2539 std::vector<size_t> dims;
2540 ✗ dims.push_back(size1);
2541 ✗ dims.push_back(size2);
2542 ✗ this->resize(dims);
2543 ✗ }
2544 ✗ virtual void setDims(const std::vector<size_t>& dims)
2545 {
2546 ✗ this->resize(dims);
2547 ✗ }
2548 };
2549
2550 /**
2551 * Dynamically allocated three dimensional array, specializes DynArray
2552 * @param T type of the array
2553 */
2554 template<typename T>
2555 class DynArrayDim3 : public DynArray<T, 3>
2556 {
2557 public:
2558 DynArrayDim3()
2559 :DynArray<T, 3>()
2560 {
2561 }
2562
2563 DynArrayDim3(const BaseArray<T>& b)
2564 :DynArray<T, 3>(b)
2565 {
2566 }
2567
2568 DynArrayDim3(size_t size1, size_t size2, size_t size3)
2569 :DynArray<T, 3>()
2570 {
2571 std::vector<size_t> dims;
2572 dims.push_back(size1);
2573 dims.push_back(size2);
2574 dims.push_back(size3);
2575 this->resize(dims);
2576 }
2577
2578 virtual ~DynArrayDim3() {}
2579
2580 DynArrayDim3<T>& operator=(const DynArrayDim3<T>& b)
2581 {
2582 this->assign(b);
2583 return *this;
2584 }
2585
2586 void setDims(size_t size1, size_t size2, size_t size3)
2587 {
2588 std::vector<size_t> dims;
2589 dims.push_back(size1);
2590 dims.push_back(size2);
2591 dims.push_back(size3);
2592 this->resize(dims);
2593 }
2594 virtual void setDims(const std::vector<size_t>& dims)
2595 {
2596 this->resize(dims);
2597 }
2598
2599 virtual const T& operator()(const vector<size_t>& idx) const
2600 {
2601 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1];
2602 const std::vector<size_t>& shape = this->_dims;
2603 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1))];
2604 }
2605
2606 virtual T& operator()(const vector<size_t>& idx)
2607 {
2608 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1];
2609 const std::vector<size_t>& shape = this->_dims;
2610 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1))];
2611 }
2612
2613 inline virtual T& operator()(size_t i, size_t j, size_t k)
2614 {
2615 //return _multi_array[i-1][j-1][k-1];
2616 const std::vector<size_t>& shape = this->_dims;
2617 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1))];
2618 }
2619
2620 inline virtual const T& operator()(size_t i, size_t j, size_t k) const
2621 {
2622 //return _multi_array[i-1][j-1][k-1];
2623 const std::vector<size_t>& shape = this->_dims;
2624 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1))];
2625 }
2626 };
2627
2628 /**
2629 * Dynamically allocated three dimensional array, specializes DynArray
2630 * @param T type of the array
2631 */
2632 template<typename T>
2633 class DynArrayDim4 : public DynArray<T, 4>
2634 {
2635 public:
2636 DynArrayDim4()
2637 :DynArray<T, 4>()
2638 {
2639 }
2640
2641 DynArrayDim4(const BaseArray<T>& b)
2642 :DynArray<T, 4>(b)
2643 {
2644 }
2645
2646 DynArrayDim4(size_t size1, size_t size2, size_t size3, size_t size4)
2647 :DynArray<T, 4>()
2648 {
2649 std::vector<size_t> dims;
2650 dims.push_back(size1);
2651 dims.push_back(size2);
2652 dims.push_back(size3);
2653 dims.push_back(size4);
2654 this->resize(dims);
2655 }
2656
2657 virtual ~DynArrayDim4() {}
2658
2659 DynArrayDim4<T>& operator=(const DynArrayDim4<T>& b)
2660 {
2661 this->assign(b);
2662 return *this;
2663 }
2664
2665 void setDims(size_t size1, size_t size2, size_t size3, size_t size4)
2666 {
2667 std::vector<size_t> dims;
2668 dims.push_back(size1);
2669 dims.push_back(size2);
2670 dims.push_back(size3);
2671 dims.push_back(size4);
2672 this->resize(dims);
2673 }
2674 virtual void setDims(const std::vector<size_t>& dims)
2675 {
2676 this->resize(dims);
2677 }
2678
2679 virtual const T& operator()(const vector<size_t>& idx) const
2680 {
2681 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1];
2682 const std::vector<size_t>& shape = this->_dims;
2683 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1)))];
2684 }
2685
2686 virtual T& operator()(const vector<size_t>& idx)
2687 {
2688 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1];
2689 const std::vector<size_t>& shape = this->_dims;
2690 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1)))];
2691 }
2692
2693 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l)
2694 {
2695 //return _multi_array[i-1][j-1][k-1][l-1];
2696 const std::vector<size_t>& shape = this->_dims;
2697 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1)))];
2698 }
2699
2700 inline virtual const T& operator()(size_t i, size_t j, size_t k, size_t l) const
2701 {
2702 //return _multi_array[i-1][j-1][k-1][l-1];
2703 const std::vector<size_t>& shape = this->_dims;
2704 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1)))];
2705 }
2706 };
2707
2708 /**
2709 * Dynamically allocated three dimensional array, specializes DynArray
2710 * @param T type of the array
2711 */
2712 template<typename T>
2713 class DynArrayDim5 : public DynArray<T, 5>
2714 {
2715 public:
2716 DynArrayDim5()
2717 :DynArray<T, 5>()
2718 {
2719 }
2720
2721 DynArrayDim5(const BaseArray<T>& b)
2722 :DynArray<T, 5>(b)
2723 {
2724 }
2725
2726 DynArrayDim5(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5)
2727 :DynArray<T, 5>()
2728 {
2729 std::vector<size_t> dims;
2730 dims.push_back(size1);
2731 dims.push_back(size2);
2732 dims.push_back(size3);
2733 dims.push_back(size4);
2734 dims.push_back(size5);
2735 this->resize(dims);
2736 }
2737
2738 virtual ~DynArrayDim5() {}
2739
2740 DynArrayDim5<T>& operator=(const DynArrayDim5<T>& b)
2741 {
2742 this->assign(b);
2743 return *this;
2744 }
2745
2746 void setDims(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5)
2747 {
2748 std::vector<size_t> dims;
2749 dims.push_back(size1);
2750 dims.push_back(size2);
2751 dims.push_back(size3);
2752 dims.push_back(size4);
2753 dims.push_back(size5);
2754 this->resize(dims);
2755 }
2756 virtual void setDims(const std::vector<size_t>& dims)
2757 {
2758 this->resize(dims);
2759 }
2760
2761 virtual const T& operator()(const vector<size_t>& idx) const
2762 {
2763 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1][idx[4]-1];
2764 const std::vector<size_t>& shape = this->_dims;
2765 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1 + shape[3]*(idx[4]-1))))];
2766 }
2767
2768 virtual T& operator()(const vector<size_t>& idx)
2769 {
2770 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1][idx[4]-1];
2771 const std::vector<size_t>& shape = this->_dims;
2772 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1 + shape[3]*(idx[4]-1))))];
2773 }
2774
2775 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m)
2776 {
2777 //return _multi_array[i-1][j-1][k-1][l-1][m-1];
2778 const std::vector<size_t>& shape = this->_dims;
2779 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1 + shape[3]*(m-1))))];
2780 }
2781
2782 inline virtual const T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
2783 {
2784 //return _multi_array[i-1][j-1][k-1][l-1][m-1];
2785 const std::vector<size_t>& shape = this->_dims;
2786 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1 + shape[3]*(m-1))))];
2787 }
2788 };
2789
2790 /**
2791 * Dynamically allocated three dimensional array, specializes DynArray
2792 * @param T type of the array
2793 */
2794 template<typename T>
2795 class DynArrayDim6 : public DynArray<T, 6>
2796 {
2797 public:
2798 DynArrayDim6()
2799 :DynArray<T, 6>()
2800 {
2801 }
2802
2803 DynArrayDim6(const BaseArray<T>& b)
2804 :DynArray<T, 6>(b)
2805 {
2806 }
2807
2808 DynArrayDim6(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6)
2809 :DynArray<T, 6>()
2810 {
2811 std::vector<size_t> dims;
2812 dims.push_back(size1);
2813 dims.push_back(size2);
2814 dims.push_back(size3);
2815 dims.push_back(size4);
2816 dims.push_back(size5);
2817 dims.push_back(size6);
2818 this->resize(dims);
2819 }
2820
2821 virtual ~DynArrayDim6() {}
2822
2823 DynArrayDim6<T>& operator=(const DynArrayDim6<T>& b)
2824 {
2825 this->assign(b);
2826 return *this;
2827 }
2828
2829 void setDims(size_t size1, size_t size2, size_t size3, size_t size4, size_t size5, size_t size6)
2830 {
2831 std::vector<size_t> dims;
2832 dims.push_back(size1);
2833 dims.push_back(size2);
2834 dims.push_back(size3);
2835 dims.push_back(size4);
2836 dims.push_back(size5);
2837 dims.push_back(size6);
2838 this->resize(dims);
2839 }
2840 virtual void setDims(const std::vector<size_t>& dims)
2841 {
2842 this->resize(dims);
2843 }
2844
2845 virtual const T& operator()(const vector<size_t>& idx) const
2846 {
2847 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1][idx[4]-1][idx[5]-1];
2848 const std::vector<size_t>& shape = this->_dims;
2849 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1 + shape[3]*(idx[4]-1 + shape[4]*(idx[5]-1)))))];
2850 }
2851
2852 virtual T& operator()(const vector<size_t>& idx)
2853 {
2854 //return _multi_array[idx[0]-1][idx[1]-1][idx[2]-1][idx[3]-1][idx[4]-1][idx[5]-1];
2855 const std::vector<size_t>& shape = this->_dims;
2856 return this->_array_data[idx[0]-1 + shape[0]*(idx[1]-1 + shape[1]*(idx[2]-1 + shape[2]*(idx[3]-1 + shape[3]*(idx[4]-1 + shape[4]*(idx[5]-1)))))];
2857 }
2858
2859 inline virtual T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n)
2860 {
2861 //return _multi_array[i-1][j-1][k-1][l-1][m-1][n-1];
2862 const std::vector<size_t>& shape = this->_dims;
2863 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1 + shape[3]*(m-1 + shape[4]*(n-1)))))];
2864 }
2865
2866 inline virtual const T& operator()(size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) const
2867 {
2868 //return _multi_array[i-1][j-1][k-1][l-1][m-1][n-1];
2869 const std::vector<size_t>& shape = this->_dims;
2870 return this->_array_data[i-1 + shape[0]*(j-1 + shape[1]*(k-1 + shape[2]*(l-1 + shape[3]*(m-1 + shape[4]*(n-1)))))];
2871 }
2872 };
2873
2874 /** @} */ // end of math
2875