A hybrid data structure combining a deque and a set (unique elements).
Project description
uniqdeque
A hybrid data structure combining a deque (double-ended queue) and a set (unique elements).
Features
- Double-ended operations:
push_front(elem): Insert at front (if not a duplicate) in O(1) time.push_back(elem): Append at back (if not a duplicate) in O(1) time.pop_front()/pop_back(): Remove from either end in O(1) time.
- Uniqueness: Automatically rejects duplicates on insertion.
- Order-preserving: Maintains insertion order.
- Fast membership testing:
elem in dqchecks in O(1) time.
Use Cases
- LRU/LFU Caches: Evict least-recently-used items while avoiding duplicates.
- Message Queues: Deduplicate messages while supporting queue/dequeue at both ends.
- Sliding Window Analytics: Track unique elements in a time window (e.g., "unique visitors in the last 5 minutes").
Installation
pip install uniqdeque
Usage Example
from uniqdeque import uniqdeque
dq = uniqdeque([1, 2, 3]) # Initialize with elements
dq.push_front(0) # -> [0, 1, 2, 3]
dq.push_back(3) # No effect (duplicate)
dq.pop_back() # Returns 3, dq -> [0, 1, 2]
Performance
| Operation | Time Complexity | Notes |
|---|---|---|
push_* |
O(1) | Rejects duplicates in O(1). |
pop_* |
O(1) | Raises KeyError if empty. |
discard(elem) |
O(1) | Safe removal if present. |
len(dq) |
O(1) |
API Reference
Core Methods
push_front(elem: T) -> NoneAdd to front if not a duplicate.push_back(elem: T) -> NoneAppend to back if not a duplicate.pop_front() -> TRemove and return front element. RaisesKeyErrorif empty.pop_back() -> TRemove and return back element. RaisesKeyErrorif empty.
Utilities
discard(elem: T) -> NoneRemove element if present (no-op otherwise).clear() -> NoneRemove all elements.copy() -> uniqdeque[T]Return a shallow copy.
Design Choices
- Backed by: An
OrderedDict(forO(1)lookups andO(1)head/tail ops). - Alternatives: Tree-based map (e.g., C++'s
std::map) for ordered elements (O(log n)lookups).
Contributing
Contributions are welcome! Please submit pull requests or open issues on GitHub.
License
MIT
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
uniqdeque-0.1.0a0.tar.gz
(3.9 kB
view details)
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 uniqdeque-0.1.0a0.tar.gz.
File metadata
- Download URL: uniqdeque-0.1.0a0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eb3345cb45d7b16f11cfa707005313770ec9df869d93d0552b51f563c2142cb
|
|
| MD5 |
e8b47f3ded9a741f3b56741705d6263c
|
|
| BLAKE2b-256 |
cf8609bfb5ff43fe033116a5af6f0c936ac4b4f51a12b6e9271c089655a294e9
|
File details
Details for the file uniqdeque-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: uniqdeque-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 4.1 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 |
b35707fa640a8ee0ecebfa4189c508dcbfbecbf10b337e250959adc306b4d35c
|
|
| MD5 |
abeb61c693ed10a32cbbbf38e8b2a830
|
|
| BLAKE2b-256 |
50f242f89daf1adc482d147a1fc6aa68098b6462011bbcdb8c23776d509d3421
|