diff options
author | vitalyisaev <vitalyisaev@yandex-team.com> | 2023-06-29 10:00:50 +0300 |
---|---|---|
committer | vitalyisaev <vitalyisaev@yandex-team.com> | 2023-06-29 10:00:50 +0300 |
commit | 6ffe9e53658409f212834330e13564e4952558f6 (patch) | |
tree | 85b1e00183517648b228aafa7c8fb07f5276f419 /contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h | |
parent | 726057070f9c5a91fc10fde0d5024913d10f1ab9 (diff) | |
download | ydb-6ffe9e53658409f212834330e13564e4952558f6.tar.gz |
YQ Connector: support managed ClickHouse
Со стороны dqrun можно обратиться к инстансу коннектора, который работает на streaming стенде, и извлечь данные из облачного CH.
Diffstat (limited to 'contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h')
-rw-r--r-- | contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h b/contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h new file mode 100644 index 0000000000..2f04bf63b1 --- /dev/null +++ b/contrib/libs/llvm14/include/llvm/FuzzMutate/Random.h @@ -0,0 +1,107 @@ +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- Random.h - Utilities for random sampling -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Utilities for random sampling. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FUZZMUTATE_RANDOM_H +#define LLVM_FUZZMUTATE_RANDOM_H + +#include <random> +#include "llvm/Support/raw_ostream.h" +namespace llvm { + +/// Return a uniformly distributed random value between \c Min and \c Max +template <typename T, typename GenT> T uniform(GenT &Gen, T Min, T Max) { + return std::uniform_int_distribution<T>(Min, Max)(Gen); +} + +/// Return a uniformly distributed random value of type \c T +template <typename T, typename GenT> T uniform(GenT &Gen) { + return uniform<T>(Gen, std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); +} + +/// Randomly selects an item by sampling into a set with an unknown number of +/// elements, which may each be weighted to be more likely choices. +template <typename T, typename GenT> class ReservoirSampler { + GenT &RandGen; + std::remove_const_t<T> Selection = {}; + uint64_t TotalWeight = 0; + +public: + ReservoirSampler(GenT &RandGen) : RandGen(RandGen) {} + + uint64_t totalWeight() const { return TotalWeight; } + bool isEmpty() const { return TotalWeight == 0; } + + const T &getSelection() const { + assert(!isEmpty() && "Nothing selected"); + return Selection; + } + + explicit operator bool() const { return !isEmpty();} + const T &operator*() const { return getSelection(); } + + /// Sample each item in \c Items with unit weight + template <typename RangeT> ReservoirSampler &sample(RangeT &&Items) { + for (auto &I : Items) + sample(I, 1); + return *this; + } + + /// Sample a single item with the given weight. + ReservoirSampler &sample(const T &Item, uint64_t Weight) { + if (!Weight) + // If the weight is zero, do nothing. + return *this; + TotalWeight += Weight; + // Consider switching from the current element to this one. + if (uniform<uint64_t>(RandGen, 1, TotalWeight) <= Weight) + Selection = Item; + return *this; + } +}; + +template <typename GenT, typename RangeT, + typename ElT = std::remove_reference_t< + decltype(*std::begin(std::declval<RangeT>()))>> +ReservoirSampler<ElT, GenT> makeSampler(GenT &RandGen, RangeT &&Items) { + ReservoirSampler<ElT, GenT> RS(RandGen); + RS.sample(Items); + return RS; +} + +template <typename GenT, typename T> +ReservoirSampler<T, GenT> makeSampler(GenT &RandGen, const T &Item, + uint64_t Weight) { + ReservoirSampler<T, GenT> RS(RandGen); + RS.sample(Item, Weight); + return RS; +} + +template <typename T, typename GenT> +ReservoirSampler<T, GenT> makeSampler(GenT &RandGen) { + return ReservoirSampler<T, GenT>(RandGen); +} + +} // End llvm namespace + +#endif // LLVM_FUZZMUTATE_RANDOM_H + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif |