-
Notifications
You must be signed in to change notification settings - Fork 0
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
in-order iteration of HeapQueue #373
Comments
Should we allow user-defined cmp function? type
CmpCallback[T] = proc (x, y: T): bool
HeapQueue*[T] = object
## A heap queue, commonly known as a priority queue.
data: seq[T]
cmp: CmpCallback[T] Yeah, compared to Dlang, we don't have Or we could use |
maxheap would be redundant, can be emulated by
absolutely; the lack of it is another limitation I ran into when using heapqueue. eg: import std/heapqueue
import ./t11358b
proc `<`(a,b: Foo): bool = a.x > b.x
proc `<`(a,b: tuple[x,y: int]): bool = a.x > b.x
proc fn3()=
var list: HeapQueue[Foo]
list.push Foo(x: 2)
list.push Foo(x: 3)
echo "fn3", list[0]
fn3()
proc fn4()=
var list: HeapQueue[tuple[x, y: int]]
list.push (x: 2, y: 30)
list.push (x: 3, y: 20)
echo "fn4:", list[0]
fn4()
# t11358b.nim
import std/heapqueue
type Foo* = object
x*: int
proc `<`(a,b: Foo): bool = a.x < b.x
proc `<`(a,b: tuple[x,y: int]): bool = a.x < b.x
proc fn1[]()=
var list: HeapQueue[Foo]
list.push Foo(x: 2)
list.push Foo(x: 3)
echo "fn1:", list[0]
proc fn2[]()=
var list: HeapQueue[tuple[x, y: int]]
list.push (x: 2, y: 30)
list.push (x: 3, y: 20)
echo "fn2:", list[0]
when defined case1:
fn1()
fn2()
As you see above, it does; unrelated code in some dependency (eg 2 modules defining some tuple) can affect each other's semantics in order-dependent ways. And that's just one of the bugs nim-lang#8677 has more (not all relate to this). With alias PR nim-lang#11992, you can avoid this, while retaining 0 cost as follows: var list: HeapQueue[Foo, alias myCmp]
# or var list = initHeapQueue(alias myCmp)
# or with a lambda, as shown in PR: var list: HeapQueue[Foo, (a,b) ~> a.x < b.x]
# or with a unary lambda, ditto: var list: HeapQueue[Foo, a ~> a.x] # uses cmp(lambda(a), lambda(b)) as shown with
how? |
made a simple draft PR: Edited: |
but did it break code, or tests in testament?
this would guarantee to lead to code duplication. |
I will make a better |
proposal
allow efficient in-order iterator for a HeapQueue
I actually fell into a trap in nim-lang#16067 (maybe docs should make this clearer) by using the seq directly, which is not in order
proposal:
see https://stackoverflow.com/a/22187277/1426932
links
The text was updated successfully, but these errors were encountered: