github.com/MetalBlockchain/metalgo@v1.11.9/utils/sampler/weighted_without_replacement.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package sampler
     5  
     6  // WeightedWithoutReplacement defines how to sample weight without replacement.
     7  // Note that the behavior is to sample the weight without replacement, not the
     8  // indices. So duplicate indices can be returned.
     9  type WeightedWithoutReplacement interface {
    10  	Initialize(weights []uint64) error
    11  	Sample(count int) ([]int, bool)
    12  }
    13  
    14  // NewDeterministicWeightedWithoutReplacement returns a new sampler
    15  func NewDeterministicWeightedWithoutReplacement(source Source) WeightedWithoutReplacement {
    16  	return &weightedWithoutReplacementGeneric{
    17  		u: NewDeterministicUniform(source),
    18  		w: NewDeterministicWeighted(),
    19  	}
    20  }
    21  
    22  // NewWeightedWithoutReplacement returns a new sampler
    23  func NewWeightedWithoutReplacement() WeightedWithoutReplacement {
    24  	return &weightedWithoutReplacementGeneric{
    25  		u: NewUniform(),
    26  		w: NewWeighted(),
    27  	}
    28  }
    29  
    30  // NewBestWeightedWithoutReplacement returns a new sampler
    31  func NewBestWeightedWithoutReplacement(
    32  	expectedSampleSize int,
    33  ) WeightedWithoutReplacement {
    34  	return &weightedWithoutReplacementGeneric{
    35  		u: NewBestUniform(expectedSampleSize),
    36  		w: NewWeighted(),
    37  	}
    38  }