Package qubx :: Module sorted_collection :: Class SortedCollection
[hide private]
[frames] | no frames]

Class SortedCollection

source code

object --+
         |
        SortedCollection

Sequence sorted by a key function.

SortedCollection() is much easier to work with than using bisect() directly. It supports key functions like those use in sorted(), min(), and max(). The result of the key function call is saved so that keys can be searched efficiently.

Instead of returning an insertion-point which can be hard to interpret, the five find-methods return a specific item in the sequence. They can scan for exact matches, the last item less-than-or-equal to a key, or the first item greater-than-or-equal to a key.

Once found, an item's ordinal position can be located with the index() method. New items can be added with the insert() and insert_right() methods. Old items can be deleted with the remove() method.

The usual sequence methods are provided to support indexing, slicing, length lookup, clearing, copying, forward and reverse iteration, contains checking, item counts, item removal, and a nice looking repr.

Finding and indexing are O(log n) operations while iteration and insertion are O(n). The initial sort is O(n log n).

The key function is stored in the 'key' attibute for easy introspection or so that you can assign a new key function (triggering an automatic re-sort).

In short, the class was designed to handle all of the common use cases for bisect but with a simpler API and support for key functions.

>>> from pprint import pprint
>>> from operator import itemgetter
>>> s = SortedCollection(key=itemgetter(2))
>>> for record in [
...         ('roger', 'young', 30),
...         ('angela', 'jones', 28),
...         ('bill', 'smith', 22),
...         ('david', 'thomas', 32)]:
...     s.insert(record)
>>> pprint(list(s))         # show records sorted by age
[('bill', 'smith', 22),
 ('angela', 'jones', 28),
 ('roger', 'young', 30),
 ('david', 'thomas', 32)]
>>> s.find_le(29)           # find oldest person aged 29 or younger
('angela', 'jones', 28)
>>> s.find_lt(28)           # find oldest person under 28
('bill', 'smith', 22)
>>> s.find_gt(28)           # find youngest person over 28
('roger', 'young', 30)
>>> r = s.find_ge(32)       # find youngest person aged 32 or older
>>> s.index(r)              # get the index of their record
3
>>> s[3]                    # fetch the record at that index
('david', 'thomas', 32)
>>> s.key = itemgetter(0)   # now sort by first name
>>> pprint(list(s))
[('angela', 'jones', 28),
 ('bill', 'smith', 22),
 ('david', 'thomas', 32),
 ('roger', 'young', 30)]
Instance Methods [hide private]
 
__init__(self, iterable=(), key=None)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
_getkey(self) source code
 
_setkey(self, key) source code
 
_delkey(self) source code
 
clear(self) source code
 
copy(self) source code
 
__len__(self) source code
 
__getitem__(self, i) source code
 
__iter__(self) source code
 
__reversed__(self) source code
 
__repr__(self)
repr(x)
source code
 
__reduce__(self)
helper for pickle
source code
 
__contains__(self, item) source code
 
index(self, item)
Find the position of an item.
source code
 
index_le(self, k)
Find the position of last item with a key <= k.
source code
 
index_lt(self, k)
Find the position of last item with a key < k.
source code
 
index_ge(self, k)
Find the position of first item with a key >= equal to k.
source code
 
index_gt(self, k)
Find the position of first item with a key > k.
source code
 
count(self, item)
Return number of occurrences of item
source code
 
insert(self, item)
Insert a new item.
source code
 
insert_right(self, item)
Insert a new item.
source code
 
remove(self, item)
Remove first occurence of item.
source code
 
find(self, k)
Return first item with a key == k.
source code
 
find_le(self, k)
Return last item with a key <= k.
source code
 
find_lt(self, k)
Return last item with a key < k.
source code
 
find_ge(self, k)
Return first item with a key >= equal to k.
source code
 
find_gt(self, k)
Return first item with a key > k.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]
  key
key function

Inherited from object: __class__

Method Details [hide private]

__init__(self, iterable=(), key=None)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

__reduce__(self)

source code 

helper for pickle

Overrides: object.__reduce__
(inherited documentation)

index(self, item)

source code 

Find the position of an item. Raise ValueError if not found.

index_le(self, k)

source code 

Find the position of last item with a key <= k. Raise ValueError if not found.

index_lt(self, k)

source code 

Find the position of last item with a key < k. Raise ValueError if not found.

index_ge(self, k)

source code 

Find the position of first item with a key >= equal to k. Raise ValueError if not found

index_gt(self, k)

source code 

Find the position of first item with a key > k. Raise ValueError if not found

insert(self, item)

source code 

Insert a new item. If equal keys are found, add to the left

insert_right(self, item)

source code 

Insert a new item. If equal keys are found, add to the right

remove(self, item)

source code 

Remove first occurence of item. Raise ValueError if not found

find(self, k)

source code 

Return first item with a key == k. Raise ValueError if not found.

find_le(self, k)

source code 

Return last item with a key <= k. Raise ValueError if not found.

find_lt(self, k)

source code 

Return last item with a key < k. Raise ValueError if not found.

find_ge(self, k)

source code 

Return first item with a key >= equal to k. Raise ValueError if not found

find_gt(self, k)

source code 

Return first item with a key > k. Raise ValueError if not found


Property Details [hide private]

key

key function

Get Method:
_getkey(self)
Set Method:
_setkey(self, key)
Delete Method:
_delkey(self)