Skip to content

Commit c6979ce

Browse files
committed
add in the example snippets
1 parent f0b6600 commit c6979ce

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
## Accessing table columns
22

3-
Not done yet.
3+
We can access table rows using either the relevant newtype or `i32` (which is identical to the `tskit-c` typedef `tsk_id_t`).
4+
The following snippet adds and edge and then validates the data for that row of the table:
5+
6+
```rust, noplaygound, ignore
7+
{{#include ../../tests/book_table_collection.rs:get_edge_table_columns}}
8+
```
9+
10+
The return type of the getters is the [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) enum. The `None` variant is returned when row indexes are out of range:
11+
12+
```rust, noplaygound, ignore
13+
{{#include ../../tests/book_table_collection.rs:get_edge_table_columns_out_of_range}}
14+
```
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
## Accessing table rows
22

3-
To be written!
3+
We may also access entire table rows by a row id:
4+
5+
```rust, noplaygound, ignore
6+
{{#include ../../tests/book_table_collection.rs:get_edge_table_row_by_id}}
7+
```
8+
9+
The row types are rust structures. The table data are *copied* into these structures, making them relatively more expensive to work with. We are looking into removing the copies and returning slices, but doing so currently impacts the design of table row iterators such as:
10+
11+
```rust, noplaygound, ignore
12+
{{#include ../../tests/book_table_collection.rs:get_edge_table_rows_by_iterator}}
13+
```
14+
15+
These iterators are rust iterator types--they `impl Iterator<Item=X>`, where `X` is a table row type.

tests/book_table_collection.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn get_data_from_edge_table() {
8282
std::mem::swap(&mut left, &mut right);
8383
}
8484

85+
// ANCHOR: get_edge_table_columns
8586
if let Ok(edge_id) = tables.add_edge(left, right, parent, child) {
8687
// Take a reference to an edge table (& tskit::EdgeTable)
8788
let edges = tables.edges();
@@ -100,8 +101,15 @@ fn get_data_from_edge_table() {
100101
} else {
101102
panic!("that should have worked...");
102103
}
104+
// ANCHOR_END: get_edge_table_columns
103105

104-
if let Some(row) = tables.edges().row(0) {
106+
// ANCHOR: get_edge_table_columns_out_of_range
107+
assert!(tables.edges().parent(tskit::EdgeId::NULL).is_none());
108+
// ANCHOR_END: get_edge_table_columns_out_of_range
109+
110+
let edge_id = tskit::EdgeId::from(0);
111+
// ANCHOR: get_edge_table_row_by_id
112+
if let Some(row) = tables.edges().row(edge_id) {
105113
assert_eq!(row.id, 0);
106114
assert_eq!(row.left, left);
107115
assert_eq!(row.right, right);
@@ -110,7 +118,9 @@ fn get_data_from_edge_table() {
110118
} else {
111119
panic!("that should have worked...");
112120
}
121+
// ANCHOR_END: get_edge_table_row_by_id
113122

123+
// ANCHOR: get_edge_table_rows_by_iterator
114124
for row in tables.edges_iter() {
115125
// there is only one row!
116126
assert_eq!(row.id, 0);
@@ -119,6 +129,7 @@ fn get_data_from_edge_table() {
119129
assert_eq!(row.parent, parent);
120130
assert_eq!(row.child, child);
121131
}
132+
// ANCHOR_END: get_edge_table_rows_by_iterator
122133

123134
assert!(tables
124135
.check_integrity(tskit::TableIntegrityCheckFlags::default())

0 commit comments

Comments
 (0)