Skip to main content

Blackjack

Blackjack is a simple implementation of the classic red-black tree as a standard Python data structure. A set and a dictionary are included:

>>> from blackjack import BJ, Deck
>>> bj = BJ()
>>> bj
BJ([])
>>> bj.add(42)
>>> 42 in bj
True
>>> d = Deck()
>>> d[1] = 2
>>> d[3] = 4
>>> d.keys()
[1, 3]
>>> d.values()
[2, 4]

Usage

Blackjacks and decks behave just like normal Python sets and dictionaries, but have different performance characteristics and different requirements for keys. All keys must be comparable, but need not be hashable:

>>> bj = BJ()
>>> bj.add([1])
>>> bj.add([1,2])
>>> bj.add([1,2,3])
>>> bj
BJ([[1], [1, 2], [1, 2, 3]])

This does impact heterogeneity somewhat, but shouldn’t be a problem for most common uses. On the other hand, the average and worst-case times for access, membership testing, insertion, and deletion are all logarithmic, which makes blackjacks ideal for storing mappings of data with untrusted keys:

$ python -mtimeit \
> -s 'l = [(x*(2**64 - 1), hash(x*(2**64 - 1))) for x in xrange(10000)]' \
> 'dict(l)'
10 loops, best of 3: 4.11 sec per loop
$ python -mtimeit \
-s 'l = [(x*(2**64 - 1), hash(x*(2**64 - 1))) for x in xrange(10000)]
from blackjack import Deck' \
'Deck(l)'
10 loops, best of 3: 2.07 sec per loop

Even on small- to medium-sized sets of data, blackjacks quickly become more effective than dictionaries in the face of untrusted input.

This package only contains the blackjack module; tests are in the module and may be run with any standard test runner:

$ trial blackjack | tail -n1
PASSED (successes=17)

Technical information

The specific trees used are left-leaning red-black trees. Red children are opportunistically reduced during balancing if nodes will be recreated anyway; this tends to shorten overall tree height by reducing the number of red children. Complexities are as follows:

Operation

Time

Space

Lookup

O(log n)

O(1)

Membership

O(log n)

O(1)

Insertion

O(log n)

O(log n)

Deletion

O(log n)

O(log n)

Update

O(log n)

O(log n)

Sort

O(1)

O(1)

Length

O(1)

O(1)

Sorting according to the provided key function is constant because the tree’s traversal order is presorted. Length is recorded and updated on mutation. Nodes are persistent and altering the tree generally requires a logarithmic space commitment to create new nodes.

Metadata

Release files for blackjack 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blackjack 1.1.1
File Size Uploaded Provenance Actions
blackjack-1.1.1.tar.gz 6.2 kB Without provenance Details for blackjack-1.1.1.tar.gz

Release files / blackjack-1.1.1.tar.gz

Download URL blackjack-1.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9b709b61fc2888f3ab76231c78c1e1642e89f4c67a3e2629481680a8100801e0
BLAKE2b-256 checksum
How to use checksums
bf36fcfea476d0def0fb62d4d65646d4ac6898381018aa99fc847f5cd44a5bc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for blackjack 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page