Lumiera  0.pre.03
»edit your freedom«
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 
44 namespace util {
45 
47  using lib::meta::disable_if;
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>
56  {
57  typedef typename lib::meta::Unwrap<T>::Type TaT;
58 
61  };
62  };
63 
64  template<typename T>
66  {
68  };
69  };
70 
71  template<typename T>
73  {
74  typedef typename lib::meta::Unwrap<T>::Type TaT;
75 
77  };
78  };
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
140 
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 = typename 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 = typename 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 = typename 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 = typename 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 with the STL for-each loop.
Definition: trait.hpp:555
Trait template to detect a type also supporting STL-style backwards iteration.
Definition: trait.hpp:619
auto first(COLL const &coll)
access the first element of a STL-like container.
Definition: util-coll.hpp:102
auto last(COLL const &coll)
access the last element of a STL-like container.
Definition: util-coll.hpp:117
typename enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition: meta/util.hpp:83
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition: trait.hpp:510
char Yes_t
helper types to detect the overload resolution chosen by the compiler
Definition: meta/util.hpp:95
Helpers for type detection, type rewriting and metaprogramming.