-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCArray.h
More file actions
86 lines (78 loc) · 1.39 KB
/
CArray.h
File metadata and controls
86 lines (78 loc) · 1.39 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
#ifndef PSCRCL_CARRAY
#define PSCRCL_CARRAY
#include "common.h"
namespace pscrCL {
template <typename T>
class CArray {
public:
CArray() {
data = 0;
size = 0;
}
explicit CArray(int _size) {
if(_size <= 0)
throw UnknownError("CArray::Invalid array size exception.");
data = new T[_size];
size = _size;
}
CArray(const CArray &_a) {
data = 0;
size = 0;
if(0 < _a.getSize()) {
data = new T[_a.getSize()];
size = _a.getSize();
for(int i = 0; i < size; i++)
(*this)[i] = _a[i];
}
}
CArray& operator=(const CArray &_a) {
if(this == &_a)
return *this;
if(0 < size)
delete [] data;
data = 0;
size = 0;
if(0 < _a.getSize()) {
data = new T[_a.getSize()];
size = _a.getSize();
for(int i = 0; i < size; i++)
(*this)[i] = _a[i];
}
return *this;
}
~CArray() {
if(0 < size)
delete [] data;
}
int getSize() const {
return size;
}
int getSizeInBytes() const {
return getSize() * sizeof(T);
}
const T* getPointer() const {
return data;
}
T* getPointer() {
return data;
}
const T& operator[](int _i) const {
#if FULL_DEBUG
if(_i < 0 || size <= _i)
throw UnknownError("CArray::Out of bounds exception.");
#endif
return data[_i];
}
T& operator[](int _i) {
#if FULL_DEBUG
if(_i < 0 || size <= _i)
throw UnknownError("CArray::Out of bounds exception.");
#endif
return data[_i];
}
private:
T *data;
int size;
};
}
#endif