Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore BinaryHeap #282

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions contracts/data/BinaryHeap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

pragma solidity ^0.8.20;

import { ArrayUtils } from '../utils/ArrayUtils.sol';

/**
* @title Binary Heap implementation
* @dev The data structure is configured as a max-heap
*/
library BinaryHeap {
using ArrayUtils for bytes32[];

struct Heap {
bytes32[] _values;
// 1-indexed to allow 0 to signify nonexistence
Expand Down Expand Up @@ -153,34 +157,20 @@ library BinaryHeap {

function toArray(
Bytes32Heap storage heap
) internal view returns (bytes32[] memory) {
return heap._inner._values;
) internal view returns (bytes32[] memory array) {
array = _toArray(heap._inner);
}

function toArray(
AddressHeap storage heap
) internal view returns (address[] memory) {
bytes32[] storage values = heap._inner._values;
address[] storage array;

assembly {
array.slot := values.slot
}

return array;
) internal view returns (address[] memory array) {
array = _toArray(heap._inner).toAddressArray();
}

function toArray(
UintHeap storage heap
) internal view returns (uint256[] memory) {
bytes32[] storage values = heap._inner._values;
uint256[] storage array;

assembly {
array.slot := values.slot
}

return array;
) internal view returns (uint256[] memory array) {
array = _toArray(heap._inner).toUint256Array();
}

function _at(
Expand Down Expand Up @@ -248,6 +238,12 @@ library BinaryHeap {
}
}

function _toArray(
Heap storage heap
) private view returns (bytes32[] storage array) {
array = heap._values;
}

function _heapify(Heap storage heap) private {
uint256 len = _length(heap);
unchecked {
Expand Down