#[repr(transparent)]pub struct NodeId(_);
Expand description
A node ID
This is an integer referring to a row of a NodeTable
.
The underlying type is tsk_id_t
.
Examples
These examples illustrate using this type as something “integer-like”.
use tskit::NodeId;
use tskit::bindings::tsk_id_t;
let x: tsk_id_t = 1;
let y: NodeId = NodeId::from(x);
assert_eq!(x, y);
assert_eq!(y, x);
assert!(y < x + 1);
assert!(y <= x);
assert!(x + 1 > y);
assert!(x + 1 >= y);
let z: NodeId = NodeId::from(x);
assert_eq!(y, z);
It is also possible to write functions accepting both the NodeId
and tsk_id_t
:
use tskit::NodeId;
use tskit::bindings::tsk_id_t;
fn interesting<N: Into<NodeId>>(x: N) -> NodeId {
x.into()
}
let x: tsk_id_t = 0;
assert_eq!(interesting(x), x);
let x: NodeId = NodeId::from(0);
assert_eq!(interesting(x), x);
The types also implement Display
:
use tskit::NodeId;
let n = NodeId::from(11);
assert_eq!(format!("{}", n), "11".to_string());
// Debug output contains type info
assert_eq!(format!("{:?}", n), "NodeId(11)".to_string());
let n = NodeId::from(NodeId::NULL);
assert_eq!(format!("{}", n), "NULL");
assert_eq!(format!("{:?}", n), "NodeId(-1)");
Implementations§
source§impl NodeId
impl NodeId
sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Return true
is self == Self::NULL
Examples found in repository?
src/tree_interface.rs (line 610)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
fn new(tree: &'a TreeInterface) -> Self {
debug_assert!(tree.right_child(tree.virtual_root()).is_some());
let mut rv = PreorderNodeIterator {
current_root: tree
.right_child(tree.virtual_root())
.unwrap_or(NodeId::NULL),
node_stack: vec![],
tree,
current_node_: None,
};
let mut c = rv.current_root;
while !c.is_null() {
rv.node_stack.push(c);
debug_assert!(rv.tree.left_sib(c).is_some());
c = rv.tree.left_sib(c).unwrap_or(NodeId::NULL);
}
rv
}
sourcepub fn as_usize(&self) -> usize
pub fn as_usize(&self) -> usize
Convenience function to convert to usize.
Implemented via as
.
Negative values with therefore wrap.
Examples found in repository?
src/tree_interface.rs (line 518)
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
pub fn total_branch_length(&self, by_span: bool) -> Result<Time, TskitError> {
let time: &[Time] = sys::generate_slice(
unsafe {
(*(*(*self.non_owned_pointer.as_ptr()).tree_sequence).tables)
.nodes
.time
},
self.num_nodes,
);
let mut b = Time::from(0.);
for n in self.traverse_nodes(NodeTraversalOrder::Preorder) {
let p = self.parent(n).ok_or(TskitError::IndexError {})?;
if p != NodeId::NULL {
b += time[p.as_usize()] - time[n.as_usize()]
}
}
match by_span {
true => Ok(b * self.span()),
false => Ok(b),
}
}
Trait Implementations§
source§impl Ord for NodeId
impl Ord for NodeId
source§impl PartialEq<NodeId> for NodeId
impl PartialEq<NodeId> for NodeId
source§impl PartialEq<NodeId> for SizeType
impl PartialEq<NodeId> for SizeType
source§impl PartialEq<NodeId> for tsk_id_t
impl PartialEq<NodeId> for tsk_id_t
source§impl PartialEq<SizeType> for NodeId
impl PartialEq<SizeType> for NodeId
source§impl PartialEq<i32> for NodeId
impl PartialEq<i32> for NodeId
source§impl PartialOrd<NodeId> for NodeId
impl PartialOrd<NodeId> for NodeId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<NodeId> for SizeType
impl PartialOrd<NodeId> for SizeType
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<NodeId> for tsk_id_t
impl PartialOrd<NodeId> for tsk_id_t
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<SizeType> for NodeId
impl PartialOrd<SizeType> for NodeId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<i32> for NodeId
impl PartialOrd<i32> for NodeId
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more