-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTwoDimArray.h
More file actions
153 lines (116 loc) · 2.71 KB
/
DTwoDimArray.h
File metadata and controls
153 lines (116 loc) · 2.71 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
// This class is just a basic, templated 2-D array.
//
// David Crandall, 2003-
// crandall@cs.cornell.edu
//
#ifndef __DTWODIMARRAY_H__
#define __DTWODIMARRAY_H__
#include <assert.h>
#include <string.h>
template<class T>
class _DTwoDimArray
{
public:
///////////////////////////////////////////////////////
// constructors
_DTwoDimArray()
{
data = 0;
data_area = 0;
_rows = _cols = 0;
}
_DTwoDimArray(int __rows, int __cols)
{
_rows = __rows;
_cols = __cols;
data = 0;
data_area = 0;
initialize_storage();
}
_DTwoDimArray(int __rows, int __cols, const T *array)
{
_rows = __rows;
_cols = __cols;
data = 0;
data_area = 0;
initialize_storage();
memcpy(data_area, array, _rows * _cols * sizeof(T));
}
_DTwoDimArray(const _DTwoDimArray<T> &other)
{
assert(this != &other);
data = 0;
data_area = 0;
*this = other;
}
///////////////////////////////////////////////////////
// destructor
~_DTwoDimArray()
{
deallocate_storage();
}
///////////////////////////////////////////////////////
// assignment operator
_DTwoDimArray<T> &operator=(const _DTwoDimArray<T> &other)
{
if(this == &other)
return *this;
// profiler->begin(4);
if(!data || _rows != other.rows() || _cols != other.cols())
{
_rows = other.rows();
_cols = other.cols();
initialize_storage();
}
memcpy(data_area, other.data_area, _rows * _cols * sizeof(T));
// profiler->end(4);
return *this;
}
///////////////////////////////////////////////////////
// element access
inline T *operator[](int row) const { return data[row]; }
///////////////////////////////////////////////////////
// informational
inline int rows() const { return _rows; }
inline int cols() const { return _cols; }
inline T *data_ptr() const { return data_area; }
inline T **row_pointers() const { return data; }
protected:
void deallocate_storage()
{
if(data)
{
delete[] data;
delete[] data_area;
data = 0;
data_area = 0;
}
}
void initialize_storage()
{
// profiler->begin(2);
if(data)
deallocate_storage();
if(_rows > 0)
{
data = new T *[_rows];
data_area = new T[_rows * _cols];
}
else
{
data = 0;
data_area = 0;
}
T *cp = data_area;
for(int i=0; i<_rows; i++, cp+=_cols)
data[i] = cp;
// profiler->end(2);
}
/// Array of pointers to the beginning of each matrix row in data_area
T **data;
/// Pointer to actual matrix data
T *data_area;
/// Size of matrix
int _rows, _cols;
};
#endif