summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2020-11-26 10:24:52 +0100
committerGitHub <noreply@github.com>2020-11-26 10:24:52 +0100
commitda753c6a2eded5a382faf22dbf2a2ec1b1fc328f (patch)
tree6bfa2a4fb8f37cd320cbec8a7aae0c311bc84986 /lib/pure
parent3e7077ac7d2f4867ecabff09b730b6bc9356979d (diff)
downloadNim-da753c6a2eded5a382faf22dbf2a2ec1b1fc328f.tar.gz
fixes #15076 (#16143)
* fixes #15076

* heapqueue: optimized for ARC

* added another test case [backport:1.4]

* code cleanup
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/collections/heapqueue.nim10
1 files changed, 5 insertions, 5 deletions
diff --git a/lib/pure/collections/heapqueue.nim b/lib/pure/collections/heapqueue.nim
index 00e7a3b9d..30fa5dae8 100644
--- a/lib/pure/collections/heapqueue.nim
+++ b/lib/pure/collections/heapqueue.nim
@@ -65,7 +65,7 @@ proc len*[T](heap: HeapQueue[T]): int {.inline.} =
   ## Returns the number of elements of `heap`.
   heap.data.len
 
-proc `[]`*[T](heap: HeapQueue[T], i: Natural): T {.inline.} =
+proc `[]`*[T](heap: HeapQueue[T], i: Natural): lent T {.inline.} =
   ## Accesses the i-th element of `heap`.
   heap.data[i]
 
@@ -111,7 +111,7 @@ proc siftup[T](heap: var HeapQueue[T], p: int) =
   heap.data[pos] = newitem
   siftdown(heap, startpos, pos)
 
-proc push*[T](heap: var HeapQueue[T], item: T) =
+proc push*[T](heap: var HeapQueue[T], item: sink T) =
   ## Pushes `item` onto heap, maintaining the heap invariant.
   heap.data.add(item)
   siftdown(heap, 0, len(heap)-1)
@@ -168,7 +168,7 @@ proc del*[T](heap: var HeapQueue[T], index: Natural) =
   if index < newLen:
     heap.siftup(index)
 
-proc replace*[T](heap: var HeapQueue[T], item: T): T =
+proc replace*[T](heap: var HeapQueue[T], item: sink T): T =
   ## Pops and returns the current smallest value, and add the new item.
   ## This is more efficient than pop() followed by push(), and can be
   ## more appropriate when using a fixed-size heap. Note that the value
@@ -186,7 +186,7 @@ proc replace*[T](heap: var HeapQueue[T], item: T): T =
   heap.data[0] = item
   siftup(heap, 0)
 
-proc pushpop*[T](heap: var HeapQueue[T], item: T): T =
+proc pushpop*[T](heap: var HeapQueue[T], item: sink T): T =
   ## Fast version of a push followed by a pop.
   runnableExamples:
     var heap = initHeapQueue[int]()
@@ -197,7 +197,7 @@ proc pushpop*[T](heap: var HeapQueue[T], item: T): T =
     assert heap[0] == 6
     assert heap.pushpop(4) == 4
   result = item
-  if heap.len > 0 and heapCmp(heap.data[0], item):
+  if heap.len > 0 and heapCmp(heap.data[0], result):
     swap(result, heap.data[0])
     siftup(heap, 0)