Skip to main content

Pure python implementation of a sparse array.

Project description

This code has been transpiled from js-sparse-array and has minimal changes.

Usage

SparseArray implements MutableSequence.

We suggest you import the SparseArray as follows:

>>> from bitmap_sparse_array import SparseArray

Setting and getting

>>> sa = SparseArray()
>>> sa[1000000] = 'value'
>>> sa[1000000]
'value'
>>> sa[1]
IndexError()
>>> len(sa)
1000001

Iterating

Due to the sparse nature of SparseArray, it does not make sense to iterate over all values in the traditional sense because of possible omitted indices. Therefore, you cannot iterate over the SparseArray itself. Instead, 3 generators are provided.

>>> sa = SparseArray()
>>> sa[0] = '0'
>>> sa[2] = '2'

SparseArray.indices() returns a generator of indices that have values.

>>> list(sa.indices())
[0, 2]

SparseArray.items() returns a generator of tuples of indices and values, similar to Mapping.items().

>>> list(sa.items())
[(0, '0'), (2, '2')]

SparseArray.values(*, default=None, start=0, end=None) returns a generator of values with a default value for non-set indices.

>>> list(sa.values())
['0', None, '2']
>>> list(sa.values(default='default'))
['0', 'default', '2']

For the same reasons, you cannot get or set slices of a SparseArray, nor insert()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitmap_sparse_array-0.1.3.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

bitmap_sparse_array-0.1.3-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page