Skip to main content

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 dq checks in O(1) time.

Use Cases

  1. LRU/LFU Caches: Evict least-recently-used items while avoiding duplicates.
  2. Message Queues: Deduplicate messages while supporting queue/dequeue at both ends.
  3. 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) -> None Add to front if not a duplicate.
  • push_back(elem: T) -> None Append to back if not a duplicate.
  • pop_front() -> T Remove and return front element. Raises KeyError if empty.
  • pop_back() -> T Remove and return back element. Raises KeyError if empty.

Utilities

  • discard(elem: T) -> None Remove element if present (no-op otherwise).
  • clear() -> None Remove all elements.
  • copy() -> uniqdeque[T] Return a shallow copy.

Design Choices

  • Backed by: An OrderedDict (for O(1) lookups and O(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


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)

Uploaded Source

Built Distribution

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

uniqdeque-0.1.0a0-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2Python 3

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

Hashes for uniqdeque-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 1eb3345cb45d7b16f11cfa707005313770ec9df869d93d0552b51f563c2142cb
MD5 e8b47f3ded9a741f3b56741705d6263c
BLAKE2b-256 cf8609bfb5ff43fe033116a5af6f0c936ac4b4f51a12b6e9271c089655a294e9

See more details on using hashes here.

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

Hashes for uniqdeque-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b35707fa640a8ee0ecebfa4189c508dcbfbecbf10b337e250959adc306b4d35c
MD5 abeb61c693ed10a32cbbbf38e8b2a830
BLAKE2b-256 50f242f89daf1adc482d147a1fc6aa68098b6462011bbcdb8c23776d509d3421

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