grid2d.hpp
Go to the documentation of this file.
1// $Id: grid2d.hpp 882 2011-09-23 13:10:16Z perroe $
2
3// Copyright (c) 2011, Norwegian Computing Center
4// All rights reserved.
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7// • Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9// • Redistributions in binary form must reproduce the above copyright notice, this list of
10// conditions and the following disclaimer in the documentation and/or other materials
11// provided with the distribution.
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
13// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
15// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
17// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22#ifndef NRLIB_GRID2D_HPP
23#define NRLIB_GRID2D_HPP
24
25#include <cassert>
26#include <sstream>
27#include <vector>
28
29namespace NRLib {
30
31template<class A>
32class Grid2D {
33public:
34 typedef typename std::vector<A>::iterator iterator;
35 typedef typename std::vector<A>::const_iterator const_iterator;
36 typedef typename std::vector<A>::reference reference;
37 typedef typename std::vector<A>::const_reference const_reference;
38
39 Grid2D();
41 Grid2D(size_t ni, size_t nj, const A& val = A());
42 virtual ~Grid2D();
43
47 virtual void Resize(size_t ni, size_t nj, const A& val = A());
48
49 inline reference operator()(size_t i, size_t j);
50 inline reference operator()(size_t index);
51
52 inline const_reference operator()(size_t i, size_t j) const;
53 inline const_reference operator()(size_t index) const;
54
55 iterator begin() { return data_.begin(); }
56 iterator end() { return data_.end(); }
57
58 const_iterator begin() const { return data_.begin(); }
59 const_iterator end() const { return data_.end(); }
60
61 size_t GetNI() const { return ni_; }
62 size_t GetNJ() const { return nj_; }
63 size_t GetN() const { return data_.size(); }
64
65 inline size_t GetIndex(size_t i, size_t j) const;
66 void GetIJ(size_t index, size_t &i, size_t &j) const;
67
68 bool IsValidIndex(int i, int j) const;
69
70 void Swap(Grid2D<A>& other);
71
72 A FindMin(A missingValue) const;
73 A FindMax(A missingValue) const;
74
75private:
76 size_t ni_;
77 size_t nj_;
79 std::vector<A> data_;
80};
81
82template<class A>
84 : ni_(0),
85 nj_(0),
86 data_()
87{}
88
89template<class A>
90Grid2D<A>::Grid2D(size_t ni, size_t nj, const A& val)
91 : ni_(ni),
92 nj_(nj),
93 data_(ni*nj, val)
94{}
95
96template<class A>
98{}
99
100template<class A>
101void Grid2D<A>::Resize(size_t ni, size_t nj, const A& val)
102{
103 ni_ = ni;
104 nj_ = nj;
105
106 data_.resize(0); //To avoid copying of elements
107 data_.resize(ni_ * nj_, val);
108}
109
110
111template<class A>
112typename Grid2D<A>::reference Grid2D<A>::operator()(size_t i, size_t j)
113{
114 return(data_[GetIndex(i, j)]);
115}
116
117
118template<class A>
120{
121 assert(index < GetN());
122
123 return(data_[index]);
124}
125
126
127template<class A>
128typename Grid2D<A>::const_reference Grid2D<A>::operator()(size_t i, size_t j) const
129{
130 return(data_[GetIndex(i, j)]);
131}
132
133
134template<class A>
136{
137 assert(index < GetN());
138
139 return(data_[index]);
140}
141
142
143template<class A>
144size_t Grid2D<A>::GetIndex(size_t i, size_t j) const
145{
146 assert(i < ni_);
147 assert(j < nj_);
148
149 return(i+j*ni_);
150}
151
152template<class A>
153void Grid2D<A>::GetIJ(size_t index, size_t &i, size_t &j) const
154{
155 assert (index < GetN());
156
157 i = (index % ni_);
158 j = ((index-i)/ni_ % nj_);
159}
160
161
162template<class A>
163bool Grid2D<A>::IsValidIndex(int i, int j) const
164{
165 if (i >= 0 && static_cast<size_t>(i) < ni_ &&
166 j >= 0 && static_cast<size_t>(j) < nj_)
167 return true;
168
169 return false;
170}
171
172
173template<class A>
175{
176 std::swap(ni_, other.ni_);
177 std::swap(nj_, other.nj_);
178 data_.swap(other.data_);
179}
180
181template<class A>
182A Grid2D<A>::FindMin(A missingValue) const
183{
184 A minVal = (*this)(0);
185 typename std::vector<A>::const_iterator i;
186 for (i = this->begin(); i < this->end(); i++) {
187 if ((minVal == missingValue || (*i) < minVal) && (*i) != missingValue)
188 minVal = *i;
189 }
190 return minVal;
191}
192
193template<class A>
194A Grid2D<A>::FindMax(A missingValue) const
195{
196 A maxVal = (*this)(0);
197 typename std::vector<A>::const_iterator i;
198 for (i = this->begin(); i < this->end(); i++) {
199 if ((maxVal == missingValue || (*i) > maxVal) && (*i) != missingValue)
200 maxVal = *i;
201 }
202 return maxVal;
203}
204
205} // namespace NRLib
206
207#endif // NRLIB_GRID2D_HPP
int index
Definition: cJSON.h:168
Definition: grid2d.hpp:32
const_iterator begin() const
Definition: grid2d.hpp:58
virtual void Resize(size_t ni, size_t nj, const A &val=A())
Definition: grid2d.hpp:101
size_t GetN() const
Definition: grid2d.hpp:63
size_t GetIndex(size_t i, size_t j) const
Definition: grid2d.hpp:144
A FindMin(A missingValue) const
Definition: grid2d.hpp:182
std::vector< A >::iterator iterator
Definition: grid2d.hpp:34
std::vector< A >::reference reference
Definition: grid2d.hpp:36
virtual ~Grid2D()
Definition: grid2d.hpp:97
void Swap(Grid2D< A > &other)
Definition: grid2d.hpp:174
Grid2D()
Definition: grid2d.hpp:83
iterator end()
Definition: grid2d.hpp:56
bool IsValidIndex(int i, int j) const
Definition: grid2d.hpp:163
void GetIJ(size_t index, size_t &i, size_t &j) const
Definition: grid2d.hpp:153
size_t GetNI() const
Definition: grid2d.hpp:61
iterator begin()
Definition: grid2d.hpp:55
std::vector< A >::const_iterator const_iterator
Definition: grid2d.hpp:35
size_t GetNJ() const
Definition: grid2d.hpp:62
reference operator()(size_t i, size_t j)
Definition: grid2d.hpp:112
const_iterator end() const
Definition: grid2d.hpp:59
std::vector< A >::const_reference const_reference
Definition: grid2d.hpp:37
A FindMax(A missingValue) const
Definition: grid2d.hpp:194
not_this_one begin(...)
Definition: exception.hpp:31