How to use the sortedcontainers.sortedlistwithkey.Pair function in sortedcontainers

To help you get started, we’ve selected a few sortedcontainers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def update(self, iterable):
        """Update the list by adding all elements from *iterable*."""
        _key = self._key
        self._list.update(Pair(_key(val), val) for val in iterable)
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def __contains__(self, value):
        """Return True if and only if *value* is an element in the list."""
        _list = self._list
        _key = self._key(value)
        _pair = Pair(_key, value)

        _maxes = _list._maxes

        if not _maxes:
            return False

        pos = bisect_left(_maxes, _pair)

        if pos == len(_maxes):
            return False

        _lists = _list._lists

        idx = bisect_left(_lists[pos], _pair)

        len_lists = len(_lists)
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def bisect_right(self, value):
        """
        Same as *bisect_left*, but if *value* is already present, the insertion
        point will be after (to the right of) any existing entries.
        """
        return self._list.bisect_right(Pair(self._key(value), value))
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def add(self, value):
        """Add the element *value* to the list."""
        self._list.add(Pair(self._key(value), value))
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def index(self, value, start=None, stop=None):
        """
        Return the smallest *k* such that L[k] == value and i <= k < j`.  Raises
        ValueError if *value* is not present.  *stop* defaults to the end of the
        list. *start* defaults to the beginning. Negative indices are supported,
        as for slice indices.
        """
        _list = self._list
        _key = self._key(value)
        _pair = Pair(_key, value)
        _len = _list._len

        if start is None:
            start = 0
        if start < 0:
            start += _len
        if start < 0:
            start = 0

        if stop is None:
            stop = _len
        if stop < 0:
            stop += _len
        if stop > _len:
            stop = _len
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def remove(self, value):
        """
        Remove first occurrence of *value*.

        Raises ValueError if *value* is not present.
        """
        _list = self._list
        _key = self._key(value)
        _pair = Pair(_key, value)

        _maxes = _list._maxes

        if not _maxes:
            raise ValueError('{0} is not in list'.format(repr(value)))

        pos = bisect_left(_maxes, _pair)

        if pos == len(_maxes):
            raise ValueError('{0} is not in list'.format(repr(value)))

        _lists = _list._lists

        idx = bisect_left(_lists[pos], _pair)

        len_lists = len(_lists)
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def __setitem__(self, index, value):
        """
        Replace the item at position *index* with *value*.

        Supports slicing.
        """
        _key = self._key
        if isinstance(index, slice):
            self._list[index] = list(Pair(_key(val), val) for val in value)
        else:
            self._list[index] = Pair(_key(value), value)
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def append(self, value):
        """
        Append the element *value* to the list. Raises a ValueError if the
        *value* would violate the sort order.
        """
        self._list.append(Pair(self._key(value), value))
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def bisect(self, value):
        """Same as bisect_right."""
        return self._list.bisect_right(Pair(self._key(value), value))
github grantjenks / python-sortedcontainers / sortedcontainers / sortedlistwithkey.py View on Github external
def bisect_left(self, value):
        """
        Similar to the *bisect* module in the standard library, this returns an
        appropriate index to insert *value*. If *value* is already present, the
        insertion point will be before (to the left of) any existing entries.
        """
        return self._list.bisect_left(Pair(self._key(value), value))