SortedCollection

class datastructuretools.SortedCollection(iterable=(), key=None)

A sorted collection.

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 = datastructuretools.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)
]

Lineage

digraph InheritanceGraph { graph [background=transparent, bgcolor=transparent, color=lightslategrey, fontname=Arial, outputorder=edgesfirst, overlap=prism, penwidth=2, rankdir=LR, root="__builtin__.object", splines=spline, style="dotted, rounded", truecolor=true]; node [colorscheme=pastel19, fontname=Arial, fontsize=12, penwidth=2, style="filled, rounded"]; edge [color=lightsteelblue2, penwidth=2]; subgraph cluster_datastructuretools { graph [label=datastructuretools]; "abjad.tools.datastructuretools.SortedCollection.SortedCollection" [color=black, fontcolor=white, group=1, label=<<B>SortedCollection</B>>, shape=box, style="filled, rounded"]; } subgraph cluster_builtins { graph [label=builtins]; "builtins.object" [color=1, group=0, label=object, shape=box]; } "builtins.object" -> "abjad.tools.datastructuretools.SortedCollection.SortedCollection"; }

Bases

  • builtins.object

Attribute summary

clear() Clears sorted collection.
copy() Copies sorted collection.
count(item) Returns number of occurrences of item
find(k) Returns first item with a key == k.
find_ge(k) Returns first item with a key >= equal to k.
find_gt(k) Returns first item with a key > k.
find_le(k) Returns last item with a key <= k.
find_lt(k) Returns last item with a key < k.
index(item) Find the position of an item.
insert(item) Insert a new item.
insert_right(item) Insert a new item.
key key function
remove(item) Remove first occurence of item.
__contains__(item) Is true when sorted collection contains item.
__getitem__(i) Gets i in sorted collection.
__iter__() Iterates sorted collection.
__len__() Length of sorted collection.
__repr__() Interpreter reprsentation of sorted collection.
__reversed__() Reverses sorted collection.

Read/write properties

SortedCollection.key

key function

Methods

SortedCollection.clear()

Clears sorted collection.

Returns none.

SortedCollection.copy()

Copies sorted collection.

Returns new sorted collection.

SortedCollection.count(item)

Returns number of occurrences of item

SortedCollection.find(k)

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

SortedCollection.find_ge(k)

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

SortedCollection.find_gt(k)

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

SortedCollection.find_le(k)

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

SortedCollection.find_lt(k)

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

SortedCollection.index(item)

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

SortedCollection.insert(item)

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

SortedCollection.insert_right(item)

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

SortedCollection.remove(item)

Remove first occurence of item. Raise ValueError if not found

Special methods

SortedCollection.__contains__(item)

Is true when sorted collection contains item. Otherwise false.

Returns true or false.

SortedCollection.__getitem__(i)

Gets i in sorted collection.

Returns item.

SortedCollection.__iter__()

Iterates sorted collection.

Yields items.

SortedCollection.__len__()

Length of sorted collection.

Defined equal to number of items in collection.

Returns nonnegatie integer.

SortedCollection.__repr__()

Interpreter reprsentation of sorted collection.

Returns string.

SortedCollection.__reversed__()

Reverses sorted collection.

Yields items.