JuliaCollections / JuliaCollections/DataStructures.jl

Feature request: setfirst!(::AbstractHeap)

Open
#665 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Julia
Stars
745
Forks
261
PR merge metrics
No merged PRs in 30d

Description

Heaps are particularly useful for keeping track of the smallest (or largest) value in a collection. When being used to keep track of the N smallest values, the root is continually popped, and new elements are put in.

However, replacing the root is currently inefficient, as it entails using both pop! (causing a percolate_down!) and a push! (causing a percolate_up!). Instead, it could be implemented more efficiently like:

function setfirst!(h::AbstractArray, x, o::Ordering=Forward)
    isempty(h) && return push!(h, x)
    @inbounds h[1] = x
    percolate_down!(h, 1, x, o)
    return h
end

It would be nice if functionality like this was exported.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the AbstractHeap implementation and the existing pop!, push!, and percolate_down! entry points. Add the requested exported setfirst! functionality for replacing a heap root, including the empty-heap behavior described in the issue, then run the relevant heap tests or add coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.