-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoreResults.h
343 lines (292 loc) · 11.5 KB
/
StoreResults.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#ifndef STORERESULTS_H
#define STORERESULTS_H
#include <algorithm> // std::sort
#include <functional> // std::greater
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <ctime>
#include <utility>
#include <vector>
template <typename TKEY, typename TDATA>
class StoreResults {
template<typename TSAMPLEKEY, typename TSAMPLE>
class SampleData {
public:
SampleData(const TSAMPLEKEY& key, const TSAMPLE& sample)
: m_sampleData(1, sample)
, m_countPerKey(1)
, m_key(key)
, m_creationTime()
{}
bool operator< (const SampleData<TSAMPLEKEY, TSAMPLE>& ts) const { return m_key < ts.m_key; }
size_t GetCount(void) const { return m_countPerKey; }
SampleData operator= ( const SampleData& data ){
m_sampleData = data.m_sampleData;
m_key = data.m_key;
m_countPerKey = data.m_countPerKey;
m_creationTime = data.m_creationTime;
return *this;
}
std::vector<TSAMPLE> m_sampleData;
size_t m_countPerKey;
TSAMPLEKEY m_key;
protected:
time_t m_creationTime;
};
public:
friend std::ostream& operator<<(std::ostream& os, const StoreResults<TKEY, TDATA>& store) {
os << "Contents of StoreResults" << std::endl;
os << "m_title " << store.GetTitle() << std::endl;
os << "m_nmax " << store.GetNmax() << std::endl;
os << "Total Seen " << store.GetTotalSeen() << std::endl;
os << "m_data.size() " << store.size() << std::endl;
os << "m_itemSeparator " << store.GetItemSeparator() << std::endl;
os << "m_valueSeparator " << store.GetValueSeparator() << std::endl;
return os;
}
StoreResults(const size_t nMax = 1)
: m_nmax(nMax)
, m_keyName("Key")
, m_creationTime(static_cast<long int>(time(NULL)))
{}
void Store(const TKEY& key, const TDATA& data) {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::iterator itfind = m_tree.find(key);
if (itfind == m_tree.end()) {
m_tree.insert(std::make_pair(key, SampleData<TKEY, TDATA>(key, data)));
}
else {
if ((*itfind).second.m_sampleData.size() < m_nmax)
(*itfind).second.m_sampleData.push_back(data);
++(*itfind).second.m_countPerKey;
}
}
size_t size(void) const { return m_tree.size(); }
bool empty(void) const { return m_tree.empty(); }
void erase( const TKEY& key) {
m_tree.erase(key);
}
TKEY GetByTreeIndex(const size_t index) const {
return m_tree[index];
}
size_t GetItemCount(const TKEY& key) const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator itfind = m_tree.find(key);
if (itfind != m_tree.end()) {
return (*itfind).second.GetCount();
}
else
return 0UL;
}
size_t GetTotalSampleCount(void) const {
std::vector < TKEY > keys = GetKeys();
size_t totalSampleCount = 0UL;
for (size_t i = 0; i < keys.size(); ++i) {
totalSampleCount += GetResult(keys[i]).size();
}
return totalSampleCount;
}
size_t GetTotalSeen() const {
size_t totalSeen = 0UL;
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
for (it = m_tree.begin(); it != m_tree.end(); ++it) {
totalSeen += (*it).second.m_countPerKey;
}
return totalSeen;
}
std::string GetHerald() const {
std::ostringstream ostr;
if (m_title.empty()) {
ostr << "ShowResults ";
}
else {
ostr << m_title << std::endl;
}
ostr << "Total Keys = " << GetKeys().size()
<< " Total Samples = " << GetTotalSampleCount()
<< " Total Seen = " << GetTotalSeen() << std::endl;
return ostr.str();
}
size_t GetNmax(void) const { return m_nmax; }
std::vector<std::pair<TKEY, TDATA> > GetResult(const TKEY& key) const {
const typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it = m_tree.find(key);
if (it == m_tree.end()) {
return std::vector<std::pair<TKEY, TDATA> >();
}
else {
std::vector<std::pair<TKEY, TDATA> > v;
for (size_t i = 0; i < (*it).second.m_sampleData.size(); ++i) {
const std::pair<TKEY, TDATA> sd(key, (*it).second.m_sampleData[i]);
v.push_back(std::make_pair(key, (*it).second.m_sampleData[i]));
}
return v;
}
}
std::vector<TKEY> GetKeys() const {
std::vector<TKEY> v;
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
for (it = m_tree.begin(); it != m_tree.end(); ++it) {
const std::pair<TKEY, SampleData<TKEY, TDATA> > ctree = *it;
v.push_back(ctree.first);
}
return v;
}
void ShowItem(const TKEY& key) const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it = m_tree.find(key);
std::cout << m_keyName << "= " << key << " (total=" << (*it).second.m_countPerKey << ")" << std::endl;
for (size_t k = 0; k < (*it).second.m_sampleData.size(); ++k) {
std::cout << (*it).second.m_sampleData[k] << std::endl;
}
}
void ShowResultsByKeyAscending(void) const {
std::vector<TKEY> vv(GetKeys());
std::sort(vv.begin(), vv.end());
ShowResultsBySortedKey(vv);
}
void ShowResultsByKeyDescending(void) const {
std::vector<TKEY> vv(GetKeys());
std::reverse(vv.begin(), vv.end());
ShowResultsBySortedKey(vv);
}
void ShowResults(void) const {
std::cout << GetHerald() << std::endl;
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
std::vector<std::pair<int, SampleData<TKEY, TDATA> > > indexToSort(PrepareListOfItemsSortedByCount());
for (size_t i = 0; i < indexToSort.size(); ++i) {
if (!m_itemSeparator.empty()) std::cout << m_itemSeparator << std::endl;
std::cout << "item " << i << " (total=" << indexToSort[i].first << ")" << " " << m_keyName << " = " << indexToSort[i].second.m_key << std::endl;
it = m_tree.find(indexToSort[i].second.m_key);
if (m_nmax > 0) {
for (size_t k = 0; k < (*it).second.m_sampleData.size(); ++k) {
if (!m_valueSeparator.empty()) std::cout << m_valueSeparator << std::endl;
std::cout << (*it).second.m_sampleData[k] << std::endl;
}
if (m_itemSeparator.empty()) std::cout << std::endl;
}
}
ShowTableOfKeysVersusCount();
}
void SetTitle(const std::string& s) {
m_title = s;
}
void SetFooter(const std::string& s) {
m_footer = s;
}
void SetKeyLabel(const std::string& s) { m_keyName = s; }
void AppendTitle(const std::string s) { m_title += " " + s; }
void SetMaxItemStore(const int n) { m_nmax = n; }
void SetItemSeparator(const std::string& s) { m_itemSeparator = s; }
void SetValueSeparator(const std::string& s) { m_valueSeparator = s; }
bool HasKey(const TKEY& key) const { return (m_tree.find(key) != m_tree.end()); }
std::string GetItemSeparator(void) const { return m_itemSeparator; }
std::string GetValueSeparator(void) const { return m_valueSeparator; }
std::string GetTitle(void) const { return m_title; }
StoreResults operator= (const StoreResults& sr) {
m_nmax = sr.m_nmax;
m_tree = sr.m_tree;
m_itemSeparator = sr.m_itemSeparator;
m_valueSeparator = sr.m_valueSeparator;
m_notes = sr.m_notes;
return *this;
}
void clear(void) {
m_tree.clear();
m_notes.clear();
m_herald.clear();
m_title.clear();
m_itemSeparator.clear();
m_valueSeparator.clear();
}
size_t DeleteIfCountLessThan(const size_t n) {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::iterator it;
size_t count = 0;
for (it = m_tree.begin(); it != m_tree.end();) {
if ((*it).second.m_countPerKey < n) {
it = m_tree.erase(it);
++count;
}
else {
++it;
}
}
return count;
}
long DeleteIfCountGreaterThan(const long n) {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::iterator it;
long count = 0;
for (it = m_tree.begin(); it != m_tree.end();) {
if ((*it).second.m_countPerKey > (size_t)(n)) {
it = m_tree.erase(it);
++count;
}
else {
++it;
}
}
return count;
}
TKEY GetMaxKey() const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it =
std::max_element(m_tree.begin(), m_tree.end());
return (*it).first;
}
TKEY GetMinKey() const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it =
std::min_element(m_tree.begin(), m_tree.end());
return (*it).first;
}
std::pair<TKEY,TDATA> GetFirstItem( const TKEY& key) const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
it = m_tree.find(key);
if (it == m_tree.end())
return std::make_pair(TKEY(), TDATA());
return std::make_pair( (*it).first, (*it).second.m_sampleData[0]);
}
void ShowTableOfKeysVersusCount() const {
std::vector<std::pair<int, SampleData<TKEY, TDATA> > > v(PrepareListOfItemsSortedByCount());
if (!v.empty()) std::cout << m_title << "\n" << "item count " << m_keyName << std::endl;
for (size_t i = 0; i<v.size(); ++i) {
std::cout << i << " " << v[i].first << " " << v[i].second.m_key << std::endl;
}
}
private:
std::vector<std::pair<int, SampleData<TKEY, TDATA> > > PrepareListOfItemsSortedByCount(void) const {
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
std::vector<std::pair<int, SampleData<TKEY, TDATA> > > indexToSort;
for (it = m_tree.begin(); it != m_tree.end(); ++it) {
indexToSort.push_back(std::make_pair((*it).second.m_countPerKey, (*it).second));
}
std::sort(indexToSort.begin(), indexToSort.end(), std::greater<std::pair<int, SampleData<TKEY, TDATA> > >());
return indexToSort;
}
void ShowResultsBySortedKey(const std::vector<TKEY>& keylist) const {
std::cout << GetHerald() << std::endl;
typename std::map<TKEY, SampleData<TKEY, TDATA> >::const_iterator it;
for (size_t i = 0; i < keylist.size(); ++i) {
if (!m_itemSeparator.empty()) std::cout << m_itemSeparator << std::endl;
it = m_tree.find(keylist[i]);
std::cout << "item " << i << " " << m_keyName << "= " << (*it).first << " count=" << (*it).second.GetCount() << std::endl;
if (m_nmax > 0) {
for (size_t k = 0; k < (*it).second.m_sampleData.size(); ++k) {
if (!m_valueSeparator.empty()) std::cout << m_valueSeparator << std::endl;
std::cout << (*it).second.m_sampleData[k] << std::endl;
}
if (m_itemSeparator.empty()) std::cout << std::endl;
}
}
ShowTableOfKeysVersusCount();
std::cout << m_footer << std::endl;
}
private:
size_t m_nmax;
std::map<TKEY, SampleData<TKEY, TDATA> > m_tree;
std::string m_itemSeparator;
std::string m_valueSeparator;
std::string m_herald;
std::string m_title;
std::string m_footer;
std::string m_notes;
std::string m_keyName;
int m_creationTime;
};
#endif