LCOV - code coverage report
Current view: top level - ugbase/lib_algebra/small_algebra/storage - variable_array_impl.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 70 0
Test Date: 2025-09-21 23:31:46 Functions: 0.0 % 11 0

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2010-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Martin Rupp
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : 
      34              : #ifndef __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
      35              : #define __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
      36              : 
      37              : #include "storage.h"
      38              : #include "variable_array.h"
      39              : #include "common/common.h"
      40              : #include <algorithm> // for min
      41              : #include <cstring>
      42              : 
      43              : namespace ug{
      44              : 
      45              : // 'tors
      46              : template<typename T>
      47            0 : VariableArray1<T>::VariableArray1()
      48              : {
      49            0 :         n = 0;
      50            0 :         values = NULL;
      51              : }
      52              : 
      53              : 
      54              : template<typename T>
      55              : VariableArray1<T>::VariableArray1(size_t n_)
      56              : {
      57              :         n = 0;
      58              :         values = NULL;
      59              :         resize(n_, false);
      60              : }
      61              : 
      62              : template<typename T>
      63            0 : VariableArray1<T>::VariableArray1(const VariableArray1<T> &other)
      64              : {
      65            0 :         if(this == &other) return;
      66            0 :         n = 0;
      67            0 :         values = NULL;
      68            0 :         resize(other.size(), false);
      69            0 :         for(size_type i=0; i<n; i++)
      70            0 :                 values[i] = other[i];
      71              : }
      72              : 
      73              : template<typename T>
      74              : VariableArray1<T>::~VariableArray1()
      75              : {
      76            0 :         if(values) { delete[] values; values = NULL; }
      77              :         n = 0;
      78              : }
      79              : 
      80              : 
      81              : // Capacity
      82              : 
      83              : template<typename T>
      84              : inline size_t
      85              : VariableArray1<T>::size() const
      86              : {
      87            0 :         return n;
      88              : }
      89              : 
      90              : template<typename T>
      91              : bool
      92            0 : VariableArray1<T>::resize(size_t newN, bool bCopyValues)
      93              : {
      94            0 :         if(newN == n) return true;
      95              : 
      96            0 :         if(newN <= 0)
      97              :         {
      98            0 :                 if(values) delete[] values;
      99            0 :                 values = NULL;
     100            0 :                 n = 0;
     101            0 :                 return true;
     102              :         }
     103            0 :         value_type *new_values = new T[newN];
     104              :         UG_ASSERT(new_values != NULL, "out of memory");
     105              :         if(new_values == NULL) return false;
     106            0 :         memset(reinterpret_cast<void *> (new_values), 0, sizeof(T)*newN); // todo: think about that
     107              : 
     108            0 :         if(bCopyValues)
     109              :         {
     110              :                 /*
     111              :                 if(storage_traits<value_type>::is_static)
     112              :                 {
     113              :                         for(int i=0; i<n; i++)
     114              :                                 new_values[i] = values[i];
     115              :                 }
     116              :                 else {
     117              :                 // we are using swap to avoid re-allocations
     118              :                  */
     119            0 :                 size_t minN = std::min(n, newN);
     120            0 :                 for(size_t i=0; i<minN; i++)
     121            0 :                         std::swap(new_values[i], values[i]);
     122              :         }
     123              : 
     124            0 :         if(values) delete[] values;
     125            0 :         values = new_values;
     126            0 :         n = newN;
     127            0 :         return true;
     128              : }
     129              : 
     130              : template<typename T>
     131              : inline size_t
     132              : VariableArray1<T>::capacity() const
     133              : {
     134              :         return n;
     135              : }
     136              : 
     137              : 
     138              : // use stl::vector if you want to use reserve
     139              : template<typename T>
     140              : inline bool
     141              : VariableArray1<T>::reserve(size_t newCapacity) const
     142              : {
     143              :         return true;
     144              : }
     145              : 
     146              : 
     147              : // Element access
     148              : 
     149              : template<typename T>
     150              : T &
     151              : VariableArray1<T>::operator[](size_t i)
     152              : {
     153              :         assert(values);
     154              :         assert(i<n);
     155            0 :         return values[i];
     156              : }
     157              : 
     158              : template<typename T>
     159              : const T &
     160              : VariableArray1<T>::operator[](size_t i) const
     161              : {
     162              :         assert(values);
     163              :         assert(i<n);
     164            0 :         return values[i];
     165              : }
     166              : 
     167              : template<typename T>
     168              : std::ostream &operator << (std::ostream &out, const VariableArray1<T> &arr)
     169              : {
     170              :         //out << "VariableArray (n=" << arr.size() << ") [ ";
     171              :         for(size_t i=0; i<arr.size(); i++)
     172              :                 out << arr[i] << " ";
     173              :         out << "]";
     174              :         return out;
     175              : }
     176              : 
     177              : 
     178              : ////////////////////////////////////////////////////////////////////////////////
     179              : 
     180              : 
     181              : template<typename T, eMatrixOrdering T_ordering>
     182            0 : VariableArray2<T, T_ordering>::VariableArray2()
     183              : {
     184            0 :         values = NULL;
     185            0 :         rows = 0;
     186            0 :         cols = 0;
     187              : }
     188              : 
     189              : 
     190              : template<typename T, eMatrixOrdering T_ordering>
     191              : VariableArray2<T, T_ordering>::VariableArray2(size_t rows, size_t cols)
     192              : {
     193              :         values = NULL;
     194              :         rows = 0;
     195              :         cols = 0;
     196              :         resize(rows, cols);
     197              : }
     198              : 
     199              : template<typename T, eMatrixOrdering T_ordering>
     200            0 : VariableArray2<T, T_ordering>::VariableArray2(const VariableArray2<T, T_ordering> &other)
     201              : {
     202            0 :         if(this == &other) return;
     203            0 :         values = NULL;
     204            0 :         rows = 0;
     205            0 :         cols = 0;
     206            0 :         resize(other.num_rows(), other.num_cols(), false);
     207            0 :         for(size_type i=0; i<rows*cols; i++)
     208            0 :                 values[i] = other.values[i];
     209              : }
     210              : 
     211              : template<typename T, eMatrixOrdering T_ordering>
     212            0 : VariableArray2<T, T_ordering>::~VariableArray2()
     213              : {
     214            0 :         if(values) { delete[] values; values = NULL; }
     215              :         rows = cols = 0;
     216            0 : }
     217              : 
     218              : // Capacity
     219              : 
     220              : template<typename T, eMatrixOrdering T_ordering>
     221              : size_t
     222              : VariableArray2<T, T_ordering>::num_rows() const
     223              : {
     224            0 :         return rows;
     225              : }
     226              : 
     227              : template<typename T, eMatrixOrdering T_ordering>
     228              : size_t
     229              : VariableArray2<T, T_ordering>::num_cols() const
     230              : {
     231            0 :         return cols;
     232              : }
     233              : 
     234              : 
     235              : template<typename T, eMatrixOrdering T_ordering>
     236              : bool
     237            0 : VariableArray2<T, T_ordering>::resize(size_t newRows, size_t newCols, bool bCopyValues)
     238              : {
     239            0 :         if(newRows == rows && newCols == cols) return true;
     240              : 
     241            0 :         if(newRows == 0 && newCols == 0)
     242              :         {
     243            0 :                 rows = cols = 0;
     244            0 :                 if(values) delete[] values;
     245            0 :                 values = NULL;
     246            0 :                 return true;
     247              :         }
     248              : 
     249            0 :         value_type *new_values = new T[newRows*newCols];
     250            0 :         memset(reinterpret_cast<void *> (new_values), 0, sizeof(T)*newRows*newCols); // todo: think about that
     251              :         UG_ASSERT(new_values != NULL, "out of memory");
     252              :         if(new_values==NULL) return false;
     253              :         /*
     254              :         if(storage_traits<value_type>::is_static)
     255              :         {
     256              :                 ...
     257              :         }
     258              :         else {
     259              : 
     260              :          */
     261            0 :         if(bCopyValues)
     262              :         {
     263            0 :                 size_t minRows = std::min(rows, newRows);
     264            0 :                 size_t minCols = std::min(cols, newCols);
     265              : 
     266              :                 // we are using swap to avoid re-allocations
     267              :                 if(T_ordering==RowMajor)
     268              :                         for(size_t r=0; r<minRows; r++)
     269              :                                 for(size_t c=0; c<minCols; c++)
     270              :                                         std::swap(new_values[c+r*newCols], values[c+r*cols]);
     271              :                 else
     272            0 :                         for(size_t r=0; r<minRows; r++)
     273            0 :                                 for(size_t c=0; c<minCols; c++)
     274            0 :                                         std::swap(new_values[r+c*newRows], values[r+c*rows]);
     275              :         }
     276              : 
     277            0 :         if(values) delete[] values;
     278            0 :         rows = newRows;
     279            0 :         cols = newCols;
     280            0 :         values = new_values;
     281            0 :         return true;
     282              : }
     283              : 
     284              : 
     285              : template<typename T, eMatrixOrdering T_ordering>
     286              : T &
     287              : VariableArray2<T, T_ordering>::operator()(size_t r, size_t c)
     288              : {
     289              :         UG_ASSERT(r<rows, "r = " << r << ", rows = " << rows);
     290              :         UG_ASSERT(c<cols, "c = " << c << ", cols = " << cols);
     291              :         if(T_ordering==RowMajor)
     292              :                 return values[c+r*cols];
     293              :         else
     294            0 :                 return values[r+c*rows];
     295              : }
     296              : 
     297              : template<typename T, eMatrixOrdering T_ordering>
     298              : const T &
     299              : VariableArray2<T, T_ordering>::operator()(size_t r, size_t c) const
     300              : {
     301              :         UG_ASSERT(r<rows, "r = " << r << ", rows = " << rows);
     302              :         UG_ASSERT(c<cols, "c = " << c << ", cols = " << cols);
     303              :         if(T_ordering==RowMajor)
     304              :                 return values[c+r*cols];
     305              :         else
     306            0 :                 return values[r+c*rows];
     307              : }
     308              : 
     309              : template<typename T, eMatrixOrdering T_ordering>
     310              : std::ostream &operator << (std::ostream &out, const VariableArray2<T, T_ordering> &arr)
     311              : {
     312              :         out << "[ ";
     313              :         //out << "VariableArray2 (" << arr.num_rows() << "x" << arr.num_cols() << "), " << ((T_ordering == ColMajor) ? "ColMajor" : "RowMajor") << endl;
     314              :         typedef size_t size_type;
     315              :         for(size_type r=0; r<arr.num_rows(); r++)
     316              :         {
     317              :                 for(size_type c=0; c<arr.num_cols(); c++)
     318              :                         out << arr(r, c) << " ";
     319              :                 if(r != arr.num_rows()-1) out << "| ";
     320              :         }
     321              :         out << "]";
     322              :         return out;
     323              : }
     324              : 
     325              : }
     326              : #endif // __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
        

Generated by: LCOV version 2.0-1