File tree Expand file tree Collapse file tree 3 files changed +38
-7
lines changed Expand file tree Collapse file tree 3 files changed +38
-7
lines changed Original file line number Diff line number Diff line change @@ -10658,6 +10658,29 @@ Tree_seek(Tree *self, PyObject *args)
10658
10658
return ret ;
10659
10659
}
10660
10660
10661
+ static PyObject *
10662
+ Tree_seek_index (Tree * self , PyObject * args )
10663
+ {
10664
+ PyObject * ret = NULL ;
10665
+ tsk_id_t index = 0 ;
10666
+ int err ;
10667
+
10668
+ if (Tree_check_state (self ) != 0 ) {
10669
+ goto out ;
10670
+ }
10671
+ if (!PyArg_ParseTuple (args , "O&" , tsk_id_converter , & index )) {
10672
+ goto out ;
10673
+ }
10674
+ err = tsk_tree_seek_index (self -> tree , index , 0 );
10675
+ if (err != 0 ) {
10676
+ handle_library_error (err );
10677
+ goto out ;
10678
+ }
10679
+ ret = Py_BuildValue ("" );
10680
+ out :
10681
+ return ret ;
10682
+ }
10683
+
10661
10684
static PyObject *
10662
10685
Tree_clear (Tree * self )
10663
10686
{
@@ -11796,6 +11819,10 @@ static PyMethodDef Tree_methods[] = {
11796
11819
.ml_meth = (PyCFunction ) Tree_seek ,
11797
11820
.ml_flags = METH_VARARGS ,
11798
11821
.ml_doc = "Seeks to the tree at the specified position" },
11822
+ { .ml_name = "seek_index" ,
11823
+ .ml_meth = (PyCFunction ) Tree_seek_index ,
11824
+ .ml_flags = METH_VARARGS ,
11825
+ .ml_doc = "Seeks to the tree at the specified index" },
11799
11826
{ .ml_name = "clear" ,
11800
11827
.ml_meth = (PyCFunction ) Tree_clear ,
11801
11828
.ml_flags = METH_NOARGS ,
Original file line number Diff line number Diff line change @@ -2968,6 +2968,16 @@ def test_seek_errors(self):
2968
2968
with pytest .raises (_tskit .LibraryError ):
2969
2969
tree .seek (bad_pos )
2970
2970
2971
+ def test_seek_index_errors (self ):
2972
+ ts = self .get_example_tree_sequence ()
2973
+ tree = _tskit .Tree (ts )
2974
+ for bad_type in ["" , "x" , {}]:
2975
+ with pytest .raises (TypeError ):
2976
+ tree .seek_index (bad_type )
2977
+ for bad_index in [- 1 , 10 ** 6 ]:
2978
+ with pytest .raises (_tskit .LibraryError ):
2979
+ tree .seek_index (bad_index )
2980
+
2971
2981
def test_root_threshold (self ):
2972
2982
for ts in self .get_example_tree_sequences ():
2973
2983
tree = _tskit .Tree (ts )
Original file line number Diff line number Diff line change @@ -820,7 +820,6 @@ def seek_index(self, index):
820
820
821
821
.. include:: substitutions/linear_traversal_warning.rst
822
822
823
-
824
823
:param int index: The tree index to seek to.
825
824
:raises IndexError: If an index outside the acceptable range is provided.
826
825
"""
@@ -829,12 +828,7 @@ def seek_index(self, index):
829
828
index += num_trees
830
829
if index < 0 or index >= num_trees :
831
830
raise IndexError ("Index out of bounds" )
832
- # This should be implemented in C efficiently using the indexes.
833
- # No point in complicating the current implementation by trying
834
- # to seek from the correct direction.
835
- self .first ()
836
- while self .index != index :
837
- self .next ()
831
+ self ._ll_tree .seek_index (index )
838
832
839
833
def seek (self , position ):
840
834
"""
You can’t perform that action at this time.
0 commit comments