@@ -515,16 +515,28 @@ function efraimidis_ares_wsample_norep!(a::AbstractArray, wv::WeightVec, x::Abst
515515
516516 # initialize priority queue
517517 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
520528 end
529+ i < k && throw (DimensionMismatch (" wv must have at least $k strictly positive entries (got $i )" ))
521530 heapify! (pq)
522531
523532 # set threshold
524533 @inbounds threshold = pq[1 ]. first
525534
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 ()
528540
529541 # if key is larger than the threshold
530542 if key > threshold
@@ -561,17 +573,28 @@ function efraimidis_aexpj_wsample_norep!(a::AbstractArray, wv::WeightVec, x::Abs
561573
562574 # initialize priority queue
563575 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
566586 end
587+ i < k && throw (DimensionMismatch (" wv must have at least $k strictly positive entries (got $i )" ))
567588 heapify! (pq)
568589
569590 # set threshold
570591 @inbounds threshold = pq[1 ]. first
571592 X = threshold* randexp ()
572593
573- @inbounds for i in k + 1 : n
594+ @inbounds for i in s + 1 : n
574595 w = wv. values[i]
596+ w < 0 && error (" Negative weight found in weight vector at index $i " )
597+ w > 0 || continue
575598 X -= w
576599 X <= 0 || continue
577600
0 commit comments