Repository navigation
Expand file tree
/
Copy pathdata_set.cpp
More file actions
155 lines (129 loc) · 3.76 KB
/
Copy pathdata_set.cpp
File metadata and controls
155 lines (129 loc) · 3.76 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
154
155
#include "data_set.h"
namespace datastream {
DataSet::DataSet(const SchemaSet& schema_set):
schema_set(schema_set)
{};
void DataSet::load(const list<SchemaElement> & schema_elements, const string & line)
{
std::smatch matched;
std::regex_match(
line,
matched,
data_row_line_pattern
);
if (!matched.length()){
throw std::domain_error("data row format does not match expected pattern.");
}
rows.emplace_back(
std::stoi(matched[match_index_data_row_id]),
std::stoi(matched[match_index_data_parent]),
rows.size()
);
//current data row is last added
rows.rbegin()->load(
schema_elements,
matched[match_index_data_element_values]
);
}
// rows are stored in a list to give us a stable address
// now put pointers to rows into vector
// for sorting and random access
void DataSet::mapRows(DataSet* parent_set_ptr)
{
//push pointer to rows from list into vector
vector<DataRow*> row_ptrs;
for(DataRow& row : rows )
{
row_ptrs.push_back(&row);
}
if(schema_set.hasChildSets()){
//sort vector by row id
std::sort(
row_ptrs.begin(),
row_ptrs.end(),
[](const DataRow* a, const DataRow* b){
if (a->id() == b->id())
{
return a->position() < b->position();
}
return a->id() < b->id();
}
);
// map of vectors handles multiple appearances of same id
for (DataRow* row_ptr : row_ptrs){
if (id_to_rows_map.find(row_ptr->id()) == id_to_rows_map.end()){
id_to_rows_map.emplace_hint(
id_to_rows_map.end(),
row_ptr->id(),
vector<DataRow*>{row_ptr});
}
else{
id_to_rows_map[row_ptr->id()].push_back(row_ptr);
}
}
}
if(schema_set.hasParent() && parent_set_ptr != nullptr){
//reuse vector
//sort vector by row parent
std::sort(
row_ptrs.begin(),
row_ptrs.end(),
[](const DataRow* a, const DataRow* b){
if (a->parent() == b->parent())
{
return a->position() < b->position();
}
return a->parent() < b->parent();
}
);
/*
create a parent : vector of * map
*/
std::map<int, std::shared_ptr<std::vector<DataRow*>>> rows_by_parent_map;
for (DataRow* row_ptr : row_ptrs){
auto rows_by_parent_map_search = rows_by_parent_map.find(row_ptr->parent());
/* Drew Dormann -
It's a shame that "std::make_shared" doesn't recognize initilizer lists
unless they're explicit. The original code here wasn't exception-
unsafe in its current form, but this change reduces 2 heap allocs to
one. C++ also seems to be evolving away from raw "new" usage when possible. */
if (rows_by_parent_map_search == rows_by_parent_map.end()){
rows_by_parent_map.emplace_hint(
rows_by_parent_map.end(),
row_ptr->parent(),
std::make_shared<std::vector<DataRow*>>(std::initializer_list<DataRow*>{row_ptr})
);
}
else {
rows_by_parent_map_search->second.get()->push_back(row_ptr);
}
}
// now nest rows into parent by passing a shared pointer
// to any matching parent rows
for (auto& parent_id_child_rows : rows_by_parent_map){
auto parent_rows_search = parent_set_ptr->id_to_rows_map.find(parent_id_child_rows.first);
if(parent_rows_search != parent_set_ptr->id_to_rows_map.end()){
auto parent_rows = parent_rows_search->second;
for(DataRow* parent_row : parent_rows)
{
parent_row->connect(id(), parent_id_child_rows.second);
}
}
}
}
}
void DataSet::write (ostream & os, Formatter& formatter) const
{
unsigned int siblings_written = 0;
formatter.open(os, schema_set.groupWrapper());
writeRows (
os,
RowWrapper::no_wrapper, //parent row wrapper - root has no parent
schema_set, //schema set for rows
rows,
formatter,
siblings_written
);
formatter.close(os, schema_set.groupWrapper());
}
}