-
Notifications
You must be signed in to change notification settings - Fork 217
add SortedArray #183
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
Open
bsamedi
wants to merge
3
commits into
grantjenks:master
Choose a base branch
from
bsamedi:feature/sortedarray
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add SortedArray #183
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,143 @@ | ||
"""Sorted Array | ||
=============== | ||
|
||
|
||
:doc:`Sorted Containers<index>` is an Apache2 licensed Python sorted | ||
collections library, written in pure-Python, and fast as C-extensions. The | ||
:doc:`introduction<introduction>` is the best way to get started. | ||
|
||
Sorted list implementations: | ||
|
||
.. currentmodule:: sortedcontainers | ||
|
||
* :class:`SortedArray` | ||
|
||
""" | ||
# pylint: disable=too-many-lines | ||
from __future__ import print_function | ||
from sys import hexversion | ||
|
||
from .sortedlist import SortedList, recursive_repr | ||
from array import array | ||
|
||
class SortedArray(SortedList): | ||
"""Sorted array is a sorted mutable sequence. | ||
|
||
Sorted array values are maintained in sorted order. | ||
|
||
Underlying data structure is the standard library array.array | ||
Enables densly packed lists floats and doubles. | ||
Enables densly packed lists of integers in CPython. | ||
|
||
Methods for adding values: | ||
|
||
* :func:`SortedArray.add` | ||
* :func:`SortedArray.update` | ||
* :func:`SortedArray.__add__` | ||
* :func:`SortedArray.__iadd__` | ||
* :func:`SortedArray.__mul__` | ||
* :func:`SortedArray.__imul__` | ||
|
||
Methods for removing values: | ||
|
||
* :func:`SortedArray.clear` | ||
* :func:`SortedArray.discard` | ||
* :func:`SortedArray.remove` | ||
* :func:`SortedArray.pop` | ||
* :func:`SortedArray.__delitem__` | ||
|
||
Methods for looking up values: | ||
|
||
* :func:`SortedArray.bisect_left` | ||
* :func:`SortedArray.bisect_right` | ||
* :func:`SortedArray.count` | ||
* :func:`SortedArray.index` | ||
* :func:`SortedArray.__contains__` | ||
* :func:`SortedArray.__getitem__` | ||
|
||
Methods for iterating values: | ||
|
||
* :func:`SortedArray.irange` | ||
* :func:`SortedArray.islice` | ||
* :func:`SortedArray.__iter__` | ||
* :func:`SortedArray.__reversed__` | ||
|
||
Methods for miscellany: | ||
|
||
* :func:`SortedArray.copy` | ||
* :func:`SortedArray.__len__` | ||
* :func:`SortedArray.__repr__` | ||
* :func:`SortedArray._check` | ||
* :func:`SortedArray._reset` | ||
|
||
Sorted lists use lexicographical ordering semantics when compared to other | ||
sequences. | ||
|
||
Some methods of mutable sequences are not supported and will raise | ||
not-implemented error. | ||
|
||
""" | ||
DEFAULT_LOAD_FACTOR = 1000 | ||
|
||
|
||
def __init__(self, typecode, initializer=None): | ||
"""Initialize sorted list instance. | ||
|
||
Optional `iterable` argument provides an initial iterable of values to | ||
initialize the sorted list. | ||
bsamedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Runtime complexity: `O(n*log(n))` | ||
|
||
>>> sl = SortedArray('i') | ||
>>> sl | ||
SortedArray('i', []) | ||
>>> sl = SortedArray('i', [3, 1, 2, 5, 4]) | ||
>>> sl | ||
SortedArray('i', [1, 2, 3, 4, 5]) | ||
|
||
:param typecode: type code for the array, as in the array.array standard library class (required) | ||
:param iterable: initial values (optional) | ||
|
||
""" | ||
self._typecode = typecode | ||
if hexversion >= 0x03000000: | ||
super().__init__(iterable=initializer, key=None) | ||
else: | ||
super(SortedArray, self).__init__(iterable=initializer, key=None) | ||
|
||
|
||
def __new__(cls, typecode, initializer=None): | ||
# pylint: disable=unused-argument | ||
if hexversion >= 0x03000000: | ||
return super().__new__(cls, iterable=initializer, key=None) | ||
else: | ||
return super(SortedArray, cls).__new__(cls, iterable=initializer, key=None) | ||
|
||
|
||
def _new_list(self): | ||
_typecode = self._typecode | ||
return array(_typecode) | ||
|
||
|
||
def _sort_in_place(self, _list): | ||
# array.array does not support sort in place | ||
sorted_list = sorted(_list) | ||
del _list[:] | ||
_list.extend(sorted_list) | ||
|
||
|
||
@recursive_repr() | ||
def __repr__(self): | ||
"""Return string representation of sorted array. | ||
|
||
``sa.__repr__()`` <==> ``repr(sa)`` | ||
|
||
:return: string representation | ||
|
||
>>> sa = SortedArray('i',[5,4,3]) | ||
>>> sa | ||
SortedArray('i', [3, 4, 5]) | ||
""" | ||
class_name = type(self).__name__ | ||
_typecode = self._typecode | ||
return "{0}('{1}', {2!r})".format(class_name, _typecode, list(self)) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.