proxygen
folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType > Class Template Reference

#include <Base-inl.h>

Inheritance diagram for folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >:
folly::gen::GenImpl< StorageType &&, Generator< Value, Source, Rand, StorageType > > folly::gen::FBounded< Self >

Public Member Functions

 Generator (Source source, size_t count, Random rng)
 
template<class Handler >
bool apply (Handler &&handler) const
 
- Public Member Functions inherited from folly::gen::GenImpl< StorageType &&, Generator< Value, Source, Rand, StorageType > >
bool apply (Handler &&handler) const
 
void foreach (Body &&body) const
 
- Public Member Functions inherited from folly::gen::FBounded< Self >
const Self & self () const
 
Self & self ()
 

Static Public Attributes

static constexpr bool infinite = false
 
- Static Public Attributes inherited from folly::gen::GenImpl< StorageType &&, Generator< Value, Source, Rand, StorageType > >
static constexpr bool infinite
 

Private Attributes

Source source_
 
size_t count_
 
Rand rng_
 

Additional Inherited Members

- Public Types inherited from folly::gen::GenImpl< StorageType &&, Generator< Value, Source, Rand, StorageType > >
typedef StorageType && ValueType
 
typedef std::decay< StorageType && >::type StorageType
 
- Protected Member Functions inherited from folly::gen::GenImpl< StorageType &&, Generator< Value, Source, Rand, StorageType > >
 GenImpl ()=default
 
 GenImpl (GenImpl &&)=default
 
 GenImpl (const GenImpl &)=default
 
GenImploperator= (GenImpl &&)=default
 
GenImploperator= (const GenImpl &)=default
 

Detailed Description

template<class Random>
template<class Value, class Source, class Rand, class StorageType = typename std::decay<Value>::type>
class folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >

Definition at line 884 of file Base-inl.h.

Constructor & Destructor Documentation

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::Generator ( Source  source,
size_t  count,
Random  rng 
)
inlineexplicit

Definition at line 897 of file Base-inl.h.

898  : source_(std::move(source)), count_(count), rng_(std::move(rng)) {}
constexpr detail::Count count
Definition: Base-inl.h:2551
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Function Documentation

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
template<class Handler >
bool folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::apply ( Handler &&  handler) const
inline

Definition at line 901 of file Base-inl.h.

References handler(), folly::gen::move, v, val, value, and testing::Value().

901  {
902  if (count_ == 0) {
903  return false;
904  }
905  std::vector<StorageType> v;
906  v.reserve(count_);
907  // use reservoir sampling to give each source value an equal chance
908  // of appearing in our output.
909  size_t n = 1;
910  source_.foreach([&](Value value) -> void {
911  if (v.size() < count_) {
912  v.push_back(std::forward<Value>(value));
913  } else {
914  // alternatively, we could create a std::uniform_int_distribution
915  // instead of using modulus, but benchmarks show this has
916  // substantial overhead.
917  size_t index = rng_() % n;
918  if (index < v.size()) {
919  v[index] = std::forward<Value>(value);
920  }
921  }
922  ++n;
923  });
924 
925  // output is unsorted!
926  for (auto& val : v) {
927  if (!handler(std::move(val))) {
928  return false;
929  }
930  }
931  return true;
932  }
auto v
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
void handler(int, siginfo_t *, void *)
bool Value(const T &value, M matcher)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)

Member Data Documentation

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
size_t folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::count_
private

Definition at line 893 of file Base-inl.h.

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
constexpr bool folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::infinite = false
static

Definition at line 935 of file Base-inl.h.

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
Rand folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::rng_
mutableprivate

Definition at line 894 of file Base-inl.h.

template<class Random>
template<class Value , class Source , class Rand , class StorageType = typename std::decay<Value>::type>
Source folly::gen::detail::Sample< Random >::Generator< Value, Source, Rand, StorageType >::source_
private

Definition at line 887 of file Base-inl.h.


The documentation for this class was generated from the following file: