1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24
25 #include <queso/Defines.h>
26 #include <queso/2dArrayOfStuff.h>
27
28 namespace QUESO {
29
30 // Default constructor -------------------------------------------------
31 template <class T>
TwoDArray(unsigned int numRows,unsigned int numCols)32 TwoDArray<T>::TwoDArray(unsigned int numRows, unsigned int numCols)
33 : m_numRows(numRows),
34 m_numCols(numCols),
35 m_data (m_numRows,NULL)
36 {
37 for (unsigned int i = 0; i < m_numRows; ++i) {
38 m_data[i] = new std::vector<T*>(m_numCols,NULL);
39 }
40 }
41
42 // Destructor ----------------------------------------------------------
43 template <class T>
~TwoDArray()44 TwoDArray<T>::~TwoDArray()
45 {
46 for (unsigned int i = 0; i < m_numRows; ++i) {
47 for (unsigned int j = 0; j < m_numCols; ++j) {
48 if ((*(m_data[i]))[j] != NULL) delete (*(m_data[i]))[j];
49 }
50 delete m_data[i];
51 }
52 }
53
54 // Property methods-----------------------------------------------------
55 template <class T>
numRows()56 unsigned int TwoDArray<T>::numRows() const
57 {
58 return m_numRows;
59 }
60
61 template <class T>
numCols()62 unsigned int TwoDArray<T>::numCols() const
63 {
64 return m_numCols;
65 }
66
67 template <class T>
setLocation(unsigned int i,unsigned int j,T * info)68 void TwoDArray<T>::setLocation(unsigned int i, unsigned int j, T* info)
69 {
70 queso_require_msg(!((i >= m_numRows) || (j >= m_numCols) || (m_data[i] == NULL)), "invalid situation");
71 (*(m_data[i]))[j] = info;
72 return;
73 }
74
75 // Accessor methods-----------------------------------------------------
76 template <class T>
operator()77 T& TwoDArray<T>::operator()(unsigned int i, unsigned int j)
78 {
79 // FIXME - Should this really be active in optimized codes? - RHS
80 queso_require_less(i, m_numRows);
81 queso_require_less(j, m_numCols);
82 queso_require(m_data[i]);
83 queso_require((*m_data[i])[j]);
84
85 return *(*(m_data[i]))[j];
86 }
87
88 template <class T>
operator()89 const T& TwoDArray<T>::operator()(unsigned int i, unsigned int j) const
90 {
91 // FIXME - Should this really be active in optimized codes? - RHS
92 queso_require_less(i, m_numRows);
93 queso_require_less(j, m_numCols);
94 queso_require(m_data[i]);
95 queso_require((*m_data[i])[j]);
96
97 return *(*(m_data[i]))[j];
98 }
99
100 } // End namespace QUESO
101