Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ptr_array.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 <ostream>
27#include <compare>
28
29#include <ext/ostream>
30#include <ext/array>
31
32#include <extensions/range.hpp>
33#include <extensions/clone.hpp>
34
35namespace ext {
36
44template < class T, std::size_t N >
45class ptr_array {
50 std::array < T *, N > m_data;
51
52public:
57 using value_type = T;
58
63 using size_type = std::size_t;
64
69 using diference_type = std::ptrdiff_t;
70
76
81 using const_reference = const value_type &;
82
87 using pointer = T *;
88
93 using const_pointer = const T *;
94
100
106
112
118
127 template < class ... Types >
128 ptr_array ( Types && ... args ) : m_data ( make_array < T * > ( ext::clone ( std::forward < Types > ( args ) ) ... ) ) {
129 }
130
137 template < class R = T >
139 if constexpr ( N != 0 )
140 for ( size_type i = 0; i < N; ++ i )
141 m_data.at ( i ) = new R ( );
142 }
143
150 ptr_array ( const ptr_array & other ) {
151 if constexpr ( N != 0 )
152 for ( size_type i = 0; i < N; ++ i )
153 m_data.at ( i ) = ext::clone ( other.at ( i ) );
154 }
155
162 ptr_array ( ptr_array && other ) noexcept {
163 if constexpr ( N != 0 )
164 for ( size_type i = 0; i < N; ++ i )
165 m_data.at ( i ) = std::exchange ( other.m_data.at ( i ), nullptr );
166 }
167
172 ~ptr_array ( ) noexcept {
173 if constexpr ( N != 0 )
174 for ( size_type i = 0; i < N; ++ i )
175 delete m_data.at ( i );
176 }
177
185 if ( this == & other )
186 return * this;
187
188 if constexpr ( N != 0 )
189 for ( size_type i = 0; i < N; ++ i ) {
190 delete m_data.at ( i );
191 m_data.at ( i ) = ext::clone ( other.at ( i ) );
192 }
193
194 return *this;
195 }
196
204 std::swap ( m_data, other.m_data );
205
206 return *this;
207 }
208
215 template < class R >
216 void fill ( const R & value ) {
217 if constexpr ( N != 0 )
218 for ( size_type i = 0; i < N; ++ i )
219 m_data.at ( i ) = ext::clone ( value );
220 }
221
231 return * m_data.at ( index );
232 }
233
242 const_reference at ( size_type index ) const {
243 return * m_data.at ( index );
244 }
245
255 return * m_data [ index ];
256 }
257
267 return * m_data [ index ];
268 }
269
277 return * m_data.front ( );
278 }
279
287 return * m_data.front ( );
288 }
289
297 return * m_data.back ( );
298 }
299
307 return * m_data.back ( );
308 }
309
318 bool null ( size_type index ) const {
319 return m_data.at ( index ) == nullptr;
320 }
321
328 iterator begin ( ) & noexcept {
329 return dereferencer ( m_data.begin ( ) );
330 }
331
338 const_iterator begin ( ) const & noexcept {
339 return dereferencer ( m_data.begin ( ) );
340 }
341
348 auto begin ( ) && noexcept {
349 return make_move_iterator ( this->end ( ) );
350 }
351
358 const_iterator cbegin ( ) const noexcept {
359 return dereferencer ( m_data.cbegin ( ) );
360 }
361
368 iterator end ( ) & noexcept {
369 return dereferencer ( m_data.end ( ) );
370 }
371
378 const_iterator end ( ) const & noexcept {
379 return dereferencer ( m_data.end ( ) );
380 }
381
388 auto end ( ) && noexcept {
389 return make_move_iterator ( this->end ( ) );
390 }
391
398 const_iterator cend ( ) const noexcept {
399 return dereferencer ( m_data.cend ( ) );
400 }
401
409 return dereferencer ( m_data.rbegin ( ) );
410 }
411
418 const_reverse_iterator rbegin ( ) const noexcept {
419 return dereferencer ( m_data.rbegin ( ) );
420 }
421
428 const_reverse_iterator crbegin ( ) const noexcept {
429 return dereferencer ( m_data.crbegin ( ) );
430 }
431
438 reverse_iterator rend ( ) noexcept {
439 return dereferencer ( m_data.rend ( ) );
440 }
441
448 const_reverse_iterator rend ( ) const noexcept {
449 return dereferencer ( m_data.rend ( ) );
450 }
451
458 const_reverse_iterator crend ( ) const noexcept {
459 return dereferencer ( m_data.crend ( ) );
460 }
461
468 auto range ( ) & {
469 auto endIter = end ( );
470 auto beginIter = begin ( );
471 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
472 }
473
480 auto range ( ) const & {
481 auto endIter = end ( );
482 auto beginIter = begin ( );
483 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
484 }
485
492 auto range ( ) && {
493 auto endIter = std::move ( * this ).end ( );
494 auto beginIter = std::move ( * this ).begin ( );
495 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
496 }
497
504 bool empty ( ) const noexcept {
505 return m_data.empty ( );
506 }
507
514 size_type size ( ) const noexcept {
515 return m_data.size ( );
516 }
517
524 size_type max_size ( ) const noexcept {
525 return m_data.max_size ( );
526 }
527
539 template < class R >
540 iterator set ( const_iterator pos, R && value ) {
541 size_type dist = std::distance ( cbegin ( ), pos );
542
543 // If the set value and value at position pos are same instances first clone, then delete
544 delete std::exchange ( m_data.at ( dist ), ext::clone ( std::forward < R > ( value ) ) );
545
546 return dereferencer ( m_data.begin ( ) + dist );
547 }
548
560 template < class R >
561 iterator set ( const_reverse_iterator pos, R && value ) {
562 size_type dist = std::distance ( crbegin ( ), pos );
563
564 // If the set value and value at position pos are same instances first clone, then delete
565 delete std::exchange ( m_data.at ( m_data.size ( ) - dist - 1 ), ext::clone ( std::forward < R > ( value ) ) );
566
567 return dereferencer ( m_data.rbegin ( ) + dist );
568 }
569
582 template < class R, class ... Args >
583 iterator emplace_set ( const_iterator pos, Args && ... args ) {
584 return set ( pos, R ( std::forward < Args > ( args ) ... ) );
585 }
586
599 template < class R, class ... Args >
600 iterator emplace_set ( const_reverse_iterator pos, Args && ... args ) {
601 return set ( pos, R ( std::forward < Args > ( args ) ... ) );
602 }
603
609 void swap ( ptr_array & other ) {
610 swap ( this->m_data, other.m_data );
611 }
612
621 template < std::size_t I >
622 auto & get ( ) & {
623 return * std::get < I > ( m_data );
624 }
625
634 template < std::size_t I >
635 const auto & get ( ) const & {
636 return * std::get < I > ( m_data );
637 }
638
647 template < std::size_t I >
648 auto && get ( ) && {
649 return std::move ( * std::get < I > ( m_data ) );
650 }
651
653 const ext::ptr_array < T, N > & first = * this;
654 return std::lexicographical_compare_three_way ( first.begin ( ), first.end ( ), second.begin ( ), second.end ( ) );
655 }
656
670 const ext::ptr_array < T, N > & first = * this;
671 return std::equal ( first.begin ( ), first.end ( ), second.begin ( ), second.end ( ) );
672 }
673
674};
675
676} /* namespace ext */
677
678namespace std {
679
692template < std::size_t I, class Type, std::size_t N >
694 return tpl.template get < I > ( );
695}
696
709template < std::size_t I, class Type, std::size_t N >
710const auto & get ( const ext::ptr_array < Type, N > & tpl ) {
711 return tpl.template get < I > ( );
712}
713
726template < std::size_t I, class Type, std::size_t N >
727auto && get ( ext::ptr_array < Type, N > && tpl ) {
728 return std::move ( tpl ).template get < I > ( );
729}
730
731} /* namespace std */
732
733namespace ext {
734
747template < class T, std::size_t N >
749 out << "[";
750
751 bool first = true;
752 for(const T& item : ptr_array) {
753 if(!first) out << ", ";
754 first = false;
755 out << item;
756 }
757
758 out << "]";
759 return out;
760}
761
774template < typename Base, typename ... Types >
775constexpr ptr_array < typename std::remove_reference < Base >::type , sizeof ... ( Types ) + 1 > make_ptr_array ( Base && first, Types && ... other ) {
776 return ptr_array < typename std::remove_reference < Base >::type, sizeof ... ( Types ) + 1 > ( std::forward < Base > ( first ), std::forward < Types > ( other ) ... );
777}
778
787template < typename Base >
790}
791
792} /* namespace ext */
793
Adaptor iterator to additionally call second dereference on the iterator dereference result.
Definition: iterator.hpp:556
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
Implementation of array storing dynamicaly allocated instances of given type. The class mimicks the i...
Definition: ptr_array.hpp:45
const_iterator cend() const noexcept
Iterator one past the last element of the values range in the array.
Definition: ptr_array.hpp:398
bool empty() const noexcept
Array emptines test.
Definition: ptr_array.hpp:504
reference at(size_type index)
Getter of value on index.
Definition: ptr_array.hpp:230
reference operator[](size_type index)
Array subscript operator.
Definition: ptr_array.hpp:254
iterator set(const_iterator pos, R &&value)
Setter of value in the array on position specified by iterator specified by pos. The value is cloned ...
Definition: ptr_array.hpp:540
auto & get() &
Getter of value on index given by template paramter.
Definition: ptr_array.hpp:622
ptr_array()
The default constructor initializing the values of the array to the defaultly constructed value of T.
Definition: ptr_array.hpp:138
iterator begin() &noexcept
Iterator to the begining of the values range in the array.
Definition: ptr_array.hpp:328
~ptr_array() noexcept
Destructor of the pointer array.
Definition: ptr_array.hpp:172
ptr_array< T, N > & operator=(const ptr_array< T, N > &other)
Copy operator of assignment. The values of the source array are cloned to the new array.
Definition: ptr_array.hpp:184
void swap(ptr_array &other)
Definition: ptr_array.hpp:609
iterator emplace_set(const_iterator pos, Args &&... args)
Setter of internaly constructed value into the array on position specified by iterator specified by p...
Definition: ptr_array.hpp:583
const_reverse_iterator crend() const noexcept
Reverse iterator one past the last element of the reversed range of values in the array.
Definition: ptr_array.hpp:458
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the begining of the reversed range of values in the array.
Definition: ptr_array.hpp:428
auto range() &&
Make range of move begin to end iterators.
Definition: ptr_array.hpp:492
ptr_array(const ptr_array &other)
Copy constructor cloning all values in the source array.
Definition: ptr_array.hpp:150
T value_type
The type of values.
Definition: ptr_array.hpp:57
reverse_iterator rbegin() noexcept
Reverse iterator to the begining of the reversed range of values in the array.
Definition: ptr_array.hpp:408
iterator end() &noexcept
Iterator one past the last element of the values range in the array.
Definition: ptr_array.hpp:368
const auto & get() const &
Getter of value on index given by template paramter.
Definition: ptr_array.hpp:635
iterator emplace_set(const_reverse_iterator pos, Args &&... args)
Setter of internaly constructed value into the array on position specified by iterator specified by p...
Definition: ptr_array.hpp:600
const_reference at(size_type index) const
Getter of value on index.
Definition: ptr_array.hpp:242
const_reference back() const
Getter of the last value in the array.
Definition: ptr_array.hpp:306
const_reference front() const
Getter of the first value in the array.
Definition: ptr_array.hpp:286
T * pointer
The type of pointer to values.
Definition: ptr_array.hpp:87
const_iterator cbegin() const noexcept
Iterator to the begining of the values range in the array.
Definition: ptr_array.hpp:358
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the begining of the reversed range of values in the array.
Definition: ptr_array.hpp:418
ptr_array(Types &&... args)
Constructor of the array of pointers from parameter values. The resulting array is of size of the par...
Definition: ptr_array.hpp:128
auto range() &
Make range of non-const begin to end iterators.
Definition: ptr_array.hpp:468
const T * const_pointer
The type of pointer to constant value.
Definition: ptr_array.hpp:93
reference front()
Getter of the first value in the array.
Definition: ptr_array.hpp:276
value_type & reference
The type of reference to values.
Definition: ptr_array.hpp:75
const_reverse_iterator rend() const noexcept
Reverse iterator one past the last element of the reversed range of values in the array.
Definition: ptr_array.hpp:448
const_iterator begin() const &noexcept
Iterator to the begining of the values range in the array.
Definition: ptr_array.hpp:338
std::size_t size_type
The type of sizes.
Definition: ptr_array.hpp:63
bool operator==(const ext::ptr_array< T, N > &second) const
Specialisation of equality operator for pointer array.
Definition: ptr_array.hpp:669
const_iterator end() const &noexcept
Iterator one past the last element of the values range in the array.
Definition: ptr_array.hpp:378
size_type max_size() const noexcept
Returns the maximal number of values possible to store inside the container.
Definition: ptr_array.hpp:524
auto range() const &
Make range of non-const begin to end iterators.
Definition: ptr_array.hpp:480
ptr_array(ptr_array &&other) noexcept
Move constructor intended to transfer ownership of pointers from source to new instance.
Definition: ptr_array.hpp:162
auto begin() &&noexcept
Move iterator to the begining of the values range in the array.
Definition: ptr_array.hpp:348
auto && get() &&
Getter of value on index given by template paramter.
Definition: ptr_array.hpp:648
const value_type & const_reference
The type of reference to constant values.
Definition: ptr_array.hpp:81
size_type size() const noexcept
Getter of the array size.
Definition: ptr_array.hpp:514
void fill(const R &value)
Fills the array with copies of value.
Definition: ptr_array.hpp:216
reference back()
Getter of the last value in the array.
Definition: ptr_array.hpp:296
iterator set(const_reverse_iterator pos, R &&value)
Setter of value in the array on position specified by iterator specified by pos. The value is cloned ...
Definition: ptr_array.hpp:561
reverse_iterator rend() noexcept
Reverse iterator one past the last element of the reversed range of values in the array.
Definition: ptr_array.hpp:438
std::ptrdiff_t diference_type
The type of size differences.
Definition: ptr_array.hpp:69
auto operator<=>(const ext::ptr_array< T, N > &second) const
Definition: ptr_array.hpp:652
auto end() &&noexcept
Move iterator to the begining of the values range in the array.
Definition: ptr_array.hpp:388
p second
Definition: ToRegExpAlgebraic.h:126
int i
Definition: AllEpsilonClosure.h:118
Definition: sigHandler.cpp:20
constexpr array< typename std::remove_reference< Base >::type, sizeof ...(Types)+1 > make_array(Base &&first, Types &&... other)
Equivalent to the make_array from standard library but produces the ext::array.
Definition: array.hpp:254
constexpr ptr_array< typename std::remove_reference< Base >::type, sizeof ...(Types)+1 > make_ptr_array(Base &&first, Types &&... other)
Array construction helper. Array is constructed from provided values, type of stored elements is dedu...
Definition: ptr_array.hpp:775
auto clone(T &&tmp)
Wrapper around clone by means of using copy constructor or clone method if available.
Definition: clone.hpp:41
dereferencing_iterator< Iterator > dereferencer(Iterator iter)
Dereferencing adaptor construction function.
Definition: iterator.hpp:815
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
Definition: FordFulkerson.hpp:16
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
void swap(ext::managed_linear_set< T, Compare, Alloc > &x, ext::managed_linear_set< T, Compare, Alloc > &y)
Specialisation of swap for linear set.
Definition: managed_linear_set.hpp:864