mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-27 11:05:44 +08:00
refs #452, allow to pick random elements from collection
(will be used in view resizing)
This commit is contained in:
committed by
Mathew Sutcliffe
parent
5718abe03c
commit
ad1938b8d8
@@ -337,6 +337,23 @@ namespace BlackMisc
|
|||||||
*/
|
*/
|
||||||
void remove(const CCollection &other) { *this = CCollection(*this).difference(other); }
|
void remove(const CCollection &other) { *this = CCollection(*this).difference(other); }
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Return some random elements from container
|
||||||
|
* \param elements how many elements
|
||||||
|
*/
|
||||||
|
CCollection randomElements(int elements) const
|
||||||
|
{
|
||||||
|
if (this->isEmpty() || elements < 1) { return CCollection(); }
|
||||||
|
int high = this->size();
|
||||||
|
CCollection r;
|
||||||
|
for (int i = 0; i < elements; i++)
|
||||||
|
{
|
||||||
|
int randomIndex = qrand() % high;
|
||||||
|
r.push_back(*(this->begin()+ randomIndex));
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Test for equality.
|
* \brief Test for equality.
|
||||||
* \todo Improve inefficient implementation.
|
* \todo Improve inefficient implementation.
|
||||||
|
|||||||
@@ -98,6 +98,12 @@ namespace BlackMisc
|
|||||||
|
|
||||||
//! Normalize: 0≤ degrees <360
|
//! Normalize: 0≤ degrees <360
|
||||||
static double normalizeDegrees(double degrees);
|
static double normalizeDegrees(double degrees);
|
||||||
|
|
||||||
|
//! Random number between low and high
|
||||||
|
static int randomIntger(int low, int high)
|
||||||
|
{
|
||||||
|
return qrand() % ((high + 1) - low) + low;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -20,7 +20,6 @@
|
|||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <QtConcurrent/QtConcurrent>
|
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -519,6 +518,23 @@ namespace BlackMisc
|
|||||||
return sorted(BlackMisc::Predicates::MemberLess(key1, keys...));
|
return sorted(BlackMisc::Predicates::MemberLess(key1, keys...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Return some random elements from container
|
||||||
|
* \param elements how many elements
|
||||||
|
*/
|
||||||
|
CSequence randomElements(int elements) const
|
||||||
|
{
|
||||||
|
if (this->isEmpty() || elements < 1) { return CSequence(); }
|
||||||
|
int high = this->size();
|
||||||
|
CSequence r;
|
||||||
|
for (int i = 0; i < elements; i++)
|
||||||
|
{
|
||||||
|
int randomIndex = qrand() % high;
|
||||||
|
r.push_back(this->operator [](randomIndex));
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
//! Equals operator.
|
//! Equals operator.
|
||||||
friend bool operator ==(const CSequence &a, const CSequence &b)
|
friend bool operator ==(const CSequence &a, const CSequence &b)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user