Skip to content

Commit

Permalink
Complexity of IntervalTree.{begin,end} is O(1), not O(n).
Browse files Browse the repository at this point in the history
They call self.boundary_table.iloc[0] (resp. [-1]), and self.boundary_table
is a SortedDict.
SortedDict.iloc is an instance of _IlocWrapper, whose __getitem__ only
does: return self._dict._list[index]
_dict is the SortedDict instance, and _dict._list is an instance of
SortedList.
Finally, SortedList.__item__ has special cases to perform in O(1)
when the index is 0 or -1.

References:
* https://github.com/grantjenks/sorted_containers/blob/ccec8d4/sortedcontainers/sorteddict.py#L34-L40
* https://github.com/grantjenks/sorted_containers/blob/b62fe6a/sortedcontainers/sortedlist.py#L599-L602
  • Loading branch information
progval committed Mar 15, 2016
1 parent 30fdc1b commit e3f747d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions intervaltree/intervaltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def begin(self):
"""
Returns the lower bound of the first interval in the tree.
Completes in O(n) time.
Completes in O(1) time.
"""
if not self.boundary_table:
return 0
Expand All @@ -828,7 +828,7 @@ def end(self):
"""
Returns the upper bound of the last interval in the tree.
Completes in O(n) time.
Completes in O(1) time.
"""
if not self.boundary_table:
return 0
Expand Down

0 comments on commit e3f747d

Please sign in to comment.