Random.h 3.2 KB

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