-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KIP-54] Remove extra dependency on sortedcontainers
- Loading branch information
Showing
4 changed files
with
82 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class SortedSet: | ||
def __init__(self, iterable=None, key=None): | ||
self._key = key if key is not None else lambda x: x | ||
self._set = set(iterable) if iterable is not None else set() | ||
|
||
self._cached_last = None | ||
self._cached_first = None | ||
|
||
def first(self): | ||
if self._cached_first is not None: | ||
return self._cached_first | ||
|
||
first = None | ||
for element in self._set: | ||
if first is None or self._key(first) > self._key(element): | ||
first = element | ||
self._cached_first = first | ||
return first | ||
|
||
def last(self): | ||
if self._cached_last is not None: | ||
return self._cached_last | ||
|
||
last = None | ||
for element in self._set: | ||
if last is None or self._key(last) < self._key(element): | ||
last = element | ||
self._cached_last = last | ||
return last | ||
|
||
def pop_last(self): | ||
value = self.last() | ||
self._set.remove(value) | ||
self._cached_last = None | ||
return value | ||
|
||
def add(self, value): | ||
if self._cached_last is not None and self._key(value) > self._key(self._cached_last): | ||
self._cached_last = value | ||
if self._cached_first is not None and self._key(value) < self._key(self._cached_first): | ||
self._cached_first = value | ||
|
||
return self._set.add(value) | ||
|
||
def remove(self, value): | ||
if self._cached_last is not None and self._cached_last == value: | ||
self._cached_last = None | ||
if self._cached_first is not None and self._cached_first == value: | ||
self._cached_first = None | ||
|
||
return self._set.remove(value) | ||
|
||
def __contains__(self, value): | ||
return value in self._set | ||
|
||
def __iter__(self): | ||
return iter(sorted(self._set, key=self._key)) | ||
|
||
def _bool(self): | ||
return len(self._set) != 0 | ||
|
||
__nonzero__ = _bool | ||
__bool__ = _bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,3 @@ pytest-mock==1.10.0 | |
sphinx-rtd-theme==0.2.4 | ||
crc32c==1.7 | ||
py==1.8.0 | ||
sortedcontainers==2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters