| 1 |
--- |
| 2 |
title: 'Python Sorted Containers' |
| 3 |
tags: |
| 4 |
- Python |
| 5 |
- sorted |
| 6 |
- list |
| 7 |
- dictionary |
| 8 |
- set |
| 9 |
authors: |
| 10 |
- name: Grant Jenks |
| 11 |
orcid: 0000-0001-5010-405X |
| 12 |
affiliation: 1 |
| 13 |
affiliations: |
| 14 |
- name: None |
| 15 |
index: 1 |
| 16 |
date: 5 March 2019 |
| 17 |
bibliography: paper.bib |
| 18 |
--- |
| 19 |
|
| 20 |
# Summary |
| 21 |
|
| 22 |
The standard library of popular languages like C++, Java, and C# provide sorted |
| 23 |
container data types based on binary tree data structures. While Python |
| 24 |
[-@python] has risen in popularity, the Standard Library still lacks these |
| 25 |
common data types. Part of the challenge has been Python's rich object model |
| 26 |
which makes binary trees implemented in Python slow in terms of both memory and |
| 27 |
processor usage. To overcome the overhead of the interpreter, C-extensions are |
| 28 |
used by the Python core developers. In doing so, flexibility is tradeoff for |
| 29 |
performance. The goal of the Python core developers is to provide the right set |
| 30 |
of high-level APIs so that algorithms and data structures can be implemented |
| 31 |
efficiently. The Python Sorted Containers library uses Python's high-level APIs |
| 32 |
to efficiently implement sorted container data structures. |
| 33 |
|
| 34 |
Python's collections data structures are based on three data types: sequences, |
| 35 |
mappings, and sets. These data types are implemented and most commonly used as |
| 36 |
list, dictionary, and set objects. The Python Sorted Containers library |
| 37 |
introduces new variants of these three data types that are each sorted: sorted |
| 38 |
list, sorted dictionary, and sorted set. In each case, the original semantics |
| 39 |
are extended to preserve sorted order of the contained elements with respect to |
| 40 |
mutating operations. When unable to preserve the sorted order constraint, the |
| 41 |
functionality is either non-existent or an error is raised from the |
| 42 |
library. Python's "sorted" built-in function also supports a "key" parameter |
| 43 |
which specifies a callable used to extract a comparison key from elements. When |
| 44 |
initializing a sorted container data type, the key paramemter is likewise |
| 45 |
supported. |
| 46 |
|
| 47 |
Internally, Python Sorted Containers uses a list of sublists data structure |
| 48 |
that is like a B-tree contstrained to two levels of nodes. The maximum of each |
| 49 |
sublist is maintained in a separate list. To lookup an element, the list of |
| 50 |
maximums is bisected using the "bisect" module in the Standard Library. Using |
| 51 |
the bisected maximums index, the corresponding sublist is bisected to find the |
| 52 |
index of the desired element. To index the k'th element, a separate positional |
| 53 |
index, known as the "Jenks" index, is built and maintained. The positional |
| 54 |
index is like the order statistic of a binary tree densely packed into a |
| 55 |
list. By maintaining the size of the sublists as proportional to the |
| 56 |
$\sqrt[3]{n}$ the amortized time complexity of all operations is |
| 57 |
$O(\sqrt[3]{n})$. This bound works well for up to billions of elements which |
| 58 |
often exhausts all available memory. |
| 59 |
|
| 60 |
Python Sorted Containers overlaps and extends the "bisect" and "heapq" modules |
| 61 |
provided in the Standard Library. In contrast to the function-oriented |
| 62 |
interface provided by these modules, Sorted Containers provides an |
| 63 |
object-oriented interface. Externally, SQLite in-memory indexes, Pandas |
| 64 |
DataFrame indexes, and Redis sorted sets provide similar functionality. These |
| 65 |
data structures are applied in priority queue, multiset, nearest neighbors, |
| 66 |
intervals, and ranking algorithms. Sorted Containers is used by scientific open |
| 67 |
source projects such as: Angr [-@angr-shoshitaishvili2016state], a binary |
| 68 |
analysis platform from UC Santa Barbara; Astropy [-@astropy:2018], a community |
| 69 |
Python package for astronomy; Dask Distributed |
| 70 |
[-@dask-matthew_rocklin-proc-scipy-2015], a library for dynamic task scheduling |
| 71 |
by Anaconda; Trio [-@trio], an asynchronous I/O library; and Zipline |
| 72 |
[-@zipline], an algorithmic trading library by Quantopian. |
| 73 |
|
| 74 |
# Acknowledgements |
| 75 |
|
| 76 |
Thank you to Daniel Stutzbach for the "blist" [-@blist] software project to |
| 77 |
which Sorted Containers owes much of the original interface design. |
| 78 |
|
| 79 |
Thank you to Raymond Hettinger for the "SortedCollection" recipe |
| 80 |
[-@sortedcollection] which originally inspired the support and design of the |
| 81 |
"key" parameter feature. |
| 82 |
|
| 83 |
Thank you to Manfred Moitzi for the "bintrees" software project [-@bintrees] |
| 84 |
which motivated the range-based tree traversal interfaces. |
| 85 |
|
| 86 |
Thank you to Dan Stromberg for the benchmark comparisons of less common binary |
| 87 |
tree data structures [-@dan-stromberg] like treap, splay, and scapegoat. |
| 88 |
|
| 89 |
Thank you to the open source community that has contributed bug reports, |
| 90 |
documentation improvements, and feature guidance in development of the project. |
| 91 |
|
| 92 |
# References |