Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

algorithm.hpp
Go to the documentation of this file.
1 //
2 // immer: immutable data structures for C++
3 // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4 //
5 // This software is distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7 //
8 
9 #pragma once
10 
11 #include <algorithm>
12 #include <numeric>
13 #include <type_traits>
14 
15 namespace immer {
16 
23 // Right now these algorithms dispatch directly to the vector
24 // implementations unconditionally. This will be changed in the
25 // future to support other kinds of containers.
26 
40 template <typename Range, typename Fn>
41 void for_each_chunk(const Range& r, Fn&& fn)
42 {
43  r.impl().for_each_chunk(std::forward<Fn>(fn));
44 }
45 
46 template <typename Iterator, typename Fn>
47 void for_each_chunk(const Iterator& first, const Iterator& last, Fn&& fn)
48 {
49  assert(&first.impl() == &last.impl());
50  first.impl().for_each_chunk(first.index(), last.index(),
51  std::forward<Fn>(fn));
52 }
53 
54 template <typename T, typename Fn>
55 void for_each_chunk(const T* first, const T* last, Fn&& fn)
56 {
57  std::forward<Fn>(fn)(first, last);
58 }
59 
74 template <typename Range, typename Fn>
75 bool for_each_chunk_p(const Range& r, Fn&& fn)
76 {
77  return r.impl().for_each_chunk_p(std::forward<Fn>(fn));
78 }
79 
80 template <typename Iterator, typename Fn>
81 bool for_each_chunk_p(const Iterator& first, const Iterator& last, Fn&& fn)
82 {
83  assert(&first.impl() == &last.impl());
84  return first.impl().for_each_chunk_p(first.index(), last.index(),
85  std::forward<Fn>(fn));
86 }
87 
88 template <typename T, typename Fn>
89 bool for_each_chunk_p(const T* first, const T* last, Fn&& fn)
90 {
91  return std::forward<Fn>(fn)(first, last);
92 }
93 
97 template <typename Range, typename T>
98 T accumulate(Range&& r, T init)
99 {
100  for_each_chunk(r, [&] (auto first, auto last) {
101  init = std::accumulate(first, last, init);
102  });
103  return init;
104 }
105 
106 template <typename Range, typename T, typename Fn>
107 T accumulate(Range&& r, T init, Fn fn)
108 {
109  for_each_chunk(r, [&] (auto first, auto last) {
110  init = std::accumulate(first, last, init, fn);
111  });
112  return init;
113 }
114 
119 template <typename Iterator, typename T>
120 T accumulate(Iterator first, Iterator last, T init)
121 {
122  for_each_chunk(first, last, [&] (auto first, auto last) {
123  init = std::accumulate(first, last, init);
124  });
125  return init;
126 }
127 
128 template <typename Iterator, typename T, typename Fn>
129 T accumulate(Iterator first, Iterator last, T init, Fn fn)
130 {
131  for_each_chunk(first, last, [&] (auto first, auto last) {
132  init = std::accumulate(first, last, init, fn);
133  });
134  return init;
135 }
136 
140 template <typename Range, typename Fn>
141 Fn&& for_each(Range&& r, Fn&& fn)
142 {
143  for_each_chunk(r, [&] (auto first, auto last) {
144  for (; first != last; ++first)
145  fn(*first);
146  });
147  return std::forward<Fn>(fn);
148 }
149 
154 template <typename Iterator, typename Fn>
155 Fn&& for_each(Iterator first, Iterator last, Fn&& fn)
156 {
157  for_each_chunk(first, last, [&] (auto first, auto last) {
158  for (; first != last; ++first)
159  fn(*first);
160  });
161  return std::forward<Fn>(fn);
162 }
163 
167 template <typename Range, typename OutIter>
168 OutIter copy(Range&& r, OutIter out)
169 {
170  for_each_chunk(r, [&] (auto first, auto last) {
171  out = std::copy(first, last, out);
172  });
173  return out;
174 }
175 
180 template <typename InIter, typename OutIter>
181 OutIter copy(InIter first, InIter last, OutIter out)
182 {
183  for_each_chunk(first, last, [&] (auto first, auto last) {
184  out = std::copy(first, last, out);
185  });
186  return out;
187 }
188 
192 template <typename Range, typename Pred>
193 bool all_of(Range&& r, Pred p)
194 {
195  return for_each_chunk_p(r, [&] (auto first, auto last) {
196  return std::all_of(first, last, p);
197  });
198 }
199 
204 template <typename Iter, typename Pred>
205 bool all_of(Iter first, Iter last, Pred p)
206 {
207  return for_each_chunk_p(first, last, [&] (auto first, auto last) {
208  return std::all_of(first, last, p);
209  });
210 }
211  // group: algorithm
213 
214 } // namespace immer
bool all_of(Range &&r, Pred p)
Definition: algorithm.hpp:193
OutIter copy(Range &&r, OutIter out)
Definition: algorithm.hpp:168
T accumulate(Iterator first, Iterator last, T init, Fn fn)
Definition: algorithm.hpp:129
void for_each_chunk(const Range &r, Fn &&fn)
Definition: algorithm.hpp:41
T accumulate(Range &&r, T init)
Definition: algorithm.hpp:98
bool all_of(Iter first, Iter last, Pred p)
Definition: algorithm.hpp:205
OutIter copy(InIter first, InIter last, OutIter out)
Definition: algorithm.hpp:181
Fn && for_each(Range &&r, Fn &&fn)
Definition: algorithm.hpp:141
bool for_each_chunk_p(const Range &r, Fn &&fn)
Definition: algorithm.hpp:75
Released under the MIT license