Skip to content

Add Heap (and DynamicHeap) classes to heapq module #62034

Closed

Description

@allyourcode
BPO 17834
Nosy @rhettinger, @pitrou, @bitdancer, @serhiy-storchaka
Files
  • heap.patch: hg diff -r e0c0bcd > heap.patch
  • heap2.diff: Draft of Heap() class
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2013-04-29.10:47:14.534>
    created_at = <Date 2013-04-24.18:52:33.336>
    labels = ['type-feature', 'library']
    title = 'Add Heap (and DynamicHeap) classes to heapq module'
    updated_at = <Date 2013-04-29.11:05:54.042>
    user = 'https://bugs.python.org/allyourcode'

    bugs.python.org fields:

    activity = <Date 2013-04-29.11:05:54.042>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2013-04-29.10:47:14.534>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2013-04-24.18:52:33.336>
    creator = 'allyourcode'
    dependencies = []
    files = ['30004', '30061']
    hgrepos = ['185']
    issue_num = 17834
    keywords = ['patch']
    message_count = 10.0
    messages = ['187723', '187749', '187750', '187753', '187756', '187863', '187865', '187894', '188060', '188065']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'pitrou', 'r.david.murray', 'serhiy.storchaka', 'allyourcode']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17834'
    versions = ['Python 3.4']

    Activity

    1. allyourcode commented on Apr 24, 2013

      allyourcodemannequin
      MannequinAuthor

      heapq already provides a bunch of functions for manipulating lists that preserve (or create) a heap invariant. My change adds two classes for representing heaps. These classes ensure that operations that violate the heap invariant are not possible (through the public interface). The also allow customization via subclassing.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Apr 24, 2013
    3. serhiy-storchaka commented on Apr 24, 2013

      @serhiy-storchaka
      Member

      queue.PriorityQueue?

    4. allyourcode commented on Apr 24, 2013

      allyourcodemannequin
      MannequinAuthor

      Hey Serhiy,

      Are you suggesting that I put my new classes in a new module and rename them? I think it'd be better to keep them in heapq, unless someone intended to add more classes to your proposed queue module, because heap functionality would be spread between two modules (heapq and queue).

      One could argue that these classes could go in the collections module, but I think sticking to heapq is best.

    5. bitdancer commented on Apr 25, 2013

      @bitdancer
      Member

      Serhiy is pointing you to an existing use of heapq in the stdlib. I'm not quite sure what his intent is in pointing it out, though it *might* be to point out that heapq is a primitive, and PriorityQueue already implements a subclassable class using it.

      I don't personally have any opinion either way on the utility of your proposed classes. I've only ever used PriorityQueue once :)

    6. allyourcode commented on Apr 25, 2013

      allyourcodemannequin
      MannequinAuthor

      Ah, Serhiy is pointing out that there's already a class named PriorityQueue in the queue module. I didn't know that exists.

      Now that I've had a chance to look at it, queue.PriorityQueue is like my Heap class, but there are some interesting difference; moreover, I'm also proposing DynamicHeap, which is pretty different from PriorityQueue. Perhaps, I should find a way to augment PriorityQueue so that it supports the interesting features of DynamicHeap?

    7. pitrou commented on Apr 26, 2013

      @pitrou
      Member

      queue.PriorityQueue is a locked class for use in multi-threaded code. That's like saying Queue is a good substitute for list :-)

      OTOH, the proposed patch raises a lot of warning signs for me. I will wait for a decision on the feature's desirability before making a review.

    8. serhiy-storchaka commented on Apr 26, 2013

      @serhiy-storchaka
      Member

      From my multilingual experience I can say that priority queue is very rarely used type of collections (but if it needed it is very usefull). On one hand, Python stdlib already contains one such type, and this type will be good enough in non-performance-critical applications. On other hand, a non-locked implementation using heapq is trivial (just encapsulate a list inside a class), no one will implement it wrong (if do not over-complicate the interface). I don't see the need for yet one priority queue. The presence of two very similar types in the stdlib will lead to confusion.

    9. serhiy-storchaka commented on Apr 27, 2013

      @serhiy-storchaka
      Member

      See also rejected bpo-1162363.

    10. self-assigned this
      on Apr 29, 2013
    11. rhettinger commented on Apr 29, 2013

      @rhettinger
      Contributor

      Daniel, I've already been in the process of adding a class to the heapq module and have done substantial work on it over the last few months. I'll look at your code to see if there are any ideas that should be merged with it before I finish it up.

      Am attaching my current draft for Heap(). A PriorityQueue() variant would also be added to 3.4.

    12. rhettinger commented on Apr 29, 2013

      @rhettinger
      Contributor

      Daniel, I'm going to close this one so we can continue work in just a single tracker item: http://bugs.python.org/issue13742

      I'll see if anything in your code should be merged in to mine and will list you as a co-contributor when it all goes in.

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions