Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
vector.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 <vector>
27#include <iostream>
28
29#include <ext/ostream>
30
31#include <extensions/range.hpp>
32
33namespace ext {
34
44template < class T, class Alloc = std::allocator < T > >
45class vector : public std::vector < T, Alloc > {
46public:
50 using std::vector< T, Alloc >::vector; // NOLINT(modernize-use-equals-default)
51
55 using std::vector< T, Alloc >::operator =;
56
61 using iterator = typename std::vector < T, Alloc >::iterator;
62
67 using const_iterator = typename std::vector < T, Alloc >::const_iterator;
68
73 using reverse_iterator = typename std::vector < T, Alloc >::reverse_iterator;
74
79 using const_reverse_iterator = typename std::vector < T, Alloc >::const_reverse_iterator;
80
81#ifndef __clang__
82
86 vector ( ) = default;
87
91 vector ( const vector & other ) = default;
92
96 vector ( vector && other ) = default;
97
101 vector & operator = ( vector && other ) = default;
102
106 vector & operator = ( const vector & other ) = default;
107#endif
115 template < class Iterator >
117 }
118
125 auto begin ( ) & {
127 }
128
135 auto begin ( ) const & {
137 }
138
145 auto begin ( ) && {
146 return make_move_iterator ( this->begin ( ) );
147 }
148
155 auto end ( ) & {
157 }
158
165 auto end ( ) const & {
167 }
168
175 auto end ( ) && {
176 return make_move_iterator ( this->end ( ) );
177 }
178
185 auto range ( ) & {
186 auto endIter = end ( );
187 auto beginIter = begin ( );
188 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
189 }
190
197 auto range ( ) const & {
198 auto endIter = end ( );
199 auto beginIter = begin ( );
200 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
201 }
202
209 auto range ( ) && {
210 auto endIter = std::move ( * this ).end ( );
211 auto beginIter = std::move ( * this ).begin ( );
212 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
213 }
214
218 using std::vector < T, Alloc >::insert;
219
230 return reverse_iterator ( std::next ( insert ( pos.base ( ), value ) ) );
231 }
232
243 return reverse_iterator ( std::next ( insert ( pos.base ( ), std::move ( value ) ) ) );
244 }
245
256 reverse_iterator insert ( const_reverse_iterator pos, size_t count, const T & value ) {
257 return reverse_iterator ( std::next ( insert ( pos.base ( ), count, value ) ) );
258 }
259
271 template < class InputIt >
272 reverse_iterator insert ( const_reverse_iterator pos, InputIt first, InputIt last ) {
273 return reverse_iterator ( std::next ( insert ( pos.base ( ), first, last ) ) );
274 }
275
285 reverse_iterator insert ( const_reverse_iterator pos, std::initializer_list < T > ilist ) {
286 return reverse_iterator ( std::next ( insert ( pos.base ( ), std::move ( ilist ) ) ) );
287 }
288
292 using std::vector < T, Alloc >::emplace;
293
305 template < class ... Args >
307 return reverse_iterator ( std::next ( emplace ( pos.base ( ), std::forward < Args > ( args ) ... ) ) );
308 }
309
313 using std::vector < T, Alloc >::erase;
314
324 return erase ( const_iterator ( pos ) );
325 }
326
336 return erase ( const_reverse_iterator ( pos ) );
337 }
338
348 return reverse_iterator ( erase ( std::next ( pos ).base ( ) ) );
349 }
350
360 iterator erase ( iterator first, iterator last ) {
361 return erase ( const_iterator ( first ), const_iterator ( last ) );
362 }
363
374 return erase ( const_reverse_iterator ( first ), const_reverse_iterator ( last ) );
375 }
376
387 return reverse_iterator ( erase ( std::next ( first ).base ( ), std::next ( last ).base ( ) ) );
388 }
389
390};
391
404template< class T , class ... Ts >
406 out << "[";
407
408 bool first = true;
409 for(const T& item : vector) {
410 if(!first) out << ", ";
411 first = false;
412 out << item;
413 }
414
415 out << "]";
416 return out;
417}
418
419/*// the same type as the ext::vector bool's internal storage type
420typedef typename std::decay < decltype ( * ( std::declval < ext::vector < bool > > ( ).begin ( )._M_p ) ) >::type vectorBoolInternalType;
421// the size of the ext::vector bool's internal storage type
422const unsigned vectorBoolInternalTypeInBits = sizeof ( vectorBoolInternalType ) * 8;
423
424// private helper for mask computation
425inline vectorBoolInternalType getMask ( size_t dist ) {
426 if ( dist == 0 )
427 return ~ vectorBoolInternalType { };
428 else
429 return ( ( vectorBoolInternalType { } + 1 ) << dist ) - 1;
430}*/
431
443template < class ... Ts >
445 // c++ implementation-specific
446 /* A.resize ( std::max ( A.size ( ), B.size ( ) ), false );
447
448 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
449 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
450
451 while ( itB + 1 < B.end ( ) ) // A is longer or of the same size as B
452 * ( itA._M_p ++ ) |= * ( itB._M_p ++ ); // word-at-a-time bitwise operation
453
454 size_t sizeWithin = B.size ( ) % vectorBoolInternalTypeInBits;
455
456 if ( itB < B.end ( ) ) {
457 * ( itA._M_p ++ ) |= * ( itB._M_p ++ ) & getMask ( sizeWithin );
458 }
459
460 // The rest of A above the size of B can be left intact */
461
462 A.resize ( std::max ( A.size ( ), B.size ( ) ), false );
463
464 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
465 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
466
467 for ( ; itB < B.end ( ); ++ itB, ++ itA )
468 * itA = * itA | * itB;
469
470 return A;
471}
472
484template < class ... Ts >
486 A |= B;
487 return A;
488}
489
501template < class ... Ts >
503 // c++ implementation-specific
504 /* A.resize ( std::max ( A.size ( ), B.size ( ) ), false );
505
506 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
507 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
508
509 while ( itB + 1 < B.end ( ) ) { // A is longer or of the same size as B
510 * ( itA._M_p ++ ) &= * ( itB._M_p ++ ); // word-at-a-time bitwise operation
511 }
512
513 size_t sizeWithin = B.size ( ) % vectorBoolInternalTypeInBits;
514
515 if ( itB < B.end ( ) ) {
516 * ( itA._M_p ++ ) &= * ( itB._M_p ++ ) & getMask ( sizeWithin );
517 }
518
519 while ( itA < A.end ( ) ) { // The rest of A above the size of B shall be zeroed
520 * ( itA._M_p ++ ) = 0;
521 }*/
522
523 // c++ compilant
524 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
525 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
526
527 for ( ; itB < B.end ( ) && itA < A.end ( ); ++ itB, ++ itA )
528 * itA = * itA & * itB;
529
530 for ( ; itA < A.end ( ); ++ itA )
531 * itA = false;
532
533 return A;
534}
535
547template < class ... Ts >
549 A &= B;
550 return A;
551}
552
564template < class ... Ts >
566 // c++ implementation-specific
567 /* A.resize ( std::max ( A.size ( ), B.size ( ) ), false );
568
569 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
570 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
571
572 while ( itB + 1 < B.end ( ) ) // A is longer or of the same size as B
573 * ( itA._M_p ++ ) ^= * ( itB._M_p ++ ); // word-at-a-time bitwise operation
574
575 size_t sizeWithin = B.size ( ) % vectorBoolInternalTypeInBits;
576
577 if ( itB < B.end ( ) ) {
578 * ( itA._M_p ++ ) ^= * ( itB._M_p ++ ) & getMask ( sizeWithin );
579 }
580
581 while ( itA < A.end ( ) ) { // The rest of A above the size of B shall be flipped
582 * ( itA._M_p ) = ~ * ( itA._M_p );
583 itA._M_p ++;
584 }*/
585
586 // c++ compilant
587 A.resize ( std::max ( A.size ( ), B.size ( ) ), false );
588
589 typename ext::vector < bool, Ts ... >::iterator itA = A.begin ( );
590 typename ext::vector < bool, Ts ... >::const_iterator itB = B.begin ( );
591
592 for ( ; itB < B.end ( ); ++ itB, ++ itA )
593 * itA = * itA ^ * itB;
594
595 return A;
596}
597
609template < class ... Ts >
611 A ^= B;
612 return A;
613}
614
625template < class ... Ts >
627 A.flip ( );
628 return A;
629}
630
642template < class ... Ts >
643ext::vector < bool, Ts ... > & operator <<= ( ext::vector < bool, Ts ... > & A, size_t dist ) {
644/* if ( A.size ( ) == 0 )
645 return A;
646
647 size_t distBlocks = dist / vectorBoolInternalTypeInBits;
648 size_t distWithin = dist % vectorBoolInternalTypeInBits;
649 size_t backDist = vectorBoolInternalTypeInBits - distWithin;
650
651 // shift blocks if needed
652 if ( distBlocks ) {
653 // simulate behavior of reverse iterator
654 auto itAReverse = A.end ( ) - 1;
655 auto itASource = itAReverse;
656 itASource._M_p -= distBlocks;
657
658 while ( itASource >= A.begin ( ) )
659 * ( itAReverse._M_p -- ) = * ( itASource._M_p -- );
660
661 while ( itAReverse >= A.begin ( ) )
662 * ( itAReverse._M_p -- ) = 0;
663 }
664
665 if ( distWithin == 0 )
666 return A;
667
668 // shift by the rest dist
669 vectorBoolInternalType bits1 { };
670 vectorBoolInternalType bits2 { };
671
672 // it might be more clear to iterate from the begining. However this is working nicely
673 auto itA = A.begin ( );
674
675 while ( itA < A.end ( ) ) {
676 bits2 = * ( itA._M_p );
677 * ( itA._M_p ) = * ( itA._M_p ) << distWithin | bits1 >> backDist;
678 bits1 = bits2;
679
680 itA._M_p ++;
681 }*/
682
683 // c++ compilant
684 if ( dist > A.size ( ) ) {
685 clear ( A );
686 } else {
687 typename ext::vector < bool, Ts ... >::reverse_iterator itASource = A.rbegin ( ) + dist;
688 typename ext::vector < bool, Ts ... >::reverse_iterator itADest = A.rbegin ( );
689
690 for ( ; itASource < A.rend ( ); ++ itASource, ++ itADest )
691 * itADest = * itASource;
692
693 for ( ; itADest < A.rend ( ); ++ itADest )
694 * itADest = false;
695 }
696
697 return A;
698}
699
711template < class ... Ts >
712ext::vector < bool, Ts ... > operator << ( ext::vector < bool, Ts ... > A, size_t dist ) {
713 A <<= dist;
714 return A;
715}
716
728template < class ... Ts >
729ext::vector < bool, Ts ... > & operator >>= ( ext::vector < bool, Ts ... > & A, size_t dist ) {
730 /*if ( A.size ( ) == 0 )
731 return A;
732
733 size_t distBlocks = dist / vectorBoolInternalTypeInBits;
734 size_t distWithin = dist % vectorBoolInternalTypeInBits;
735 size_t sizeWithin = A.size ( ) % vectorBoolInternalTypeInBits;
736 size_t backDist = vectorBoolInternalTypeInBits - distWithin;
737
738 // upper part of the last word in the ext::vector can contain some garbage so it needs to be cleared
739 * ( ( A.end ( ) - 1 )._M_p ) &= getMask ( sizeWithin );
740
741 // shift blocks if needed
742 if ( distBlocks ) {
743 auto itA = A.begin ( );
744 auto itASource = itA;
745 itASource._M_p += distBlocks;
746
747 while ( itASource < A.end ( ) )
748 * ( itA._M_p ++ ) = * ( itASource._M_p ++ );
749
750 while ( itA < A.end ( ) )
751 * ( itA._M_p ++ ) = 0;
752 }
753
754 if ( distWithin == 0 )
755 return A;
756
757 // shift by the rest dist
758 vectorBoolInternalType bits1 { };
759 vectorBoolInternalType bits2 { };
760
761 // it might be more clear to iterate from the begining. However this is working nicely
762 auto itAReverse = A.end ( ) - 1;
763
764 // simulate behavior of reverse iterator
765 while ( itAReverse >= A.begin ( ) ) {
766 bits2 = * ( itAReverse._M_p );
767 * ( itAReverse._M_p ) = * ( itAReverse._M_p ) >> distWithin | bits1 << backDist;
768 bits1 = bits2;
769
770 itAReverse._M_p --;
771 }*/
772
773 // c++ compilant
774 if ( dist > A.size ( ) ) {
775 clear ( A );
776 } else {
777 typename ext::vector < bool, Ts ... >::iterator itASource = A.begin ( ) + dist;
778 typename ext::vector < bool, Ts ... >::iterator itADest = A.begin ( );
779
780 for ( ; itASource < A.end ( ); ++ itASource, ++ itADest )
781 * itADest = * itASource;
782
783 for ( ; itADest < A.end ( ); ++ itADest )
784 * itADest = false;
785 }
786
787 return A;
788}
789
801template < class ... Ts >
802ext::vector < bool, Ts ... > operator >> ( ext::vector < bool, Ts ... > A, size_t dist ) {
803 A >>= dist;
804 return A;
805}
806
819template < class ... Ts >
821 // c++ implementation-specific
822 /*typename ext::vector < bool, Ts ... >::const_iterator itV = v.begin ( );
823
824 size_t sizeWithin = v.size ( ) % vectorBoolInternalTypeInBits;
825
826 while ( itV + 1 < v.end ( ) )
827 if ( * ( itV._M_p ++ ) != 0 )
828 return true;
829
830 return ( * itV._M_p & getMask ( sizeWithin ) ) != 0;*/
831
832 // c++ compilant
833 typename ext::vector < bool, Ts ... >::const_iterator itV = v.begin ( );
834 for ( ; itV != v.end ( ); ++ itV )
835 if ( * itV )
836 return true;
837
838 return false;
839}
840
851template < class ... Ts >
853 // c++ implementation-specific
854 /*typename ext::vector < bool, Ts ... >::iterator itV = v.begin ( );
855
856 while ( itV < v.end ( ) )
857 * ( itV._M_p ++ ) = ~ vectorBoolInternalType { };*/
858
859 // c++ compilant
860 typename ext::vector < bool, Ts ... >::iterator itV = v.begin ( );
861 for ( ; itV != v.end ( ); ++ itV )
862 * itV = true;
863}
864
875template < class ... Ts >
877 // c++ implementation-specific
878 /* typename ext::vector < bool, Ts ... >::iterator itV = v.begin ( );
879
880 while ( itV < v.end ( ) )
881 * ( itV._M_p ++ ) = 0;*/
882
883 // c++ compilant
884 typename ext::vector < bool, Ts ... >::iterator itV = v.begin ( );
885 for ( ; itV != v.end ( ); ++ itV )
886 * itV = false;
887}
888
889} /* namespace ext */
890
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
Definition: ostream.h:14
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
vector(const vector &other)=default
reverse_iterator insert(const_reverse_iterator pos, InputIt first, InputIt last)
Inserts the values from the given range to positions starting at given iterator pos.
Definition: vector.hpp:272
auto range() const &
Make range of non-const begin to end iterators.
Definition: vector.hpp:197
auto end() &&
Inherited behavior of end for rvalues.
Definition: vector.hpp:175
typename std::vector< T, Alloc >::reverse_iterator reverse_iterator
The type of reverse values iterator.
Definition: vector.hpp:73
auto range() &&
Make range of move begin to end iterators.
Definition: vector.hpp:209
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
iterator erase(iterator pos)
Removes element from the container at position given by parameter pos.
Definition: vector.hpp:323
reverse_iterator erase(const_reverse_iterator first, const_reverse_iterator last)
Removes elements from the container in range given parameters first and last.
Definition: vector.hpp:386
typename std::vector< T, Alloc >::iterator iterator
The type of values iterator.
Definition: vector.hpp:61
reverse_iterator erase(reverse_iterator pos)
Removes element from the container at position given by parameter pos.
Definition: vector.hpp:335
vector & operator=(vector &&other)=default
reverse_iterator insert(const_reverse_iterator pos, T &&value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:242
iterator erase(iterator first, iterator last)
Removes elements from the container in range given parameters first and last.
Definition: vector.hpp:360
reverse_iterator erase(const_reverse_iterator pos)
Removes element from the container at position given by parameter pos.
Definition: vector.hpp:347
auto begin() const &
Inherited behavior of begin for const instance.
Definition: vector.hpp:135
vector()=default
typename std::vector< T, Alloc >::const_iterator const_iterator
The type of constant values iterator.
Definition: vector.hpp:67
reverse_iterator insert(const_reverse_iterator pos, const T &value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:229
reverse_iterator insert(const_reverse_iterator pos, std::initializer_list< T > ilist)
Inserts the values from the given initializer list to positions starting at given iterator pos.
Definition: vector.hpp:285
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
auto begin() &&
Inherited behavior of begin for rvalues.
Definition: vector.hpp:145
reverse_iterator emplace(const_reverse_iterator pos, Args &&... args)
Inserts a new value to the container at position given by parameter pos. The new value is constructed...
Definition: vector.hpp:306
vector(const ext::iterator_range< Iterator > &range)
Definition: vector.hpp:116
vector(vector &&other)=default
auto end() const &
Inherited behavior of end for const instance.
Definition: vector.hpp:165
typename std::vector< T, Alloc >::const_reverse_iterator const_reverse_iterator
The type of constant reverse values iterator.
Definition: vector.hpp:79
auto range() &
Make range of non-const begin to end iterators.
Definition: vector.hpp:185
reverse_iterator insert(const_reverse_iterator pos, size_t count, const T &value)
Inserts the count copies of value on position given by iterator pos.
Definition: vector.hpp:256
reverse_iterator erase(reverse_iterator first, reverse_iterator last)
Removes elements from the container in range given parameters first and last.
Definition: vector.hpp:373
Definition: sigHandler.cpp:20
ext::vector< bool, Ts ... > & operator^=(ext::vector< bool, Ts ... > &A, const ext::vector< bool, Ts ... > &B)
Support for assigning xor operator.
Definition: vector.hpp:565
ext::vector< bool, Ts ... > & operator>>=(ext::vector< bool, Ts ... > &A, size_t dist)
Support for assigning bit shift to the right operator. The operator is intended to look at the vector...
Definition: vector.hpp:729
ext::vector< bool, Ts ... > operator|(ext::vector< bool, Ts ... > A, const ext::vector< bool, Ts ... > &B)
Support for or operator.
Definition: vector.hpp:485
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
ext::vector< bool, Ts ... > & operator|=(ext::vector< bool, Ts ... > &A, const ext::vector< bool, Ts ... > &B)
Support for assigning or operator.
Definition: vector.hpp:444
void clear(ext::vector< bool, Ts ... > &v)
Clears all bits in the vector of booleans.
Definition: vector.hpp:876
ext::vector< bool, Ts ... > & operator<<=(ext::vector< bool, Ts ... > &A, size_t dist)
Support for assigning bit shift to the left operator. The operator is intended to look at the vector ...
Definition: vector.hpp:643
ext::vector< bool, Ts ... > operator^(ext::vector< bool, Ts ... > A, const ext::vector< bool, Ts ... > &B)
Support for xor operator.
Definition: vector.hpp:610
ext::vector< bool, Ts ... > operator~(ext::vector< bool, Ts ... > A)
Support for not operator.
Definition: vector.hpp:626
void fill(ext::vector< bool, Ts ... > &v)
Sets all bits in the vector of booleans.
Definition: vector.hpp:852
ext::vector< bool, Ts ... > operator&(ext::vector< bool, Ts ... > A, const ext::vector< bool, Ts ... > &B)
Support for and operator.
Definition: vector.hpp:548
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
bool any(const ext::vector< bool, Ts ... > &v)
Tests the vector of booleans whether at least one bit inside is set.
Definition: vector.hpp:820
std::istream & operator>>(ext::reference_wrapper< std::istream > &is, std::istream &(*const func)(std::istream &))
Overloaded function allowing same operations on wrapped input stream as on the actual input stream,...
Definition: GlobalData.cpp:53
ext::vector< bool, Ts ... > & operator&=(ext::vector< bool, Ts ... > &A, const ext::vector< bool, Ts ... > &B)
Support for assigning and operator.
Definition: vector.hpp:502
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19