-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.h
159 lines (122 loc) · 3.22 KB
/
field.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef _FIELD_H_
#define _FIELD_H_
#include "misc.h"
// A 2D square field/grid of elements.
namespace sim{
template<typename T>
class Field{
public:
typedef index_t size_type;
Field();
Field(size_type _size);
Field(Field &&model);
Field(const Field &model);
~Field(){ free(); }
// Assignment.
Field& operator=(const Field &model){
if(this == &model)
return *this;
free();
size = model.size;
copyElements(model);
return *this;
}
Field& operator=(Field &&model){
if(this == &model)
return *this;
free();
data = model.data;
size = model.size;
model.data = nullptr;
return *this;
}
// Data access.
inline T& at(size_type column, size_type row){
if(row >= size || column >= size){
// throw std::out_of_range("Field::at: demanded element out of range.");
dummy = default_dummy_state;
return dummy;
}
return operator()(column, row);
}
inline const T& at(size_type column, size_type row) const{
if(row >= size || column >= size){
// throw std::out_of_range("Field::at: demanded element out of range.");
dummy = default_dummy_state;
return dummy;
}
return operator()(column, row);
}
inline T& operator()(size_type column, size_type row){
return data[row*size + column];
}
inline const T& operator()(size_type column, size_type row) const{
return data[row*size + column];
}
// Getters.
inline size_type dimension() const { return size; }
T sum() const {
// Get sum of all the elements. Based on Kahan summation algorithm.
T result = T();
T c = T();
for(index_t i = 0; i != size*size; ++i){
T y = data[i] - c;
T t = result + y;
c = t - result - y;
result = t;
}
return result;
}
// Setters.
inline void setDefaultDummyState(const T &value){
default_dummy_state = value;
}
private:
T *data;
index_t size;
// A dummy variable which is returned when out-of-range element is requested.
mutable T dummy;
T default_dummy_state;
inline void free(){
if(data){
delete[] data;
data = nullptr;
}
}
void copyElements(const Field &model);
};
//************************************************************
template<typename T>
Field<T>::Field()
: data(nullptr), size(0), default_dummy_state(T()) {}
//************************************************************
template<typename T>
Field<T>::Field(Field<T>::size_type _size)
: size(_size), default_dummy_state(T()) {
data = new T[size*size];
}
//************************************************************
template<typename T>
Field<T>::Field(Field<T> &&model)
: data(model.data), size(model.size), default_dummy_state(T()) {
model.data = nullptr;
}
//************************************************************
template<typename T>
Field<T>::Field(const Field<T> &model)
: size(model.size), default_dummy_state(T()) {
copyElements(model);
}
//************************************************************
// Only copy elements. Assume memory deallocation is not needed,
// as well as copying 'size' from 'model'.
template<typename T>
void Field<T>::copyElements(const Field<T> &model){
auto count = size*size;
data = new T[count];
for(size_type i = 0; i != count; ++i)
data[i] = model.data[i];
}
//************************************************************
}
#endif //_FIELD_H_