DeSiGNAR  0.5a
Data Structures General Library
containeralgorithms.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGCONTAINERALGORITHMS_H
26 # define DSGCONTAINERALGORITHMS_H
27 
28 # include <italgorithms.H>
29 
30 namespace Designar
31 {
32  template<class ContainerType, typename T>
34  {
35  ContainerType & me()
36  {
37  return *static_cast<ContainerType *>(this);
38  }
39 
40  const ContainerType & const_me() const
41  {
42  return *static_cast<const ContainerType *>(this);
43  }
44 
45  public:
46  T * nth_ptr(nat_t i)
47  {
48  return nth_ptr_it<T>(me().begin(), me().end(), i);
49  }
50 
51  T & nth(nat_t i)
52  {
53  return nth_it<T>(me().begin(), me().end(), i);
54  }
55 
56  const T & nth(nat_t i) const
57  {
58  return nth_it(const_me().begin(), const_me().end(), i);
59  }
60 
61  template <class Op>
62  void for_each(Op & op) const
63  {
64  for_each_it(const_me().begin(), const_me().end(), op);
65  }
66 
67  template <class Op>
68  void for_each(Op && op = Op()) const
69  {
70  for_each_it(const_me().begin(), const_me().end(), std::forward<Op>(op));
71  }
72 
73  template <class ContainerRet, class Pred>
74  ContainerRet filter(Pred & pred) const
75  {
76  return filter_it<ContainerRet>(const_me().begin(), const_me().end(),
77  pred);
78  }
79 
80  template <class ContainerRet = SLList<T>, class Pred>
81  ContainerRet filter(Pred && pred = Pred()) const
82  {
83  return filter_it<ContainerRet>(const_me().begin(), const_me().end(),
84  std::forward<Pred>(pred));
85  }
86 
87  template <typename RetT = T, class ContainerRet = SLList<RetT>, class Op>
88  ContainerRet map(Op & op) const
89  {
90  return map_it<ContainerRet>(const_me().begin(), const_me().end(), op);
91  }
92 
93  template <class RetT = T, class ContainerRet = SLList<RetT>, class Op>
94  ContainerRet map(Op && op = Op()) const
95  {
96  return map_it<ContainerRet>(const_me().begin(), const_me().end(),
97  std::forward<Op>(op));
98  }
99 
100  template <class RetT = T, class ContainerRet = SLList<RetT>,
101  class Op, class Pred>
102  ContainerRet map_if(Op & op, Pred & pred) const
103  {
104  return map_if_it<ContainerRet>(const_me().begin(), const_me().end(),
105  op, pred);
106  }
107 
108  template <class RetT = T, class ContainerRet = SLList<RetT>,
109  class Op, class Pred>
110  ContainerRet map_if(Op & op, Pred && pred = Pred()) const
111  {
112  return map_if_it<ContainerRet>(const_me().begin(), const_me().end(),
113  op, std::forward<Pred>(pred));
114  }
115 
116  template <class RetT = T, class ContainerRet = SLList<RetT>,
117  class Op, class Pred>
118  ContainerRet map_if(Op && op, Pred & pred) const
119  {
120  return map_if_it<ContainerRet>(const_me().begin(), const_me().end(),
121  std::forward<Op>(op), pred);
122  }
123 
124  template <typename RetT = T, class ContainerRet = SLList<RetT>,
125  class Op, class Pred>
126  ContainerRet map_if(Op && op = Op(), Pred && pred = Pred()) const
127  {
128  return map_if_it<ContainerRet>(const_me().begin(), const_me().end(),
129  std::forward<Op>(op),
130  std::forward<Pred>(pred));
131  }
132 
133  template <typename RetT = T, class Op>
134  RetT fold(const RetT & init_val, Op & op) const
135  {
136  return fold_it<RetT>(const_me().begin(), const_me().end(), init_val, op);
137  }
138 
139  template <typename RetT = T, class Op>
140  RetT fold(const RetT & init_val, Op && op = Op()) const
141  {
142  return fold_it<RetT>(const_me().begin(), const_me().end(), init_val,
143  std::forward<Op>(op));
144  }
145 
146  template <typename RetT = T, class Op>
147  RetT fold(RetT && init_val, Op & op) const
148  {
149  return fold_it<RetT>(const_me().begin(), const_me().end(),
150  std::forward<RetT>(init_val), op);
151  }
152 
153  template <typename RetT = T, class Op>
154  RetT fold(RetT && init_val, Op && op = Op()) const
155  {
156  return fold_it<RetT>(const_me().begin(), const_me().end(),
157  std::forward<RetT>(init_val), std::forward<Op>(op));
158  }
159 
160  template <class Pred>
161  bool all(Pred & pred) const
162  {
163  return all_it(const_me().begin(), const_me().end(), pred);
164  }
165 
166  template <class Pred>
167  bool all(Pred && pred = Pred()) const
168  {
169  return all_it(const_me().begin(), const_me().end(),
170  std::forward<Pred>(pred));
171  }
172 
173  template <class Pred>
174  bool exists(Pred & pred) const
175  {
176  return exists_it(const_me().begin(), const_me().end(), pred);
177  }
178 
179  template <class Pred>
180  bool exists(Pred && pred = Pred()) const
181  {
182  return exists_it(const_me().begin(), const_me().end(),
183  std::forward<Pred>(pred));
184  }
185 
186  template <class Pred>
187  bool none(Pred & pred) const
188  {
189  return none_it(const_me().begin(), const_me().end(), pred);
190  }
191 
192  template <class Pred>
193  bool none(Pred && pred = Pred()) const
194  {
195  return none_it(const_me().begin(), const_me().end(),
196  std::forward<Pred>(pred));
197  }
198 
199  template <class Pred>
200  T * search_ptr(Pred & pred) const
201  {
202  return search_ptr_it<T>(const_me().begin(), const_me().end(), pred);
203  }
204 
205  template <class Pred>
206  T * search_ptr(Pred && pred = Pred()) const
207  {
208  return search_ptr_it<T>(const_me().begin(), const_me().end(),
209  std::forward<Pred>(pred));
210  }
211 
212  template <class Pred>
213  bool remove_first_if(Pred & pred)
214  {
215  return remove_first_if_it(me().begin(), me().end(), pred);
216  }
217 
218  template <class Pred>
219  bool remove_first_if(Pred && pred = Pred())
220  {
221  return remove_first_if_it(me().begin(), me().end(),
222  std::forward<Pred>(pred));
223  }
224 
225  template <class Pred>
226  void remove_if(Pred & pred)
227  {
228  remove_if_it(me().begin(), me().end(), pred);
229  }
230 
231  template <class Pred>
232  void remove_if(Pred && pred = Pred())
233  {
234  remove_if_it(me().begin(), me().end(), std::forward<Pred>(pred));
235  }
236 
237  template <class ContainerType2 = ContainerType, class Eq>
238  bool equal(const ContainerType2 & c, Eq & eq) const
239  {
240  return equal_it(const_me().begin(), const_me().end(),
241  c.begin(), c.end(), eq);
242  }
243 
244  template <class ContainerType2 = ContainerType, class Eq = std::equal_to<T>>
245  bool equal(const ContainerType2 & c, Eq && eq = Eq()) const
246  {
247  return equal_it(const_me().begin(), const_me().end(),
248  c.begin(), c.end(), std::forward<Eq>(eq));
249  }
250 
251  template <class Cmp>
252  bool is_sorted(Cmp & cmp) const
253  {
254  return is_sorted_it(const_me().begin(), const_me().end(), cmp);
255  }
256 
257  template <class Cmp = std::less<T>>
258  bool is_sorted(Cmp && cmp = Cmp()) const
259  {
260  return is_sorted_it(const_me().begin(), const_me().end(),
261  std::forward<Cmp>(cmp));
262  }
263 
264  template <class ContainerType2 = ContainerType>
266  zip(const ContainerType2 & c) const
267  {
268  using T2 = typename ContainerType2::KeyType;
269 
270  return zip_it<T, T2>(const_me().begin(), const_me().end(),
271  c.begin(), c.end());
272  }
273 
274  template <class ContainerType2 = ContainerType>
276  zip_eq(const ContainerType2 & c) const
277  {
278  using T2 = typename ContainerType2::KeyType;
279 
280  return zip_eq_it<T, T2>(const_me().begin(), const_me().end(),
281  c.begin(), c.end());
282  }
283 
284  template <class ContainerType2 = ContainerType>
286  zip_left(const ContainerType2 & c) const
287  {
288  using T2 = typename ContainerType2::KeyType;
289 
290  return zip_left_it<T, T2>(const_me().begin(), const_me().end(),
291  c.begin(), c.end());
292  }
293 
294  template <class ContainerType2 = ContainerType>
296  zip_right(const ContainerType2 & c) const
297  {
298  using T2 = typename ContainerType2::KeyType;
299 
300  return zip_right_it<T, T2>(const_me().begin(), const_me().end(),
301  c.begin(), c.end());
302  }
303 
305  {
306  return to_array_it<T>(const_me().begin(), const_me().end());
307  }
308 
310  {
311  return to_list_it<T>(const_me().begin(), const_me().end());
312  }
313  };
314 
315 } // end namespace Designar
316 
317 # endif // DSGCONTAINERALGORITHMS_H
SLList< T > to_list() const
Definition: containeralgorithms.H:309
T * search_ptr(Pred &&pred=Pred()) const
Definition: containeralgorithms.H:206
RetT fold(const RetT &init_val, Op &&op=Op()) const
Definition: containeralgorithms.H:140
SLList< std::pair< T, typename ContainerType2::KeyType > > zip(const ContainerType2 &c) const
Definition: containeralgorithms.H:266
RetT fold(RetT &&init_val, Op &op) const
Definition: containeralgorithms.H:147
bool none(Pred &&pred=Pred()) const
Definition: containeralgorithms.H:193
bool none_it(const It &, const It &, Pred &)
Definition: italgorithms.H:325
T * nth_ptr(nat_t i)
Definition: containeralgorithms.H:46
bool equal(const ContainerType2 &c, Eq &eq) const
Definition: containeralgorithms.H:238
bool exists(Pred &pred) const
Definition: containeralgorithms.H:174
bool none(Pred &pred) const
Definition: containeralgorithms.H:187
T & nth(nat_t i)
Definition: containeralgorithms.H:51
void for_each(Op &op) const
Definition: containeralgorithms.H:62
void remove_if(Pred &pred)
Definition: containeralgorithms.H:226
bool exists(Pred &&pred=Pred()) const
Definition: containeralgorithms.H:180
bool equal_it(const It1 &, const It1 &, const It2 &, const It2 &, Eq &)
Definition: italgorithms.H:391
bool equal(const ContainerType2 &c, Eq &&eq=Eq()) const
Definition: containeralgorithms.H:245
ContainerRet map_if(Op &op, Pred &&pred=Pred()) const
Definition: containeralgorithms.H:110
RetT fold(RetT &&init_val, Op &&op=Op()) const
Definition: containeralgorithms.H:154
RetT & nth_it(const It &, const It &, nat_t)
Definition: italgorithms.H:172
RetT fold(const RetT &init_val, Op &op) const
Definition: containeralgorithms.H:134
bool is_sorted(Cmp &&cmp=Cmp()) const
Definition: containeralgorithms.H:258
SLList< std::pair< T, typename ContainerType2::KeyType > > zip_eq(const ContainerType2 &c) const
Definition: containeralgorithms.H:276
bool exists_it(const It &, const It &, Pred &)
Definition: italgorithms.H:310
bool remove_first_if(Pred &pred)
Definition: containeralgorithms.H:213
Definition: array.H:375
ContainerRet map_if(Op &&op=Op(), Pred &&pred=Pred()) const
Definition: containeralgorithms.H:126
ContainerRet map_if(Op &&op, Pred &pred) const
Definition: containeralgorithms.H:118
ContainerRet map(Op &&op=Op()) const
Definition: containeralgorithms.H:94
ContainerRet map(Op &op) const
Definition: containeralgorithms.H:88
void for_each_it(const It &, const It &, Op &)
Definition: italgorithms.H:183
bool is_sorted_it(const It &, const It &, Cmp &)
Definition: italgorithms.H:412
Definition: italgorithms.H:33
bool all(Pred &&pred=Pred()) const
Definition: containeralgorithms.H:167
Definition: containeralgorithms.H:33
ContainerRet filter(Pred &&pred=Pred()) const
Definition: containeralgorithms.H:81
void remove_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:375
SLList< std::pair< T, typename ContainerType2::KeyType > > zip_left(const ContainerType2 &c) const
Definition: containeralgorithms.H:286
bool remove_first_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:356
ContainerRet map_if(Op &op, Pred &pred) const
Definition: containeralgorithms.H:102
void for_each(Op &&op=Op()) const
Definition: containeralgorithms.H:68
Definition: array.H:32
ContainerRet filter(Pred &pred) const
Definition: containeralgorithms.H:74
T * search_ptr(Pred &pred) const
Definition: containeralgorithms.H:200
unsigned long int nat_t
Definition: types.H:50
bool is_sorted(Cmp &cmp) const
Definition: containeralgorithms.H:252
bool remove_first_if(Pred &&pred=Pred())
Definition: containeralgorithms.H:219
SLList< std::pair< T, typename ContainerType2::KeyType > > zip_right(const ContainerType2 &c) const
Definition: containeralgorithms.H:296
bool all(Pred &pred) const
Definition: containeralgorithms.H:161
void remove_if(Pred &&pred=Pred())
Definition: containeralgorithms.H:232
const T & nth(nat_t i) const
Definition: containeralgorithms.H:56
DynArray< T > to_array() const
Definition: containeralgorithms.H:304
bool all_it(const It &, const It &, Pred &)
Definition: italgorithms.H:295