Skip to main content

Add your description here

Project description

stl-treemap

CI build status badge Codacy Code Quality Test Coverage License Badge

A Python library of ordered, tree-based associative containers. All containers are backed by a red-black tree and keep their elements in ascending key order at all times. Insertion, deletion, and lookup are all O(log n).

The following containers are provided:

  • TreeMap — an ordered map associating unique keys with values. Behaves like a Python dict with sorted keys.
  • TreeSet — an ordered set of unique keys. Behaves like a Python set with sorted elements.
  • TreeMultiMap — an ordered map that allows multiple entries with the same key.
  • TreeMultiSet — an ordered set that allows duplicate keys.

Installation

pip install stl-treemap

Or with uv:

uv add stl-treemap

Quick start

TreeMap

from stl_treemap import TreeMap

# Create and populate
m: TreeMap[int, str] = TreeMap()
m[2] = "B"
m[1] = "A"
m[3] = "C"

# Iterate in ascending key order
for key, value in m:
    print(f"key: {key}, value: {value}")
# key: 1, value: A
# key: 2, value: B
# key: 3, value: C

# Reverse iteration
for key, value in m.backwards():
    print(f"key: {key}, value: {value}")
# key: 3, value: C
# key: 2, value: B
# key: 1, value: A

# Initialize from a dict or list of pairs
m2 = TreeMap({2: "B", 1: "A", 3: "C"})
m3 = TreeMap([[2, "B"], [1, "A"], [3, "C"]])

TreeSet

from stl_treemap import TreeSet

s = TreeSet([3, 1, 2, 1])  # duplicates dropped → {1,2,3}
s.add(4)
s.discard(2)
print(list(s))  # [1, 3, 4]

# Set algebra — same operators as Python's built-in set
s1 = TreeSet([1, 2, 3])
s2 = TreeSet([2, 3, 4])
print(s1 | s2)  # {1,2,3,4}
print(s1 & s2)  # {2,3}
print(s1 - s2)  # {1}
print(s1 ^ s2)  # {1,4}

TreeMultiMap

from stl_treemap import TreeMultiMap

m: TreeMultiMap[int, str] = TreeMultiMap()
m[2] = "b1"
m[2] = "b2"   # second entry for the same key
m[1] = "a"

for key, value in m:
    print(f"key: {key}, value: {value}")
# key: 1, value: a
# key: 2, value: b1
# key: 2, value: b2

TreeMultiSet

from stl_treemap import TreeMultiSet

s = TreeMultiSet([1, 2, 2, 3])
s.add(2)           # another duplicate
print(list(s))     # [1, 2, 2, 2, 3]
s.discard(2)       # removes one occurrence
print(list(s))     # [1, 2, 2, 3]

Iteration

All containers support Python's standard iteration protocol as well as STL-style explicit iterators for range-based traversal.

Python-style

# Forward
for key in s:
    print(key)

# Reverse
for key in s.backwards():
    print(key)

STL-style iterators

Use begin() / end() for forward iteration and rbegin() / rend() for reverse. lower_bound and upper_bound let you iterate over a key range.

# Iterate a specific key range [10, 20]
it = m.lower_bound(10)
stop = m.upper_bound(20)
while not it.equals(stop):
    print(f"key: {it.key}, value: {it.value}")
    it.next()

# Erase elements while iterating (erase a different iterator than the current one)
prev = None
it = m.lower_bound(10)
stop = m.upper_bound(20)
while not it.equals(stop):
    if prev is not None and prev.key == 15:
        m.erase(prev)
    from stl_treemap.iterators import TreeIterator
    prev = TreeIterator(it)   # copy current iterator
    it.next()

Custom comparator

When keys are not natively comparable, supply a 3-way comparison function. It must return a negative int, zero, or positive int when lhs < rhs, lhs == rhs, or lhs > rhs respectively.

from stl_treemap import TreeMap

def compare_str_len(a: str, b: str) -> int:
    return (len(a) > len(b)) - (len(a) < len(b))

m: TreeMap[str, int] = TreeMap()
m.compare_func = compare_str_len
m["bb"] = 2
m["aaa"] = 3
m["c"] = 1

print(list(m.keys()))  # ['c', 'bb', 'aaa']

The same compare_func property is available on TreeSet, TreeMultiMap, and TreeMultiSet. Setting it clears all existing elements because the current ordering is no longer valid.

Why stl-treemap?

Python's built-in dict and set are hash-based and unordered. sortedcontainers provides sorted sequences, but this library's red-black tree gives true O(log n) worst-case complexity for all operations and exposes STL-style iterators for precise range manipulation — useful when you need to iterate, modify, or erase entries in a specific key range without rebuilding the container.

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

stl_treemap-1.0.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stl_treemap-1.0.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file stl_treemap-1.0.0.tar.gz.

File metadata

  • Download URL: stl_treemap-1.0.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for stl_treemap-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1c391491d417dac63cc1277ca0f686e4cc5c4cd561f68000cfed2e9a6936d611
MD5 8f475f81c5860ae91843399fc71b6d62
BLAKE2b-256 001b82da1a330943af2bff1ab055782772c286c5ecbb2552fb53c990586fc499

See more details on using hashes here.

File details

Details for the file stl_treemap-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: stl_treemap-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for stl_treemap-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83e1d404e42eeac8419a8b011253ded19b3e6cbfab86df5d5a9400e60b9b62ea
MD5 26640695f566be34f0bc6f2cc288b531
BLAKE2b-256 4cdd5cf79f41d1cd64a352fda8a6068d447b1aa60d0700cceaaa167f721e9d13

See more details on using hashes here.

Supported by

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