-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray1D.H
More file actions
289 lines (220 loc) · 5.05 KB
/
Copy pathArray1D.H
File metadata and controls
289 lines (220 loc) · 5.05 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***********************************************************************
Array1D.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros (martiandna@gmail.com).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#ifndef INCL_BOOM_Array1D_H
#define INCL_BOOM_Array1D_H
using namespace std;
#include <iostream>
#include <sstream>
#include "Stacktrace.H"
#include "File.H"
#include "Vector.H"
#include "Exceptions.H"
//#define DEBUG
namespace BOOM {
template<class T>
class Array1D
{
public:
Array1D(int size=0);
Array1D(const BOOM::Array1D<T> &);
Array1D(const BOOM::Vector<T> &);
virtual ~Array1D();
T &operator[](int);
const T &operator[](int) const;
int size() const;
void printOn(ostream &os) const;
void readFrom(istream &is);
void resize(int newSize);
void safeResize(int newSize); // retains original contents (nondestructive)
void setAllTo(T);
void saveBytes(BOOM::File &); // use only for basic data types!
void loadBytes(BOOM::File &); // use only for basic data types!
BOOM::Array1D<T> &operator=(const BOOM::Array1D<T> &);
BOOM::Array1D<T> &operator=(const BOOM::Vector<T> &);
T *getRawArray() {return array;}
void cut(int index);
private:
int n;
T *array;
void checkIndex(int index) const;
};
typedef BOOM::Array1D<double> DblArray1D;
typedef BOOM::Array1D<float> FloatArray1D;
typedef BOOM::Array1D<int> IntArray1D;
typedef BOOM::Array1D<long> LongArray1D;
typedef BOOM::Array1D<unsigned> UnsignedArray1D;
typedef BOOM::Array1D<short> ShortArray1D;
typedef BOOM::Array1D<char> CharArray1D;
template<class T>
ostream &operator<<(ostream &os,const BOOM::Array1D<T> &a);
template<class T>
istream &operator>>(istream &os,BOOM::Array1D<T> &a);
}
template<class T>
BOOM::Array1D<T>::Array1D(const BOOM::Array1D<T> &other)
: array(new T[other.n]), n(other.n)
{
int n=other.size();
if(size()!=n) resize(n);
for(int i=0 ; i<n ; ++i)
array[i]=other.array[i];
}
template<class T>
BOOM::Array1D<T>::Array1D(const BOOM::Vector<T> &V)
: array(new T[V.size()]), n(V.size())
{
Array1D<T> &self=*this;
for(int i=0 ; i<n ; ++i)
self[i]=V[i];
}
template<class T>
BOOM::Array1D<T>::Array1D(int ni)
: n(ni), array(ni?new T[ni]:NULL)
{
// ctor
}
template<class T>
BOOM::Array1D<T>::~Array1D()
{
delete [] array;
}
template<class T>
ostream &BOOM::operator<<(ostream &os,const BOOM::Array1D<T> &a)
{
a.printOn(os);
return os;
}
template<class T>
T &BOOM::Array1D<T>::operator[](int i)
{
#ifdef DEBUG
checkIndex(i);
#endif
return array[i];
}
template<class T>
const T &BOOM::Array1D<T>::operator[](int i) const
{
#ifdef DEBUG
checkIndex(i);
#endif
return array[i];
}
template<class T>
int BOOM::Array1D<T>::size() const
{
return n;
}
template<class T>
void BOOM::Array1D<T>::checkIndex(int index) const
{
if(index<0 || index>=n)
{
stringstream os;
os << "Invalid index ("<<index<<" vs. "<<n<<") in BOOM::Array1D"
<< ends;
throw BOOM::Stacktrace(os.str());
}
}
template<class T>
void BOOM::Array1D<T>::printOn(ostream &os) const
{
os<<n<<endl;
int i;
for(i=0 ; i<n ; ++i)
os << array[i] << '\t';
}
template<class T>
void BOOM::Array1D<T>::readFrom(istream &is)
{
is>>n;
resize(n);
int i;
for(i=0 ; i<n ; ++i)
is >> array[i];
}
template<class T>
istream &BOOM::operator>>(istream &is,BOOM::Array1D<T> &a)
{
a.readFrom(is);
return is;
}
template<class T>
void BOOM::Array1D<T>::resize(int newSize)
{
delete [] array;
array=(newSize ? new T[newSize] : NULL);
n=newSize;
}
template<class T>
void BOOM::Array1D<T>::safeResize(int newSize)
{
if(newSize==n) return;
T *newArray=new T[newSize];
int numCopy=min(n,newSize);
for(int i=0 ; i<numCopy ; ++i) newArray[i]=array[i];
delete [] array;
array=newArray;
n=newSize;
}
template<class T>
void BOOM::Array1D<T>::setAllTo(T t)
{
int i;
for(i=0 ; i<n ; ++i)
array[i]=t;
}
template<class T>
void BOOM::Array1D<T>::saveBytes(BOOM::File &file)
{
file << n;
int size=n*sizeof(T);
file.write(size,static_cast<void*>(array));
}
template<class T>
void BOOM::Array1D<T>::loadBytes(BOOM::File &file)
{
file >> n;
int size=n*sizeof(T);
resize(n);
file.read(size,static_cast<void*>(array));
}
template<class T>
BOOM::Array1D<T> &BOOM::Array1D<T>::operator=(const BOOM::Array1D<T> &other)
{
if(n!=other.n)
{
delete [] array;
n=other.n;
array=new T[n];
}
for(int i=0 ; i<n ; ++i)
array[i]=other.array[i];
return *this;
}
template<class T>
BOOM::Array1D<T> &BOOM::Array1D<T>::operator=(const BOOM::Vector<T> &V)
{
int n=V.size();
resize(n);
BOOM::Array1D<T> &self=*this;
for(int i=0 ; i<n ; ++i)
self[i]=V[i];
return self;
}
template<class T>
void BOOM::Array1D<T>::cut(int index)
{
T *newArray=new T[n-1];
for(int from=0, to=0 ; from<n ; ++from)
if(from!=index) newArray[to++]=array[from];
delete [] array;
array=newArray;
--n;
}
#endif