Skip to content

Commit 8fd1913

Browse files
Use C implementation for seek_index
1 parent 6763118 commit 8fd1913

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

python/_tskitmodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10658,6 +10658,29 @@ Tree_seek(Tree *self, PyObject *args)
1065810658
return ret;
1065910659
}
1066010660

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+
1066110684
static PyObject *
1066210685
Tree_clear(Tree *self)
1066310686
{
@@ -11796,6 +11819,10 @@ static PyMethodDef Tree_methods[] = {
1179611819
.ml_meth = (PyCFunction) Tree_seek,
1179711820
.ml_flags = METH_VARARGS,
1179811821
.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" },
1179911826
{ .ml_name = "clear",
1180011827
.ml_meth = (PyCFunction) Tree_clear,
1180111828
.ml_flags = METH_NOARGS,

python/tests/test_lowlevel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,6 +2968,16 @@ def test_seek_errors(self):
29682968
with pytest.raises(_tskit.LibraryError):
29692969
tree.seek(bad_pos)
29702970

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+
29712981
def test_root_threshold(self):
29722982
for ts in self.get_example_tree_sequences():
29732983
tree = _tskit.Tree(ts)

python/tskit/trees.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ def seek_index(self, index):
820820
821821
.. include:: substitutions/linear_traversal_warning.rst
822822
823-
824823
:param int index: The tree index to seek to.
825824
:raises IndexError: If an index outside the acceptable range is provided.
826825
"""
@@ -829,12 +828,7 @@ def seek_index(self, index):
829828
index += num_trees
830829
if index < 0 or index >= num_trees:
831830
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)
838832

839833
def seek(self, position):
840834
"""

0 commit comments

Comments
 (0)