|
1 | 1 | ## Accessing table rows
|
2 | 2 |
|
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 | +By holding references, row views have the usual implications for borrowing. |
| 20 | +The row objects, however, own their data and are thus independent of their parent |
| 21 | +objects. |
| 22 | + |
| 23 | +### Row views |
| 24 | + |
| 25 | +To generate a row view using a row id: |
| 26 | + |
| 27 | +```rust, noplaygound, ignore |
| 28 | +{{#include ../../tests/book_table_collection.rs:get_edge_table_row_by_id}} |
| 29 | +``` |
| 30 | + |
| 31 | +To iterate over all views we use *lending* iterators: |
| 32 | + |
| 33 | +```rust, noplaygound, ignore |
| 34 | +{{#include ../../tests/book_table_collection.rs:get_edge_table_rows_by_lending_iterator}} |
| 35 | +``` |
| 36 | + |
| 37 | +#### Lending iterators |
| 38 | + |
| 39 | +The lending iterators are implemented using the [`streaming_iterator`](https://docs.rs/streaming-iterator/latest/streaming_iterator/) crate. |
| 40 | +(The community now prefers the term "lending" over "streaming" for this concept.) |
| 41 | +The `tskit` prelude includes the trait declarations that allow the code shown above to compile. |
| 42 | + |
| 43 | +rust 1.65.0 stabilized Generic Associated Types, or GATs. |
| 44 | +GATs allows lending iterators to be implemented directly without the workarounds used in the `streaming_iterator` crate. |
| 45 | +We have decided not to implement our own lending iterator using GATs. |
| 46 | +Rather, we will see what the community settles on and will decide in the future whether or not to adopt it. |
| 47 | + |
| 48 | +### Row objects |
| 49 | + |
| 50 | +We may access entire table rows by a row id: |
4 | 51 |
|
5 | 52 | ```rust, noplaygound, ignore
|
6 | 53 | {{#include ../../tests/book_table_collection.rs:get_edge_table_row_by_id}}
|
|
0 commit comments