mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-06-13 22:04:36 -06:00
Functions4U: Added LinSpaced and EqualDecimals
git-svn-id: svn://ultimatepp.org/upp/trunk@15597 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
fc39be66a0
commit
d490af6a8a
1 changed files with 30 additions and 2 deletions
|
|
@ -634,6 +634,19 @@ class ThreadSafe {
|
|||
...
|
||||
};*/
|
||||
|
||||
template <class Range>
|
||||
void LinSpaced(Range &v, int n, typename Range::value_type min, typename Range::value_type max) {
|
||||
ASSERT(n > 0);
|
||||
v.SetCount(n);
|
||||
if (n == 1)
|
||||
v[0] = min;
|
||||
else {
|
||||
typename Range::value_type d = (max - min)/(n - 1);
|
||||
for (int i = 0; i < n; ++i)
|
||||
v[i] = min + d*i;
|
||||
}
|
||||
}
|
||||
|
||||
template <class C>
|
||||
static void ShuffleAscending(C &data, std::default_random_engine &generator) {
|
||||
for (int i = 0; i < data.size() - 2; i++) {
|
||||
|
|
@ -686,6 +699,11 @@ bool EqualRatio(const T& a, const T& b, const T& ratio, const T& zero = 0) {
|
|||
return false;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool EqualDecimals(const T& a, const T& b, int numdecimals) {
|
||||
return FormatDouble(a, numdecimals) == FormatDouble(b, numdecimals);
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
int Find(const Range& r, const typename Range::value_type& value, int from = 0) {
|
||||
for(int i = from; i < r.size(); i++)
|
||||
|
|
@ -772,8 +790,8 @@ bool Compare(const Range& a, const Range& b) {
|
|||
return true;
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
bool CompareRatio(const Range& a, const Range& b, const typename Range::value_type& ratio) {
|
||||
template <class Range1, class Range2>
|
||||
bool CompareRatio(const Range1& a, const Range2& b, const typename Range1::value_type& ratio) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
for(int i = 0; i < a.size(); i++)
|
||||
|
|
@ -782,6 +800,16 @@ bool CompareRatio(const Range& a, const Range& b, const typename Range::value_ty
|
|||
return true;
|
||||
}
|
||||
|
||||
template <class Range1, class Range2>
|
||||
bool CompareDecimals(const Range1& a, const Range2& b, int numDecimals) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
for(int i = 0; i < a.size(); i++)
|
||||
if (!EqualDecimals(a[i], b[i], numDecimals))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
String ToString(const Range& a) {
|
||||
String ret;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue