DeSiGNAR  0.5a
Data Structures General Library
italgorithms.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 DSGITALGORITHMS_H
26 # define DSGITALGORITHMS_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
32  template <typename T> class DynArray;
33  template <typename T> class SLList;
34 
35  template <typename RetT, class It>
36  RetT * nth_ptr_it(const It &, const It &, nat_t);
37 
38  template <typename RetT, class It>
39  RetT & nth_it(const It &, const It &, nat_t);
40 
41  template <class It, class Op>
42  void for_each_it(const It &, const It &, Op &);
43 
44  template <class It, class Op>
45  void for_each_it(const It &, const It &, Op && op = Op());
46 
47  template <class It, class ContainerRet, class Pred>
48  ContainerRet filter_it(const It &, const It &, Pred &);
49 
50  template <class ContainerRet, class It, class Pred>
51  ContainerRet filter_it(const It &, const It &, Pred && pred = Pred());
52 
53  template <class ContainerRet, class It, class Op>
54  ContainerRet map_it(const It &, const It &, Op &);
55 
56  template <class ContainerRet, class It, class Op>
57  ContainerRet map_it(const It &, const It &, Op && op = Op());
58 
59  template <class ContainerRet, class It, class Op, class Pred>
60  ContainerRet map_if_it(const It &, const It &, Op &, Pred &);
61 
62  template <class ContainerRet, class It, class Op, class Pred>
63  ContainerRet map_if_it(const It &, const It &, Op &, Pred && pred = Pred());
64 
65  template <class ContainerRet, class It, class Op, class Pred>
66  ContainerRet map_if_it(const It &, const It &, Op &&, Pred &);
67 
68  template <class ContainerRet, class It, class Op, class Pred>
69  ContainerRet map_if_it(const It &, const It &, Op && op = Op(),
70  Pred && pred = Pred());
71 
72  template <typename RetT, class It, class Pred>
73  RetT * search_ptr_it(const It &, const It &, Pred &);
74 
75  template <typename RetT, class It, class Pred>
76  RetT * search_ptr_it(const It &, const It &, Pred && pred = Pred());
77 
78  template <class It, class Pred>
79  bool all_it(const It &, const It &, Pred &);
80 
81  template <class It, class Pred>
82  bool all_it(const It &, const It &, Pred && pred = Pred());
83 
84  template <class It, class Pred>
85  bool exists_it(const It &, const It &, Pred &);
86 
87  template <class It, class Pred>
88  bool exists_it(const It &, const It &, Pred && pred = Pred());
89 
90  template <class It, class Pred>
91  bool none_it(const It &, const It &, Pred &);
92 
93  template <class It, class Pred>
94  bool none_it(const It &, const It &, Pred && pred = Pred());
95 
96  template <typename RetT, class It, class Op>
97  RetT fold_it(const It &, const It &, const RetT &, Op &);
98 
99  template <typename RetT, class It, class Op>
100  RetT fold_it(const It &, const It &, const RetT &, Op && op = Op());
101 
102  template <typename RetT, class It, class Op>
103  RetT fold_it(const It &, const It &, RetT &&, Op &);
104 
105  template <typename RetT, class It, class Op>
106  RetT fold_it(const It &, const It &, RetT &&, Op && op = Op());
107 
108  template <class It, class Pred>
109  bool remove_first_if_it(const It &, const It &, Pred &);
110 
111  template <class It, class Pred>
112  bool remove_first_if_it(const It &, const It &, Pred && pred = Pred());
113 
114  template <class It, class Pred>
115  void remove_if_it(const It &, const It &, Pred &);
116 
117  template <class It, class Pred>
118  void remove_if_it(const It &, const It &, Pred && pred = Pred());
119 
120  template <class It1, class It2, class Eq>
121  bool equal_it(const It1 &, const It1 &, const It2 &, const It2 &, Eq &);
122 
123  template <class It1, class It2, class Eq>
124  bool equal_it(const It1 &, const It1 &,
125  const It2 &, const It2 &, Eq && eq = Eq());
126 
127  template <class It, class Cmp>
128  bool is_sorted_it(const It &, const It &, Cmp &);
129 
130  template <class It, class Cmp>
131  bool is_sorted_it(const It &, const It &, Cmp && cmp = Cmp());
132 
133  template <typename T1, typename T2, class It1, class It2>
135  zip_it(const It1 &, const It1 &, const It2 &, const It2 &);
136 
137  template <typename T1, typename T2, class It1, class It2>
139  zip_eq_it(const It1 &, const It1 &, const It2 &, const It2 &);
140 
141  template <typename T1, typename T2, class It1, class It2>
143  zip_left_it(const It1 &, const It1 &, const It2 &, const It2 &);
144 
145  template <typename T1, typename T2, class It1, class It2>
147  zip_right_it(const It1 &, const It1 &, const It2 &, const It2 &);
148 
149  template <class ContainerType, class It>
150  ContainerType to_container(const It &, const It &);
151 
152  template <typename T, class It>
153  SLList<T> to_list_it(const It &, const It &);
154 
155  template <typename T, class It>
156  DynArray<T> to_array_it(const It &, const It &);
157 
158  template <typename RetT, class It>
159  RetT * nth_ptr_it(const It & a, const It & b, nat_t pos)
160  {
161  for (It i = a; i != b; ++i)
162  {
163  if (pos == 0)
164  return &*i;
165  --pos;
166  }
167 
168  return nullptr;
169  }
170 
171  template <typename RetT, class It>
172  RetT & nth_it(const It & a, const It & b, nat_t pos)
173  {
174  RetT * ret_val = nth_ptr_it<RetT, It>(a, b, pos);
175 
176  if (ret_val == nullptr)
177  throw std::overflow_error("Index is to large");
178 
179  return *ret_val;
180  }
181 
182  template <class It, class Op>
183  void for_each_it(const It & a, const It & b, Op & op)
184  {
185  for (It i = a; i != b; ++i)
186  op(*i);
187  }
188 
189  template <class It, class Op>
190  void for_each_it(const It & a, const It & b, Op && op)
191  {
192  for_each_it<It, Op>(a, b, op);
193  }
194 
195  template <class ContainerRet, class It, class Pred>
196  ContainerRet filter_it(const It & a, const It & b, Pred & pred)
197  {
198  ContainerRet ret;
199 
200  for (It i = a; i != b; ++i)
201  if (pred(*i))
202  ret.append(*i);
203 
204  return ret;
205  }
206 
207  template <class ContainerRet, class It, class Pred>
208  ContainerRet filter_it(const It & a, const It & b, Pred && pred)
209  {
210  return filter_it<ContainerRet, It, Pred>(a, b, pred);
211  }
212 
213  template <class ContainerRet, class It, class Op>
214  ContainerRet map_it(const It & a, const It & b, Op & op)
215  {
216  ContainerRet ret;
217 
218  for (It i = a; i != b; ++i)
219  ret.append(op(*i));
220 
221  return ret;
222  }
223 
224  template <class ContainerRet, class It, class Op>
225  ContainerRet map_it(const It & a, const It & b, Op && op)
226  {
227  return map_it<ContainerRet, It, Op>(a, b, op);
228  }
229 
230  template <class ContainerRet, class It, class Op, class Pred>
231  ContainerRet map_if_it(const It & a, const It & b, Op & op, Pred & pred)
232  {
233  ContainerRet ret;
234 
235  for (It i = a; i != b; ++i)
236  if (pred(*i))
237  ret.append(op(*i));
238 
239  return ret;
240  }
241 
242  template <class ContainerRet, class It, class Op, class Pred>
243  ContainerRet map_if_it(const It & a, const It & b, Op & op, Pred && pred)
244  {
245  return map_if_it<ContainerRet, It, Op, Pred>(a, b, op, pred);
246  }
247 
248  template <class ContainerRet, class It, class Op, class Pred>
249  ContainerRet map_if_it(const It & a, const It & b, Op && op, Pred & pred)
250  {
251  return map_if_it<ContainerRet, It, Op, Pred>(a, b, op, pred);
252  }
253 
254  template <class ContainerRet, class It, class Op, class Pred>
255  ContainerRet map_if_it(const It & a, const It & b, Op && op, Pred && pred)
256  {
257  return map_if_it<ContainerRet, It, Op, Pred>(a, b, op, pred);
258  }
259 
260  template <typename RetT, class It, class Op>
261  RetT fold_it(const It & a, const It & b, const RetT & init_value, Op & op)
262  {
263  RetT ret_val = init_value;
264 
265  for (It i = a; i != b; ++i)
266  ret_val = op(*i, ret_val);
267 
268  return ret_val;
269  }
270 
271  template <typename RetT, class It, class Op>
272  RetT fold_it(const It & a, const It & b, const RetT & init_value, Op && op)
273  {
274  return fold_it<RetT, It, Op>(a, b, init_value, op);
275  }
276 
277  template <typename RetT, class It, class Op>
278  RetT fold_it(const It & a, const It & b, RetT && init_value, Op & op)
279  {
280  RetT ret_val = std::move(init_value);
281 
282  for (It i = a; i != b; ++i)
283  ret_val = op(*i, ret_val);
284 
285  return ret_val;
286  }
287 
288  template <typename RetT, class It, class Op>
289  RetT fold_it(const It & a, const It & b, RetT && init_value, Op && op)
290  {
291  return fold_it<RetT, It, Op>(a, b, std::forward<RetT>(init_value), op);
292  }
293 
294  template <class It, class Pred>
295  bool all_it(const It & a, const It & b, Pred & pred)
296  {
297  for (It i = a; i != b; ++i)
298  if (not pred(*i))
299  return false;
300  return true;
301  }
302 
303  template <class It, class Pred>
304  bool all_it(const It & a, const It & b, Pred && pred)
305  {
306  return all_it<It, Pred>(a, b, pred);
307  }
308 
309  template <class It, class Pred>
310  bool exists_it(const It & a, const It & b, Pred & pred)
311  {
312  for (It i = a; i != b; ++i)
313  if (pred(*i))
314  return true;
315  return false;
316  }
317 
318  template <class It, class Pred>
319  bool exists_it(const It & a, const It & b, Pred && pred)
320  {
321  return exists_it<It, Pred>(a, b, pred);
322  }
323 
324  template <class It, class Pred>
325  bool none_it(const It & a, const It & b, Pred & pred)
326  {
327  for (It i = a; i != b; ++i)
328  if (pred(*i))
329  return false;
330  return true;
331  }
332 
333  template <class It, class Pred>
334  bool none_it(const It & a, const It & b, Pred && pred)
335  {
336  return none_it<It, Pred>(a, b, pred);
337  }
338 
339  template <typename RetT, class It, class Pred>
340  RetT * search_ptr_it(const It & a, const It & b, Pred & pred)
341  {
342  for (It i = a; i != b; ++i)
343  if (pred(*i))
344  return &*i;
345 
346  return nullptr;
347  }
348 
349  template <typename RetT, class It, class Pred>
350  RetT * search_ptr_it(const It & a, const It & b, Pred && pred)
351  {
352  return search_ptr_it<RetT, It, Pred>(a, b, pred);
353  }
354 
355  template <class It, class Pred>
356  bool remove_first_if_it(const It & a, const It & b, Pred & pred)
357  {
358  for (It i = a; i != b; ++i)
359  if (pred(*i))
360  {
361  i.del();
362  return true;
363  }
364 
365  return false;
366  }
367 
368  template <class It, class Pred>
369  bool remove_first_if_it(const It & a, const It & b, Pred && pred)
370  {
371  return remove_first_if_it<It, Pred>(a, b, pred);
372  }
373 
374  template <class It, class Pred>
375  void remove_if_it(const It & a, const It & b, Pred & pred)
376  {
377  for (It i = a; i != b; )
378  if (pred(*i))
379  i.del();
380  else
381  ++i;
382  }
383 
384  template <class It, class Pred>
385  void remove_if_it(const It & a, const It & b, Pred && pred)
386  {
387  remove_if_it<It, Pred>(a, b, pred);
388  }
389 
390  template <class It1, class It2, class Eq>
391  bool equal_it(const It1 & a1, const It1 & b1,
392  const It2 & a2, const It2 & b2, Eq & eq)
393  {
394  It1 i1 = a1;
395  It2 i2 = a2;
396 
397  for ( ; i1 != b1 and i2 != b2; ++i1, ++i2)
398  if (not eq(*i1, *i2))
399  return false;
400 
401  return (i1 == b1 and i2 == b2);
402  }
403 
404  template <class It1, class It2, class Eq>
405  bool equal_it(const It1 & a1, const It1 & b1,
406  const It2 & a2, const It2 & b2, Eq && eq)
407  {
408  return equal_it<It1, It2, Eq>(a1, b1, a2, b2, eq);
409  }
410 
411  template <class It, class Cmp>
412  bool is_sorted_it(const It & a, const It & b, Cmp & cmp)
413  {
414  if (a == b)
415  return true;
416 
417  It i = a;
418  It j = a;
419  ++j;
420 
421  for ( ; j != b; ++i, ++j)
422  if (cmp(*j, *i))
423  return false;
424 
425  return true;
426  }
427 
428  template <class It, class Cmp>
429  bool is_sorted_it(const It & a, const It & b, Cmp && cmp)
430  {
431  return is_sorted_it<It, Cmp>(a, b, cmp);
432  }
433 
434  template <typename T1, typename T2, class It1, class It2>
436  zip_it(const It1 & a1, const It1 & b1, const It2 & a2, const It2 & b2)
437  {
439 
440  It1 i1 = a1;
441  It2 i2 = a2;
442 
443  for ( ; i1 != b1 and i2 != b2; ++i1, ++i2)
444  ret.append(std::make_pair(*i1, *i2));
445 
446  return ret;
447  }
448 
449  template <typename T1, typename T2, class It1, class It2>
451  zip_eq_it(const It1 & a1, const It1 & b1, const It2 & a2, const It2 & b2)
452  {
454 
455  It1 i1 = a1;
456  It2 i2 = a2;
457 
458  for ( ; i1 != b1 and i2 != b2; ++i1, ++i2)
459  ret.append(std::make_pair(*i1, *i2));
460 
461  if (i1 != b1 or i2 != b2)
462  throw std::length_error("Container sizes mismatch");
463 
464  return ret;
465  }
466 
467  template <typename T1, typename T2, class It1, class It2>
469  zip_left_it(const It1 & a1, const It1 & b1, const It2 & a2, const It2 & b2)
470  {
472 
473  It1 i1 = a1;
474  It2 i2 = a2;
475 
476  for ( ; i1 != b1; ++i1)
477  {
478  ret.append(std::make_pair(*i1, *i2));
479 
480  ++i2;
481 
482  if (i2 == b2)
483  i2 = a2;
484  }
485 
486  return ret;
487  }
488 
489  template <typename T1, typename T2, class It1, class It2>
491  zip_right_it(const It1 & a1, const It1 & b1, const It2 & a2, const It2 & b2)
492  {
494 
495  It1 i1 = a1;
496  It2 i2 = a2;
497 
498  for ( ; i2 != b2; ++i2)
499  {
500  ret.append(std::make_pair(*i1, *i2));
501 
502  ++i1;
503 
504  if (i1 == b1)
505  i1 = a1;
506  }
507 
508  return ret;
509  }
510 
511  template <class ContainerType, class It>
512  ContainerType to_container(const It & a, const It & b)
513  {
514  ContainerType ret;
515 
516  for (It i = a; i != b; ++i)
517  ret.append(*i);
518 
519  return ret;
520  }
521 
522  template <typename T, class It>
523  SLList<T> to_list_it(const It & a, const It & b)
524  {
525  return to_container<SLList<T>, It>(a, b);
526  }
527 
528  template <typename T, class It>
529  DynArray<T> to_array_it(const It & a, const It & b)
530  {
531  return to_container<DynArray<T>, It>(a, b);
532  }
533 
534 } // end namespace Designar
535 
536 # endif // DSGITALGORITHMS_H
SLList< std::pair< T1, T2 > > zip_left_it(const It1 &, const It1 &, const It2 &, const It2 &)
Definition: italgorithms.H:469
RetT * nth_ptr_it(const It &, const It &, nat_t)
Definition: italgorithms.H:159
T & append(const T &item)
Definition: list.H:265
bool none_it(const It &, const It &, Pred &)
Definition: italgorithms.H:325
DynArray< T > to_array_it(const It &, const It &)
Definition: italgorithms.H:529
bool equal_it(const It1 &, const It1 &, const It2 &, const It2 &, Eq &)
Definition: italgorithms.H:391
ContainerRet filter_it(const It &, const It &, Pred &)
Definition: italgorithms.H:196
ContainerRet map_if_it(const It &, const It &, Op &, Pred &)
Definition: italgorithms.H:231
RetT & nth_it(const It &, const It &, nat_t)
Definition: italgorithms.H:172
RetT fold_it(const It &, const It &, const RetT &, Op &)
Definition: italgorithms.H:261
SLList< std::pair< T1, T2 > > zip_eq_it(const It1 &, const It1 &, const It2 &, const It2 &)
Definition: italgorithms.H:451
bool exists_it(const It &, const It &, Pred &)
Definition: italgorithms.H:310
Definition: array.H:375
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
ContainerType to_container(const It &, const It &)
Definition: italgorithms.H:512
SLList< std::pair< T1, T2 > > zip_it(const It1 &, const It1 &, const It2 &, const It2 &)
Definition: italgorithms.H:436
void remove_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:375
SLList< T > to_list_it(const It &, const It &)
Definition: italgorithms.H:523
ContainerRet map_it(const It &, const It &, Op &)
Definition: italgorithms.H:214
bool remove_first_if_it(const It &, const It &, Pred &)
Definition: italgorithms.H:356
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
SLList< std::pair< T1, T2 > > zip_right_it(const It1 &, const It1 &, const It2 &, const It2 &)
Definition: italgorithms.H:491
RetT * search_ptr_it(const It &, const It &, Pred &)
Definition: italgorithms.H:340
bool all_it(const It &, const It &, Pred &)
Definition: italgorithms.H:295