10#include <tracking/trackFindingCDC/utilities/Range.h>
22 namespace TrackFindingCDC {
28 template <
class Ts,
class ACategoryFunction,
class ACategory>
29 ACategory common(
const Ts& items,
const ACategoryFunction& catFunc,
const ACategory defaultCat)
31 auto it = std::begin(items);
32 auto itEnd = std::end(items);
33 return common(it, itEnd, catFunc, defaultCat);
40 template <
class It,
class ACategoryFunction,
class ACategory>
41 ACategory common(It itBegin, It itEnd,
const ACategoryFunction& catFunc,
const ACategory defaultCat)
43 if (itBegin == itEnd)
return defaultCat;
44 const ACategory cat = catFunc(*itBegin);
45 for (It it = itBegin; it != itEnd; ++it) {
46 if (cat != catFunc(*it)) {
56 template <
class Ts,
class APredicate>
57 void erase_remove_if(Ts& ts,
const APredicate& predicate)
59 ts.erase(std::remove_if(std::begin(ts), std::end(ts), predicate), std::end(ts));
63 void only_best_N(Ts& ts,
const size_t N)
65 auto newEnd = std::next(ts.begin(), std::min(N, ts.size()));
66 ts.erase(newEnd, ts.end());
73 void erase_unique(Ts& ts)
75 ts.erase(std::unique(std::begin(ts), std::end(ts)), std::end(ts));
81 template <
class Ts,
class AEqual>
82 void erase_unique(Ts& ts,
const AEqual& equal)
84 ts.erase(std::unique(std::begin(ts), std::end(ts), equal), std::end(ts));
91 std::vector<std::pair<It, int> > unique_count(It itBegin, It itEnd)
93 std::vector<std::pair<It, int> > result;
94 if (itBegin == itEnd)
return result;
96 result.emplace_back(it, 1);
98 for (; it != itEnd; ++it) {
99 if (*it == *result.back().first) {
100 ++result.back().second;
102 result.emplace_back(it, 1);
111 template <
class It,
class AEqual>
112 std::vector<std::pair<It, int> > unique_count(It itBegin, It itEnd,
const AEqual& equal)
114 std::vector<std::pair<It, int> > result;
115 if (itBegin == itEnd)
return result;
117 result.emplace_back(it, 1);
119 for (; it != itEnd; ++it) {
120 if (equal(*it, *result.back().first)) {
121 ++result.back().second;
123 result.emplace_back(it, 1);
133 std::vector<Range<It> > unique_ranges(It itBegin, It itEnd)
135 std::vector<std::pair<It, It> > result;
136 if (itBegin == itEnd)
return result;
138 It it2 = itBegin + 1;
139 result.emplace_back(it1, it2);
140 for (; it2 != itEnd; ++it1, ++it2) {
141 if (not(*it1 == *it2)) {
142 result.emplace_back(it2, it2);
144 ++result.back().second;
152 template <
class It,
class AEqual>
153 std::vector<Range<It> > unique_ranges(It itBegin, It itEnd,
const AEqual& equal)
155 std::vector<Range<It> > result;
156 if (itBegin == itEnd)
return result;
158 It it2 = itBegin + 1;
159 result.emplace_back(it1, it2);
160 for (; it2 != itEnd; ++it1, ++it2) {
161 if (not equal(*it1, *it2)) {
162 result.emplace_back(it2, it2);
164 ++result.back().second;
172 template <
class It,
class ACategoryFunction>
173 std::vector<Range<It>> adjacent_groupby(It itBegin, It itEnd,
const ACategoryFunction& catFunc)
175 std::vector<Range<It>> result;
176 if (itBegin == itEnd)
return result;
178 It itFirstOfGroup = itBegin;
179 auto catOfGroup = catFunc(*itBegin);
181 for (It it = itBegin; it != itEnd; ++it) {
182 auto cat = catFunc(*it);
183 if (catOfGroup != cat) {
184 result.emplace_back(itFirstOfGroup, it);
189 result.emplace_back(itFirstOfGroup, itEnd);
197 template <
class AInputIterator,
class AOutputIterator,
class ABinaryOperation>
198 AOutputIterator transform_adjacent_pairs(AInputIterator itBegin,
199 AInputIterator itEnd,
200 AOutputIterator result,
201 const ABinaryOperation& map)
203 if (itBegin == itEnd)
return result;
205 AInputIterator second = itBegin;
207 while (second != itEnd) {
208 *result = map(*itBegin, *second);
220 template <
class AInputIterator,
class AOutputIterator,
class ATrinaryOperation>
221 AOutputIterator transform_adjacent_triples(AInputIterator itBegin,
222 AInputIterator itEnd,
223 AOutputIterator result,
224 const ATrinaryOperation& map)
226 if (not(itBegin != itEnd))
return result;
228 AInputIterator second = itBegin;
230 if (not(second != itEnd))
return result;
232 AInputIterator third = second;
234 while (third != itEnd) {
235 *result = map(*itBegin, *second, *third);
248 template <
class Ts,
class TCopyIfPredicate>
249 Ts copy_if(Ts
const& inputContainer, TCopyIfPredicate pred)
254 std::copy_if(inputContainer.begin(), inputContainer.end(), std::back_inserter(outputContainer), pred);
255 return outputContainer;
262 template <
class Ts,
class AUnaryPredicate>
263 bool any(
const Ts& ts,
const AUnaryPredicate& comparator)
265 return std::any_of(std::begin(ts), std::end(ts), comparator);
271 template <
class Ts,
class AItem>
272 bool is_in(
const AItem& item,
const Ts& ts)
274 return std::find(std::begin(ts), std::end(ts), item) != std::end(ts);
280 template <
class T,
class Ts>
281 std::vector<T*> as_pointers(Ts& ts)
285 std::size_t vsize = end(ts) - begin(ts);
286 std::vector<T*> result(vsize,
nullptr);
287 std::transform(begin(ts), end(ts), result.begin(), [](T & t) { return std::addressof<T>(t);});
Abstract base class for different kinds of events.