Add your description here
Project description
stl-treemap
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
dictwith sorted keys. - TreeSet — an ordered set of unique keys. Behaves like a Python
setwith 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stl_treemap-0.1.0.tar.gz.
File metadata
- Download URL: stl_treemap-0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e0c93c2dea92a2efa0b4449b91c497114367722dddfe447d83f0126d9e5f9d6
|
|
| MD5 |
c237c9f0d7475aa2624cb4f620322558
|
|
| BLAKE2b-256 |
4b98fefde45802dfc51d3c0905b34c7aae81e4327e7272d753497d85beff1027
|
File details
Details for the file stl_treemap-0.1.0-py3-none-any.whl.
File metadata
- Download URL: stl_treemap-0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70c6c360d923288aced6d8c509694b36d72abfbb5273ccd415d61c946811d319
|
|
| MD5 |
62956997df56ac93042278fe5b0fd84d
|
|
| BLAKE2b-256 |
222a93d59df5e9d2c7cfc35d527292005712f500bb31b2121c9a2496e355d71f
|