ATLAS Offline Software
Loading...
Searching...
No Matches
minmax_transformed_element.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
21
22
23#ifndef CXXUTILS_MINMAX_TRANSFORMED_ELEMENT_H
24#define CXXUTILS_MINMAX_TRANSFORMED_ELEMENT_H
25
26
27#include <ranges>
28#include <utility>
29#include <cstdlib>
30
31
32namespace CxxUtils {
33
34
48template <class RANGE, class FUNC>
49inline
50auto min_transformed_element (RANGE&& r, FUNC&& f)
51{
52 if (std::ranges::empty (r)) std::abort();
53 auto it = std::ranges::begin(r);
54 auto itmin = it;
55 auto val = f(*it);
56 for (++it; it != std::ranges::end(r); ++it) {
57 auto val2 = f(*it);
58 if (val2 < val) {
59 val = val2;
60 itmin = it;
61 }
62 }
63 return std::make_pair (val, itmin);
64}
65
66
80template <class RANGE, class FUNC>
81inline
82auto max_transformed_element (RANGE&& r, FUNC&& f)
83{
84 if (std::ranges::empty (r)) std::abort();
85 auto it = std::ranges::begin(r);
86 auto itmax = it;
87 auto val = f(*it);
88 for (++it; it != std::ranges::end(r); ++it) {
89 auto val2 = f(*it);
90 if (val2 > val) {
91 val = val2;
92 itmax = it;
93 }
94 }
95 return std::make_pair (val, itmax);
96}
97
98
99} // namespace CxxUtils
100
101
102#endif // not CXXUTILS_MINMAX_TRANSFORMED_ELEMENT_H
int r
Definition globals.cxx:22
auto min_transformed_element(RANGE &&r, FUNC &&f)
Find the minimum transformed element in a range.
auto max_transformed_element(RANGE &&r, FUNC &&f)
Find the maximum transformed element in a range.