Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
foreach.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <tuple>
27#include <utility>
28
30
31namespace ext {
32
37template < class ... Iterators >
42 ext::tuple < Iterators ... > current;
43
52 template < size_t ... I >
53 ext::tuple < const typename Iterators::value_type & ... > callDerefOperator ( const std::index_sequence < I ... > & ) const {
54 return ext::tie ( * std::get < I > ( current ) ... );
55 }
56
61 template < size_t ... I >
62 void callIncOperator ( const std::index_sequence < I ... > & ) {
63 ext::tie ( ++std::get < I > ( current ) ... );
64 }
65
70 template < size_t ... I >
71 void callDecOperator ( const std::index_sequence < I ... > & ) {
72 ext::tie ( --std::get < I > ( current ) ... );
73 }
74
75public:
83 explicit const_tuple_foreach_iterator ( Iterators ... it ) : current ( it ... ) {
84 }
85
92 template < size_t I >
93 decltype ( std::get < I > ( current ) ) base ( ) const {
94 return std::get < I > ( current );
95 }
96
103 template < size_t ... I >
104 ext::tuple < const typename Iterators::value_type & ... > operator *( ) const {
105 return callDerefOperator ( std::index_sequence_for < Iterators ... > { } );
106 }
107
115 callIncOperator ( std::index_sequence_for < Iterators ... > { } );
116 return * this;
117 }
118
126 callDecOperator ( std::index_sequence_for < Iterators ... > { } );
127 return * this;
128 }
129
137 const_tuple_foreach_iterator temp = * this;
138
139 ++( * this );
140 return temp;
141 }
142
150 const_tuple_foreach_iterator temp = * this;
151
152 --( * this );
153 return temp;
154 }
155
165 return this->current == other.current;
166 }
167
177 return !( * this == other );
178 }
179
180};
181
192template < class ... Iterators >
193const_tuple_foreach_iterator < Iterators ... > make_tuple_foreach_iterator ( Iterators ... its ) {
194 return const_tuple_foreach_iterator < Iterators ... > ( its ... );
195}
196
203template < class ... Types >
205
210 const ext::tuple < const Types & ... > data;
211
220 template < size_t ... I >
221 const_tuple_foreach_iterator < typename Types::const_iterator ... > callBegin ( const std::index_sequence < I ... > & ) const {
222 return make_tuple_foreach_iterator ( std::get < I > ( data ).cbegin ( ) ... );
223 }
224
233 template < size_t ... I >
234 const_tuple_foreach_iterator < typename Types::const_iterator ... > callEnd ( const std::index_sequence < I ... > & ) const {
235 return make_tuple_foreach_iterator ( std::get < I > ( data ).cend ( ) ... );
236 }
237
238public:
245 const_tuple_foreach ( const Types & ... args ) : data ( args ... ) { }
246
253 const_tuple_foreach_iterator < typename Types::const_iterator ... > begin ( ) const {
254 return callBegin ( std::index_sequence_for < Types ... > { } );
255 }
256
263 const_tuple_foreach_iterator < typename Types::const_iterator ... > end ( ) const {
264 return callEnd ( std::index_sequence_for < Types ... > { } );
265 }
266
267};
268
279template < class ... Types >
280const_tuple_foreach < Types ... > make_tuple_foreach ( const Types & ... args ) {
281 return const_tuple_foreach < Types ... > ( args ... );
282}
283
290template < class IntegralType >
296 IntegralType m_data;
297
298public:
299 typedef IntegralType value_type;
300
301 typedef std::ptrdiff_t difference_type;
302
303 typedef IntegralType * pointer;
304
305 typedef IntegralType reference;
306
307 typedef std::bidirectional_iterator_tag iterator_category;
308
315 virtual_pointer_to_integer ( IntegralType data ) : m_data ( data ) {
316 }
317
324 IntegralType operator * ( ) const {
325 return m_data;
326 }
327
335 m_data ++;
336 return * this;
337 }
338
346 m_data --;
347 return * this;
348 }
349
357 virtual_pointer_to_integer temp = * this;
358
359 ++( * this );
360 return temp;
361 }
362
370 virtual_pointer_to_integer temp = * this;
371
372 --( * this );
373 return temp;
374 }
375
385 return this->m_data == other.m_data;
386 }
387
397 return !( * this == other );
398 }
399};
400
407template < class IntegralType >
408class sequence {
413 IntegralType m_first;
414
419 IntegralType m_last;
420
421public:
429 sequence ( IntegralType first, IntegralType last ) : m_first ( first ), m_last ( last ) {
430 }
431
438 sequence ( IntegralType size ) : m_first ( 0 ), m_last ( size ) {
439 }
440
449 }
450
459 }
460};
461
462} /* namespace ext */
463
A class for packing a tuple of iterators and synchronizing them. All are incremented,...
Definition: foreach.hpp:38
bool operator!=(const const_tuple_foreach_iterator< Iterators ... > &other) const
Implementation of inequality comparison operator on tuple of iterators.
Definition: foreach.hpp:176
const_tuple_foreach_iterator(Iterators ... it)
Constructor of the iterator tuple from a pack of iterators.
Definition: foreach.hpp:83
const_tuple_foreach_iterator & operator--()
Prefix version of decrement operator that propagates the call to all underlying iterators.
Definition: foreach.hpp:125
decltype(std::get< I >(current)) base() const
Getter of an Ith iterator from the tuple of underlying iterators.
Definition: foreach.hpp:93
ext::tuple< const typename Iterators::value_type &... > operator*() const
Implementation of dereference operator on all iterators in the tuple.
Definition: foreach.hpp:104
bool operator==(const const_tuple_foreach_iterator< Iterators ... > &other) const
Implementation of equality comparison operator on tuple of iterators.
Definition: foreach.hpp:164
const_tuple_foreach_iterator & operator++()
Prefix version of increment operator that propagates the call to all underlying iterators.
Definition: foreach.hpp:114
Class providing begin and end methods to allow simple use of packed iterators in foreach variant of t...
Definition: foreach.hpp:204
const_tuple_foreach(const Types &... args)
Constructor of foreach tuple pack helper.
Definition: foreach.hpp:245
const_tuple_foreach_iterator< typename Types::const_iterator ... > end() const
Getter of pack of end iterators.
Definition: foreach.hpp:263
const_tuple_foreach_iterator< typename Types::const_iterator ... > begin() const
Getter of pack of begin iterators.
Definition: foreach.hpp:253
Representation of integer sequence usable in foreach form of for loop.
Definition: foreach.hpp:408
virtual_pointer_to_integer< IntegralType > begin()
Getter of the begin iterator into the sequence.
Definition: foreach.hpp:447
sequence(IntegralType size)
Constructor of the sequence class based on the size.
Definition: foreach.hpp:438
virtual_pointer_to_integer< IntegralType > end()
Getter of the end iterator into the sequence.
Definition: foreach.hpp:457
sequence(IntegralType first, IntegralType last)
The constructor of the sequence based on the range.
Definition: foreach.hpp:429
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Class wrapping an integral type, but with pointer iterface.
Definition: foreach.hpp:291
bool operator==(const virtual_pointer_to_integer< IntegralType > &other) const
Equality operator. Two classes are equal if the data they hold is the same.
Definition: foreach.hpp:384
bool operator!=(const virtual_pointer_to_integer< IntegralType > &other) const
Inequality operator. Two classes are not equal if the data they hold is different.
Definition: foreach.hpp:396
std::bidirectional_iterator_tag iterator_category
Definition: foreach.hpp:307
std::ptrdiff_t difference_type
Definition: foreach.hpp:301
virtual_pointer_to_integer & operator++()
Prefix increment operator to increment the hold value.
Definition: foreach.hpp:334
virtual_pointer_to_integer & operator--()
Prefix decrement operator to decrement the hold value.
Definition: foreach.hpp:345
IntegralType operator*() const
Method to retrieve the integral value.
Definition: foreach.hpp:324
IntegralType value_type
Definition: foreach.hpp:299
IntegralType reference
Definition: foreach.hpp:305
virtual_pointer_to_integer(IntegralType data)
Constructor of the virtual pointer class.
Definition: foreach.hpp:315
IntegralType * pointer
Definition: foreach.hpp:303
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
const_tuple_foreach_iterator< Iterators ... > make_tuple_foreach_iterator(Iterators ... its)
Function construction of the tuple iterator.
Definition: foreach.hpp:193
const_tuple_foreach< Types ... > make_tuple_foreach(const Types &... args)
Function construction of foreach tuple pack helper.
Definition: foreach.hpp:280