Home » U++ Library support » U++ Core » Patch for Algo.h
Patch for Algo.h [message #53185] |
Sun, 15 March 2020 18:56 |
Novo
Messages: 1371 Registered: December 2006
|
Ultimate Contributor |
|
|
Below is a tiny patch for Algo.h
Storing r.GetCount() on stack improves performance with ranges which do not store length explicitly.
template <class Range, class C>
int FindMatch(const Range& r, const C& match, int from = 0)
{
const int count = r.GetCount();
for(int i = from; i < count; i++)
if(match(r[i])) return i;
return -1;
}
template <class Range, class V>
int FindIndex(const Range& r, const V& value, int from = 0)
{
const int count = r.GetCount();
for(int i = from; i < count; i++)
if(r[i] == value) return i;
return -1;
}
template <class Range, class Predicate>
Vector<int> FindAll(const Range& r, Predicate match, int from = 0)
{
Vector<int> ndx;
const int count = r.GetCount();
for(int i = from; i < count; i++)
if(match(r[i]))
ndx.Add(i);
return ndx;
}
template <class Range, class Predicate>
Vector<int> FindAlli(const Range& r, Predicate match, int from = 0)
{
Vector<int> ndx;
const int count = r.GetCount();
for(int i = from; i < count; i++)
if(match(i))
ndx.Add(i);
return ndx;
}
Regards,
Novo
|
|
|
Goto Forum:
Current Time: Fri Sep 20 13:47:30 CEST 2024
Total time taken to generate the page: 0.02880 seconds
|