Skip to content

Commit 8ec71f2

Browse files
committed
need table-specific columns?
1 parent 3429588 commit 8ec71f2

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
pub use sys::bindings;
8484

8585
mod _macros; // Starts w/_ to be sorted at front by rustfmt!
86-
mod table_column;
8786
mod edge_differences;
8887
mod edge_table;
8988
pub mod error;
@@ -98,15 +97,14 @@ pub mod prelude;
9897
mod site_table;
9998
mod sys;
10099
mod table_collection;
100+
mod table_column;
101101
mod table_iterator;
102102
mod traits;
103103
mod trees;
104104
pub mod types;
105105
mod util;
106106

107-
108107
pub use edge_differences::*;
109-
pub use table_column::{TableColumn, MutableTableColumn};
110108
pub use edge_table::{EdgeTable, EdgeTableRow};
111109
pub use error::TskitError;
112110
pub use individual_table::{IndividualTable, IndividualTableRow};
@@ -119,6 +117,7 @@ pub use site_table::{SiteTable, SiteTableRow};
119117
pub use sys::flags::*;
120118
pub use sys::NodeTraversalOrder;
121119
pub use table_collection::TableCollection;
120+
pub use table_column::{MutableTableColumn, NodeTableColumn, TableColumn};
122121
pub use traits::IndividualLocation;
123122
pub use traits::IndividualParents;
124123
pub use trees::{Tree, TreeSequence};

src/node_table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,8 @@ impl NodeTable {
807807
/// Get the population column as a slice
808808
=> population, population_slice_raw, crate::sys::bindings::tsk_id_t);
809809

810-
pub fn individual_column(&self) -> crate::TableColumn<IndividualId> {
811-
crate::table_column::new_table_column(self.individual_slice())
810+
pub fn individual_column(&self) -> crate::table_column::NodeTableColumn<IndividualId> {
811+
crate::table_column::NodeTableColumn::new(self.individual_slice())
812812
}
813813

814814
pub fn population_column(&self) -> crate::TableColumn<PopulationId> {

src/table_column.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,47 @@ impl<T> MutableTableColumn<'_, T> {
3131
}
3232
}
3333

34+
macro_rules! make_table_column {
35+
($name: ident, $index: ident) => {
36+
/// Immutable view of a column
37+
pub struct $name<'table, T>(&'table [T]);
38+
39+
impl<'table, T> $name<'table, T> {
40+
pub(crate) fn new(column: &'table [T]) -> $name<'table, T> {
41+
Self(column)
42+
}
43+
44+
/// View the underlying slice
45+
pub fn as_slice(&self) -> &[T] {
46+
self.0
47+
}
48+
}
49+
50+
impl<T> std::ops::Index<usize> for $name<'_, T> {
51+
type Output = T;
52+
fn index(&self, index: usize) -> &Self::Output {
53+
&self.0[index]
54+
}
55+
}
56+
57+
impl<T> std::ops::Index<crate::$index> for $name<'_, T> {
58+
type Output = T;
59+
fn index(&self, index: crate::$index) -> &Self::Output {
60+
&self.0[usize::try_from(index).unwrap()]
61+
}
62+
}
63+
64+
impl<T> std::ops::Index<crate::SizeType> for $name<'_, T> {
65+
type Output = T;
66+
fn index(&self, index: crate::SizeType) -> &Self::Output {
67+
&self.0[usize::try_from(index).unwrap()]
68+
}
69+
}
70+
};
71+
}
72+
73+
make_table_column!(NodeTableColumn, NodeId);
74+
3475
// NOT part of tskit's public API
3576
pub fn new_table_column<T>(data: &[T]) -> TableColumn<'_, T> {
3677
TableColumn(data)

0 commit comments

Comments
 (0)