Add assertions that `GetRandInt*` functions are called with `nMax >= 0`.
All existing uses have been checked to ensure they are consistent with this assertion. Signed-off-by: Daira Emma Hopwood <daira@jacaranda.org>
This commit is contained in:
parent
75030610f2
commit
9af901e1a9
|
@ -47,6 +47,7 @@ uint128_t GetRandUInt128(uint128_t nMax)
|
||||||
|
|
||||||
int128_t GetRandInt128(int128_t nMax)
|
int128_t GetRandInt128(int128_t nMax)
|
||||||
{
|
{
|
||||||
|
assert(nMax >= 0);
|
||||||
return GetRandUInt128(nMax);
|
return GetRandUInt128(nMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,11 +58,13 @@ uint64_t GetRand(uint64_t nMax)
|
||||||
|
|
||||||
int64_t GetRandInt64(int64_t nMax)
|
int64_t GetRandInt64(int64_t nMax)
|
||||||
{
|
{
|
||||||
|
assert(nMax >= 0);
|
||||||
return GetRand(nMax);
|
return GetRand(nMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetRandInt(int nMax)
|
int GetRandInt(int nMax)
|
||||||
{
|
{
|
||||||
|
assert(nMax >= 0);
|
||||||
return GetRand(nMax);
|
return GetRand(nMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,9 @@ void GetRandBytes(unsigned char* buf, size_t num);
|
||||||
template <typename I>
|
template <typename I>
|
||||||
I GetRandGeneric(I nMax)
|
I GetRandGeneric(I nMax)
|
||||||
{
|
{
|
||||||
|
// I must be an unsigned numeric type.
|
||||||
static_assert(std::numeric_limits<I>::min() == 0);
|
static_assert(std::numeric_limits<I>::min() == 0);
|
||||||
|
// I() for primitive signed integer types returns 0.
|
||||||
if (nMax == I())
|
if (nMax == I())
|
||||||
return I();
|
return I();
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ public:
|
||||||
|
|
||||||
int RandomInt(int nMax)
|
int RandomInt(int nMax)
|
||||||
{
|
{
|
||||||
|
assert(nMax >= 0);
|
||||||
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
|
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
|
||||||
return (unsigned int)(state % nMax);
|
return (unsigned int)(state % nMax);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,13 +22,17 @@
|
||||||
// weights of the children. This allows for addition, removal, and random
|
// weights of the children. This allows for addition, removal, and random
|
||||||
// selection/dropping in logarithmic time.
|
// selection/dropping in logarithmic time.
|
||||||
//
|
//
|
||||||
// random(w) must be defined to return a uniform random value between zero
|
// random(w) (which will only be called with positive w) must be defined to
|
||||||
// inclusive and w exclusive. The type W must support addition, binary and
|
// return a uniform random value between zero inclusive and w exclusive.
|
||||||
// unary -, and < comparisons, and W() must construct the zero value (these
|
// The type W must be a signed numeric type that supports addition, binary
|
||||||
// constraints are met for primitive signed integer types).
|
// and unary -, and < and <= comparisons, and W() must construct the zero value
|
||||||
|
// (these constraints are met for primitive signed integer types).
|
||||||
template <typename K, typename V, typename W, W random(W)>
|
template <typename K, typename V, typename W, W random(W)>
|
||||||
class WeightedMap
|
class WeightedMap
|
||||||
{
|
{
|
||||||
|
// W must be a signed numeric type.
|
||||||
|
static_assert(std::numeric_limits<W>::min() < W());
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
K key;
|
K key;
|
||||||
V value;
|
V value;
|
||||||
|
@ -180,6 +184,7 @@ public:
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
W totalWeight = getTotalWeight();
|
W totalWeight = getTotalWeight();
|
||||||
|
assert(W() < totalWeight);
|
||||||
W randomWeight = random(totalWeight);
|
W randomWeight = random(totalWeight);
|
||||||
assert(W() <= randomWeight && randomWeight < totalWeight);
|
assert(W() <= randomWeight && randomWeight < totalWeight);
|
||||||
size_t index = findByWeight(0, randomWeight);
|
size_t index = findByWeight(0, randomWeight);
|
||||||
|
|
Loading…
Reference in New Issue