Skip to content

Commit dc6b6d6

Browse files
committed
Update README
1 parent 456cf39 commit dc6b6d6

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

README.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ These are the most useful operations.
44
- `.sort_by_field<col_idx>()` sort all columns in tandem based on particular column
55
- `operator[row_idx]` read data out as tuple of references
66
- `.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
78

89
Code Example (scratch.cpp)
910
------------------------
@@ -16,6 +17,10 @@ int main(int argc, char *argv[])
1617
{
1718
// presidents will be a soa representing
1819
// order, first name, last name
20+
21+
constexpr int ORDER = 0;
22+
constexpr int FIRST_NAME = 1;
23+
constexpr int LAST_NAME = 2;
1924
vapid::soa<int, std::string, std::string> presidents;
2025

2126
presidents.insert(0, "Abraham", "Lincoln");
@@ -29,35 +34,48 @@ int main(int argc, char *argv[])
2934
std::cout << presidents << "\n";
3035

3136
// sort by time (first column)
32-
presidents.sort_by_field<0>();
37+
presidents.sort_by_field<ORDER>();
3338
std::cout << "Presidents sorted by temporal order" << "\n";
3439
std::cout << presidents << "\n";
3540

3641
// sort by first name (second column)
37-
presidents.sort_by_field<1>();
42+
presidents.sort_by_field<FIRST_NAME>();
3843
std::cout << "Presidents sorted by first name" << "\n";
3944
std::cout << presidents << "\n";
4045

4146
// sort by last name (third column)
42-
presidents.sort_by_field<2>();
47+
presidents.sort_by_field<LAST_NAME>();
4348
std::cout << "Presidents sorted by last name" << "\n";
4449
std::cout << presidents << "\n";
4550

4651
// operator[] returns a tuple of references
4752
// 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+
}
5269

5370
// get_column<idx> returns direct access
5471
// 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";
6179

6280
// We can pass a custom comparator when sorting
6381
// Let's sort based on length of last name
@@ -69,7 +87,6 @@ int main(int argc, char *argv[])
6987

7088
return 0;
7189
}
72-
7390
```
7491
7592
Manual Installation

0 commit comments

Comments
 (0)