Skip to content

Commit ada5e08

Browse files
committed
restore all pub API functionality
1 parent aafef87 commit ada5e08

File tree

1 file changed

+93
-192
lines changed

1 file changed

+93
-192
lines changed

src/table_views.rs

Lines changed: 93 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -85,77 +85,170 @@ impl TableViews {
8585
Self::new_from_NonNull_table_collection(&mut n)
8686
}
8787

88+
/// Get reference to the [``EdgeTable``](crate::EdgeTable).
8889
pub fn edges(&self) -> &EdgeTable {
8990
&self.edges
9091
}
9192

93+
/// Get reference to the [``NodeTable``](crate::NodeTable).
9294
pub fn nodes(&self) -> &NodeTable {
9395
&self.nodes
9496
}
9597

98+
/// Get mutable reference to the [``NodeTable``](crate::NodeTable).
9699
pub fn nodes_mut(&mut self) -> &mut NodeTable {
97100
&mut self.nodes
98101
}
99102

103+
/// Get reference to the [``SiteTable``](crate::SiteTable).
100104
pub fn sites(&self) -> &SiteTable {
101105
&self.sites
102106
}
103107

108+
/// Get reference to the [``MutationTable``](crate::MutationTable).
104109
pub fn mutations(&self) -> &MutationTable {
105110
&self.mutations
106111
}
107112

113+
/// Get reference to the [``IndividualTable``](crate::IndividualTable).
108114
pub fn individuals(&self) -> &IndividualTable {
109115
&self.individuals
110116
}
111117

118+
/// Get reference to the [``PopulationTable``](crate::PopulationTable).
112119
pub fn populations(&self) -> &PopulationTable {
113120
&self.populations
114121
}
115122

123+
/// Get reference to the [``MigrationTable``](crate::MigrationTable).
116124
pub fn migrations(&self) -> &MigrationTable {
117125
&self.migrations
118126
}
119127

120128
#[cfg(feature = "provenance")]
129+
#[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
130+
/// Get reference to the [``ProvenanceTable``](crate::provenance::ProvenanceTable)
121131
pub fn provenances(&self) -> &ProvenanceTable {
122132
&self.provenances
123133
}
124134

135+
/// Return an iterator over the edges.
125136
pub fn edges_iter(&self) -> impl Iterator<Item = crate::EdgeTableRow> + '_ {
126137
self.edges.iter()
127138
}
128139

140+
/// Return an iterator over the nodes.
129141
pub fn nodes_iter(&self) -> impl Iterator<Item = crate::NodeTableRow> + '_ {
130142
self.nodes.iter()
131143
}
132144

145+
/// Return an iterator over the sites.
133146
pub fn sites_iter(&self) -> impl Iterator<Item = crate::SiteTableRow> + '_ {
134147
self.sites.iter()
135148
}
136149

150+
/// Return an iterator over the mutations.
137151
pub fn mutations_iter(&self) -> impl Iterator<Item = crate::MutationTableRow> + '_ {
138152
self.mutations.iter()
139153
}
140154

155+
/// Return an iterator over the individuals.
141156
pub fn individuals_iter(&self) -> impl Iterator<Item = crate::IndividualTableRow> + '_ {
142157
self.individuals.iter()
143158
}
144159

160+
/// Return an iterator over the populations.
145161
pub fn populations_iter(&self) -> impl Iterator<Item = crate::PopulationTableRow> + '_ {
146162
self.populations.iter()
147163
}
148164

165+
/// Return an iterator over the migrations.
149166
pub fn migrations_iter(&self) -> impl Iterator<Item = crate::MigrationTableRow> + '_ {
150167
self.migrations.iter()
151168
}
152169

153170
#[cfg(feature = "provenance")]
171+
#[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
172+
/// Return an iterator over provenances
154173
pub fn provenances_iter(
155174
&self,
156175
) -> impl Iterator<Item = crate::provenance::ProvenanceTableRow> + '_ {
157176
self.provenances.iter()
158177
}
178+
179+
/// Obtain a vector containing the indexes ("ids")
180+
/// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
181+
/// is `true`.
182+
///
183+
/// The provided implementation dispatches to
184+
/// [`crate::NodeTable::samples_as_vector`].
185+
pub fn samples_as_vector(&self) -> Vec<crate::NodeId> {
186+
self.nodes().samples_as_vector()
187+
}
188+
189+
/// Obtain a vector containing the indexes ("ids") of all nodes
190+
/// satisfying a certain criterion.
191+
///
192+
/// The provided implementation dispatches to
193+
/// [`crate::NodeTable::create_node_id_vector`].
194+
///
195+
/// # Parameters
196+
///
197+
/// * `f`: a function. The function is passed the current table
198+
/// collection and each [`crate::node_table::NodeTableRow`].
199+
/// If `f` returns `true`, the index of that row is included
200+
/// in the return value.
201+
///
202+
/// # Examples
203+
///
204+
/// Get all nodes with time > 0.0:
205+
///
206+
/// ```
207+
/// use tskit::bindings::tsk_id_t;
208+
///
209+
/// let mut tables = tskit::TableCollection::new(100.).unwrap();
210+
/// tables
211+
/// .add_node(tskit::TSK_NODE_IS_SAMPLE, 0.0, tskit::PopulationId::NULL,
212+
/// tskit::IndividualId::NULL)
213+
/// .unwrap();
214+
/// tables
215+
/// .add_node(tskit::TSK_NODE_IS_SAMPLE, 1.0, tskit::PopulationId::NULL,
216+
/// tskit::IndividualId::NULL)
217+
/// .unwrap();
218+
/// let samples = tables.create_node_id_vector(
219+
/// |row: &tskit::NodeTableRow| row.time > 0.,
220+
/// );
221+
/// assert_eq!(samples[0], 1);
222+
///
223+
/// // Get all nodes that have a mutation:
224+
///
225+
/// fn node_has_mutation(
226+
/// // dyn trait here means this
227+
/// // will work with TreeSequence, too.
228+
/// tables_type: &dyn std::ops::Deref<Target=tskit::table_views::TableViews>,
229+
/// row: &tskit::NodeTableRow,
230+
/// ) -> bool {
231+
/// for mrow in tables_type.mutations_iter() {
232+
/// if mrow.node == row.id {
233+
/// return true;
234+
/// }
235+
/// }
236+
/// false
237+
/// }
238+
///
239+
/// // Get all nodes that have a mutation:
240+
///
241+
/// tables.add_mutation(0, 0, tskit::MutationId::NULL, 0.0, None).unwrap();
242+
/// let samples_with_mut = tables.create_node_id_vector(
243+
/// |row: &tskit::NodeTableRow| node_has_mutation(&tables, row));
244+
/// assert_eq!(samples_with_mut[0], 0);
245+
/// ```
246+
pub fn create_node_id_vector(
247+
&self,
248+
f: impl FnMut(&crate::NodeTableRow) -> bool,
249+
) -> Vec<crate::NodeId> {
250+
self.nodes().create_node_id_vector(f)
251+
}
159252
}
160253

161254
#[cfg(test)]
@@ -181,195 +274,3 @@ mod table_views_tests {
181274
}
182275
}
183276
}
184-
185-
// Immutable access to tables.
186-
//
187-
// For objects that contain the full suite of tables,
188-
// implementing this trait provides immutable access
189-
// to their data.
190-
//
191-
// For most types, the provided implementations of `_iter`
192-
// methods should do.
193-
//
194-
// # Examples
195-
//
196-
// ```
197-
//
198-
//
199-
// let mut tables = tskit::TableCollection::new(1.).unwrap();
200-
// // The borrows are immuatble, so we can
201-
// // take multiple table references from the same object.
202-
// let e = tables.edges();
203-
// let n = tables.nodes();
204-
// ```
205-
//
206-
// The borrow checker will keep you from getting in trouble:
207-
//
208-
// ```compile_fail
209-
// let mut tables = tskit::TableCollection::new(1.).unwrap();
210-
// let e = tables.edges();
211-
// tables.add_edge(0.0, 1.0, 0, 1).unwrap();
212-
// let p = e.parent(0).unwrap(); // FAIL!
213-
// ```
214-
//pub trait TableAccess {
215-
// /// Get reference to the [``EdgeTable``](crate::EdgeTable).
216-
// fn edges(&self) -> EdgeTable;
217-
//
218-
// /// Return an iterator over the edges.
219-
// fn edges_iter(&self) -> Box<dyn Iterator<Item = crate::edge_table::EdgeTableRow> + '_> {
220-
// Box::new(make_table_iterator::<EdgeTable>(self.edges()))
221-
// }
222-
//
223-
// /// Get reference to the [``NodeTable``](crate::NodeTable).
224-
// fn nodes(&self) -> NodeTable;
225-
//
226-
// /// Return an iterator over the nodes.
227-
// fn nodes_iter(&self) -> Box<dyn Iterator<Item = crate::node_table::NodeTableRow> + '_> {
228-
// Box::new(make_table_iterator::<NodeTable>(self.nodes()))
229-
// }
230-
//
231-
// /// Get reference to the [``MutationTable``](crate::MutationTable).
232-
// fn mutations(&self) -> MutationTable;
233-
//
234-
// /// Return an iterator over the mutations.
235-
// fn mutations_iter(
236-
// &self,
237-
// ) -> Box<dyn Iterator<Item = crate::mutation_table::MutationTableRow> + '_> {
238-
// Box::new(make_table_iterator::<MutationTable>(self.mutations()))
239-
// }
240-
//
241-
// /// Get reference to the [``SiteTable``](crate::SiteTable).
242-
// fn sites(&self) -> SiteTable;
243-
//
244-
// /// Return an iterator over the sites.
245-
// fn sites_iter(&self) -> Box<dyn Iterator<Item = crate::site_table::SiteTableRow> + '_> {
246-
// Box::new(make_table_iterator::<SiteTable>(self.sites()))
247-
// }
248-
//
249-
// /// Get reference to the [``PopulationTable``](crate::PopulationTable).
250-
// fn populations(&self) -> PopulationTable;
251-
//
252-
// /// Return an iterator over the populations.
253-
// fn populations_iter(
254-
// &self,
255-
// ) -> Box<dyn Iterator<Item = crate::population_table::PopulationTableRow> + '_> {
256-
// Box::new(make_table_iterator::<PopulationTable>(self.populations()))
257-
// }
258-
//
259-
// /// Get reference to the [``MigrationTable``](crate::MigrationTable).
260-
// fn migrations(&self) -> MigrationTable;
261-
//
262-
// /// Return an iterator over the migration events.
263-
// fn migrations_iter(
264-
// &self,
265-
// ) -> Box<dyn Iterator<Item = crate::migration_table::MigrationTableRow> + '_> {
266-
// Box::new(make_table_iterator::<MigrationTable>(self.migrations()))
267-
// }
268-
//
269-
// /// Get reference to the [``IndividualTable``](crate::IndividualTable).
270-
// fn individuals(&self) -> IndividualTable;
271-
//
272-
// /// Return an iterator over the individuals.
273-
// fn individuals_iter(
274-
// &self,
275-
// ) -> Box<dyn Iterator<Item = crate::individual_table::IndividualTableRow> + '_> {
276-
// Box::new(make_table_iterator::<IndividualTable>(self.individuals()))
277-
// }
278-
//
279-
// #[cfg(feature = "provenance")]
280-
// #[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
281-
// /// Get reference to the [``ProvenanceTable``](crate::provenance::ProvenanceTable)
282-
// fn provenances(&self) -> crate::provenance::ProvenanceTable;
283-
//
284-
// #[cfg(feature = "provenance")]
285-
// #[cfg_attr(doc_cfg, doc(cfg(feature = "provenance")))]
286-
// /// Return an iterator over provenances
287-
// fn provenances_iter(
288-
// &self,
289-
// ) -> Box<dyn Iterator<Item = crate::provenance::ProvenanceTableRow> + '_> {
290-
// Box::new(crate::table_iterator::make_table_iterator::<
291-
// crate::provenance::ProvenanceTable,
292-
// >(self.provenances()))
293-
// }
294-
//}
295-
//
296-
///// Interface for returning lists of node ids from
297-
///// types implementing [`TableAccess`].
298-
//pub trait NodeListGenerator: TableAccess {
299-
// /// Obtain a vector containing the indexes ("ids")
300-
// /// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
301-
// /// is `true`.
302-
// ///
303-
// /// The provided implementation dispatches to
304-
// /// [`crate::NodeTable::samples_as_vector`].
305-
// fn samples_as_vector(&self) -> Vec<crate::NodeId> {
306-
// self.nodes().samples_as_vector()
307-
// }
308-
//
309-
// /// Obtain a vector containing the indexes ("ids") of all nodes
310-
// /// satisfying a certain criterion.
311-
// ///
312-
// /// The provided implementation dispatches to
313-
// /// [`crate::NodeTable::create_node_id_vector`].
314-
// ///
315-
// /// # Parameters
316-
// ///
317-
// /// * `f`: a function. The function is passed the current table
318-
// /// collection and each [`crate::node_table::NodeTableRow`].
319-
// /// If `f` returns `true`, the index of that row is included
320-
// /// in the return value.
321-
// ///
322-
// /// # Examples
323-
// ///
324-
// /// Get all nodes with time > 0.0:
325-
// ///
326-
// /// ```
327-
// /// use tskit::bindings::tsk_id_t;
328-
// ///
329-
// /// use tskit::NodeListGenerator;
330-
// ///
331-
// /// let mut tables = tskit::TableCollection::new(100.).unwrap();
332-
// /// tables
333-
// /// .add_node(tskit::TSK_NODE_IS_SAMPLE, 0.0, tskit::PopulationId::NULL,
334-
// /// tskit::IndividualId::NULL)
335-
// /// .unwrap();
336-
// /// tables
337-
// /// .add_node(tskit::TSK_NODE_IS_SAMPLE, 1.0, tskit::PopulationId::NULL,
338-
// /// tskit::IndividualId::NULL)
339-
// /// .unwrap();
340-
// /// let samples = tables.create_node_id_vector(
341-
// /// |row: &tskit::NodeTableRow| row.time > 0.,
342-
// /// );
343-
// /// assert_eq!(samples[0], 1);
344-
// ///
345-
// /// // Get all nodes that have a mutation:
346-
// ///
347-
// /// fn node_has_mutation(
348-
// /// // dyn trait here means this
349-
// /// // will work with TreeSequence, too.
350-
// /// tables_type: &dyn tskit::TableAccess,
351-
// /// row: &tskit::NodeTableRow,
352-
// /// ) -> bool {
353-
// /// for mrow in tables_type.mutations_iter() {
354-
// /// if mrow.node == row.id {
355-
// /// return true;
356-
// /// }
357-
// /// }
358-
// /// false
359-
// /// }
360-
// ///
361-
// /// // Get all nodes that have a mutation:
362-
// ///
363-
// /// tables.add_mutation(0, 0, tskit::MutationId::NULL, 0.0, None).unwrap();
364-
// /// let samples_with_mut = tables.create_node_id_vector(
365-
// /// |row: &tskit::NodeTableRow| node_has_mutation(&tables, row));
366-
// /// assert_eq!(samples_with_mut[0], 0);
367-
// /// ```
368-
//
369-
// fn create_node_id_vector(
370-
// &self,
371-
// f: impl FnMut(&crate::NodeTableRow) -> bool,
372-
// ) -> Vec<crate::NodeId> {
373-
// self.nodes().create_node_id_vector(f)
374-
// }
375-
//}

0 commit comments

Comments
 (0)