Skip to main content

A thread safe implementation of the MinHeap and MaxHeap

Project description

Hipster [Heap*]

Description

Hipster provides a thread-safe implementation of the MinHeap and MaxHeap. Each collection type exposes a simple API to push, pop, peek, remove and clear. The data structure is built on top of the heapq algorithm.

Object Usage

For adding an object to the heap, the object needs to implement it's comparator logic.

class GitRepo:
    def __init__(self, url, stars, forks):
        self.url = url
        self.stars = stars
        self.forks = forks

    # The object needs to implement the following methods
    def __eq__(self, other):
        return self.name == other.name and self.stars == other.stars and self.forks == other.forks

    def __ne__(self, other):
        return self.name != other.name or self.stars != other.stars or self.forks != other.forks

    def __lt__(self, other):
        # The two repos are first compared based on stars then forks and then sorted lexically based on url
        if self.stars == other.stars and self.forks == other.forks return self.url < other.url

    def __le__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url <= other.url

    def __gt__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url > other.url

    def __ge__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url >= other.url


Dependencies

Python 3

Installation

pip install --upgrade hipster

Usage

from hipster.max_heap import MaxHeap
from hipster.min_heap import MinHeap

max_heap = MaxHeap()           # Creates an empty MaxHeap
max_heap.push(item)            # Pushes a new item on the MaxHeap
item = max_heap.peek()         # Returns the largest item from the heap without removing it, throws HeapError when called on an empty Heap
item = max_heap.pop()          # Pops an item off the MaxHeap, throws HeapError when called on an empty Heap
max_heap.remove(item)          # Removes the item from the heap if its present, or throws HeapError if the item is not present
max_heap.clear()               # Removes all items from the heap



min_heap = MinHeap()           # Creates an empty MinHeap
min_heap.push(item)            # Pushes a new item on the MinHeap
item = min_heap.peek()         # Returns the samllest item from the heap without removing it, throws HeapError when called on an empty Heap
item = min_heap.pop()          # Pops an item off the MinHeap, throws HeapError when called on an empty Heap
min_heap.remove(item)          # Removes the item from the heap if its present, or throws HeapError if the item is not present
min_heap.clear()               # Removes all items from the heap

License

MIT

Changelog

3.0.5

Removed copy

3.0.4

Fixed a bug with the copy method

3.0.3

Added copy method

3.0.1

Fixed a bug with thread safety on remove

3.0.0

Added remove, refactored duplicated code to base class, added more tests, updated README

2.0.2

fixed README

2.0.1

Updated object usage example, updated README

2.0.0

Added thread safety, added more tests

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

hipster-3.0.5.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

hipster-3.0.5-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file hipster-3.0.5.tar.gz.

File metadata

  • Download URL: hipster-3.0.5.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5rc1

File hashes

Hashes for hipster-3.0.5.tar.gz
Algorithm Hash digest
SHA256 76de33e61636db63be7ea83779f0fd74cc54432fa53d46659903ea5ebb58ef8f
MD5 07f506d2d00b859acab1506097f5887e
BLAKE2b-256 928601f43a03676b18046d5b41c38dcd2d9f1d476343f07125684c007a0cd5b6

See more details on using hashes here.

File details

Details for the file hipster-3.0.5-py3-none-any.whl.

File metadata

  • Download URL: hipster-3.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5rc1

File hashes

Hashes for hipster-3.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 658108c001068984b825a515a49a264dce0abbc5ab31d1bc2783549e7cec04f4
MD5 0db2ce22b2fd3876ddb7666e614bead8
BLAKE2b-256 e2a0febd144bf30a89df31f26ef0c6b48a5e99a03719b06ecb4ad4e04e30cc92

See more details on using hashes here.

Supported by

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