refs #481 Algorithms to copy random elements from containers.

This commit is contained in:
Mathew Sutcliffe
2015-10-03 00:14:35 +01:00
parent aad527933d
commit 86913471a9
3 changed files with 118 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
#include "blackmiscexport.h"
#include "iterator.h"
#include "predicates.h"
#include "algorithm.h"
#include <QtGlobal>
#include <QDebug>
#include <algorithm>
@@ -128,6 +129,22 @@ namespace BlackMisc
return containsBy(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...));
}
//! Copy n elements from the container at random.
Derived randomElements(int n) const
{
Derived result;
BlackMisc::copyRandomElements(derived().begin(), derived().end(), std::inserter(result, result.end()), n);
return result;
}
//! Copy n elements from the container, randomly selected but evenly distributed.
Derived sampleElements(int n) const
{
Derived result;
BlackMisc::copySampleElements(derived().begin(), derived().end(), std::inserter(result, result.end()), n);
return result;
}
private:
Derived &derived() { return static_cast<Derived &>(*this); }
const Derived &derived() const { return static_cast<const Derived &>(*this); }