SPH
ArrayUtils.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "math/MathUtils.h"
11 
13 
14 template <typename U, typename T, typename TFunctor, typename TComparator>
16  TFunctor&& functor,
17  const U defaultValue,
18  TComparator&& comparator) {
19  U maxValue = defaultValue;
20  Iterator<T> maxIter = container.begin();
21  for (Iterator<T> iter = container.begin(); iter != container.end(); ++iter) {
22  U value = functor(*iter);
23  if (comparator(value, maxValue)) {
24  maxIter = iter;
25  maxValue = value;
26  }
27  }
28  return maxIter;
29 }
30 
31 template <typename U, typename T, typename TFunctor>
32 Iterator<T> findByMaximum(ArrayView<T> container, TFunctor&& functor) {
33  return findByComparator<U>(
34  container, std::forward<TFunctor>(functor), U(-INFTY), [](const U& v1, const U& v2) {
35  return v1 > v2;
36  });
37 }
38 
39 
40 template <typename U, typename T, typename TFunctor>
41 Iterator<T> findByMinimum(ArrayView<T> container, TFunctor&& functor) {
42  return findByComparator<U>(
43  container, std::forward<TFunctor>(functor), U(INFTY), [](const U& v1, const U& v2) {
44  return v1 < v2;
45  });
46 }
47 
50 template <typename U, typename T, typename TFunctor, typename TComparator>
52  TFunctor&& functor,
53  const U defaultValue,
54  TComparator&& comparator) {
55  Iterator<T> maxOuterIter = container.begin();
56  Iterator<T> maxInnerIter = container.end();
57  U maxValue = defaultValue;
58  for (Iterator<T> outerIter = container.begin(); outerIter != container.end(); ++outerIter) {
59  for (Iterator<T> innerIter = container.begin(); innerIter != container.end(); ++innerIter) {
60  if (outerIter == innerIter) {
61  continue;
62  }
63  U value = functor(*innerIter, *outerIter);
64  if (comparator(value, maxValue)) {
65  maxOuterIter = outerIter;
66  maxInnerIter = innerIter;
67  maxValue = value;
68  }
69  }
70  }
71  return maxInnerIter > maxOuterIter ? makeTuple(maxOuterIter, maxInnerIter)
72  : makeTuple(maxInnerIter, maxOuterIter);
73 }
74 
75 template <typename U, typename T, typename TFunctor>
76 Tuple<Iterator<T>, Iterator<T>> findPairByMaximum(ArrayView<T> container, TFunctor&& functor) {
77  return findPairByComparator<U>(
78  container, std::forward<TFunctor>(functor), U(-INFTY), [](const U& v1, const U& v2) {
79  return v1 > v2;
80  });
81 }
82 
83 template <typename U, typename T, typename TFunctor>
84 Tuple<Iterator<T>, Iterator<T>> findPairByMinimum(ArrayView<T> container, TFunctor&& functor) {
85  return findPairByComparator<U>(
86  container, std::forward<TFunctor>(functor), U(INFTY), [](const U& v1, const U& v2) {
87  return v1 < v2;
88  });
89 }
90 
91 template <typename TStorage, typename TFunctor>
92 int getCountMatching(const TStorage& container, TFunctor&& functor) {
93  int cnt = 0;
94  for (const auto& t : container) {
95  if (functor(t)) {
96  cnt++;
97  }
98  }
99  return cnt;
100 }
101 
102 template <typename TStorage, typename TFunctor>
103 bool areAllMatching(const TStorage& container, TFunctor&& functor) {
104  for (const auto& t : container) {
105  if (!functor(t)) {
106  return false;
107  }
108  }
109  return true;
110 }
111 
112 template <typename TStorage, typename TFunctor>
113 bool isAnyMatching(const TStorage& container, TFunctor&& functor) {
114  for (const auto& t : container) {
115  if (functor(t)) {
116  return true;
117  }
118  }
119  return false;
120 }
121 
124 template <typename TStorage>
125 bool areElementsUnique(const TStorage& container) {
126  // stupid O(N^2) solution
127  for (auto iter1 = container.begin(); iter1 != container.end(); ++iter1) {
128  for (auto iter2 = iter1; iter2 != container.end(); ++iter2) {
129  if (*iter2 == *iter1 && iter2 != iter1) {
130  return false;
131  }
132  }
133  }
134  return true;
135 }
136 
138 template <typename TStorage1, typename TStorage2>
139 bool haveCommonElements(const TStorage1& c1, const TStorage2& c2) {
140  for (const auto& t1 : c1) {
141  for (const auto& t2 : c2) {
142  if (t1 == t2) {
143  return true;
144  }
145  }
146  }
147  return false;
148 }
149 
bool areElementsUnique(const TStorage &container)
Definition: ArrayUtils.h:125
bool haveCommonElements(const TStorage1 &c1, const TStorage2 &c2)
Returns true if two containers have at least one element with the same value.
Definition: ArrayUtils.h:139
Iterator< T > findByMaximum(ArrayView< T > container, TFunctor &&functor)
Definition: ArrayUtils.h:32
Tuple< Iterator< T >, Iterator< T > > findPairByComparator(ArrayView< T > container, TFunctor &&functor, const U defaultValue, TComparator &&comparator)
Definition: ArrayUtils.h:51
Tuple< Iterator< T >, Iterator< T > > findPairByMaximum(ArrayView< T > container, TFunctor &&functor)
Definition: ArrayUtils.h:76
NAMESPACE_SPH_BEGIN Iterator< T > findByComparator(ArrayView< T > container, TFunctor &&functor, const U defaultValue, TComparator &&comparator)
Definition: ArrayUtils.h:15
int getCountMatching(const TStorage &container, TFunctor &&functor)
Definition: ArrayUtils.h:92
Tuple< Iterator< T >, Iterator< T > > findPairByMinimum(ArrayView< T > container, TFunctor &&functor)
Definition: ArrayUtils.h:84
bool isAnyMatching(const TStorage &container, TFunctor &&functor)
Definition: ArrayUtils.h:113
bool areAllMatching(const TStorage &container, TFunctor &&functor)
Definition: ArrayUtils.h:103
Iterator< T > findByMinimum(ArrayView< T > container, TFunctor &&functor)
Definition: ArrayUtils.h:41
Simple non-owning view of a container.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Additional math routines (with more includes).
constexpr Float INFTY
Definition: MathUtils.h:38
#define NAMESPACE_SPH_END
Definition: Object.h:12
Re-implementation of std::tuple with some additional functionality.
INLINE auto makeTuple(TArgs &&... args)
Creates a tuple from a pack of values, utilizing type deduction.
Definition: Tuple.h:298
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE Iterator< StorageType > begin()
Definition: ArrayView.h:55
INLINE Iterator< StorageType > end()
Definition: ArrayView.h:63
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18
Heterogeneous container capable of storing a fixed number of values.
Definition: Tuple.h:146