Alpine3D 20250205.625fd38
VFSymetricMatrix.h
Go to the documentation of this file.
1/***********************************************************************************/
2/* Copyright 2009-2015 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3/***********************************************************************************/
4/* This file is part of Alpine3D.
5 Alpine3D is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Alpine3D is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with Alpine3D. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef VFSYMETRICMATRIX_H
19#define VFSYMETRICMATRIX_H
20
21#if defined(__clang__)
22 #include <unordered_map>
23#elif defined(__GNUC__)
24 #if __GNUC__ < 4
25 #include <ext/hash_map>
26 #else
27 #include <tr1/unordered_map>
28 #endif
29#else
30 #include <unordered_map>
31#endif
32#include <map>
33#include <meteoio/MeteoIO.h>
34
35/*
36This data structure store the symetric matrix of view factor.
37T represents the type of stored value
38U represents the type of passed and returned value of the structure
39*/
40template<class T, class U> class VFSymetricMatrix
41{
42 public:
44 VFSymetricMatrix(const int anx, const int any);
46
47 void resize(const int nx, const int ny);
48
49 void GetSize(int &nx, int &ny);
50
51 void Destroy();
52
53 U getElement(const unsigned int x, const unsigned int y);
54
55 const U operator()(const unsigned int& x, const unsigned int& y) const;
56
57 void setElement(const unsigned int x, const unsigned int y, U val);
58
59 int size();
60
62
63 private:
64#if defined(__clang__)
65 typedef std::unordered_map< int, T > my_map;
66#elif defined(__GNUC__)
67 #if __GNUC__ < 4
68 typedef __gnu_cxx::hash_map< int, T > my_map;
69 #else
70 typedef std::tr1::unordered_map< int, T > my_map;
71 #endif
72#else
73 typedef std::tr1::unordered_map< int, T > my_map;
74#endif
75 //typedef map< int, T > my_map;
76
77 my_map mapData; //for ordered map, declare <, = and index operators in constructor
78 unsigned int nx;
79 unsigned int ny;
80
81};
82
83template<class T, class U> int VFSymetricMatrix<T, U>::size()
84{
85 return (int) mapData.size();
86}
87
88template<class T, class U> U VFSymetricMatrix<T, U>::getElement(const unsigned int x, const unsigned int y)
89{
90 #ifndef NOSAFECHECKS
91 if ((x >= nx) || (y >= ny))
92 throw mio::IndexOutOfBoundsException(std::string(), AT);
93 #endif
94 int idx = x*ny+y;
95 if (x > y) {
96 idx = y*ny+x;
97 }
98 typename my_map::iterator j = mapData.find(idx);
99 if ( j == mapData.end() ) {
100 return static_cast<U>(0.);
101 } else {
102 return static_cast<U>((*j).second);
103 }
104}
105
106template<class T, class U> const U VFSymetricMatrix<T, U>::operator()(const unsigned int& x, const unsigned int& y) const
107{
108 #ifndef NOSAFECHECKS
109 if ((x >= nx) || (y >= ny))
110 throw mio::IndexOutOfBoundsException(std::string(), AT);
111 #endif
112 int idx = x*ny+y;
113 if (x > y) {
114 idx = y*ny+x;
115 }
116 typename my_map::const_iterator j = mapData.find(idx);
117 if ( j == mapData.end() ) {
118 return static_cast<U>(0.);
119 } else {
120 return static_cast<U>((*j).second);
121 }
122}
123
124template<class T, class U> void VFSymetricMatrix<T, U>::setElement(unsigned int x, unsigned int y, U val)
125{
126 #ifndef NOSAFECHECKS
127 if ((x >= nx) || (y >= ny))
128 throw mio::IndexOutOfBoundsException(std::string(), AT);
129 #endif
130 T tval = static_cast<T>(val);
131 if (tval != 0.) {
132 int idx = x*ny+y;
133 if (x > y) {
134 idx = y*ny+x;
135 }
136 mapData[idx] = tval;
137 }
138}
139
141{
142 nx = ny = 0;
143}
144
145template<class T, class U> VFSymetricMatrix<T, U>::VFSymetricMatrix(const int anx, const int any)
146{
147 nx = ny = 0;
148 resize(anx,any);
149}
150
152{
153 Destroy();
154}
155
156template<class T, class U> void VFSymetricMatrix<T, U>::resize(const int anx, const int any)
157{
158 Destroy();
159 if ((anx > 0) && (any > 0)){
160 nx = anx;
161 ny = any;
162 } else {
163 throw mio::IndexOutOfBoundsException(std::string(), AT);
164 }
165}
166
167template<class T, class U> void VFSymetricMatrix<T, U>::GetSize(int &anx, int &any)
168{
169 anx=nx;
170 any=ny;
171}
172
173template<class T, class U> void VFSymetricMatrix<T, U>::Destroy()
174{
175 mapData.clear();
176 nx=ny=0;
177}
178
180{
181 int anx,any;
182 val.GetSize(anx,any);
183
184 mapData = val.mapData;
185 nx = anx;
186 ny = any;
187
188 return *this;
189}
190
191#endif
Definition: VFSymetricMatrix.h:41
const U operator()(const unsigned int &x, const unsigned int &y) const
Definition: VFSymetricMatrix.h:106
void setElement(const unsigned int x, const unsigned int y, U val)
Definition: VFSymetricMatrix.h:124
~VFSymetricMatrix()
Definition: VFSymetricMatrix.h:151
U getElement(const unsigned int x, const unsigned int y)
Definition: VFSymetricMatrix.h:88
void Destroy()
Definition: VFSymetricMatrix.h:173
int size()
Definition: VFSymetricMatrix.h:83
VFSymetricMatrix()
Definition: VFSymetricMatrix.h:140
void resize(const int nx, const int ny)
Definition: VFSymetricMatrix.h:156
void GetSize(int &nx, int &ny)
Definition: VFSymetricMatrix.h:167
VFSymetricMatrix(const int anx, const int any)
Definition: VFSymetricMatrix.h:145
VFSymetricMatrix< T, U > & operator=(VFSymetricMatrix< T, U > &val)
Definition: VFSymetricMatrix.h:179