python-sortedcontainers
python-sortedcontainers copied to clipboard
feat: return index where item is added when calling SortedList.add()
Hi,
For a use case I'm working with at the moment (today's LeetCode.com challenge 🤓) it is necessary to know the index at which an item gets inserted to after calling s.add(item), where s is a SortedList.
However, the add method returns nothing and so I have to call s.bisect_left(item) to get the index and then s.add(item) to perform the update. This is a waste of computation, however.
Currently s.add(item) returns nothing so it would be a backwards compatible change to return the index where the item is added. I had a look at sortedlist.py and it appears that this would be a fairly straight forward feature to implement, but I couldn't go about it myself as I can't wrap my head around a few edge cases e.g. how best to handle when the inserted item already exists and hence the index it enters is arbitrary.
Thanks,
Marrick.