Skip to content

Commit

Permalink
[#45] impl: BinaryHeap::IntoSortedVec
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Nov 4, 2023
1 parent 1bd35e8 commit 78ba115
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions binary_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ func (self *BinaryHeap[T]) IntoVec() Vec[T] {
return self.vec
}

// Consumes the BinaryHeap and returns a vector in sorted (ascending) order.
//
// heap := gost.BinaryHeapNew[gost.I32]()
// heap.Push(1)
// heap.Push(3)
// heap.Push(2)
// vec := gost.VecNew[gost.I32]()
// vec.Push(1)
// vec.Push(2)
// vec.Push(3)
// gost.AssertEq(heap.IntoSortedVec(), vec)
func (self *BinaryHeap[T]) IntoSortedVec() Vec[T] {
sortedVec := VecWithLen[T](self.Len())

index := self.Len() - 1

for !self.IsEmpty() {
sortedVec.SetUnchecked(index, self.Pop().Unwrap())

index -= 1
}

return sortedVec
}

/// Hole represents a hole in a slice i.e., an index without valid value
/// (because it was moved from or duplicated).
/// In drop, `Hole` will restore the slice by filling the hole
Expand Down

0 comments on commit 78ba115

Please sign in to comment.