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