@@ -4,6 +4,7 @@ These are the most useful operations.
4
4
- ` .sort_by_field<col_idx>() ` sort all columns in tandem based on particular column
5
5
- ` operator[row_idx] ` read data out as tuple of references
6
6
- ` .get_column<col_idx>() ` direct access to underlying std::vector column
7
+ - ` .view<col_idx1, col_idx2, ...>() ` read subset of the columns out as a tuple of references
7
8
8
9
Code Example (scratch.cpp)
9
10
------------------------
@@ -16,6 +17,10 @@ int main(int argc, char *argv[])
16
17
{
17
18
// presidents will be a soa representing
18
19
// order, first name, last name
20
+
21
+ constexpr int ORDER = 0;
22
+ constexpr int FIRST_NAME = 1;
23
+ constexpr int LAST_NAME = 2;
19
24
vapid::soa<int, std::string, std::string> presidents;
20
25
21
26
presidents.insert(0, "Abraham", "Lincoln");
@@ -29,35 +34,48 @@ int main(int argc, char *argv[])
29
34
std::cout << presidents << "\n";
30
35
31
36
// sort by time (first column)
32
- presidents.sort_by_field<0 >();
37
+ presidents.sort_by_field<ORDER >();
33
38
std::cout << "Presidents sorted by temporal order" << "\n";
34
39
std::cout << presidents << "\n";
35
40
36
41
// sort by first name (second column)
37
- presidents.sort_by_field<1 >();
42
+ presidents.sort_by_field<FIRST_NAME >();
38
43
std::cout << "Presidents sorted by first name" << "\n";
39
44
std::cout << presidents << "\n";
40
45
41
46
// sort by last name (third column)
42
- presidents.sort_by_field<2 >();
47
+ presidents.sort_by_field<LAST_NAME >();
43
48
std::cout << "Presidents sorted by last name" << "\n";
44
49
std::cout << presidents << "\n";
45
50
46
51
// operator[] returns a tuple of references
47
52
// Let's update Joe Biden to Joseph Biden
48
- auto [order, fname, lname] = presidents[0];
49
- fname = "Joseph"; // Joe => Joseph
50
- std::cout << "Editing the first row to update Joe=>Joseph" << "\n";
51
- std::cout << presidents << "\n";
53
+ {
54
+ std::cout << "Editing the first row to update Joe => Joseph" << "\n";
55
+ auto [order, fname, lname] = presidents[0];
56
+ fname = "Joseph"; // Joe => Joseph
57
+ std::cout << presidents << "\n";
58
+ }
59
+
60
+ // view is templated to return a subset of fields
61
+ // Let's update Abraham Lincoln to George Washington
62
+ {
63
+ std::cout << "Editing the third row to update Abraham Lincoln => George Washington" << "\n";
64
+ auto [fname, lname] = presidents.view<FIRST_NAME,LAST_NAME>(3);
65
+ fname = "George";
66
+ lname = "Washington";
67
+ std::cout << presidents << "\n";
68
+ }
52
69
53
70
// get_column<idx> returns direct access
54
71
// to the underlying std::vector.
55
- // Let's update the first name column, changing
56
- // the entry George to George W.
57
- std::cout << "Editing the second row to update George=>George W." << "\n";
58
- auto& first_names = presidents.get_column<1>();
59
- first_names[1] = "George W.";
60
- std::cout << presidents << "\n";
72
+ // Let's sum the characters of the first names.
73
+ std::cout << "Summing first name lengths\n";
74
+ int length_sum = 0;
75
+ for (const auto& fname : presidents.get_column<FIRST_NAME>()) {
76
+ length_sum += fname.length();
77
+ }
78
+ std::cout << "Total characters used in first names = " << length_sum << "\n\n";
61
79
62
80
// We can pass a custom comparator when sorting
63
81
// Let's sort based on length of last name
@@ -69,7 +87,6 @@ int main(int argc, char *argv[])
69
87
70
88
return 0;
71
89
}
72
-
73
90
```
74
91
75
92
Manual Installation
0 commit comments