dune-common 3.0-git
rangeutilities.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_COMMON_RANGE_UTILITIES_HH
4#define DUNE_COMMON_RANGE_UTILITIES_HH
5
7#include <utility>
8#include <type_traits>
9#include <bitset>
10
22namespace Dune
23{
29 template <typename T,
30 typename std::enable_if<is_range<T>::value, int>::type = 0>
31 typename T::value_type
32 max_value(const T & v) {
33 using std::max;
34 typename T::value_type m;
35 for (const auto & e : v)
36 m = max(e,m);
37 return m;
38 };
39
40 template <typename T,
41 typename std::enable_if<!is_range<T>::value, int>::type = 0>
42 const T & max_value(const T & v) { return v; };
43
49 template <typename T,
50 typename std::enable_if<is_range<T>::value, int>::type = 0>
51 typename T::value_type
52 min_value(const T & v) {
53 using std::min;
54 typename T::value_type m;
55 for (const auto & e : v)
56 m = min(e,m);
57 return m;
58 };
59
60 template <typename T,
61 typename std::enable_if<!is_range<T>::value, int>::type = 0>
62 T & min_value(const T & v) { return v; };
63
69 template <typename T,
70 typename std::enable_if<is_range<T>::value, int>::type = 0>
71 bool any_true(const T & v) {
72 bool b = false;
73 for (const auto & e : v)
74 b = b or bool(e);
75 return b;
76 };
77
78 template <typename T,
79 typename std::enable_if<!is_range<T>::value, int>::type = 0>
80 bool any_true(const T & v) { return v; };
81
82 template<std::size_t N>
83 bool any_true(const std::bitset<N> & b)
84 {
85 return b.any();
86 }
87
93 template <typename T,
94 typename std::enable_if<is_range<T>::value, int>::type = 0>
95 bool all_true(const T & v) {
96 bool b = true;
97 for (const auto & e : v)
98 b = b and bool(e);
99 return b;
100 };
101
102 template <typename T,
103 typename std::enable_if<!is_range<T>::value, int>::type = 0>
104 bool all_true(const T & v) { return v; };
105
106 template<std::size_t N>
107 bool all_true(const std::bitset<N> & b)
108 {
109 return b.all();
110 }
111
112}
113
114#endif // DUNE_COMMON_RANGE_UTILITIES_HH
Traits for type conversions and type information.
bool any_true(const T &v)
similar to std::bitset<N>::any() return true, if any entries is true
Definition rangeutilities.hh:71
bool all_true(const T &v)
similar to std::bitset<N>::all() return true, if any entries is true
Definition rangeutilities.hh:95
T::value_type min_value(const T &v)
compute the minimum value over a range
Definition rangeutilities.hh:52
T::value_type max_value(const T &v)
compute the maximum value over a range
Definition rangeutilities.hh:32
Dune namespace.
Definition alignment.hh:11