-
Notifications
You must be signed in to change notification settings - Fork 0
/
SA.h
executable file
·244 lines (209 loc) · 5.04 KB
/
SA.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
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
/* template array with bounds checking inspired by Shane Ryan's SArray class */
#pragma once
#ifndef HEADER_SA_H
#define HEADER_SA_H
#include <cmath>
#include <vector>
#include <fstream>
#include <ostream>
#include <float.h>
#include <memory.h>
#include <iostream>
#include <algorithm>
using namespace std;
using std::ostream;
using std::vector;
namespace _SA{
template<class T>
class SA{
public:
long int vsizei, vsizej;
void init( long int _sizei, long int _sizej, long int _sizek){
if(elements){
free(elements);
}
sizei = _sizei;
sizej = _sizej;
sizek = _sizek;
long int size = sizei* ((sizej==0)?1:sizej) * ((sizek==0)?1:sizek);
mySize = size;
elements=NULL;
if(size > 0){
mySize = size;
elements = NULL;
elements= new T[size];
if(!elements){
printf("Error: SA.h: failed to allocate array.\n");
exit(1);
}
memset(&(elements[0]), '\0', mySize*sizeof(T));
}
}
void inline init(long int i, long int j){
init(i,j,0);
}
void inline init(long int i){
init(i,0,0);
}
SA(){
elements=NULL;
init(0,0,0);
}
SA(long int size){
elements=NULL;
init( size, 0, 0);
}
SA(long int sizei, long int sizej){
elements=NULL;
init(sizei, sizej, 0);
}
SA(long int sizei, long int sizej, long int sizek){
elements=NULL;
init(sizei, sizej, sizek);
}
~SA(){
free(elements);
}
inline SA(SA<T> * other){
if(!other){
mySize=0;
elements=NULL;
}
mySize = other->size();
if(mySize==0){
elements=NULL;
return;
}
else{
elements= new T[mySize];
for(long int i=0; i<mySize; i++)
elements[i]=(*other)[i];
}
sizei = other->sizei;
sizej = other->sizej;
sizek = other->sizek;
}
// return the number of elements
inline long int size(){
return mySize;
}
// return the number of elements
inline long int length(){
return mySize;
}
inline T & at(long int subscript){
return elements[subscript];
}
inline T & at(long int indi, long int indj){
return elements[ (indi * sizej) + indj];
}
inline T & at(long int indi, long int indj, long int indk){
if(sizei * sizej * sizek <1){
printf("Error: must only use 3-d index with 3d array.\n");
exit(1);
}
return elements[(indi * sizej *sizek) + (indj*sizek) + indk];
}
inline void set(long int subscript, T * e){
elements[subscript]=*e ;
}
inline T & operator[](long int subscript){
if(mySize == 0){
std::cerr << "\nError: SA.cpp: size()=0 Subscript " << subscript << " out of range" << std::endl;
exit( 1 );
}
if(subscript > mySize){
std::cerr << "\nError: SA.cpp: Subscript " << subscript << " out of range" << std::endl;
exit( 1 );
}
return elements[subscript];
}
inline SA<T> & operator = (SA<T> &a){
if( a.size() != mySize){
printf("SA.h: error: tried to equate arrays of different lengths.\n");
exit(1);
}
long int i=0;
for(i=0; i<mySize; i++){
elements[i] = a[i];
}
return a;
}
inline T total(){
T a = 0;
for(long int i=0; i<mySize; i++){
a += elements[i];
}
return a;
}
inline void sortf(){
// for float only
float * adr = (float*)((void*)(&elements[0]));
sort(adr, adr + (mySize));
}
float percentBinBot(float _percent_){
long int ind = (long int)(((float)(mySize-1))*(_percent_/100.));
return (float)(elements[ind]);
}
float percentBinTop(float _percent_){
long int ind = (mySize-1) - (long int)(((float)(mySize-1))*(_percent_/100.));
return (float)(elements[ind]);
}
float max(){
float m = FLT_MIN;
for(long int i=0; i<mySize; i++){
if(!((isnan(elements[i])||(isinf(elements[i])))))
if(elements[i]>m){
m = elements[i];
}
}
return m;
}
float max2(){
return max();
}
float min(){
float m = FLT_MAX;
for(long int i=0; i<mySize; i++){
if(!((isnan(elements[i])||isinf(elements[i]))))
if(elements[i]<m){
m=elements[i];
}
}
return m;
}
public:
long int mySize, sizei, sizej, sizek;
T* elements;
public:
inline void clear(void){
memset(elements, '\0', mySize*sizeof(T));
}
};
template <class T>
inline ostream &operator<<(ostream &output, SA<T> &out ){
long int i=0;
for(i=0; i<(out.length()); i++){
if(i!=(out.length()-1)){
output << out[i]<<",";
}
else{
output<<out[i];
}
}
return output;
}
template <typename T>
static inline void XORSwap(T&a, T&b){
if(&a != &b){
a^=b;
b^=a;
a^=b;
}
}
template <typename T>
static inline T _abs(T a){
return (a<0)?(-a):(a);
}
};
#endif