-
Notifications
You must be signed in to change notification settings - Fork 31
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
Mutable dynamic arrays, heaps, and simple union find data structures #585
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
626768c
Add basic implementation of dynamic arrays
marzipankaiser d7728f1
Add simple mutable 2-ary minheap implementation
marzipankaiser c24fd13
Add simple union-find datastructure
marzipankaiser 20095db
Panic instead of <>
marzipankaiser efc4c30
use examples::main and fun -> box
marzipankaiser 13746f2
Move tests into examples namespace
marzipankaiser 4facba9
ResizableArray.size_ -> rawSizePtr
marzipankaiser 72c8825
Use doc-comment syntax
marzipankaiser 8a50435
Assert in heap tests
marzipankaiser 88c4f5e
Move tests from stdlib files to examples/stdlib tests
marzipankaiser 81b29c1
Update acme.effekt
marzipankaiser 70043aa
Remove TODO
marzipankaiser 37c4911
Explain setCapacity better
marzipankaiser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
HeapTests | ||
✓ simple heap sort on integers | ||
|
||
1 pass | ||
0 fail | ||
1 tests total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import heap | ||
import test | ||
|
||
def main() = { | ||
suite("HeapTests", false) { | ||
test("simple heap sort on integers") { | ||
with on[OutOfBounds].default { assertTrue(false); <> }; | ||
val h = heap[Int](box { (x: Int, y: Int) => | ||
if (x < y) { | ||
Less() | ||
} else Greater() // hacky, should sometimes be Equal() | ||
}) | ||
h.insert(12) | ||
h.insert(10) | ||
h.insert(7) | ||
h.insert(11) | ||
h.insert(14) | ||
assert(h.deleteMin(), 7) | ||
assert(h.deleteMin(), 10) | ||
assert(h.deleteMin(), 11) | ||
assert(h.deleteMin(), 12) | ||
assert(h.deleteMin(), 14) | ||
assert(h.size, 0) | ||
} | ||
}; | ||
() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ResizableArrayTests | ||
✓ usage as stack | ||
|
||
1 pass | ||
0 fail | ||
1 tests total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import resizable_array | ||
import test | ||
|
||
def main() = { | ||
suite("ResizableArrayTests", false) { | ||
test("usage as stack") { | ||
with on[OutOfBounds].default { assertTrue(false, "out of bounds") } | ||
val a = resizableArray() | ||
a.add(1) | ||
a.add(1) | ||
a.add(2) | ||
a.add(3) | ||
a.add(13) | ||
a.add(21) | ||
a.add(34) | ||
a.add(55) | ||
assert(a.popRight(), 55) | ||
assert(a.popRight(), 34) | ||
assert(a.popRight(), 21) | ||
assert(a.popRight(), 13) | ||
a.add(5) | ||
a.add(8) | ||
assert(a.popRight(), 8) | ||
assert(a.popRight(), 5) | ||
assert(a.popRight(), 3) | ||
assert(a.popRight(), 2) | ||
assert(a.popRight(), 1) | ||
assert(a.popRight(), 1) | ||
} | ||
}; | ||
() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
UnionFindTests | ||
✓ three elements | ||
|
||
1 pass | ||
0 fail | ||
1 tests total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import union_find | ||
import test | ||
|
||
def main() = { | ||
suite("UnionFindTests", false) { | ||
test("three elements") { | ||
with on[MissingValue].default { assertTrue(false, "MissingValue exception") } | ||
val u = unionFind() | ||
val a = u.makeSet() | ||
val b = u.makeSet() | ||
val c = u.makeSet() | ||
assert(u.find(a), a) | ||
assert(u.find(b), b) | ||
assert(u.find(c), c) | ||
u.union(a,b) | ||
assert(u.find(a), u.find(b)) | ||
assert(u.find(c), c) | ||
} | ||
}; | ||
() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
module heap | ||
import resizable_array | ||
|
||
/// Resizable 2-ary min-heap, backed by a resizable array | ||
/// `cmp` defines the ordering of elements | ||
record Heap[T](rawContents: ResizableArray[T], cmp: (T, T) => Ordering at {}) | ||
|
||
/// Make a new Heap with the given comparison operation | ||
def heap[T](cmp: (T,T) => Ordering at {}) = | ||
Heap[T](resizableArray(), cmp) | ||
|
||
/// Make a new Heap with the given comparison operation and initial capacity | ||
def heap[T](cmp: (T,T) => Ordering at {}, capacity: Int) = | ||
Heap[T](resizableArray(capacity), cmp) | ||
|
||
namespace internal { | ||
def left(idx: Int) = 2 * idx + 1 | ||
def right(idx: Int) = 2 * idx + 2 | ||
def parent(idx: Int) = (idx - 1) / 2 | ||
|
||
def bubbleUp[A](heap: Heap[A], idx: Int) = { | ||
val arr = heap.rawContents | ||
arr.boundsCheck(idx) // idx > parent(idx), parent(parent(idx)) etc | ||
|
||
def go(idx: Int): Unit = { | ||
if (idx > 0 and (heap.cmp)(arr.unsafeGet(parent(idx)), arr.unsafeGet(idx)) is Greater()) { | ||
arr.unsafeSwap(parent(idx), idx) | ||
go(parent(idx)) | ||
} | ||
} | ||
go(idx) | ||
} | ||
def sinkDown[A](heap: Heap[A], idx: Int) = { | ||
val arr = heap.rawContents | ||
|
||
def infixLt(x: A, y: A) = { | ||
if ((heap.cmp)(x,y) is Less()) { true } else { false } | ||
} | ||
|
||
def go(idx: Int): Unit = { | ||
if (right(idx) < arr.size) { | ||
val v = arr.unsafeGet(idx) | ||
val l = arr.unsafeGet(left(idx)) | ||
val r = arr.unsafeGet(right(idx)) | ||
if (l < v && r < v) { | ||
// swap with the smaller one | ||
if (l < r) { | ||
arr.unsafeSwap(left(idx), idx) | ||
go(left(idx)) | ||
} else { | ||
arr.unsafeSwap(right(idx), idx) | ||
go(right(idx)) | ||
} | ||
} else if (l < v) { | ||
arr.unsafeSwap(left(idx), idx) | ||
go(left(idx)) | ||
} else if (r < v) { | ||
arr.unsafeSwap(right(idx), idx) | ||
go(right(idx)) | ||
} | ||
} else if (left(idx) < arr.size) { | ||
if (arr.unsafeGet(left(idx)) < arr.unsafeGet(idx)) { | ||
arr.unsafeSwap(left(idx), idx) | ||
go(left(idx)) | ||
} | ||
} // else: we are at the bottom | ||
} | ||
go(idx) | ||
} | ||
} | ||
|
||
/// Insert value into heap | ||
/// | ||
/// O(log n) worst case if capacity suffices, O(1) average | ||
def insert[T](heap: Heap[T], value: T): Unit = { | ||
with on[OutOfBounds].panic(); | ||
val idx = heap.rawContents.add(value) | ||
internal::bubbleUp(heap, idx) | ||
} | ||
|
||
/// find and return (but not remove) the minimal element in this heap | ||
/// | ||
/// O(1) | ||
def findMin[T](heap: Heap[T]): T / Exception[OutOfBounds] = { | ||
heap.rawContents.get(0) | ||
} | ||
|
||
/// find and remove the minimal element in this heap | ||
/// | ||
/// O(log n) | ||
def deleteMin[T](heap: Heap[T]): T / Exception[OutOfBounds] = { | ||
val res = heap.rawContents.get(0) | ||
heap.rawContents.unsafeSet(0, heap.rawContents.popRight()) | ||
internal::sinkDown(heap, 0) | ||
res | ||
} | ||
|
||
/// Number of elements in the heap | ||
/// | ||
/// O(1) | ||
def size[T](heap: Heap[T]): Int = { | ||
heap.rawContents.size | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bike shed, in the comment you write "remove" but the function is called
deleteMin
. Some languages also call this operationpop
(andinsert
thenpush
):https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose the names based on what https://en.wikipedia.org/wiki/Binary_heap calls the operations, but we can rename them - I don't have a strong opinion here.