Struct tskit::OwningNodeTable

source ·
pub struct OwningNodeTable { /* private fields */ }
Expand description

A standalone node table that owns its data.

Examples

use tskit::OwningNodeTable;

let mut nodes = OwningNodeTable::default();
let rowid = nodes.add_row(0, 1.1, -1, -1).unwrap();
assert_eq!(rowid, 0);
assert_eq!(nodes.num_rows(), 1);

An example with metadata. This requires the cargo feature "derive" for tskit.

use tskit::OwningNodeTable;

#[derive(serde::Serialize,
         serde::Deserialize,
         tskit::metadata::NodeMetadata)]
#[serializer("serde_json")]
struct NodeMetadata {
    value: i32,
}

let metadata = NodeMetadata{value: 42};

let mut nodes = OwningNodeTable::default();

let rowid = nodes.add_row_with_metadata(0, 1., -1, -1, &metadata).unwrap();
assert_eq!(rowid, 0);

match nodes.metadata::<NodeMetadata>(rowid) {
    // rowid is in range, decoding succeeded
    Some(Ok(decoded)) => assert_eq!(decoded.value, 42),
    // rowid is in range, decoding failed
    Some(Err(e)) => panic!("error decoding metadata: {:?}", e),
    None => panic!("row id out of range")
}

Implementations§

source§

impl OwningNodeTable

source

pub fn clear(&mut self) -> TskReturnValue

Clear the table.

source§

impl OwningNodeTable

source§

impl OwningNodeTable

source

pub fn add_row<F, T, P, I>( &mut self, flags: F, time: T, population: P, individual: I ) -> Result<NodeId, TskitError>where F: Into<NodeFlags>, T: Into<Time>, P: Into<PopulationId>, I: Into<IndividualId>,

source

pub fn add_row_with_metadata<F, T, P, I, N>( &mut self, flags: F, time: T, population: P, individual: I, metadata: &N ) -> Result<NodeId, TskitError>where F: Into<NodeFlags>, T: Into<Time>, P: Into<PopulationId>, I: Into<IndividualId>, N: NodeMetadata,

source

pub fn add_row_with_defaults<T: Into<Time> + Copy, D: DefaultNodeData>( &mut self, time: T, defaults: &D ) -> Result<NodeId, TskitError>

Add row with defaults

Examples
let node_defaults = tskit::NodeDefaults::default();
let rv = nodes.add_row_with_defaults(1.0, &node_defaults).unwrap();
assert_eq!(rv, 0);
let rv = nodes.add_row_with_defaults(1.0, &node_defaults).unwrap();
assert_eq!(rv, 1);

Methods from Deref<Target = NodeTable>§

source

pub fn num_rows(&self) -> SizeType

Return the number of rows

source

pub fn time<N: Into<NodeId> + Copy>(&self, row: N) -> Option<Time>

Return the time value from row row of the table.

Returns
  • Some(time) if row is valid.
  • None otherwise.
Examples
if let Some(time) = tables.nodes().time(0) {
// then node id 0 is a valid row id
}
source

pub fn flags<N: Into<NodeId> + Copy>(&self, row: N) -> Option<NodeFlags>

Return the flags value from row row of the table.

Returns
  • Some(flags) if row is valid.
  • None otherwise.
Examples
if let Some(flags) = tables.nodes().flags(0) {
// then node id 0 is a valid row id
}
source

pub fn flags_array_mut(&mut self) -> &mut [NodeFlags]

👎Deprecated since 0.12.0: use flags_slice_mut instead
source

pub fn time_array_mut(&mut self) -> &mut [Time]

👎Deprecated since 0.12.0: use time_slice_mut instead
source

pub fn population<N: Into<NodeId> + Copy>(&self, row: N) -> Option<PopulationId>

Return the population value from row row of the table.

Examples
if let Some(pop) = tables.nodes().population(0) {
// then node id 0 is a valid row id
}
Returns
  • Some(population) if row is valid.
  • None otherwise.
source

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) if row is valid.
  • None otherwise.
source

pub fn individual<N: Into<NodeId> + Copy>(&self, row: N) -> Option<IndividualId>

Return the individual value from row row of the table.

Examples
if let Some(individual) = tables.nodes().individual(0) {
// then node id 0 is a valid row id
}
Returns
  • Some(individual) if row is valid.
  • None otherwise.
source

pub fn metadata<T: NodeMetadata>( &self, row: NodeId ) -> Option<Result<T, TskitError>>

Retrieve decoded metadata for a row.

Returns
  • Some(Ok(T)) if row is valid and decoding succeeded.
  • Some(Err(_)) if row is not valid and decoding failed.
  • None if row is not valid.
Errors
Examples.

The big-picture semantics are the same for all table types. See crate::IndividualTable::metadata for examples.

source

pub fn iter(&self) -> impl Iterator<Item = NodeTableRow> + '_

Return an iterator over rows of the table. The value of the iterator is NodeTableRow.

source

pub fn lending_iter(&self) -> NodeTableRowView<'_>

source

pub fn row<N: Into<NodeId> + Copy>(&self, r: N) -> Option<NodeTableRow>

Return row r of the table.

Parameters
  • r: the row id.
Returns
  • Some(row) if r is valid
  • None otherwise
source

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) if r is valid
  • None otherwise
source

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.

source

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.

source

pub fn time_slice(&self) -> &[Time]

Get the time column as a slice

source

pub fn time_slice_raw(&self) -> &[f64]

Get the time column as a slice

source

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.

source

pub fn time_slice_raw_mut(&mut self) -> &mut [f64]

Get the time column as a mutable slice

source

pub fn flags_slice(&self) -> &[NodeFlags]

Get the flags column as a slice

source

pub fn flags_slice_raw(&self) -> &[tsk_flags_t]

Get the flags column as a slice

source

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.

source

pub fn flags_slice_raw_mut(&mut self) -> &mut [tsk_flags_t]

Get the flags column as a mutable slice

source

pub fn individual_slice(&self) -> &[IndividualId]

Get the individual column as a slice

source

pub fn individual_slice_raw(&self) -> &[tsk_id_t]

Get the individual column as a slice

source

pub fn population_slice(&self) -> &[PopulationId]

Get the population column as a slice

source

pub fn population_slice_raw(&self) -> &[tsk_id_t]

Get the population column as a slice

Trait Implementations§

source§

impl Default for OwningNodeTable

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Deref for OwningNodeTable

§

type Target = NodeTable

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for OwningNodeTable

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Free for T

source§

unsafe default fn free(ptr_ref: NonNull<T>)

Drops the content pointed by this pointer and frees it. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.