A Python implementation of a prefix-free, sorted set of COWList, backed by a trie data structure for efficient prefix checking and maintenance of sorted order.
Project description
prefix-free-sorted-cowlist-set
A Python implementation of a prefix-free, sorted set of COWList, backed by a trie data structure for efficient prefix checking and maintenance of sorted order.
Features
- Prefix-Free Guarantee: Ensures no sequence in the set is a prefix of any other sequence
- Sorted Order: Maintains all COWList in lexicographical order
- Efficient Operations:
- O(k) time complexity for add/discard operations (where k is sequence length)
- Fast prefix checking via trie traversal
- Memory Efficient: Uses Copy-On-Write semantics for list storage
- Pythonic Interface: Implements both
MutableSetandSequenceprotocols
Installation
pip install prefix-free-sorted-cowlist-set
Usage
# coding=utf-8
from prefix_free_sorted_cowlist_set import PrefixFreeSortedCOWListSet
from cowlist import COWList
# Create a new set
pf_set = PrefixFreeSortedCOWListSet()
# Add sequences (returns True if successful)
assert pf_set.add(COWList([1, 2, 3])) # True
assert pf_set.add(COWList([1, 2, 4])) # True
assert pf_set.add(COWList([1, 3])) # True
# Try to add a prefix (will fail)
assert not pf_set.add(COWList([1, 2])) # False - violates prefix-free property
# Check membership
assert COWList([1, 2, 3]) in pf_set # True
# Iterate in sorted order
assert list(pf_set) == [COWList([1, 2, 3]), COWList([1, 2, 4]), COWList([1, 3])]
# Reverse iteration
assert list(reversed(pf_set)) == [COWList([1, 3]), COWList([1, 2, 4]), COWList([1, 2, 3])]
# Remove sequences
assert pf_set.discard(COWList([1, 2, 3])
assert list(pf_set) == [COWList([1, 2, 4]), COWList([1, 3])]
# Length of set
assert len(pf_set) == 2
# Index access (sorted order)
assert pf_set[-1] == COWList([1, 3])
Use Cases
File System Paths
from prefix_free_sorted_cowlist_set import PrefixFreeSortedCOWListSet
from cowlist import COWList
# Store unique file paths where no path is a prefix of another
paths = PrefixFreeSortedCOWListSet()
paths.add(COWList(["usr", "local", "bin"]))
paths.add(COWList(["usr", "local", "lib"]))
# paths.add(COWList(["usr", "local"])) # This would fail - prefix violation
Domain Names
from prefix_free_sorted_cowlist_set import PrefixFreeSortedCOWListSet
from cowlist import COWList
# Store domain names ensuring no subdomain conflicts
domains = PrefixFreeSortedCOWListSet()
domains.add(COWList(["example", "com"]))
domains.add(COWList(["sub", "example", "com"]))
# domains.add(COWList(["example"])) # This would fail
Routing Tables
from prefix_free_sorted_cowlist_set import PrefixFreeSortedCOWListSet
from cowlist import COWList
# Network routing paths where no route should be a prefix of another
routes = PrefixFreeSortedCOWListSet()
routes.add(COWList([192, 168, 1, 0]))
routes.add(COWList([192, 168, 2, 0]))
API Reference
PrefixFreeSortedCOWListSet
Methods
add(cowlist: COWList[T]) -> bool: Add a COWList to the set if it doesn't violate the prefix-free propertydiscard(cowlist: COWList[T]) -> bool: Remove a COWList from the set if present__contains__(item: COWList[T]) -> bool: Check if a COWList is in the set__getitem__(index: int) -> COWList[T]: Get COWList at specified index (sorted order)__iter__() -> Iterator[COWList[T]]: Iterate over COWList in sorted order__len__() -> int: Get number of COWList objects in the set__reversed__() -> Iterator[COWList[T]]: Iterate in reverse sorted order
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
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 prefix_free_sorted_cowlist_set-0.1.0a0.tar.gz.
File metadata
- Download URL: prefix_free_sorted_cowlist_set-0.1.0a0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf8da75433493bef1934fac92dbf956c09d595a503f8a07a0cec4aa573a71418
|
|
| MD5 |
c4ff362cf8d5a705f8ad8503fd126798
|
|
| BLAKE2b-256 |
dc466d63a90ac4e745c1cca12e2cb3800d1b027746f38bf3f632388455cfa190
|
File details
Details for the file prefix_free_sorted_cowlist_set-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: prefix_free_sorted_cowlist_set-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab27462e21815dc9239aa7c410980afdbfb55237fca378c0af59ed9265efa79d
|
|
| MD5 |
169607910474df8fbd7186019c47617c
|
|
| BLAKE2b-256 |
e159472d40ff0d872e6845a5e887ae8e23c5c2baeb2f9e287e5f549a51d935da
|