@@ -515,16 +515,28 @@ function efraimidis_ares_wsample_norep!(a::AbstractArray, wv::WeightVec, x::Abst
515
515
516
516
# initialize priority queue
517
517
pq = Vector {Pair{Float64,Int}} (k)
518
- @inbounds for i in 1 : k
519
- pq[i] = (wv. values[i]/ randexp () => i)
518
+ i = 0
519
+ s = 0
520
+ @inbounds for s in 1 : n
521
+ w = wv. values[s]
522
+ w < 0 && error (" Negative weight found in weight vector at index $s " )
523
+ if w > 0
524
+ i += 1
525
+ pq[i] = (w/ randexp () => s)
526
+ end
527
+ i >= k && break
520
528
end
529
+ i < k && throw (DimensionMismatch (" wv must have at least $k strictly positive entries (got $i )" ))
521
530
heapify! (pq)
522
531
523
532
# set threshold
524
533
@inbounds threshold = pq[1 ]. first
525
534
526
- @inbounds for i in k+ 1 : n
527
- key = wv. values[i]/ randexp ()
535
+ @inbounds for i in s+ 1 : n
536
+ w = wv. values[i]
537
+ w < 0 && error (" Negative weight found in weight vector at index $i " )
538
+ w > 0 || continue
539
+ key = w/ randexp ()
528
540
529
541
# if key is larger than the threshold
530
542
if key > threshold
@@ -561,17 +573,28 @@ function efraimidis_aexpj_wsample_norep!(a::AbstractArray, wv::WeightVec, x::Abs
561
573
562
574
# initialize priority queue
563
575
pq = Vector {Pair{Float64,Int}} (k)
564
- @inbounds for i in 1 : k
565
- pq[i] = (wv. values[i]/ randexp () => i)
576
+ i = 0
577
+ s = 0
578
+ @inbounds for s in 1 : n
579
+ w = wv. values[s]
580
+ w < 0 && error (" Negative weight found in weight vector at index $s " )
581
+ if w > 0
582
+ i += 1
583
+ pq[i] = (w/ randexp () => s)
584
+ end
585
+ i >= k && break
566
586
end
587
+ i < k && throw (DimensionMismatch (" wv must have at least $k strictly positive entries (got $i )" ))
567
588
heapify! (pq)
568
589
569
590
# set threshold
570
591
@inbounds threshold = pq[1 ]. first
571
592
X = threshold* randexp ()
572
593
573
- @inbounds for i in k + 1 : n
594
+ @inbounds for i in s + 1 : n
574
595
w = wv. values[i]
596
+ w < 0 && error (" Negative weight found in weight vector at index $i " )
597
+ w > 0 || continue
575
598
X -= w
576
599
X <= 0 || continue
577
600
0 commit comments