grid5d.hpp
Go to the documentation of this file.
1// $Id: grid5d.hpp 1142 2013-03-18 15:13:41Z hgolsen $
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_GRID5D_HPP
23#define NRLIB_GRID5D_HPP
24
25#include <cassert>
26#include <sstream>
27#include <vector>
28
29namespace NRLib {
30
31template<class A>
32class Grid5D {
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 Grid5D();
41 Grid5D(size_t ni, size_t nj, size_t nk, size_t nl, size_t nm, const A& val = A());
42 virtual ~Grid5D();
43
47 void Resize(size_t ni, size_t nj, size_t nk, size_t nl, size_t nm, const A& val = A());
48
49 inline reference operator()(size_t i, size_t j, size_t k, size_t l, size_t m);
50 inline reference operator()(size_t index);
51
52 inline const_reference operator()(size_t i, size_t j, size_t k, size_t l, size_t m) 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 GetNK() const { return(nk_); }
64 size_t GetNL() const { return(nl_); }
65 size_t GetNM() const { return(nm_); }
66 size_t GetN() const { return data_.size(); }
67
68 inline size_t GetIndex(size_t i, size_t j, size_t k, size_t l, size_t m) const;
69 void GetIJKLM(size_t index, size_t &i, size_t &j, size_t &k, size_t &l, size_t &m) const;
70
71 void Swap(Grid5D<A>& other);
72
73private:
74 size_t ni_;
75 size_t nj_;
76 size_t nk_;
77 size_t nl_;
78 size_t nm_;
79
81 std::vector<A> data_;
82};
83
84template<class A>
86 : ni_(0),
87 nj_(0),
88 nk_(0),
89 nl_(0),
90 nm_(0),
91 data_()
92{}
93
94template<class A>
95Grid5D<A>::Grid5D(size_t ni, size_t nj, size_t nk, size_t nl, size_t nm, const A& val)
96 : ni_(ni),
97 nj_(nj),
98 nk_(nk),
99 nl_(nl),
100 nm_(nm),
101 data_(ni*nj*nk*nl*nm, val)
102{}
103
104template<class A>
106{}
107
108template<class A>
109void Grid5D<A>::Resize(size_t ni, size_t nj, size_t nk, size_t nl, size_t nm, const A& val)
110{
111 ni_ = ni;
112 nj_ = nj;
113 nk_ = nk;
114 nl_ = nl;
115 nm_ = nm
116
117 data_.resize(0); //To avoid copying of elements
118 data_.resize(ni_ * nj_ * nk_ * nl * nm, val);
119}
120
121
122template<class A>
123typename Grid5D<A>::reference Grid5D<A>::operator()(size_t i, size_t j, size_t k, size_t l, size_t m)
124{
125 return data_[GetIndex(i, j, k, l, m)];
126}
127
128
129template<class A>
131{
132 assert(index < GetN());
133
134 return data_[index];
135}
136
137
138template<class A>
139typename Grid5D<A>::const_reference Grid5D<A>::operator()(size_t i, size_t j, size_t k, size_t l, size_t m) const
140{
141 return data_[GetIndex(i, j, k, l, m)];
142}
143
144
145template<class A>
147{
148 assert(index < GetN());
149
150 return data_[index];
151}
152
153
154template<class A>
155size_t Grid5D<A>::GetIndex(size_t i, size_t j, size_t k, size_t l, size_t m) const
156{
157 assert(i < GetNI() && j < GetNJ() && k < GetNK() && l < GetNL() && m < GetNM());
158
159 return i + j*ni_ + k*ni_*nj_ + l*ni_*nj_*nk_ + m*ni_*nj_*nk_*nl_;
160}
161
162template<class A>
163void Grid5D<A>::GetIJKLM(size_t index, size_t &i, size_t &j, size_t &k, size_t &l, size_t &m) const
164{
165 assert(index < GetN());
166
167 i = index % ni_;
168 j = (index-i)/ni_ % nj_;
169 k = (index - j*ni_ - i)/ni_/nj_;
170 l = (index - k*ni_*nj_)/ni_/nj_/nk_; //?
171 m = (index - m*ni_*nj_*nk_)/ni_/nj_/nk_/nl_; //?
172}
173
174template<class A>
176{
177 std::swap(ni_, other.ni_);
178 std::swap(nj_, other.nj_);
179 std::swap(nk_, other.nk_);
180 std::swap(nl_, other.nl_);
181 std::swap(nm_, other.nm_);
182 data_.swap(other.data_);
183}
184
185}
186#endif
187
188
189
190
191
192
193
int index
Definition: cJSON.h:168
Definition: grid5d.hpp:32
void Resize(size_t ni, size_t nj, size_t nk, size_t nl, size_t nm, const A &val=A())
Definition: grid5d.hpp:109
std::vector< A >::iterator iterator
Definition: grid5d.hpp:34
const_iterator begin() const
Definition: grid5d.hpp:58
size_t GetNL() const
Definition: grid5d.hpp:64
virtual ~Grid5D()
Definition: grid5d.hpp:105
std::vector< A >::reference reference
Definition: grid5d.hpp:36
iterator end()
Definition: grid5d.hpp:56
void Swap(Grid5D< A > &other)
Definition: grid5d.hpp:175
size_t GetNM() const
Definition: grid5d.hpp:65
std::vector< A >::const_iterator const_iterator
Definition: grid5d.hpp:35
iterator begin()
Definition: grid5d.hpp:55
size_t GetNI() const
Definition: grid5d.hpp:61
reference operator()(size_t i, size_t j, size_t k, size_t l, size_t m)
Definition: grid5d.hpp:123
const_iterator end() const
Definition: grid5d.hpp:59
size_t GetNK() const
Definition: grid5d.hpp:63
size_t GetN() const
Definition: grid5d.hpp:66
Grid5D()
Definition: grid5d.hpp:85
void GetIJKLM(size_t index, size_t &i, size_t &j, size_t &k, size_t &l, size_t &m) const
Definition: grid5d.hpp:163
size_t GetIndex(size_t i, size_t j, size_t k, size_t l, size_t m) const
Definition: grid5d.hpp:155
std::vector< A >::const_reference const_reference
Definition: grid5d.hpp:37
size_t GetNJ() const
Definition: grid5d.hpp:62
Definition: exception.hpp:31