Skip to content

Commit 6aa971d

Browse files
authored
Add proc find to heapqueue (#14628)
* Unwind just the "pseudorandom probing" (whole hash-code-keyed variable stride double hashing) part of recent sets & tables changes (which has still been causing bugs over a month later (e.g., two days ago #13794) as well as still having several "figure this out" implementation question comments in them (see just diffs of this PR). This topic has been discussed in many places: #13393 #13418 #13440 #13794 Alternative/non-mandatory stronger integer hashes (or vice-versa opt-in identity hashes) are a better solution that is more general (no illusion of one hard-coded sequence solving all problems) while retaining the virtues of linear probing such as cache obliviousness and age-less tables under delete-heavy workloads (still untested after a month of this change). The only real solution for truly adversarial keys is a hash keyed off of data unobservable to attackers. That all fits better with a few families of user-pluggable/define-switchable hashes which can be provided in a separate PR more about `hashes.nim`. This PR carefully preserves the better (but still hard coded!) probing of the `intsets` and other recent fixes like `move` annotations, hash order invariant tests, `intsets.missingOrExcl` fixing, and the move of `rightSize` into `hashcommon.nim`. * Fix `data.len` -> `dataLen` problem. * Add neglected API call `find` to heapqueue. * Add a changelog.md entry, `since` annotation and rename parameter to be `heap` like all the other procs for consistency. * Add missing import.
1 parent 8bbdb8f commit 6aa971d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110

111111
- new module `std/jsonutils` with hookable `jsonTo,toJson,fromJson` for json serialization/deserialization of custom types.
112112

113+
- new proc `heapqueue.find[T](heap: HeapQueue[T], x: T): int` to get index of element ``x``.
114+
113115
## Language changes
114116
- In the newruntime it is now allowed to assign discriminator field without restrictions as long as case object doesn't have custom destructor. Discriminator value doesn't have to be a constant either. If you have custom destructor for case object and you do want to freely assign discriminator fields, it is recommended to refactor object into 2 objects like this:
115117

lib/pure/collections/heapqueue.nim

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
5151
assert jobs[0].priority == 1
5252
]##
53+
import std/private/since
5354

5455
type HeapQueue*[T] = object
5556
## A heap queue, commonly known as a priority queue.
@@ -125,6 +126,12 @@ proc pop*[T](heap: var HeapQueue[T]): T =
125126
else:
126127
result = lastelt
127128

129+
proc find*[T](heap: HeapQueue[T], x: T): int {.since: (1, 3).} =
130+
## Linear scan to find index of item ``x`` or -1 if not found.
131+
result = -1
132+
for i in 0 ..< heap.len:
133+
if heap[i] == x: return i
134+
128135
proc del*[T](heap: var HeapQueue[T], index: Natural) =
129136
## Removes the element at `index` from `heap`, maintaining the heap invariant.
130137
swap(heap.data[^1], heap.data[index])
@@ -207,18 +214,20 @@ when isMainModule:
207214
heap.del(0)
208215
doAssert(heap[0] == 1)
209216

210-
heap.del(heap.data.find(7))
217+
heap.del(heap.find(7))
211218
doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 5, 6, 8, 9])
212219

213-
heap.del(heap.data.find(5))
220+
heap.del(heap.find(5))
214221
doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 6, 8, 9])
215222

216-
heap.del(heap.data.find(6))
223+
heap.del(heap.find(6))
217224
doAssert(heap.toSortedSeq == @[1, 2, 3, 4, 8, 9])
218225

219-
heap.del(heap.data.find(2))
226+
heap.del(heap.find(2))
220227
doAssert(heap.toSortedSeq == @[1, 3, 4, 8, 9])
221228

229+
doAssert(heap.find(2) == -1)
230+
222231
block: # Test del last
223232
var heap = initHeapQueue[int]()
224233
let data = [1, 2, 3]

0 commit comments

Comments
 (0)