-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have .add(value) return the index for value #6
Comments
Can you describe your use case in more detail? The .add method doesn't compute the new index at which the item was added. Adding this functionality would decrease the performance of the current method. Returning that index would be pretty similar to: def add(self, val):
self._original_add(val)
return self.index(val) You might simply monkey-patch the add method yourself to suit your needs. I could also imagine others wanting the added value returned or the list/set itself. |
The use case is timeseries-sorted data. I have multiple data streams, each Also, looking at the code for your library, I don't think this would impact |
This seems like a pretty ideal case for subclassing and getting the behavior you want: from sortedcontainers import SortedList as OriginalSortedList
class SortedList(OriginalSortedList):
def add(self, val):
super(SortedList, self).add(val)
return self.index(val) The >>> from timeit import timeit
>>> def test(command, setup):
... times = [timeit(command, setup=setup, number=1) for rpt in xrange(99)]
... times.sort()
... avg = sum(times) / len(times)
... return 'median:', times[49], 'average:', avg
>>> test('insort(l, 42)', 'from bisect import insort; l = list(xrange(100))')
('median:', 9.5367431640625e-07, 'average:', 1.2715657552083333e-06)
>>> test('pos = bisect(l, 42); l.insert(pos, 42)', 'from bisect import bisect; l = list(xrange(100))')
('median:', 1.1920928955078125e-06, 'average:', 1.625581221147017e-06) Also, in most cases it should be just "a bit of math to adjust the index" if the _index cache is valid. But in case it's not, iterating the lengths of the segments is required. This can be a slow process. |
It would be very useful for the .add methods on both the SortedList & SortedSet classes to return the new index at which the added item exists in the logical array.
The text was updated successfully, but these errors were encountered: