Skip to content

Commit 37db5af

Browse files
committed
doc: book section on lending row iterators
1 parent 5ccfc4f commit 37db5af

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

book/src/table_collection_row_access.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
## Accessing table rows
22

3-
We may also access entire table rows by a row id:
3+
The rows of a table contain two types of data:
4+
5+
* Numerical data consisting of a single value.
6+
* Ragged data. Examples include metadata for all tables,
7+
ancestral states for sites, derived states for mutations,
8+
parent and location information for individuals, etc..
9+
10+
`tskit` provides two ways to access row data.
11+
The first is by a "view", which contains non-owning references
12+
to the ragged column data.
13+
The second is by row objects containing *copies* of the ragged column data.
14+
15+
The former will be more efficient when the ragged columns are populated.
16+
The latter will be more convenient to work with because the API is a standard
17+
rust iterator.
18+
19+
### Row views
20+
21+
To iterate over all views we use *lending* iterators:
22+
23+
```rust, noplaygound, ignore
24+
{{#include ../../tests/book_table_collection.rs:get_edge_table_rows_by_lending_iterator}}
25+
```
26+
27+
#### Lending iterators
28+
29+
The lending iterators are implemented using the [`streaming_iterator`](https://docs.rs/streaming-iterator/latest/streaming_iterator/) crate.
30+
(The community now prefers the term "lending" over "streaming" for this concept.)
31+
The `tskit` prelude includes the trait declarations that allow the code shown above to compile.
32+
33+
rust 1.65.0 stabilized Generic Associated Types, or GATs.
34+
GATs allows lending iterators to be implemented directly without the workarounds used in the `streaming_iterator` crate.
35+
We have decided not to implement our own lending iterator using GATs.
36+
Rather, we will see what the community settles on and will decide in the future whether or not to adopt it.
37+
38+
### Row objects
39+
40+
We may access entire table rows by a row id:
441

542
```rust, noplaygound, ignore
643
{{#include ../../tests/book_table_collection.rs:get_edge_table_row_by_id}}

tests/book_table_collection.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fn add_node_handle_error() {
6969

7070
#[test]
7171
fn get_data_from_edge_table() {
72+
use tskit::prelude::*;
7273
use rand::distributions::Distribution;
7374
let sequence_length = tskit::Position::from(100.0);
7475
let mut rng = rand::thread_rng();
@@ -119,6 +120,23 @@ fn get_data_from_edge_table() {
119120
panic!("that should have worked...");
120121
}
121122
// ANCHOR_END: get_edge_table_row_by_id
123+
//
124+
// ANCHOR: get_edge_table_rows_by_lending_iterator
125+
let mut edge_table_lending_iter = tables.edges().lending_iter();
126+
while let Some(row_view) = edge_table_lending_iter.next() {
127+
// there is only one row!
128+
assert_eq!(row_view.id, 0);
129+
assert_eq!(row_view.left, left);
130+
assert_eq!(row_view.right, right);
131+
assert_eq!(row_view.parent, parent);
132+
assert_eq!(row_view.child, child);
133+
assert!(row_view.metadata.is_none()); // no metadata in our table
134+
}
135+
// ANCHOR_END: get_edge_table_rows_by_lending_iterator
136+
137+
assert!(tables
138+
.check_integrity(tskit::TableIntegrityCheckFlags::default())
139+
.is_ok());
122140

123141
// ANCHOR: get_edge_table_rows_by_iterator
124142
for row in tables.edges_iter() {

0 commit comments

Comments
 (0)