mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 10:45:37 +08:00
refs #481 Algorithms to copy random elements from containers.
This commit is contained in:
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user