ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
for_each_pair.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <algorithm>
23#include <iterator>
24
25namespace Utils {
26
27/**
28 * @brief Execute op for each pair of elements in [first, last) once.
29 *
30 * Diagonal elements are excluded. Pairs are traversed ordered, so that
31 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
32 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
33 */
34template <typename ForwardIterator, typename BinaryOp>
36 while (first != last) {
37 auto next = std::next(first);
38 std::for_each(next, last, [&](auto const &nth) { op(*first, nth); });
39 ++first;
40 }
41}
42
43/** @overload */
44template <typename ForwardRange, typename BinaryOp>
46 using std::begin;
47 using std::end;
48 for_each_pair(begin(rng), end(rng), std::forward<BinaryOp>(op));
49}
50
51/**
52 * @brief Execute op for each pair of elements between [first1, last1) and
53 * [first2, last2).
54 *
55 * Diagonal elements are *not* excluded. Pairs are traversed ordered, so that
56 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
57 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
58 */
59template <typename ForwardIterator, typename BinaryOp>
62 BinaryOp op) {
63 while (first1 != last1) {
64 std::for_each(first2, last2, [&](auto const &nth) { op(*first1, nth); });
65 ++first1;
66 }
67}
68
69/** @overload */
70template <typename ForwardRange, typename BinaryOp>
72 BinaryOp &&op) {
73 using std::begin;
74 using std::end;
75 for_each_cartesian_pair(begin(rng1), end(rng1), begin(rng2), end(rng2),
76 std::forward<BinaryOp>(op));
77}
78
79/**
80 * @brief Execute op for each pair of elements between [first1, last1) and
81 * [first2, last2) if a condition is satisfied.
82 *
83 * Diagonal elements are *not* excluded. Pairs are traversed ordered, so that
84 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
85 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
86 */
87template <typename ForwardIterator, typename BinaryOp, typename BinaryCmp>
91 while (first1 != last1) {
92 std::for_each(first2, last2, [&](auto const &nth) {
93 if (cmp(*first1, nth)) {
94 op(*first1, nth);
95 }
96 });
97 ++first1;
98 }
99}
100
101/** @overload */
102template <typename ForwardRange, typename BinaryOp, typename BinaryCmp>
105 using std::begin;
106 using std::end;
107 for_each_cartesian_pair_if(begin(rng1), end(rng1), begin(rng2), end(rng2),
108 std::forward<BinaryOp>(op),
109 std::forward<BinaryCmp>(cmp));
110}
111} // namespace Utils
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
void for_each_pair(ForwardIterator first, ForwardIterator last, BinaryOp op)
Execute op for each pair of elements in [first, last) once.
void for_each_cartesian_pair(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryOp op)
Execute op for each pair of elements between [first1, last1) and [first2, last2).
void for_each_cartesian_pair_if(ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryOp op, BinaryCmp cmp)
Execute op for each pair of elements between [first1, last1) and [first2, last2) if a condition is sa...