Skip to content

gh-112498: Allow removal/update of an object at an arbitary position in a a heap#112497

Open
kristjanvalur wants to merge 7 commits intopython:mainfrom
kristjanvalur:kristjan/heapremove
Open

gh-112498: Allow removal/update of an object at an arbitary position in a a heap#112497
kristjanvalur wants to merge 7 commits intopython:mainfrom
kristjanvalur:kristjan/heapremove

Conversation

@kristjanvalur
Copy link
Copy Markdown
Contributor

@kristjanvalur kristjanvalur commented Nov 28, 2023

Add a function, heapremove() to remove an object at a known place in the heap, while maintaining the heap invariant.
A replacement object can be provided to place on the heap in stead of the removed object.
This function can also be used to restore the heap invariant after the value of an object on the heap has
changed.

I have selected the name heapremove() for this functionality but am open to suggestions. heapmodify()? It could also be split into two functions (e.g. heapdel and heapupdate), one for removing an item at an arbitrary place, and another for merely update the heap if there is a modification at a given index.

Some of the existing functions could also be augmented with optional arguments, although providing a default value for 'index' is not easy, particularly if we allow it to be negative, like normal list indices.


📚 Documentation preview 📚: https://cpython-previews--112497.org.readthedocs.build/

@kristjanvalur kristjanvalur changed the title Kristjan/heapremove gh-112498: Allow removal/update of an object at an arbitary position in a a heap Nov 28, 2023
@kristjanvalur kristjanvalur marked this pull request as ready for review November 28, 2023 17:51
@rhettinger rhettinger self-assigned this Nov 29, 2023
@kristjanvalur
Copy link
Copy Markdown
Contributor Author

Maybe worth mentioning as "future music", that it would be easy to add an optional, keyword-only, argument "map" to all the methods, to maintain a dict[Any, int], so that it would be easy to look up the index of an object, rather than have to search for it. But again, future music.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants