Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
linear_set.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 <algorithm>
28
30#include <extensions/range.hpp>
31
32#include "vector.hpp"
33
34namespace ext {
35
44template < class T, class Compare = std::less<T>, class Alloc = std::allocator<T> >
51
55 Compare m_comp;
56
64 bool eq ( const T & first, const T & second ) {
65 return ! m_comp ( first, second ) && ! m_comp ( second, first );
66 }
67
72 void sort_unique ( ) {
73 std::sort ( m_data.begin ( ), m_data.end ( ), m_comp );
74 m_data.erase ( std::unique ( m_data.begin ( ), m_data.end ( ), std::bind ( &linear_set < T >::eq, this, std::placeholders::_1, std::placeholders::_2 ) ), m_data.end ( ) );
75 }
76
77public:
82 typedef T value_type;
83
89
95
101
107
115 explicit linear_set (const Compare& comp = Compare(), const Alloc& alloc = Alloc()) : m_data ( alloc ), m_comp ( comp ) {
116 }
117
124 explicit linear_set (const Alloc& alloc) : m_data ( alloc ), m_comp ( Compare ( ) ) {
125 }
126
136 template <class InputIterator>
137 linear_set (InputIterator first, InputIterator last, const Compare& comp = Compare(), const Alloc& alloc = Alloc()) : m_data ( first, last, alloc ), m_comp ( comp ) {
138 sort_unique ( );
139 }
140
148 template < class Iterator >
150 }
151
158 linear_set (const linear_set& x) : m_data ( x.m_data ), m_comp ( x.m_comp ) {
159 }
160
168 linear_set (const linear_set& x, const Alloc& alloc) : m_data ( x.m_data, alloc ), m_comp ( x.m_comp ) {
169 }
170
177 linear_set (linear_set&& x) : m_data ( std::move ( x.m_data ) ), m_comp ( x.m_comp ) {
178 }
179
187 linear_set (linear_set&& x, const Alloc& alloc) : m_data ( std::move ( x.m_data ), alloc ), m_comp ( x.m_comp ) {
188 }
189
198 linear_set (std::initializer_list<T> il, const Compare& comp = Compare(), const Alloc& alloc = Alloc()) : m_data ( std::move ( il ), alloc ), m_comp ( comp ) {
199 sort_unique ( );
200 }
201
207 }
208
215 iterator begin() & noexcept {
216 return m_data.begin ( );
217 }
218
225 const_iterator begin() const & noexcept {
226 return m_data.begin ( );
227 }
228
235 auto begin ( ) && noexcept {
236 return make_set_move_iterator ( this->begin ( ) );
237 }
238
245 const_iterator cbegin() const noexcept {
246 return m_data.cbegin ( );
247 }
248
255 iterator end() & noexcept {
256 return m_data.end ( );
257 }
258
265 const_iterator end() const & noexcept {
266 return m_data.end ( );
267 }
268
275 auto end ( ) && noexcept {
276 return make_set_move_iterator ( this->end ( ) );
277 }
278
285 const_iterator cend() const noexcept {
286 return m_data.cend ( );
287 }
288
295 auto range ( ) & {
296 auto endIter = end ( );
297 auto beginIter = begin ( );
298 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
299 }
300
307 auto range ( ) const & {
308 auto endIter = end ( );
309 auto beginIter = begin ( );
310 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
311 }
312
319 auto range ( ) && {
320 auto endIter = std::move ( * this ).end ( );
321 auto beginIter = std::move ( * this ).begin ( );
322 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
323 }
324
330 void clear() noexcept {
331 m_data.clear ( );
332 }
333
340 size_t count ( const T& value ) const {
341 return std::binary_search ( m_data.begin ( ), m_data.end ( ), value );
342 }
343
351 return m_data.crbegin ( );
352 }
353
360 const_reverse_iterator crend() const noexcept {
361 return m_data.crend ( );
362 }
363
374 template <class... Args>
375 std::pair<iterator,bool> emplace (Args&&... args) {
376 return insert ( T ( std::forward ( args ) ... ) );
377 }
378
390 template <class... Args>
391 iterator emplace_hint (const_iterator position, Args&&... args) {
392 return insert ( position, T ( std::forward ( args ) ... ) );
393 }
394
401 bool empty() const noexcept {
402 return m_data.empty ( );
403 }
404
413 std::pair<const_iterator,const_iterator> equal_range (const T& val) const {
414 return std::make_pair ( lower_bound ( val ), upper_bound ( val ) );
415 }
416
425 std::pair<iterator,iterator> equal_range (const T& val) {
426 return std::make_pair ( lower_bound ( val ), upper_bound ( val ) );
427 }
428
438 return m_data.erase ( position );
439 }
440
449 size_t erase (const T& val) {
450 const_iterator position = lower_bound ( val );
451 if ( position != end ( ) && eq ( * position, val ) )
452 return m_data.erase ( position ), 1;
453 else
454 return 0;
455 }
456
467 return m_data.erase ( first, last );
468 }
469
478 const_iterator find (const T& val) const {
479 const_iterator position = lower_bound ( begin ( ), end ( ), val, m_comp );
480 if ( eq ( * position, val ) )
481 return position;
482 else
483 return end ( );
484 }
485
494 iterator find (const T& val) {
495 iterator position = lower_bound ( val );
496 if ( eq ( * position, val ) )
497 return position;
498 else
499 return end ( );
500 }
501
508 Alloc get_allocator() const noexcept {
509 return m_data.get_allocator ( );
510 }
511
520 std::pair<iterator,bool> insert (const T& val) {
521 return insert ( T ( val ) );
522 }
523
532 std::pair<iterator,bool> insert (T&& val) {
533 const_iterator position = lower_bound ( val );
534 if ( position != end ( ) && eq ( * position, val ) )
535 return std::make_pair ( position, false );
536 else
537 return std::make_pair ( m_data.emplace ( position, std::move ( val ) ), true );
538 }
539
549 iterator insert (const_iterator position, const T& val) {
550 return insert ( position, T ( val ) );
551 }
552
562 iterator insert (const_iterator position, T&& val) {
563 if ( position != end ( ) && eq ( * position, val ) )
564 return position;
565
566 if ( position != end ( ) && m_comp ( val, * position ) && ( position == begin ( ) || m_comp ( * std::prev ( position ), val ) ) )
567 return m_data.emplace ( position, std::move ( val ) );
568
569 return insert ( std::move ( val ) ).first;
570 }
571
579 template <class InputIterator>
580 void insert (InputIterator first, InputIterator last) {
581 m_data.insert ( m_data.end ( ), first, last );
582 sort_unique ( );
583 }
584
591 void insert (std::initializer_list<T> il) {
592 insert ( il.begin ( ), il.end ( ) );
593 }
594
601 Compare key_comp() const {
602 return m_comp;
603 }
604
613 iterator lower_bound (const T& val) {
614 return std::lower_bound ( begin ( ), end ( ), val, m_comp );
615 }
616
625 const_iterator lower_bound (const T& val) const {
626 return std::lower_bound ( begin ( ), end ( ), val, m_comp );
627 }
628
635 size_t max_size() const noexcept {
636 return m_data.max_size ( );
637 }
638
648 m_data = x.m_data;
649 m_comp = x.m_comp;
650 return *this;
651 }
652
662 m_data = std::move ( x.m_data );
663 m_comp = std::move ( x.m_comp );
664 return *this;
665 }
666
675 linear_set& operator= (std::initializer_list<T> il) {
676 m_data = std::move ( il );
677 std::sort ( m_data.begin ( ), m_data.end ( ), m_comp );
678 return *this;
679 }
680
688 return m_data.rbegin ( );
689 }
690
698 return m_data.rbegin ( );
699 }
700
708 return m_data.rend ( );
709 }
710
717 const_reverse_iterator rend() const noexcept {
718 return m_data.rend ( );
719 }
720
727 size_t size() const noexcept {
728 return m_data.size ( );
729 }
730
737 void swap (linear_set& x) {
738 using std::swap;
739 swap ( m_data, x.m_data );
740 swap ( m_comp, x.m_comp );
741 }
742
751 iterator upper_bound (const T& val) {
752 return std::upper_bound ( begin ( ), end ( ), val, m_comp );
753 }
754
763 const_iterator upper_bound (const T& val) const {
764 return std::upper_bound ( begin ( ), end ( ), val, m_comp );
765 }
766
773 Compare value_comp() const {
774 return m_comp;
775 }
776
785 bool contains ( const T & key ) const {
786 return this->count ( key ) != 0U;
787 }
788
799 template < class K >
800 bool contains ( const K & key ) const {
801 return this->count ( key ) != 0U;
802 }
803
813 bool operator == ( const linear_set<T,Compare,Alloc>& rhs ) const {
814 const linear_set<T,Compare,Alloc>& lhs = * this;
815 return lhs.m_data == rhs.m_data;
816 }
817
828 const linear_set<T,Compare,Alloc>& lhs = * this;
829 return lhs.m_data <=> rhs.m_data;
830 }
831
832};
833
845template <class T, class Compare, class Alloc>
847 x.swap ( y );
848}
849
862template< class T, class ... Ts >
863std::ostream& operator<<(std::ostream& out, const ext::linear_set<T, Ts ...>& value) {
864 out << "{";
865
866 bool first = true;
867 for(const T& item : value) {
868 if(!first) out << ", ";
869 first = false;
870 out << item;
871 }
872
873 out << "}";
874 return out;
875}
876
877} /* namespace ext */
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
Implementation of set mimicking the iterface of the standard library set. The inner representation is...
Definition: linear_set.hpp:45
~linear_set()
The destructor of the linear set.
Definition: linear_set.hpp:206
linear_set(InputIterator first, InputIterator last, const Compare &comp=Compare(), const Alloc &alloc=Alloc())
Set constructor from a range of values.
Definition: linear_set.hpp:137
void insert(InputIterator first, InputIterator last)
Insert values from a range speified by pair of iterators.
Definition: linear_set.hpp:580
linear_set(const Compare &comp=Compare(), const Alloc &alloc=Alloc())
Default constructor of the empty set.
Definition: linear_set.hpp:115
ext::vector< T, Alloc >::const_iterator iterator
The type of iterator over values in the set. It is the same as the underling vector's const iterator.
Definition: linear_set.hpp:88
reverse_iterator rbegin() noexcept
Getter of a reverse iterator to the begining of range of values in the container.
Definition: linear_set.hpp:687
std::pair< iterator, bool > emplace(Args &&... args)
Emplace a value to the container. Internaly the method uses insert since the place to put the value r...
Definition: linear_set.hpp:375
bool empty() const noexcept
Tests whether the container is empty.
Definition: linear_set.hpp:401
std::pair< iterator, bool > insert(T &&val)
Inserts a new value to the container.
Definition: linear_set.hpp:532
bool operator==(const linear_set< T, Compare, Alloc > &rhs) const
Compares two set instances for equvalence.
Definition: linear_set.hpp:813
void swap(linear_set &x)
Swaps two instances of linear set.
Definition: linear_set.hpp:737
iterator lower_bound(const T &val)
Returns an iterator pointing to the first element in the range [first,last) which does not compare le...
Definition: linear_set.hpp:613
auto end() &&noexcept
Getter of a move iterator to the end of range of values in the container.
Definition: linear_set.hpp:275
auto operator<=>(const linear_set< T, Compare, Alloc > &rhs) const
Compares two set instances by less relation.
Definition: linear_set.hpp:827
linear_set(const linear_set &x, const Alloc &alloc)
Copy constructor including allocator specification.
Definition: linear_set.hpp:168
linear_set(linear_set &&x, const Alloc &alloc)
Move constructor including allocator specification.
Definition: linear_set.hpp:187
Compare value_comp() const
Getter of the value comparator instance. Actually the value_type is the same as key_type so this is a...
Definition: linear_set.hpp:773
size_t count(const T &value) const
Computes the number of values in the container.
Definition: linear_set.hpp:340
const_iterator lower_bound(const T &val) const
Returns an iterator pointing to the first element in the range [first,last) which does not compare le...
Definition: linear_set.hpp:625
void insert(std::initializer_list< T > il)
Insert values from a range speified by initializer list.
Definition: linear_set.hpp:591
auto range() &&
Make range of move begin to end iterators.
Definition: linear_set.hpp:319
iterator end() &noexcept
Getter of an iterator to the end of range of values in the container.
Definition: linear_set.hpp:255
iterator insert(const_iterator position, const T &val)
Inserts a new value to the container. The method accepts a position hint where to place the new value...
Definition: linear_set.hpp:549
const_reverse_iterator crbegin() const noexcept
Getter of a const revese iterator to the begining of reverse range of values in the container.
Definition: linear_set.hpp:350
const_iterator cbegin() const noexcept
Getter of a const iterator to the begining of range of values in the container.
Definition: linear_set.hpp:245
ext::vector< T, Alloc >::const_reverse_iterator const_reverse_iterator
The type of const reverse iterator over values in the set. It is the same as the underling vector's c...
Definition: linear_set.hpp:106
Alloc get_allocator() const noexcept
Getter of the allocator.
Definition: linear_set.hpp:508
const_iterator upper_bound(const T &val) const
Returns an iterator pointing to the first element in the range [first,last) which compares greater th...
Definition: linear_set.hpp:763
const_reverse_iterator rbegin() const noexcept
Getter of a const reverse iterator to the begining of range of values in the container.
Definition: linear_set.hpp:697
std::pair< iterator, iterator > equal_range(const T &val)
Returns a range of values equal to the val. The range is specified by iterators.
Definition: linear_set.hpp:425
ext::vector< T, Alloc >::const_iterator const_iterator
The type of const iterator over values in the set. It is the same as the underling vector's const ite...
Definition: linear_set.hpp:94
linear_set(linear_set &&x)
Move constructor.
Definition: linear_set.hpp:177
iterator insert(const_iterator position, T &&val)
Inserts a new value to the container. The method accepts a position hint where to place the new value...
Definition: linear_set.hpp:562
auto range() const &
Make range of non-const begin to end iterators.
Definition: linear_set.hpp:307
const_reverse_iterator crend() const noexcept
Getter of a const revese iterator to the end of reverse range of values in the container.
Definition: linear_set.hpp:360
T value_type
The type of values in the set.
Definition: linear_set.hpp:82
const_iterator find(const T &val) const
Function to binary search for given value.
Definition: linear_set.hpp:478
const_iterator end() const &noexcept
Getter of a const iterator to the end of range of values in the container.
Definition: linear_set.hpp:265
iterator upper_bound(const T &val)
Returns an iterator pointing to the first element in the range [first,last) which compares greater th...
Definition: linear_set.hpp:751
std::pair< iterator, bool > insert(const T &val)
Inserts a new value to the container.
Definition: linear_set.hpp:520
linear_set(const ext::iterator_range< Iterator > &range)
Definition: linear_set.hpp:149
linear_set(const Alloc &alloc)
Constructor of the empty set with specified allocator.
Definition: linear_set.hpp:124
linear_set(const linear_set &x)
Copy constructor.
Definition: linear_set.hpp:158
iterator erase(const_iterator first, const_iterator last)
Removes values in the specified range. The range is specified by pair of iterators.
Definition: linear_set.hpp:466
iterator erase(const_iterator position)
Removes value from the container based on the position given by iterator.
Definition: linear_set.hpp:437
const_iterator begin() const &noexcept
Getter of a const iterator to the begining of range of values in the container.
Definition: linear_set.hpp:225
const_reverse_iterator rend() const noexcept
Getter of a const reverse iterator to the end of range of values in the container.
Definition: linear_set.hpp:717
linear_set & operator=(const linear_set &x)
Copy operator of assignmet.
Definition: linear_set.hpp:647
iterator emplace_hint(const_iterator position, Args &&... args)
Emplace a value to the container with provided position as a hint. Internaly the method uses insert s...
Definition: linear_set.hpp:391
iterator begin() &noexcept
Getter of an iterator to the begining of range of values in the container.
Definition: linear_set.hpp:215
auto range() &
Make range of non-const begin to end iterators.
Definition: linear_set.hpp:295
ext::vector< T, Alloc >::const_reverse_iterator reverse_iterator
The type of reverse iterator over values in the set. It is the same as the underling vector's const r...
Definition: linear_set.hpp:100
iterator find(const T &val)
Function to binary search for given value.
Definition: linear_set.hpp:494
reverse_iterator rend() noexcept
Getter of a reverse iterator to the end of range of values in the container.
Definition: linear_set.hpp:707
auto begin() &&noexcept
Getter of a move iterator to the begining of range of values in the container.
Definition: linear_set.hpp:235
bool contains(const T &key) const
Test whether the set contains a given key.
Definition: linear_set.hpp:785
size_t size() const noexcept
Getter of the number of values inside the container.
Definition: linear_set.hpp:727
Compare key_comp() const
Getter of the key comparator instance.
Definition: linear_set.hpp:601
size_t erase(const T &val)
Removes value from the container.
Definition: linear_set.hpp:449
std::pair< const_iterator, const_iterator > equal_range(const T &val) const
Returns a range of values equal to the val. The range is specified by const iterators.
Definition: linear_set.hpp:413
size_t max_size() const noexcept
Returns the maximal number of values possible to store inside the container.
Definition: linear_set.hpp:635
const_iterator cend() const noexcept
Getter of a const iterator to the end of range of values in the container.
Definition: linear_set.hpp:285
linear_set(std::initializer_list< T > il, const Compare &comp=Compare(), const Alloc &alloc=Alloc())
Set constructor from initializer list.
Definition: linear_set.hpp:198
bool contains(const K &key) const
Test whether the set contains a given key.
Definition: linear_set.hpp:800
void clear() noexcept
Removes all values from the conainer,.
Definition: linear_set.hpp:330
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
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
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
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
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
typename std::vector< T, Alloc >::const_reverse_iterator const_reverse_iterator
The type of constant reverse values iterator.
Definition: vector.hpp:79
p second
Definition: ToRegExpAlgebraic.h:126
Definition: sigHandler.cpp:20
ForwardIterator unique(ForwardIterator first, ForwardIterator last)
Shuffles values in a sequece so that consecutive duplicate values are pushed to the front and others ...
Definition: algorithm.hpp:384
set_move_iterator< Iterator > make_set_move_iterator(Iterator it)
Move from set iterator adaptor construction function.
Definition: iterator.hpp:200
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
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
void swap(ext::linear_set< T, Compare, Alloc > &x, ext::linear_set< T, Compare, Alloc > &y)
Specialisation of swap for linear set.
Definition: linear_set.hpp:846
Definition: FordFulkerson.hpp:16
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