20 #ifndef MIRTK_Algorithm_H    21 #define MIRTK_Algorithm_H    23 #include "mirtk/Array.h"    24 #include "mirtk/UnorderedSet.h"    25 #include "mirtk/Pair.h"    35 using std::min_element;
    36 using std::max_element;
    37 using std::minmax_element;
    39 using std::binary_search;
    41 using std::stable_sort;
    42 using std::partial_sort;
    43 using std::nth_element;
    46 using std::set_intersection;
    49 using std::back_inserter;
    50 using std::front_inserter;
    58 using std::multiplies;
    68   if (values.empty()) 
return T(0);
    69   return *min_element(values.begin(), values.end());
    76   if (values.empty()) 
return T(0);
    77   return *max_element(values.begin(), values.end());
    84   if (values.empty()) 
return MakePair(T(0), T(0));
    85   auto minmax = minmax_element(values.begin(), values.end());
    86   return MakePair(*minmax.first, *minmax.second);
    90 template <
class T, 
class UnaryOperation>
    93   transform(values.begin(), values.end(), values.begin(), op);
    97 template <
class AttributeType>
   100   const Array<AttributeType> &_Values;
   109   template <
class IndexType>
   110   bool operator ()(IndexType i, IndexType j)
 const   112     return _Values[i] < _Values[j];
   117 template <
class AttributeType>
   120   const Array<AttributeType> &_Values;
   129   template <
class IndexType>
   130   bool operator ()(IndexType i, IndexType j)
 const   132     return _Values[i] > _Values[j];
   140   sort(values.begin(), values.end());
   144 template <
class T, 
class Compare>
   145 void Sort(Array<T> &values, Compare comp)
   147   sort(values.begin(), values.end(), comp);
   154   stable_sort(values.begin(), values.end());
   158 template <
class T, 
class Compare>
   161   stable_sort(values.begin(), values.end(), comp);
   168   partial_sort(values.begin(), values.begin() + n + 1, values.end());
   172 template <
class T, 
class Compare>
   175   partial_sort(values.begin(), values.begin() + n + 1, values.end(), comp);
   183   nth_element(values.begin(), values.begin() + n, values.end());
   190 template <
class T, 
class Compare>
   193   nth_element(values.begin(), values.begin() + n, values.end(), comp);
   201   Array<T> copy(values);
   202   return NthElement(copy, static_cast<int>(values.size())/2);
   209   Array<int> indices(values.size());
   210   for (
size_t i = 0; i < values.size(); ++i) {
   211     indices[i] = 
static_cast<int>(i);
   221   Array<int> indices(values.size());
   222   for (
size_t i = 0; i < values.size(); ++i) {
   223     indices[i] = 
static_cast<int>(i);
   233 Array<T> 
Permutation(
const Array<int> &order, 
const Array<T> &values)
   235   Array<T> permuted_values;
   236   permuted_values.reserve(values.size());
   237   for (
auto i : order) permuted_values.push_back(values[i]);
   238   return permuted_values;
   243 typename Array<T>::iterator
   244 Find(Array<T> &values, 
const T &value)
   246   return find(values.begin(), values.end(), value);
   251 typename Array<T>::const_iterator
   252 Find(
const Array<T> &values, 
const T &value)
   254   return find(values.begin(), values.end(), value);
   261   auto it = find(values.begin(), values.end(), value);
   262   if (it == values.end()) 
return -1;
   263   return static_cast<int>(std::distance(values.begin(), it));
   268 typename Array<T>::iterator
   271   return binary_search(values.begin(), values.end(), value);
   276 typename Array<T>::const_iterator
   279   return binary_search(values.begin(), values.end(), value);
   283 template <
class T, 
class Compare>
   284 typename Array<T>::iterator
   287   return binary_search(values.begin(), values.end(), value, comp);
   291 template <
class T, 
class Compare>
   292 typename Array<T>::const_iterator
   295   return binary_search(values.begin(), values.end(), value, comp);
   302 UnorderedSet<T> 
Intersection(
const UnorderedSet<T> &a, 
const UnorderedSet<T> &b)
   304   if (b.size() < a.size()) {
   309       if (b.find(v) != b.end()) c.insert(v);
   319 UnorderedSet<T> 
Union(
const UnorderedSet<T> &a, 
const UnorderedSet<T> &b)
   321   if (b.size() > a.size()) {
   324     UnorderedSet<T> c = a;
   325     for (
auto v : b) c.insert(v);
   333 #endif // MIRTK_Algorithm_H Array< int > IncreasingOrder(const Array< T > &values)
Get permutation of array indices corresponding to sorted order of values. 
Array< T >::iterator Find(Array< T > &values, const T &value)
Find value in unsorted Array. 
void Sort(Array< T > &values)
Sort values in array. 
UnorderedSet< T > Intersection(const UnorderedSet< T > &a, const UnorderedSet< T > &b)
Compare functor used to sort array indices by decreasing element attribute. 
void StableSort(Array< T > &values)
Sort values in array while preserving order of equal entries. 
Array< T > Permutation(const Array< int > &order, const Array< T > &values)
T MaxElement(const Array< T > &values)
Get maximum array element. 
Array< int > DecreasingOrder(const Array< T > &values)
Get permutation of array indices corresponding to sorted order of values. 
Compare functor used to sort array indices increasing element attribute. 
void PartialSort(Array< T > &values, int n)
Sort first n values in array. 
T MedianValue(const Array< T > &values)
Get median value. 
Array< T >::iterator BinarySearch(Array< T > &values, const T &value)
Find value in sorted Array using binary search. 
UnorderedSet< T > Union(const UnorderedSet< T > &a, const UnorderedSet< T > &b)
int FindIndex(const Array< T > &values, const T &value)
Find value in unsorted Array. 
T MinElement(const Array< T > &values)
Get minimum array element. 
Pair< T, T > MinMaxElement(const Array< T > &values)
Get minimum and maximum array element. 
T & NthElement(Array< T > &values, int n)
void Transform(Array< T > &values, UnaryOperation op)
Apply unary operation for each array element in-place.