-
Notifications
You must be signed in to change notification settings - Fork 1
/
FTable.cpp
265 lines (230 loc) · 6.63 KB
/
FTable.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
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
// More code for the FTable
#include "FTable.h"
using namespace img;
void
FTable::filter(long rowStart, long rowEnd,
const vector<string>& regexps) {
D->filter(D, rowStart, rowEnd, regexps);
}
void
FTable::filterRows(vector<bool>& vb) {
D->filterRows(D, vb);
}
void
FTable::filterRows(const string& expression) {
vector<bool> vb;
D->evaluate(vb, expression, H);
D->filterRows(D, vb);
}
FTable
FTable::extract(long rowStart, long rowEnd,
const vector<string>& regexps) const {
FTable result;
D->filter(result.D, rowStart, rowEnd, regexps);
result.D->clearChanged();
result.H->copyFrom(*H);
return result;
}
FTable
FTable::extractRows(vector<bool>& vb) const {
FTable result;
D->filterRows(result.D, vb);
result.D->clearChanged();
result.H->copyFrom(*H);
return result;
}
FTable
FTable::extractRows(const string& expression) const {
FTable result;
vector<bool> vb;
D->evaluate(vb, expression, H);
D->filterRows(result.D, vb);
result.D->clearChanged();
result.H->copyFrom(*H);
return result;
}
/////////////////////////////////////////////////
/// TableData implementations
/////////////////////////////////////////////////
TableData::~TableData() {
// Unlock for destruction:
lock = false;
for (iterator i=begin(); i!=end(); ++i) delete *i;
}
TableData*
TableData::duplicate() const {
TableData* dup = new TableData(rowReserve);
for (const_iterator i=begin(); i!=end(); ++i) dup->add((*i)->duplicate());
return dup;
}
void
TableData::copyFrom(const TableData& rhs) {
checkLock("TableData::copyFrom()");
clear();
rowReserve = rhs.rowReserve;
rowCount = rhs.rowCount;
isAltered = true;
for (const_iterator i=rhs.begin(); i!=rhs.end(); ++i) {
add((*i)->duplicate());
}
}
vector<string>
TableData::listColumns() const {
vector<string> out;
for (const_iterator i = begin();
i != end();
++i)
out.push_back((*i)->name());
return out;
}
bool
TableData::hasColumn(const string& colname) const {
for (const_iterator i = begin();
i != end();
++i) {
if (stringstuff::nocaseEqual((*i)->name(),colname))
return true;
}
return false;
}
const ColumnBase*
TableData::constColumn(string colname) const {
Index::const_iterator i=columns.find(colname);
if (i==columns.end()) throw FTableNonExistentColumn(colname);
return i->second;
}
ColumnBase*
TableData::operator[](string colname) {
checkLock("operator[]");
Index::iterator i=columns.find(colname);
if (i==columns.end()) throw FTableNonExistentColumn(colname);
return i->second;
}
void
TableData::erase(string columnName) {
checkLock("erase()");
Index::iterator i=columns.find(columnName);
if (i==columns.end())
throw FTableNonExistentColumn(columnName);
// Destroy the column:
delete i->second;
columns.erase(i);
}
void
TableData::erase(iterator i) {
checkLock("erase()");
delete *i;
columns.erase(i.i);
}
void
TableData::clear() {
checkLock("clear()");
for (Index::iterator i=columns.begin();
i != columns.end();
++i)
delete i->second;
columns.clear();
rowCount = 0;
rowReserve=0; // Choose to eliminate reservations too. ??
}
void
TableData::eraseRows(long rowStart, long rowEnd) {
checkLock("eraseRows()");
Assert(rowStart>=0);
if (rowStart>=nrows()) return; // don't erase past end
if (columns.empty()) return;
if (rowEnd<0) rowEnd = nrows();
if (rowStart>=rowEnd) return;
for (iterator i=begin(); i!=end(); ++i) (*i)->eraseRows(rowStart, rowEnd);
// Update row count from first column:
rowCount = (*begin())->size();
}
void
TableData::insertRows(long insertBeforeRow, long insertNumber) {
checkLock("insertRows()");
if (insertBeforeRow > nrows()) throw FTableError("insertRows beyond end of table");
for (iterator i=begin(); i!=end(); ++i) (*i)->insertRows(insertBeforeRow, insertNumber);
rowCount += insertNumber;
Assert(rowCount==(*begin())->size());
}
// Expand all columns to some new size:
void
TableData::growRows(long targetRows) {
checkLock("growRows()");
if (targetRows <= nrows()) return;
rowCount = targetRows;
for (iterator i = begin(); i!=end(); ++i)
(*i)->resize(targetRows);
}
void
TableData::add(ColumnBase* newColumn) {
string addname = newColumn->name();
if (columns.find(addname) != columns.end())
throw FTableError("Adding column with duplicate name <" + addname + ">");
// Pad column to current table length, or vice-versa, to keep all columns same length
if (newColumn->size() < nrows()) newColumn->resize(nrows());
else if (newColumn->size() > nrows()) growRows(newColumn->size());
// Reserve requested space
newColumn->reserve(rowReserve);
columns.insert(std::pair<string, ColumnBase*>(addname, newColumn));
}
TableData*
TableData::extract(long rowStart, long rowEnd,
const vector<string>& regexps) const {
if (rowEnd < 0) rowEnd = nrows();
if (rowEnd < rowStart)
throw FTableError("TableData::extract() with rowEnd < rowStart");
TableData* td = new TableData(rowEnd - rowStart);
filter(td, rowStart, rowEnd, regexps);
return td;
}
void
TableData::filter(TableData* td, long rowStart, long rowEnd,
const vector<string>& regexps) const {
if (rowEnd < 0) rowEnd = nrows();
// rowStart and rowEnd checked before entry here.
td->checkLock("filter()");
// Get set of all columns for output:
bool writeSelf = (this==td);
if (!writeSelf) td->clear();
td->touch();
// Tell destination table the size that all columns will end up with:
td->rowCount = rowEnd - rowStart;
vector<string> allColumns = listColumns();
set<string> getThese = stringstuff::findMatches(regexps, allColumns);
for (vector<string>::iterator i = allColumns.begin();
i != allColumns.end();
++i) {
bool keeper = getThese.find(*i) != getThese.end();
if ( keeper) {
ColumnBase* shorter = (*this)[*i]->copyRows(rowStart, rowEnd);
if (writeSelf) td->erase(*i);
td->add(shorter);
} else {
// Unwanted column
if (writeSelf) td->erase(*i);
}
}
}
void
TableData::filterRows(TableData* td, vector<bool>& vb) const {
td->checkLock("filter()");
// Get set of all columns for output:
vector<string> allColumns = listColumns();
bool writeSelf = (this==td);
if (!writeSelf) td->clear();
td->touch();
// Count up the number of output rows:
int outRows = 0;
for (int i=0; i<vb.size(); i++)
if (vb[i]) outRows++;
// Tell destination table the size that all columns will end up with:
td->rowCount = outRows;
for (vector<string>::iterator i = allColumns.begin();
i != allColumns.end();
++i) {
ColumnBase* shorter = (*this)[*i]->copyRows(vb, outRows);
if (writeSelf) td->erase(*i);
td->add(shorter);
}
}