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#include <utility>
25
26namespace Utils {
27
28/**
29 * @brief Execute op for each pair of elements in [first, last) once.
30 *
31 * Diagonal elements are excluded. Pairs are traversed ordered, so that
32 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
33 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
34 */
35template <typename ForwardIterator, typename BinaryOp>
37 while (first != last) {
38 auto next = std::next(first);
39 std::for_each(next, last, [&](auto const &nth) { op(*first, nth); });
40 ++first;
41 }
42}
43
44/** @overload */
45template <typename ForwardRange, typename BinaryOp>
47 using std::begin;
48 using std::end;
49 for_each_pair(begin(rng), end(rng), std::forward<BinaryOp>(op));
50}
51
52/**
53 * @brief Execute op for each pair of elements between [first1, last1) and
54 * [first2, last2).
55 *
56 * Diagonal elements are *not* excluded. Pairs are traversed ordered, so that
57 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
58 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
59 */
60template <typename ForwardIterator, typename BinaryOp>
63 BinaryOp op) {
64 while (first1 != last1) {
65 std::for_each(first2, last2, [&](auto const &nth) { op(*first1, nth); });
66 ++first1;
67 }
68}
69
70/** @overload */
71template <typename ForwardRange, typename BinaryOp>
73 BinaryOp &&op) {
74 using std::begin;
75 using std::end;
76 for_each_cartesian_pair(begin(rng1), end(rng1), begin(rng2), end(rng2),
77 std::forward<BinaryOp>(op));
78}
79
80/**
81 * @brief Execute op for each pair of elements between [first1, last1) and
82 * [first2, last2) if a condition is satisfied.
83 *
84 * Diagonal elements are *not* excluded. Pairs are traversed ordered, so that
85 * for op(*it, *jt), it holds that distance(it - first) < distance(jt - first),
86 * and distance(it_n - first) < distance(it_n+1 - first) for consecutive calls.
87 */
88template <typename ForwardIterator, typename BinaryOp, typename BinaryCmp>
92 while (first1 != last1) {
93 std::for_each(first2, last2, [&](auto const &nth) {
94 if (cmp(*first1, nth)) {
95 op(*first1, nth);
96 }
97 });
98 ++first1;
99 }
100}
101
102/** @overload */
103template <typename ForwardRange, typename BinaryOp, typename BinaryCmp>
105 BinaryOp &&op, BinaryCmp cmp) {
106 using std::begin;
107 using std::end;
108 for_each_cartesian_pair_if(begin(rng1), end(rng1), begin(rng2), end(rng2),
109 std::forward<BinaryOp>(op),
110 std::forward<BinaryCmp>(cmp));
111}
112} // 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...