LeechCraft  0.6.70-9312-g4cc613a2df
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
prelude.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <type_traits>
33 #include <iterator>
34 #include <QPair>
35 #include <QStringList>
36 #include <boost/optional.hpp>
37 #include "oldcppkludges.h"
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
43  template<typename T>
44  struct WrapType
45  {
46  using type = T;
47  };
48 
49  template<typename T>
50  using WrapType_t = typename WrapType<T>::type;
51 
52  template<>
53  struct WrapType<QList<QString>>
54  {
55  using type = QStringList;
56  };
57 
58  template<typename T1, typename T2, template<typename U> class Container, typename F>
59  auto ZipWith (const Container<T1>& c1, const Container<T2>& c2, F f) -> WrapType_t<Container<std::decay_t<std::result_of_t<F (T1, T2)>>>>
60  {
62 
63  using std::begin;
64  using std::end;
65 
66  auto i1 = begin (c1), e1 = end (c1);
67  auto i2 = begin (c2), e2 = end (c2);
68  for ( ; i1 != e1 && i2 != e2; ++i1, ++i2)
69  result.push_back (f (*i1, *i2));
70  return result;
71  }
72 
73  template<typename T1, typename T2,
74  template<typename U> class Container,
75  template<typename U1, typename U2> class Pair = QPair>
76  auto Zip (const Container<T1>& c1, const Container<T2>& c2) -> Container<Pair<T1, T2>>
77  {
78  return ZipWith (c1, c2,
79  [] (const T1& t1, const T2& t2) -> Pair<T1, T2>
80  { return { t1, t2}; });
81  }
82 
83  namespace detail
84  {
85  template<typename Res, typename T>
86  void Append (Res& result, T&& val, decltype (result.push_back (std::forward<T> (val)))* = nullptr)
87  {
88  result.push_back (std::forward<T> (val));
89  }
90 
91  template<typename Res, typename T>
92  void Append (Res& result, T&& val, decltype (result.insert (std::forward<T> (val)))* = nullptr)
93  {
94  result.insert (std::forward<T> (val));
95  }
96 
97  template<typename T, typename F>
98  constexpr bool IsInvokableWithConstImpl (std::result_of_t<F (const T&)>*)
99  {
100  return true;
101  }
102 
103  template<typename T, typename F>
104  constexpr bool IsInvokableWithConstImpl (...)
105  {
106  return false;
107  }
108 
109  template<typename T, typename F>
110  constexpr bool IsInvokableWithConst ()
111  {
112  return IsInvokableWithConstImpl<std::decay_t<T>, F> (0);
113  }
114 
115  template<typename>
116  struct CountArgs
117  {
118  static const size_t ArgsCount = 0;
119  };
120 
121  template<template<typename...> class Container, typename... Args>
122  struct CountArgs<Container<Args...>>
123  {
124  static const size_t ArgsCount = sizeof... (Args);
125  };
126 
127  template<typename C>
128  constexpr bool IsSimpleContainer ()
129  {
130  return CountArgs<std::decay_t<C>>::ArgsCount == 1;
131  }
132 
133  template<typename F, typename Cont>
134  constexpr bool DoesReturnVoid ()
135  {
136  using Ret_t = decltype (Invoke (std::declval<F> (), *std::declval<Cont> ().begin ()));
137  return std::is_same<void, Ret_t>::value;
138  }
139  }
140 
141  template<
142  typename T,
143  template<typename U> class Container,
144  typename F,
145  typename = std::enable_if_t<detail::IsSimpleContainer<Container<T>> ()>,
146  typename = std::enable_if_t<!detail::DoesReturnVoid<F, Container<T>> ()>
147  >
148  auto Map (const Container<T>& c, F f)
149  {
150  WrapType_t<Container<std::decay_t<decltype (Invoke (f, *c.begin ()))>>> result;
151  for (auto&& t : c)
152  detail::Append (result, Invoke (f, t));
153  return result;
154  }
155 
156  template<
157  typename Container,
158  typename F,
159  template<typename> class ResultCont = QList,
160  typename = std::enable_if_t<!detail::IsSimpleContainer<Container> ()>,
161  typename = std::enable_if_t<!detail::DoesReturnVoid<F, Container> ()>
162  >
163  auto Map (const Container& c, F f)
164  {
165  WrapType_t<ResultCont<std::decay_t<decltype (Invoke (f, *c.begin ()))>>> cont;
166  for (auto&& t : c)
167  detail::Append (cont, Invoke (f, t));
168  return cont;
169  }
170 
171  template<
172  typename Container,
173  typename F,
174  typename = std::enable_if_t<detail::DoesReturnVoid<F, Container> ()>
175  >
176  auto Map (const Container& c, F f)
177  {
178  for (auto&& t : c)
179  Invoke (f, t);
180  }
181 
182  template<
183  typename Container,
184  typename F,
185  typename = std::enable_if_t<detail::DoesReturnVoid<F, Container> ()>
186  >
187  auto Map (Container& c, F f)
188  {
189  for (auto&& t : c)
190  Invoke (f, t);
191  }
192 
193  template<typename T, template<typename U> class Container, typename F>
194  Container<T> Filter (const Container<T>& c, F f)
195  {
196  Container<T> result;
197  for (const auto& item : c)
198  if (Invoke (f, item))
199  detail::Append (result, item);
200  return result;
201  }
202 
203  template<template<typename> class Container, typename T>
204  Container<T> Concat (const Container<Container<T>>& containers)
205  {
206  Container<T> result;
207  for (const auto& cont : containers)
208  std::copy (cont.begin (), cont.end (), std::back_inserter (result));
209  return result;
210  }
211 
212  template<template<typename...> class Container, typename... ContArgs>
213  auto Concat (const Container<ContArgs...>& containers) -> std::decay_t<decltype (*containers.begin ())>
214  {
215  std::decay_t<decltype (*containers.begin ())> result;
216  for (const auto& cont : containers)
217  std::copy (cont.begin (), cont.end (), std::back_inserter (result));
218  return result;
219  }
220 
221  template<typename Cont, typename F>
222  auto ConcatMap (Cont&& c, F&& f) -> decltype (Concat (Map (std::forward<Cont> (c), std::forward<F> (f))))
223  {
224  return Concat (Map (std::forward<Cont> (c), std::forward<F> (f)));
225  }
226 
227  template<template<typename> class Container, typename T>
228  Container<Container<T>> SplitInto (size_t numChunks, const Container<T>& container)
229  {
230  Container<Container<T>> result;
231 
232  const size_t chunkSize = container.size () / numChunks;
233  for (size_t i = 0; i < numChunks; ++i)
234  {
235  Container<T> subcont;
236  const auto start = container.begin () + chunkSize * i;
237  const auto end = start + chunkSize;
238  std::copy (start, end, std::back_inserter (subcont));
239  result.push_back (subcont);
240  }
241 
242  const auto lastStart = container.begin () + chunkSize * numChunks;
243  const auto lastEnd = container.end ();
244  std::copy (lastStart, lastEnd, std::back_inserter (result.front ()));
245 
246  return result;
247  }
248 
249  template<template<typename Pair, typename... Rest> class Cont, template<typename K, typename V> class Pair, typename K, typename V, typename KV, typename... Rest>
250  boost::optional<V> Lookup (const KV& key, const Cont<Pair<K, V>, Rest...>& cont)
251  {
252  for (const auto& pair : cont)
253  if (pair.first == key)
254  return pair.second;
255 
256  return {};
257  }
258 
259  template<typename Cont>
260  Cont Sorted (Cont&& cont)
261  {
262  std::sort (cont.begin (), cont.end ());
263  return cont;
264  }
265 
266  const auto Id = [] (const auto& t) { return t; };
267 
268  template<typename R>
269  auto ComparingBy (R r)
270  {
271  return [r] (const auto& left, const auto& right) { return Invoke (r, left) < Invoke (r, right); };
272  }
273 
274  const auto Apply = [] (const auto& t) { return t (); };
275 
276  const auto Fst = [] (const auto& pair) { return pair.first; };
277 
278  const auto Snd = [] (const auto& pair) { return pair.second; };
279 
280  template<typename F>
281  auto First (F&& f)
282  {
283  return [f = std::forward<F> (f)] (const auto& pair) { return Invoke (f, pair.first); };
284  }
285 
286  template<typename F>
287  auto Second (F&& f)
288  {
289  return [f = std::forward<F> (f)] (const auto& pair) { return Invoke (f, pair.second); };
290  }
291 
292  template<typename F>
293  auto Flip (F&& f)
294  {
295  return [f = std::move (f)] (auto&& left, auto&& right)
296  {
297  return f (std::forward<decltype (right)> (right),
298  std::forward<decltype (left)> (left));
299  };
300  }
301 }
302 }
auto First(F &&f)
Definition: prelude.h:281
auto Map(const Container< T > &c, F f)
Definition: prelude.h:148
constexpr bool IsInvokableWithConstImpl(std::result_of_t< F(const T &)> *)
Definition: prelude.h:98
const auto Id
Definition: prelude.h:266
auto Zip(const Container< T1 > &c1, const Container< T2 > &c2) -> Container< Pair< T1, T2 >>
Definition: prelude.h:76
auto ZipWith(const Container< T1 > &c1, const Container< T2 > &c2, F f) -> WrapType_t< Container< std::decay_t< std::result_of_t< F(T1, T2)>>>>
Definition: prelude.h:59
auto ComparingBy(R r)
Definition: prelude.h:269
boost::optional< V > Lookup(const KV &key, const Cont< Pair< K, V >, Rest...> &cont)
Definition: prelude.h:250
const auto Snd
Definition: prelude.h:278
static const size_t ArgsCount
Definition: prelude.h:118
Container< T > Concat(const Container< Container< T >> &containers)
Definition: prelude.h:204
auto ConcatMap(Cont &&c, F &&f) -> decltype(Concat(Map(std::forward< Cont >(c), std::forward< F >(f))))
Definition: prelude.h:222
void Append(Res &result, T &&val, decltype(result.push_back(std::forward< T >(val)))*=nullptr)
Definition: prelude.h:86
auto Flip(F &&f)
Definition: prelude.h:293
const auto Apply
Definition: prelude.h:274
Container< T > Filter(const Container< T > &c, F f)
Definition: prelude.h:194
constexpr bool IsSimpleContainer()
Definition: prelude.h:128
constexpr bool DoesReturnVoid()
Definition: prelude.h:134
Cont Sorted(Cont &&cont)
Definition: prelude.h:260
auto Second(F &&f)
Definition: prelude.h:287
const auto Fst
Definition: prelude.h:276
auto Invoke(F &&f, Args &&...args) -> decltype(std::forward< F >(f)(std::forward< Args >(args)...))
Definition: oldcppkludges.h:40
typename WrapType< T >::type WrapType_t
Definition: prelude.h:50
constexpr bool IsInvokableWithConst()
Definition: prelude.h:110
Container< Container< T > > SplitInto(size_t numChunks, const Container< T > &container)
Definition: prelude.h:228