#[repr(transparent)]pub struct NodeTable { /* private fields */ }
Expand description
An immtable view of a node table.
Implementations§
source§impl NodeTable
impl NodeTable
pub fn flags_array_mut(&mut self) -> &mut [NodeFlags]
pub fn time_array_mut(&mut self) -> &mut [Time]
sourcepub fn population<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId>
pub fn population<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId>
sourcepub fn deme<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId>
pub fn deme<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId>
Return the population
value from row row
of the table.
Examples
See NodeTable::population
for examples.
Returns
Some(population)
ifrow
is valid.None
otherwise.
sourcepub fn individual<N: Into<NodeId> + Copy>(&self, row: N) -> Option<IndividualId>
pub fn individual<N: Into<NodeId> + Copy>(&self, row: N) -> Option<IndividualId>
sourcepub fn metadata<T: NodeMetadata>(
&self,
row: NodeId
) -> Option<Result<T, TskitError>>
pub fn metadata<T: NodeMetadata>( &self, row: NodeId ) -> Option<Result<T, TskitError>>
Retrieve decoded metadata for a row
.
Returns
Some(Ok(T))
ifrow
is valid and decoding succeeded.Some(Err(_))
ifrow
is not valid and decoding failed.None
ifrow
is not valid.
Errors
TskitError::MetadataError
if decoding fails.
Examples.
The big-picture semantics are the same for all table types.
See crate::IndividualTable::metadata
for examples.
sourcepub fn iter(&self) -> impl Iterator<Item = NodeTableRow> + '_
pub fn iter(&self) -> impl Iterator<Item = NodeTableRow> + '_
Return an iterator over rows of the table.
The value of the iterator is NodeTableRow
.
pub fn lending_iter(&self) -> NodeTableRowView<'_>
sourcepub fn row_view<N: Into<NodeId> + Copy>(
&self,
r: N
) -> Option<NodeTableRowView<'_>>
pub fn row_view<N: Into<NodeId> + Copy>( &self, r: N ) -> Option<NodeTableRowView<'_>>
Return a view of row r
of the table.
Parameters
r
: the row id.
Returns
Some(row view)
ifr
is validNone
otherwise
sourcepub fn samples_as_vector(&self) -> Vec<NodeId>
pub fn samples_as_vector(&self) -> Vec<NodeId>
Obtain a vector containing the indexes (“ids”)
of all nodes for which crate::TSK_NODE_IS_SAMPLE
is true
.
sourcepub fn create_node_id_vector(
&self,
f: impl FnMut(&NodeTableRow) -> bool
) -> Vec<NodeId>
pub fn create_node_id_vector( &self, f: impl FnMut(&NodeTableRow) -> bool ) -> Vec<NodeId>
Obtain a vector containing the indexes (“ids”) of all nodes satisfying a certain criterion.
sourcepub fn time_slice(&self) -> &[Time]
pub fn time_slice(&self) -> &[Time]
Get the time column as a slice
sourcepub fn time_slice_raw(&self) -> &[f64]
pub fn time_slice_raw(&self) -> &[f64]
Get the time column as a slice
sourcepub fn time_slice_mut(&mut self) -> &mut [Time]
pub fn time_slice_mut(&mut self) -> &mut [Time]
Get the time column as a mutable slice
Examples
For a crate::TableCollection
, accessing the table creates a temporary
that will be dropped, causing this code to not compile:
let time = tables.nodes().time_slice_mut();
println!("{}", time.len()); // ERROR: the temporary node table is dropped by now
Treating the returned slice as an iterable succeeds:
for time in tables.nodes_mut().time_slice_mut() {
*time = 55.0.into(); // change each node's time value
}
assert!(tables.nodes_mut().time_slice_mut().iter().all(|t| t == &55.0));
Panics
Internally, we rely on a conversion of u64 to usize. This conversion is fallible on some platforms. If the conversion fails, this function will panic.
sourcepub fn time_slice_raw_mut(&mut self) -> &mut [f64]
pub fn time_slice_raw_mut(&mut self) -> &mut [f64]
Get the time column as a mutable slice
sourcepub fn flags_slice(&self) -> &[NodeFlags]
pub fn flags_slice(&self) -> &[NodeFlags]
Get the flags column as a slice
sourcepub fn flags_slice_raw(&self) -> &[tsk_flags_t]
pub fn flags_slice_raw(&self) -> &[tsk_flags_t]
Get the flags column as a slice
sourcepub fn flags_slice_mut(&mut self) -> &mut [NodeFlags]
pub fn flags_slice_mut(&mut self) -> &mut [NodeFlags]
Get the flags column as a mutable slice
Examples
let flags = tables.nodes_mut().flags_slice_mut();
for flag in flags {
// Can do something...
}
for flag in tables.nodes_mut().flags_slice_mut() {
}
The returned slice is mutable, allowing one to do things like clear the sample status of all nodes:
A copy of the flags can be obtained by collecting results into Vec
:
for flag in tables.nodes_mut().flags_slice_mut() {
flag.remove(tskit::NodeFlags::IS_SAMPLE);
}
assert!(!tables.nodes_mut().flags_slice_mut().iter().any(|f| f.is_sample()));
assert!(tables.nodes().samples_as_vector().is_empty());
let flags = tables.nodes_mut().flags_slice_mut().to_vec();
Owning tables
The ownership semantics differ when tables are not part of a table collection:
let mut nodes = tskit::OwningNodeTable::default();
assert!(nodes.add_row(tskit::NodeFlags::IS_SAMPLE, 10., -1, -1).is_ok());
let flags = nodes.flags_slice_mut();
assert!(flags.iter().all(|f| f.is_sample()));
// while we are at it, let's use our node
// table to populate a table collection.
let mut tables = tskit::TableCollection::new(10.0).unwrap();
tables.set_nodes(&nodes);
assert_eq!(tables.nodes().num_rows(), 1);
assert_eq!(tables.nodes_mut().flags_slice_mut().iter().filter(|f| f.is_sample()).count(), 1);
Panics
Internally, we rely on a conversion of u64 to usize. This conversion is fallible on some platforms. If the conversion fails, this function will panic.
sourcepub fn flags_slice_raw_mut(&mut self) -> &mut [tsk_flags_t]
pub fn flags_slice_raw_mut(&mut self) -> &mut [tsk_flags_t]
Get the flags column as a mutable slice
sourcepub fn individual_slice(&self) -> &[IndividualId]
pub fn individual_slice(&self) -> &[IndividualId]
Get the individual column as a slice
sourcepub fn individual_slice_raw(&self) -> &[tsk_id_t]
pub fn individual_slice_raw(&self) -> &[tsk_id_t]
Get the individual column as a slice
sourcepub fn population_slice(&self) -> &[PopulationId]
pub fn population_slice(&self) -> &[PopulationId]
Get the population column as a slice
sourcepub fn population_slice_raw(&self) -> &[tsk_id_t]
pub fn population_slice_raw(&self) -> &[tsk_id_t]
Get the population column as a slice