-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprpl-cellStream.cpp
209 lines (177 loc) · 5.05 KB
/
prpl-cellStream.cpp
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
#include "prpl-cellStream.h"
vector<pair<unsigned int, unsigned int> >::const_iterator pRPL::CellStream::
_getLayerInfo(int lyrID) const {
vector<pair<unsigned int, unsigned int> >::const_iterator itrInfo = _vLayerInfos.begin();
while(itrInfo != _vLayerInfos.end()) {
if(itrInfo->first == lyrID) {
break;
}
itrInfo++;
}
return itrInfo;
}
pRPL::CellStream::
CellStream()
:_size(0),
_aStream(NULL) {}
pRPL::CellStream::
~CellStream() {
clear();
}
pRPL::CellStream& pRPL::CellStream::
operator=(const pRPL::CellStream &rhs) {
if(this != &rhs) {
clear();
_size = rhs._size;
_vCellCounts = rhs._vCellCounts;
_vLayerInfos = rhs._vLayerInfos;
_aStream = (void *)malloc(_size);
memcpy((char *)_aStream, (char *)(rhs._aStream), _size);
}
return *this;
}
void pRPL::CellStream::
clear() {
if(_aStream != NULL) {
free(_aStream);
_aStream = NULL;
}
_size = 0;
_vCellCounts.clear();
_vLayerInfos.clear();
}
size_t pRPL::CellStream::
size() const {
return _size;
}
int pRPL::CellStream::
getTotalCellCount() const {
int nCells = 0;
for(int iLyr = 0; iLyr < _vCellCounts.size(); iLyr++) {
nCells += _vCellCounts[iLyr];
}
return nCells;
}
vector<int>& pRPL::CellStream::
getCellCounts() {
return _vCellCounts;
}
const vector<pair<unsigned int, unsigned int> >& pRPL::CellStream::
getLayerInfos() const {
return _vLayerInfos;
}
bool pRPL::CellStream::
resize() {
if(_aStream != NULL) {
free(_aStream);
_aStream = NULL;
}
_size = 0;
for(int iLyr = 0; iLyr < _vLayerInfos.size(); iLyr++) {
unsigned int dataSize = _vLayerInfos[iLyr].second;
int nCells = _vCellCounts[iLyr];
if(dataSize <= 0 || nCells < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid data size (" << dataSize \
<< "), or invalid number of cells (" << nCells \
<< "), for Layer (" << _vLayerInfos[iLyr].first \
<< ")" << endl;
return false;
}
_size += (sizeof(long) + dataSize) * nCells;
}
_aStream = (void *)malloc(_size);
return true;
}
const vector<int>& pRPL::CellStream::
getCellCounts() const {
return _vCellCounts;
}
void* pRPL::CellStream::
getStream() {
return _aStream;
}
const void* pRPL::CellStream::
getStream() const {
return _aStream;
}
bool pRPL::CellStream::
addLayer(int lyrID,
unsigned int typeSize) {
if(lyrID < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid Layer ID (" << lyrID \
<< ")" << endl;
return false;
}
if(typeSize <= 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid data size (" << typeSize \
<< ")" << endl;
return false;
}
_vLayerInfos.push_back(make_pair(lyrID, typeSize));
_vCellCounts.push_back(0);
return true;
}
bool pRPL::CellStream::
addCell(long cellIdx,
const void *aCellVal) {
if(cellIdx < 0) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: invalid cell index (" << cellIdx << ")" \
<< endl;
return false;
}
if(aCellVal == NULL) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: NULL pointer to the cell's value" \
<< endl;
return false;
}
unsigned int typeSize = _vLayerInfos.back().second;
_aStream = (char *)realloc(_aStream, _size+sizeof(long)+typeSize);
if(!_aStream) {
cerr << __FILE__ << " function:" << __FUNCTION__ \
<< " Error: failed to re-allocate memory of the CellStream" << endl;
return false;
}
memcpy((char *)((char *)_aStream+_size), &cellIdx, sizeof(long));
memcpy((char *)((char *)_aStream+_size+sizeof(long)), aCellVal, typeSize);
_size += sizeof(long) + typeSize;
_vCellCounts.back()++;
return true;
}
unsigned int pRPL::CellStream::
getNumLayers() const {
return _vCellCounts.size();
}
unsigned int pRPL::CellStream::
getTypeSizeOnLayer(int lyrID) const {
vector<pair<unsigned int, unsigned int> >::const_iterator itrInfo = _getLayerInfo(lyrID);
return (itrInfo == _vLayerInfos.end()) ? 0 : itrInfo->second;
}
unsigned int pRPL::CellStream::
getNumCellsOnLayer(int lyrID) const {
vector<pair<unsigned int, unsigned int> >::const_iterator itrInfo = _getLayerInfo(lyrID);
return (itrInfo == _vLayerInfos.end()) ? 0 : _vCellCounts[itrInfo - _vLayerInfos.begin()];
}
void* pRPL::CellStream::
getCellsOnLayer(int lyrID) {
void *pCells = NULL;
vector<pair<unsigned int, unsigned int> >::const_iterator itrInfo = _getLayerInfo(lyrID);
if(itrInfo != _vLayerInfos.end()) {
size_t nBytesBefore = 0;
int iItem = 0;
unsigned int iLyr2, typeSize2, nCells2;
vector<pair<unsigned int, unsigned int> >::const_iterator itrInfo2 = _vLayerInfos.begin();
while(itrInfo2 < itrInfo) {
typeSize2 = itrInfo2->second;
nCells2 = _vCellCounts[itrInfo2 - _vLayerInfos.begin()];
nBytesBefore += (sizeof(long) + typeSize2) * nCells2;
itrInfo2++;
} // end -- while(itrInfo2 < itrInfo)
pCells = (char *)_aStream + nBytesBefore;
} // end -- if(itrInfo != _vLayerInfos.end())
return pCells;
}