Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
util-coll.hpp
Go to the documentation of this file.
1/*
2 UTIL-COLL.hpp - helpers and convenience shortcuts for working with collections
3
4 Copyright (C)
5 2012, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
34#ifndef UTIL_COLL_H
35#define UTIL_COLL_H
36
37#include "lib/util.hpp"
38#include "lib/meta/trait.hpp"
39
40#include <limits>
41
42
43
44namespace util {
45
48
49 namespace { // predicates to pick the right implementation
50
51 using lib::meta::Yes_t;
52 using lib::meta::No_t;
53
54 template<typename T>
63
64 template<typename T>
70
71 template<typename T>
79
80
81 template<typename COL>
82 inline void
83 __ensure_nonempty(COL const& coll)
84 {
85 if (util::isnil(coll))
86 throw lumiera::error::Logic("attempt to access the first element of an empty collection"
87 ,lumiera::error::LUMIERA_ERROR_BOTTOM_VALUE);
88 }
89 }
90
91
92
93
94 /* === specialisations for STL-like containers and Lumiera Forward Iterators === */
95
100 template <typename COLL, typename = enable_if< treat_as_STL_Container<COLL>>>
101 inline auto
102 first (COLL const& coll)
103 {
104 using lib::meta::unwrap;
105
106 __ensure_nonempty(coll);
107 return *(unwrap(coll).begin());
108 }
109
110
115 template <typename COLL, typename = enable_if< can_direct_access_Last<COLL>>>
116 inline auto
117 last (COLL const& coll)
118 {
119 using lib::meta::unwrap;
120
121 __ensure_nonempty(coll);
122 return *(unwrap(coll).rbegin());
123 }
124
125
126
130 template <typename IT, typename = enable_if<treat_as_LumieraIterator<IT>>>
131 inline auto
132 first (IT ii)
133 {
134 __ensure_nonempty(ii);
135 return *ii;
136 }
137
138
139#ifdef LIB_ITERTOOLS_H
145 template <typename IT, typename = enable_if<treat_as_LumieraIterator<IT>>>
146 inline auto
147 last (IT ii)
148 {
149 __ensure_nonempty(ii);
150 return lib::pull_last (ii);
151 }
152#endif
153
154
155
156 /* === generic container helpers === */
157
158 template<class IT>
159 inline auto
160 max (IT&& elms)
161 {
162 using Val = std::remove_reference_t<IT>::value_type;
163 Val res = std::numeric_limits<Val>::min();
164 for (auto const& elm : std::forward<IT> (elms))
165 if (elm > res)
166 res = elm;
167 return res;
168 }
169
170 template<class CON>
171 inline auto
172 max (CON const& elms)
173 {
174 using Val = std::remove_reference_t<CON>::value_type;
175 Val res = std::numeric_limits<Val>::min();
176 for (auto const& elm : elms)
177 if (elm > res)
178 res = elm;
179 return res;
180 }
181
182
183 template<class IT>
184 inline auto
185 min (IT&& elms)
186 {
187 using Val = std::remove_reference_t<IT>::value_type;
188 Val res = std::numeric_limits<Val>::max();
189 for (auto const& elm : std::forward<IT> (elms))
190 if (elm < res)
191 res = elm;
192 return res;
193 }
194
195 template<class CON>
196 inline auto
197 min (CON const& elms)
198 {
199 using Val = std::remove_reference_t<CON>::value_type;
200 Val res = std::numeric_limits<Val>::max();
201 for (auto const& elm : elms)
202 if (elm < res)
203 res = elm;
204 return res;
205 }
206
207
208
209} // namespace util
210#endif /*UTIL_COLL_H*/
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition trait.hpp:511
Trait template to detect a type usable with the STL for-each loop.
Definition trait.hpp:556
Trait template to detect a type also supporting STL-style backwards iteration.
Definition trait.hpp:620
char Yes_t
helper types to detect the overload resolution chosen by the compiler
Definition meta/util.hpp:99
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
enable_if_c< not Cond::value, T >::type disable_if
Definition meta/util.hpp:90
Unwrap< X >::Type & unwrap(X const &wrapped)
convenience shortcut: unwrapping free function.
Definition trait.hpp:240
IT::value_type pull_last(IT iter)
LumieraError< LERR_(LOGIC)> Logic
Definition error.hpp:207
void __ensure_nonempty(COL const &coll)
Definition util-coll.hpp:83
auto first(COLL const &coll)
access the first element of a STL-like container.
auto max(IT &&elms)
auto last(COLL const &coll)
access the last element of a STL-like container.
auto min(IT &&elms)
bool isnil(lib::time::Duration const &dur)
Helpers for type detection, type rewriting and metaprogramming.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...