Struct tskit::OwningEdgeTable
source · pub struct OwningEdgeTable { /* private fields */ }
Expand description
A standalone edge table that owns its data.
Examples
use tskit::OwningEdgeTable;
let mut edges = OwningEdgeTable::default();
let rowid = edges.add_row(1., 2., 0, 1).unwrap();
assert_eq!(rowid, 0);
assert_eq!(edges.num_rows(), 1);
edges.clear().unwrap();
assert_eq!(edges.num_rows(), 0);
An example with metadata.
This requires the cargo feature "derive"
for tskit
.
use tskit::OwningEdgeTable;
#[derive(serde::Serialize,
serde::Deserialize,
tskit::metadata::EdgeMetadata)]
#[serializer("serde_json")]
struct EdgeMetadata {
value: i32,
}
let metadata = EdgeMetadata{value: 42};
let mut edges = OwningEdgeTable::default();
let rowid = edges.add_row_with_metadata(0., 1., 5, 10, &metadata).unwrap();
assert_eq!(rowid, 0);
match edges.metadata::<EdgeMetadata>(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 OwningEdgeTable
impl OwningEdgeTable
sourcepub fn clear(&mut self) -> TskReturnValue
pub fn clear(&mut self) -> TskReturnValue
Clear the table.
source§impl OwningEdgeTable
impl OwningEdgeTable
sourcepub fn as_ptr(&self) -> *const tsk_edge_table_t
pub fn as_ptr(&self) -> *const tsk_edge_table_t
Examples found in repository?
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
pub fn set_edges(&mut self, edges: &crate::OwningEdgeTable) -> TskReturnValue {
// SAFETY: neither self nor edges are possible
// to create with null pointers.
let rv = unsafe {
ll_bindings::tsk_edge_table_set_columns(
&mut self.inner.edges,
(*edges.as_ptr()).num_rows,
(*edges.as_ptr()).left,
(*edges.as_ptr()).right,
(*edges.as_ptr()).parent,
(*edges.as_ptr()).child,
(*edges.as_ptr()).metadata,
(*edges.as_ptr()).metadata_offset,
)
};
handle_tsk_return_value!(rv)
}
pub fn as_mut_ptr(&mut self) -> *mut tsk_edge_table_t
source§impl OwningEdgeTable
impl OwningEdgeTable
pub fn add_row<L, R, P, C>(
&mut self,
left: L,
right: R,
parent: P,
child: C
) -> Result<EdgeId, TskitError>where
L: Into<Position>,
R: Into<Position>,
P: Into<NodeId>,
C: Into<NodeId>,
pub fn add_row_with_metadata<L, R, P, C, M>(
&mut self,
left: L,
right: R,
parent: P,
child: C,
metadata: &M
) -> Result<EdgeId, TskitError>where
L: Into<Position>,
R: Into<Position>,
P: Into<NodeId>,
C: Into<NodeId>,
M: EdgeMetadata,
Methods from Deref<Target = EdgeTable>§
sourcepub fn num_rows(&self) -> SizeType
pub fn num_rows(&self) -> SizeType
Return the number of rows
Examples found in repository?
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
/// Return the ``child`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
/// Return the ``left`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
/// Return the ``right`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
sourcepub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId>
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId>
Return the parent
value from row row
of the table.
Returns
Some(parent)
ifu
is valid.None
otherwise.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
Some(EdgeTableRow {
id: pos.into(),
left: table.left(pos)?,
right: table.right(pos)?,
parent: table.parent(pos)?,
child: table.child(pos)?,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type EdgeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a EdgeTable>;
pub(crate) type EdgeTableIterator = crate::table_iterator::TableIterator<EdgeTable>;
impl<'a> Iterator for EdgeTableRefIterator<'a> {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for EdgeTableIterator {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
/// Row of an [`EdgeTable`]
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> EdgeTableRowView<'a> {
fn new(table: &'a EdgeTable) -> Self {
Self {
table,
id: (-1).into(),
left: f64::NAN.into(),
right: f64::NAN.into(),
parent: NodeId::NULL,
child: NodeId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for EdgeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
impl<'a> Eq for EdgeTableRowView<'a> {}
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
fn eq(&self, other: &EdgeTableRow) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
fn eq(&self, other: &EdgeTableRowView) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.left = self.table.left(self.id).unwrap_or_else(|| f64::NAN.into());
self.right = self.table.right(self.id).unwrap_or_else(|| f64::NAN.into());
self.parent = self.table.parent(self.id).unwrap_or(NodeId::NULL);
self.child = self.table.child(self.id).unwrap_or(NodeId::NULL);
self.metadata = self.table.raw_metadata(self.id);
}
}
/// An immutable view of an edge table.
///
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
table_: sys::LLEdgeTableRef,
}
impl EdgeTable {
pub(crate) fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLEdgeTableRef::new_from_table(edges)?;
Ok(EdgeTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_edge_table_t {
self.table_.as_ref()
}
/// Return the number of rows
pub fn num_rows(&self) -> crate::SizeType {
self.as_ref().num_rows.into()
}
raw_metadata_getter_for_tables!(EdgeId);
/// Return the ``parent`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(parent)` if `u` is valid.
/// * `None` otherwise.
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
/// Return the ``child`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
/// Return the ``left`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
/// Return the ``right`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
/// 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
///
/// * [`TskitError::MetadataError`] if decoding fails.
///
/// # Examples.
///
/// The big-picture semantics are the same for all table types.
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
/// Return an iterator over rows of the table.
/// The value of the iterator is [`EdgeTableRow`].
///
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&EdgeTable>(self)
}
pub fn lending_iter(&self) -> EdgeTableRowView {
EdgeTableRowView::new(self)
}
/// Return row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().into(), self, make_edge_table_row)
}
/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row_view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRowView> {
let view = EdgeTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
parent: self.parent(r)?,
child: self.child(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
sourcepub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId>
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId>
Return the child
value from row row
of the table.
Returns
Some(child)
ifu
is valid.None
otherwise.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
Some(EdgeTableRow {
id: pos.into(),
left: table.left(pos)?,
right: table.right(pos)?,
parent: table.parent(pos)?,
child: table.child(pos)?,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type EdgeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a EdgeTable>;
pub(crate) type EdgeTableIterator = crate::table_iterator::TableIterator<EdgeTable>;
impl<'a> Iterator for EdgeTableRefIterator<'a> {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for EdgeTableIterator {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
/// Row of an [`EdgeTable`]
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> EdgeTableRowView<'a> {
fn new(table: &'a EdgeTable) -> Self {
Self {
table,
id: (-1).into(),
left: f64::NAN.into(),
right: f64::NAN.into(),
parent: NodeId::NULL,
child: NodeId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for EdgeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
impl<'a> Eq for EdgeTableRowView<'a> {}
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
fn eq(&self, other: &EdgeTableRow) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
fn eq(&self, other: &EdgeTableRowView) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.left = self.table.left(self.id).unwrap_or_else(|| f64::NAN.into());
self.right = self.table.right(self.id).unwrap_or_else(|| f64::NAN.into());
self.parent = self.table.parent(self.id).unwrap_or(NodeId::NULL);
self.child = self.table.child(self.id).unwrap_or(NodeId::NULL);
self.metadata = self.table.raw_metadata(self.id);
}
}
/// An immutable view of an edge table.
///
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
table_: sys::LLEdgeTableRef,
}
impl EdgeTable {
pub(crate) fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLEdgeTableRef::new_from_table(edges)?;
Ok(EdgeTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_edge_table_t {
self.table_.as_ref()
}
/// Return the number of rows
pub fn num_rows(&self) -> crate::SizeType {
self.as_ref().num_rows.into()
}
raw_metadata_getter_for_tables!(EdgeId);
/// Return the ``parent`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(parent)` if `u` is valid.
/// * `None` otherwise.
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
/// Return the ``child`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
/// Return the ``left`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
/// Return the ``right`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
/// 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
///
/// * [`TskitError::MetadataError`] if decoding fails.
///
/// # Examples.
///
/// The big-picture semantics are the same for all table types.
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
/// Return an iterator over rows of the table.
/// The value of the iterator is [`EdgeTableRow`].
///
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&EdgeTable>(self)
}
pub fn lending_iter(&self) -> EdgeTableRowView {
EdgeTableRowView::new(self)
}
/// Return row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().into(), self, make_edge_table_row)
}
/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row_view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRowView> {
let view = EdgeTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
parent: self.parent(r)?,
child: self.child(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
sourcepub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position>
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position>
Return the left
value from row row
of the table.
Returns
Some(position)
ifu
is valid.None
otherwise.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
Some(EdgeTableRow {
id: pos.into(),
left: table.left(pos)?,
right: table.right(pos)?,
parent: table.parent(pos)?,
child: table.child(pos)?,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type EdgeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a EdgeTable>;
pub(crate) type EdgeTableIterator = crate::table_iterator::TableIterator<EdgeTable>;
impl<'a> Iterator for EdgeTableRefIterator<'a> {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for EdgeTableIterator {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
/// Row of an [`EdgeTable`]
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> EdgeTableRowView<'a> {
fn new(table: &'a EdgeTable) -> Self {
Self {
table,
id: (-1).into(),
left: f64::NAN.into(),
right: f64::NAN.into(),
parent: NodeId::NULL,
child: NodeId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for EdgeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
impl<'a> Eq for EdgeTableRowView<'a> {}
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
fn eq(&self, other: &EdgeTableRow) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
fn eq(&self, other: &EdgeTableRowView) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.left = self.table.left(self.id).unwrap_or_else(|| f64::NAN.into());
self.right = self.table.right(self.id).unwrap_or_else(|| f64::NAN.into());
self.parent = self.table.parent(self.id).unwrap_or(NodeId::NULL);
self.child = self.table.child(self.id).unwrap_or(NodeId::NULL);
self.metadata = self.table.raw_metadata(self.id);
}
}
/// An immutable view of an edge table.
///
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
table_: sys::LLEdgeTableRef,
}
impl EdgeTable {
pub(crate) fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLEdgeTableRef::new_from_table(edges)?;
Ok(EdgeTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_edge_table_t {
self.table_.as_ref()
}
/// Return the number of rows
pub fn num_rows(&self) -> crate::SizeType {
self.as_ref().num_rows.into()
}
raw_metadata_getter_for_tables!(EdgeId);
/// Return the ``parent`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(parent)` if `u` is valid.
/// * `None` otherwise.
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
/// Return the ``child`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
/// Return the ``left`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
/// Return the ``right`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
/// 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
///
/// * [`TskitError::MetadataError`] if decoding fails.
///
/// # Examples.
///
/// The big-picture semantics are the same for all table types.
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
/// Return an iterator over rows of the table.
/// The value of the iterator is [`EdgeTableRow`].
///
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&EdgeTable>(self)
}
pub fn lending_iter(&self) -> EdgeTableRowView {
EdgeTableRowView::new(self)
}
/// Return row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().into(), self, make_edge_table_row)
}
/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row_view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRowView> {
let view = EdgeTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
parent: self.parent(r)?,
child: self.child(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
sourcepub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position>
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position>
Return the right
value from row row
of the table.
Returns
Some(position)
ifu
is valid.None
otherwise.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
Some(EdgeTableRow {
id: pos.into(),
left: table.left(pos)?,
right: table.right(pos)?,
parent: table.parent(pos)?,
child: table.child(pos)?,
metadata: table.raw_metadata(pos).map(|m| m.to_vec()),
})
}
pub(crate) type EdgeTableRefIterator<'a> = crate::table_iterator::TableIterator<&'a EdgeTable>;
pub(crate) type EdgeTableIterator = crate::table_iterator::TableIterator<EdgeTable>;
impl<'a> Iterator for EdgeTableRefIterator<'a> {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(self.table, self.pos);
self.pos += 1;
rv
}
}
impl Iterator for EdgeTableIterator {
type Item = EdgeTableRow;
fn next(&mut self) -> Option<Self::Item> {
let rv = make_edge_table_row(&self.table, self.pos);
self.pos += 1;
rv
}
}
/// Row of an [`EdgeTable`]
#[derive(Debug)]
pub struct EdgeTableRowView<'a> {
table: &'a EdgeTable,
pub id: EdgeId,
pub left: Position,
pub right: Position,
pub parent: NodeId,
pub child: NodeId,
pub metadata: Option<&'a [u8]>,
}
impl<'a> EdgeTableRowView<'a> {
fn new(table: &'a EdgeTable) -> Self {
Self {
table,
id: (-1).into(),
left: f64::NAN.into(),
right: f64::NAN.into(),
parent: NodeId::NULL,
child: NodeId::NULL,
metadata: None,
}
}
}
impl<'a> PartialEq for EdgeTableRowView<'a> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& self.metadata == other.metadata
}
}
impl<'a> Eq for EdgeTableRowView<'a> {}
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
fn eq(&self, other: &EdgeTableRow) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
fn eq(&self, other: &EdgeTableRowView) -> bool {
self.id == other.id
&& self.parent == other.parent
&& self.child == other.child
&& crate::util::partial_cmp_equal(&self.left, &other.left)
&& crate::util::partial_cmp_equal(&self.right, &other.right)
&& optional_container_comparison!(self.metadata, other.metadata)
}
}
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
type Item = Self;
row_lending_iterator_get!();
fn advance(&mut self) {
self.id = (i32::from(self.id) + 1).into();
self.left = self.table.left(self.id).unwrap_or_else(|| f64::NAN.into());
self.right = self.table.right(self.id).unwrap_or_else(|| f64::NAN.into());
self.parent = self.table.parent(self.id).unwrap_or(NodeId::NULL);
self.child = self.table.child(self.id).unwrap_or(NodeId::NULL);
self.metadata = self.table.raw_metadata(self.id);
}
}
/// An immutable view of an edge table.
///
/// These are not created directly but are accessed
/// by types implementing [`std::ops::Deref`] to
/// [`crate::table_views::TableViews`]
#[repr(transparent)]
#[derive(Debug)]
pub struct EdgeTable {
table_: sys::LLEdgeTableRef,
}
impl EdgeTable {
pub(crate) fn new_from_table(
edges: *mut ll_bindings::tsk_edge_table_t,
) -> Result<Self, TskitError> {
let table_ = sys::LLEdgeTableRef::new_from_table(edges)?;
Ok(EdgeTable { table_ })
}
pub(crate) fn as_ref(&self) -> &ll_bindings::tsk_edge_table_t {
self.table_.as_ref()
}
/// Return the number of rows
pub fn num_rows(&self) -> crate::SizeType {
self.as_ref().num_rows.into()
}
raw_metadata_getter_for_tables!(EdgeId);
/// Return the ``parent`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(parent)` if `u` is valid.
/// * `None` otherwise.
pub fn parent<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().parent, self.num_rows())
}
/// Return the ``child`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(child)` if `u` is valid.
/// * `None` otherwise.
pub fn child<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<NodeId> {
sys::tsk_column_access::<NodeId, _, _, _>(row.into(), self.as_ref().child, self.num_rows())
}
/// Return the ``left`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn left<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(row.into(), self.as_ref().left, self.num_rows())
}
/// Return the ``right`` value from row ``row`` of the table.
///
/// # Returns
///
/// * `Some(position)` if `u` is valid.
/// * `None` otherwise.
pub fn right<E: Into<EdgeId> + Copy>(&self, row: E) -> Option<Position> {
sys::tsk_column_access::<Position, _, _, _>(
row.into(),
self.as_ref().right,
self.num_rows(),
)
}
/// 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
///
/// * [`TskitError::MetadataError`] if decoding fails.
///
/// # Examples.
///
/// The big-picture semantics are the same for all table types.
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
}
/// Return an iterator over rows of the table.
/// The value of the iterator is [`EdgeTableRow`].
///
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_ {
crate::table_iterator::make_table_iterator::<&EdgeTable>(self)
}
pub fn lending_iter(&self) -> EdgeTableRowView {
EdgeTableRowView::new(self)
}
/// Return row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row)` if `r` is valid
/// * `None` otherwise
pub fn row<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRow> {
table_row_access!(r.into().into(), self, make_edge_table_row)
}
/// Return a view of row `r` of the table.
///
/// # Parameters
///
/// * `r`: the row id.
///
/// # Returns
///
/// * `Some(row_view)` if `r` is valid
/// * `None` otherwise
pub fn row_view<E: Into<EdgeId> + Copy>(&self, r: E) -> Option<EdgeTableRowView> {
let view = EdgeTableRowView {
table: self,
id: r.into(),
left: self.left(r)?,
right: self.right(r)?,
parent: self.parent(r)?,
child: self.child(r)?,
metadata: self.raw_metadata(r.into()),
};
Some(view)
}
sourcepub fn metadata<T: EdgeMetadata>(
&self,
row: EdgeId
) -> Option<Result<T, TskitError>>
pub fn metadata<T: EdgeMetadata>(
&self,
row: EdgeId
) -> 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 = EdgeTableRow> + '_
pub fn iter(&self) -> impl Iterator<Item = EdgeTableRow> + '_
Return an iterator over rows of the table.
The value of the iterator is EdgeTableRow
.
pub fn lending_iter(&self) -> EdgeTableRowView<'_>
sourcepub fn row_view<E: Into<EdgeId> + Copy>(
&self,
r: E
) -> Option<EdgeTableRowView<'_>>
pub fn row_view<E: Into<EdgeId> + Copy>(
&self,
r: E
) -> Option<EdgeTableRowView<'_>>
Return a view of row r
of the table.
Parameters
r
: the row id.
Returns
Some(row_view)
ifr
is validNone
otherwise
sourcepub fn left_slice(&self) -> &[Position]
pub fn left_slice(&self) -> &[Position]
Get the left column as a slice
sourcepub fn left_slice_raw(&self) -> &[f64]
pub fn left_slice_raw(&self) -> &[f64]
Get the left column as a slice of f64
sourcepub fn right_slice(&self) -> &[Position]
pub fn right_slice(&self) -> &[Position]
Get the right column as a slice
sourcepub fn right_slice_raw(&self) -> &[f64]
pub fn right_slice_raw(&self) -> &[f64]
Get the left column as a slice of f64
sourcepub fn parent_slice(&self) -> &[NodeId]
pub fn parent_slice(&self) -> &[NodeId]
Get the parent column as a slice
sourcepub fn parent_slice_raw(&self) -> &[tsk_id_t]
pub fn parent_slice_raw(&self) -> &[tsk_id_t]
Get the parent column as a slice of crate::bindings::tsk_id_t
sourcepub fn child_slice(&self) -> &[NodeId]
pub fn child_slice(&self) -> &[NodeId]
Get the child column as a slice
sourcepub fn child_slice_raw(&self) -> &[tsk_id_t]
pub fn child_slice_raw(&self) -> &[tsk_id_t]
Get the child column as a slice of crate::bindings::tsk_id_t