-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_carray.h
197 lines (176 loc) · 6 KB
/
wrapper_carray.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/********************************************/
/* Turbo Turtle CArray Wrapper Class */
/* */
/* Copyright (c) 2009 by Richard Goedeken */
/* [email protected] */
/********************************************/
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdlib.h>
#include <stdarg.h>
#include <memory.h>
template <typename T, int Dim>
class CArray
{ };
template <typename T>
class CArray<T,1>
{
public:
CArray() : pData(NULL), iSize1(0), iStart(0) {}
// default constuctor for a 0-filled array
CArray(int size, int start) : iSize1(size), iStart(start)
{
this->pData = new T[size];
for (int i = 0; i < size; i++)
this->pData[i] = 0.0;
}
// destructor
~CArray() { delete [] this->pData; }
// copy constructor
CArray(const CArray<T,1> &ref) : iSize1(ref.iSize1), iStart(ref.iStart)
{
this->pData = new T[this->iSize1];
memcpy(this->pData, ref.pData, this->iSize1 * sizeof(T));
}
// assignment operator
CArray<T,1> & operator=(const CArray<T,1> &ref)
{
// check for initializing from self
if (this == &ref)
return *this;
// otherwise free any memory I may be holding on to
if (this->pData)
delete [] this->pData;
// then construct myself from the input object
this->iSize1 = ref.iSize1;
this->iStart = ref.iStart;
this->pData = new T[this->iSize1];
memcpy(this->pData, ref.pData, this->iSize1 * sizeof(T));
return *this;
}
T Get(int idx)
{
if (idx < this->iStart || idx >= this->iStart + this->iSize1)
return 0.0;
return pData[idx - this->iStart];
}
void Set(T value, int idx)
{
if (idx >= this->iStart && idx < this->iStart + this->iSize1)
pData[idx - this->iStart] = value;
}
private:
T *pData;
int iSize1;
int iStart;
};
template <typename T>
class CArray<T,2>
{
public:
CArray() : pData(NULL), iSize1(0), iSize2(0) {}
// default constuctor for a 0-filled array
CArray(int size1, int size2) : iSize1(size1), iSize2(size2)
{
this->pData = new T[size1*size2];
for (int i = 0; i < size1*size2; i++)
this->pData[i] = 0.0;
}
// destructor
~CArray() { delete [] this->pData; }
// copy constructor
CArray(const CArray<T,2> &ref) : iSize1(ref.iSize1), iSize2(ref.iSize2)
{
this->pData = new T[this->iSize1*this->iSize2];
memcpy(this->pData, ref.pData, this->iSize1 * this->iSize2 * sizeof(T));
}
// assignment operator
CArray<T,2> & operator=(const CArray<T,2> &ref)
{
// check for initializing from self
if (this == &ref)
return *this;
// otherwise free any memory I may be holding on to
if (this->pData)
delete [] this->pData;
// then construct myself from the input object
this->iSize1 = ref.iSize1;
this->iSize2 = ref.iSize2;
this->pData = new T[this->iSize1*this->iSize2];
memcpy(this->pData, ref.pData, this->iSize1 * this->iSize2 * sizeof(T));
return *this;
}
T Get(int idx1, int idx2)
{
if (idx1 < 0 || idx2 < 0 || idx1 >= this->iSize1 || idx2 >= this->iSize2)
return 0.0;
return pData[idx1 * this->iSize2 + idx2];
}
void Set(T value, int idx1, int idx2)
{
if (idx1 >= 0 && idx2 >= 0 && idx1 < this->iSize1 && idx2 < this->iSize2)
pData[idx1 * this->iSize2 + idx2] = value;
}
private:
T *pData;
int iSize1, iSize2;
};
template <typename T>
class CArray<T,3>
{
public:
CArray() : pData(NULL), iSize1(0), iSize2(0), iSize3(0) {}
// default constuctor for a 0-filled array
CArray(int size1, int size2, int size3) : iSize1(size1), iSize2(size2), iSize3(size3)
{
this->pData = new T[size1*size2*size3];
for (int i = 0; i < size1*size2*size3; i++)
this->pData[i] = 0.0;
}
// destructor
~CArray() { delete [] this->pData; }
// copy constructor
CArray(const CArray<T,3> &ref) : iSize1(ref.iSize1), iSize2(ref.iSize2), iSize3(ref.iSize3)
{
this->pData = new T[this->iSize1*this->iSize2*this->iSize3];
memcpy(this->pData, ref.pData, this->iSize1 * this->iSize2 * this->iSize3 * sizeof(T));
}
// assignment operator
CArray<T,3> & operator=(const CArray<T,3> &ref)
{
// check for initializing from self
if (this == &ref)
return *this;
// otherwise free any memory I may be holding on to
if (this->pData)
delete [] this->pData;
// then construct myself from the input object
this->iSize1 = ref.iSize1;
this->iSize2 = ref.iSize2;
this->iSize3 = ref.iSize3;
this->pData = new T[this->iSize1*this->iSize2*this->iSize3];
memcpy(this->pData, ref.pData, this->iSize1 * this->iSize2 * this->iSize3 * sizeof(T));
return *this;
}
T Get(int idx1, int idx2, int idx3)
{
if (idx1 < 0 || idx2 < 0 || idx3 < 0 || idx1 >= this->iSize1 || idx2 >= this->iSize2 || idx3 >= this->iSize3)
return 0.0;
return pData[idx1 * this->iSize2 * this->iSize3 + idx2 * this->iSize3 + idx3];
}
void Set(T value, int idx1, int idx2, int idx3)
{
if (idx1 >= 0 && idx2 >= 0 && idx3 >= 0 && idx1 < this->iSize1 && idx2 < this->iSize2 && idx3 < this->iSize3)
pData[idx1 * this->iSize2 * this->iSize3 + idx2 * this->iSize3 + idx3] = value;
}
private:
T *pData;
int iSize1, iSize2, iSize3;
};