Random.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===--- Random.h - Utilities for random sampling -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Utilities for random sampling.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_FUZZMUTATE_RANDOM_H
  13. #define LLVM_FUZZMUTATE_RANDOM_H
  14. #include <random>
  15. #include "llvm/Support/raw_ostream.h"
  16. namespace llvm {
  17. /// Return a uniformly distributed random value between \c Min and \c Max
  18. template <typename T, typename GenT> T uniform(GenT &Gen, T Min, T Max) {
  19. return std::uniform_int_distribution<T>(Min, Max)(Gen);
  20. }
  21. /// Return a uniformly distributed random value of type \c T
  22. template <typename T, typename GenT> T uniform(GenT &Gen) {
  23. return uniform<T>(Gen, std::numeric_limits<T>::min(),
  24. std::numeric_limits<T>::max());
  25. }
  26. /// Randomly selects an item by sampling into a set with an unknown number of
  27. /// elements, which may each be weighted to be more likely choices.
  28. template <typename T, typename GenT> class ReservoirSampler {
  29. GenT &RandGen;
  30. std::remove_const_t<T> Selection = {};
  31. uint64_t TotalWeight = 0;
  32. public:
  33. ReservoirSampler(GenT &RandGen) : RandGen(RandGen) {}
  34. uint64_t totalWeight() const { return TotalWeight; }
  35. bool isEmpty() const { return TotalWeight == 0; }
  36. const T &getSelection() const {
  37. assert(!isEmpty() && "Nothing selected");
  38. return Selection;
  39. }
  40. explicit operator bool() const { return !isEmpty();}
  41. const T &operator*() const { return getSelection(); }
  42. /// Sample each item in \c Items with unit weight
  43. template <typename RangeT> ReservoirSampler &sample(RangeT &&Items) {
  44. for (auto &I : Items)
  45. sample(I, 1);
  46. return *this;
  47. }
  48. /// Sample a single item with the given weight.
  49. ReservoirSampler &sample(const T &Item, uint64_t Weight) {
  50. if (!Weight)
  51. // If the weight is zero, do nothing.
  52. return *this;
  53. TotalWeight += Weight;
  54. // Consider switching from the current element to this one.
  55. if (uniform<uint64_t>(RandGen, 1, TotalWeight) <= Weight)
  56. Selection = Item;
  57. return *this;
  58. }
  59. };
  60. template <typename GenT, typename RangeT,
  61. typename ElT = std::remove_reference_t<
  62. decltype(*std::begin(std::declval<RangeT>()))>>
  63. ReservoirSampler<ElT, GenT> makeSampler(GenT &RandGen, RangeT &&Items) {
  64. ReservoirSampler<ElT, GenT> RS(RandGen);
  65. RS.sample(Items);
  66. return RS;
  67. }
  68. template <typename GenT, typename T>
  69. ReservoirSampler<T, GenT> makeSampler(GenT &RandGen, const T &Item,
  70. uint64_t Weight) {
  71. ReservoirSampler<T, GenT> RS(RandGen);
  72. RS.sample(Item, Weight);
  73. return RS;
  74. }
  75. template <typename T, typename GenT>
  76. ReservoirSampler<T, GenT> makeSampler(GenT &RandGen) {
  77. return ReservoirSampler<T, GenT>(RandGen);
  78. }
  79. } // End llvm namespace
  80. #endif // LLVM_FUZZMUTATE_RANDOM_H