Skip to content

Give access to TableCollection edge indexes. #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,43 @@ impl TableCollection {
handle_tsk_return_value!(rv)
}

/// Return `true` if tables are indexed.
pub fn is_indexed(&self) -> bool {
unsafe { ll_bindings::tsk_table_collection_has_index(self.as_ptr(), 0) }
}

/// If `self.is_indexed()` is `true`, return a non-owning
/// slice containing the edge insertion order.
/// Otherwise, return `None`.
pub fn edge_insertion_order(&self) -> Option<&[tsk_id_t]> {
if self.is_indexed() {
Some(unsafe {
std::slice::from_raw_parts(
(*self.as_ptr()).indexes.edge_insertion_order,
(*self.as_ptr()).indexes.num_edges as usize,
)
})
} else {
None
}
}

/// If `self.is_indexed()` is `true`, return a non-owning
/// slice containing the edge removal order.
/// Otherwise, return `None`.
pub fn edge_removal_order(&self) -> Option<&[tsk_id_t]> {
if self.is_indexed() {
Some(unsafe {
std::slice::from_raw_parts(
(*self.as_ptr()).indexes.edge_removal_order,
(*self.as_ptr()).indexes.num_edges as usize,
)
})
} else {
None
}
}

/// Sort the tables.
/// The [``bookmark``](crate::types::Bookmark) can
/// be used to affect where sorting starts from for each table.
Expand Down Expand Up @@ -716,6 +753,30 @@ mod test {
}
}

#[test]
fn test_edge_index_access() {
let tables = make_small_table_collection();
assert_eq!(tables.is_indexed(), true);
assert_eq!(
tables.edge_insertion_order().unwrap().len(),
tables.edges().num_rows() as usize
);
assert_eq!(
tables.edge_removal_order().unwrap().len(),
tables.edges().num_rows() as usize
);

for i in tables.edge_insertion_order().unwrap() {
assert!(*i >= 0);
assert!(*i < tables.edges().num_rows() as tsk_id_t);
}

for i in tables.edge_removal_order().unwrap() {
assert!(*i >= 0);
assert!(*i < tables.edges().num_rows() as tsk_id_t);
}
}

#[test]
fn test_add_site() {
let mut tables = TableCollection::new(1000.).unwrap();
Expand Down