Version: SMASH-3.4
algorithms.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2015,2017-2018,2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_ALGORITHMS_H_
11 #define SRC_INCLUDE_SMASH_ALGORITHMS_H_
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <utility>
16 
17 /**
18  * \file
19  *
20  * Generic algorithms on containers and ranges.
21  *
22  * This file collects generic algorithms that follow the general idea of C++
23  * algorithms as defined in the C++ standard library. These typically work with
24  * iterators from arbitrary containers.
25  *
26  * The C++ standard itself categorizes algorithms into the following:
27  * * Non-modifying sequence operations
28  * * Mutating sequence operations
29  * * Sorting and related operations
30  */
31 
32 namespace smash {
33 
34 /**
35  * Enforces periodic boundaries on the given collection of values.
36  *
37  * The values in an arbitrary container, starting from \p begin and ending at \p
38  * end, will be checked. If the value is less than 0, \p length will be added to
39  * it. If the value is greater than or equal to \p length, \p length will be
40  * subtracted from it.
41  *
42  * The implementation therefore assumes that the values are at most one \p
43  * length away from the 0 to \p length range.
44  *
45  * \tparam Iterator Type of the iterator.
46  * \param begin Iterator pointing to the first value to check.
47  * \param end End iterator.
48  * \param length The length of the valid interval.
49  *
50  * \return Whether a correction was done.
51  */
52 template <typename Iterator>
54  Iterator begin, const Iterator &end,
55  typename std::iterator_traits<Iterator>::value_type length) {
56  bool had_to_wrap = false;
57  for (; begin != end; ++begin) {
58  auto &x = *begin;
59  if (x < 0) {
60  had_to_wrap = true;
61  x += length;
62  } else if (x >= length) {
63  had_to_wrap = true;
64  x -= length;
65  }
66  }
67  return had_to_wrap;
68 }
69 
70 /**
71  * Convenience wrapper for \c std::all_of that operates on a complete container.
72  *
73  * \tparam Container Type of the container.
74  * \tparam UnaryPredicate Type of the predicate.
75  * \param c A container of elements to examine.
76  * \param p Unary predicate.
77  * \return Whether all elements in \p c return \c true when passed to \p p.
78  */
79 template <typename Container, typename UnaryPredicate>
80 inline bool all_of(Container &&c, UnaryPredicate &&p) {
81  return std::all_of(std::begin(c), std::end(c),
82  std::forward<UnaryPredicate>(p));
83 }
84 
85 /**
86  * Convenience wrapper for \c std::for_each that operates on a complete
87  * container.
88  *
89  * \tparam Container Type of the container.
90  * \tparam UnaryFunction Type of the function.
91  * \param c A container of elements on which to perform the function f
92  * \param f A function to apply on all elements of the container c
93  * \return The function that was applied to all elements.
94  */
95 template <typename Container, typename UnaryFunction>
96 inline UnaryFunction for_each(Container &&c, UnaryFunction &&f) {
97  return std::for_each(std::begin(c), std::end(c),
98  std::forward<UnaryFunction>(f));
99 }
100 
101 } // namespace smash
102 
103 #endif // SRC_INCLUDE_SMASH_ALGORITHMS_H_
constexpr int p
Proton.
Definition: action.h:24
UnaryFunction for_each(Container &&c, UnaryFunction &&f)
Convenience wrapper for std::for_each that operates on a complete container.
Definition: algorithms.h:96
static bool enforce_periodic_boundaries(Iterator begin, const Iterator &end, typename std::iterator_traits< Iterator >::value_type length)
Enforces periodic boundaries on the given collection of values.
Definition: algorithms.h:53
bool all_of(Container &&c, UnaryPredicate &&p)
Convenience wrapper for std::all_of that operates on a complete container.
Definition: algorithms.h:80