Skip to content

Commit add217a

Browse files
authored
feat: get fns for table column types (#729)
1 parent 15420ef commit add217a

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/table_column.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ macro_rules! make_table_column {
1414
pub fn as_slice(&self) -> &[T] {
1515
self.0
1616
}
17+
18+
pub fn get_with_id(&self, index: crate::$index) -> Option<&T> {
19+
self.get_with_usize(usize::try_from(index).ok()?)
20+
}
21+
22+
pub fn get_with_size_type(&self, index: crate::SizeType) -> Option<&T> {
23+
self.get_with_usize(usize::try_from(index).ok()?)
24+
}
25+
26+
pub fn get_with_usize(&self, index: usize) -> Option<&T> {
27+
self.0.get(index)
28+
}
1729
}
1830

1931
impl<T> std::ops::Index<usize> for $name<'_, T> {

tests/test_tables.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,34 @@ fn test_node_table_column_access() {
557557
let node = t
558558
.add_row(tskit::NodeFlags::new_sample(), 0.0, -1, -1)
559559
.unwrap();
560-
let individual = t.individual_column();
561-
assert_eq!(individual[node], tskit::IndividualId::NULL);
562-
let population = t.population_column();
563-
assert_eq!(population[node], tskit::PopulationId::NULL);
564-
let time = t.time_column();
565-
assert_eq!(time[node], 0.0);
566-
let flags = t.flags_column();
567-
assert_eq!(flags[node], tskit::NodeFlags::IS_SAMPLE);
560+
{
561+
let individual = t.individual_column();
562+
assert_eq!(individual[node], tskit::IndividualId::NULL);
563+
assert_eq!(
564+
individual.get_with_id(node).unwrap(),
565+
&tskit::IndividualId::NULL
566+
);
567+
assert!(individual.get_with_size_type(t.num_rows()).is_none());
568+
}
569+
{
570+
let population = t.population_column();
571+
assert_eq!(population[node], tskit::PopulationId::NULL);
572+
assert_eq!(
573+
population.get_with_id(node).unwrap(),
574+
&tskit::PopulationId::NULL
575+
);
576+
}
577+
{
578+
let time = t.time_column();
579+
assert_eq!(time[node], 0.0);
580+
assert_eq!(time.get_with_id(node).unwrap(), &0.0);
581+
}
582+
{
583+
let flags = t.flags_column();
584+
assert_eq!(flags[node], tskit::NodeFlags::IS_SAMPLE);
585+
assert_eq!(
586+
flags.get_with_id(node).unwrap(),
587+
&tskit::NodeFlags::IS_SAMPLE
588+
);
589+
}
568590
}

0 commit comments

Comments
 (0)