@@ -85,77 +85,170 @@ impl TableViews {
85
85
Self :: new_from_NonNull_table_collection ( & mut n)
86
86
}
87
87
88
+ /// Get reference to the [``EdgeTable``](crate::EdgeTable).
88
89
pub fn edges ( & self ) -> & EdgeTable {
89
90
& self . edges
90
91
}
91
92
93
+ /// Get reference to the [``NodeTable``](crate::NodeTable).
92
94
pub fn nodes ( & self ) -> & NodeTable {
93
95
& self . nodes
94
96
}
95
97
98
+ /// Get mutable reference to the [``NodeTable``](crate::NodeTable).
96
99
pub fn nodes_mut ( & mut self ) -> & mut NodeTable {
97
100
& mut self . nodes
98
101
}
99
102
103
+ /// Get reference to the [``SiteTable``](crate::SiteTable).
100
104
pub fn sites ( & self ) -> & SiteTable {
101
105
& self . sites
102
106
}
103
107
108
+ /// Get reference to the [``MutationTable``](crate::MutationTable).
104
109
pub fn mutations ( & self ) -> & MutationTable {
105
110
& self . mutations
106
111
}
107
112
113
+ /// Get reference to the [``IndividualTable``](crate::IndividualTable).
108
114
pub fn individuals ( & self ) -> & IndividualTable {
109
115
& self . individuals
110
116
}
111
117
118
+ /// Get reference to the [``PopulationTable``](crate::PopulationTable).
112
119
pub fn populations ( & self ) -> & PopulationTable {
113
120
& self . populations
114
121
}
115
122
123
+ /// Get reference to the [``MigrationTable``](crate::MigrationTable).
116
124
pub fn migrations ( & self ) -> & MigrationTable {
117
125
& self . migrations
118
126
}
119
127
120
128
#[ cfg( feature = "provenance" ) ]
129
+ #[ cfg_attr( doc_cfg, doc( cfg( feature = "provenance" ) ) ) ]
130
+ /// Get reference to the [``ProvenanceTable``](crate::provenance::ProvenanceTable)
121
131
pub fn provenances ( & self ) -> & ProvenanceTable {
122
132
& self . provenances
123
133
}
124
134
135
+ /// Return an iterator over the edges.
125
136
pub fn edges_iter ( & self ) -> impl Iterator < Item = crate :: EdgeTableRow > + ' _ {
126
137
self . edges . iter ( )
127
138
}
128
139
140
+ /// Return an iterator over the nodes.
129
141
pub fn nodes_iter ( & self ) -> impl Iterator < Item = crate :: NodeTableRow > + ' _ {
130
142
self . nodes . iter ( )
131
143
}
132
144
145
+ /// Return an iterator over the sites.
133
146
pub fn sites_iter ( & self ) -> impl Iterator < Item = crate :: SiteTableRow > + ' _ {
134
147
self . sites . iter ( )
135
148
}
136
149
150
+ /// Return an iterator over the mutations.
137
151
pub fn mutations_iter ( & self ) -> impl Iterator < Item = crate :: MutationTableRow > + ' _ {
138
152
self . mutations . iter ( )
139
153
}
140
154
155
+ /// Return an iterator over the individuals.
141
156
pub fn individuals_iter ( & self ) -> impl Iterator < Item = crate :: IndividualTableRow > + ' _ {
142
157
self . individuals . iter ( )
143
158
}
144
159
160
+ /// Return an iterator over the populations.
145
161
pub fn populations_iter ( & self ) -> impl Iterator < Item = crate :: PopulationTableRow > + ' _ {
146
162
self . populations . iter ( )
147
163
}
148
164
165
+ /// Return an iterator over the migrations.
149
166
pub fn migrations_iter ( & self ) -> impl Iterator < Item = crate :: MigrationTableRow > + ' _ {
150
167
self . migrations . iter ( )
151
168
}
152
169
153
170
#[ cfg( feature = "provenance" ) ]
171
+ #[ cfg_attr( doc_cfg, doc( cfg( feature = "provenance" ) ) ) ]
172
+ /// Return an iterator over provenances
154
173
pub fn provenances_iter (
155
174
& self ,
156
175
) -> impl Iterator < Item = crate :: provenance:: ProvenanceTableRow > + ' _ {
157
176
self . provenances . iter ( )
158
177
}
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
+ }
159
252
}
160
253
161
254
#[ cfg( test) ]
@@ -181,195 +274,3 @@ mod table_views_tests {
181
274
}
182
275
}
183
276
}
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