Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
algorithm.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 <algorithm>
27#include <functional>
28
29namespace ext {
30
45template<class InputIt1, class InputIt2, class Compare>
46bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) {
47 while (first2 != last2 && first1 != last1) {
48 if (comp(*first2, *first1)) {
49 ++first2;
50 } else if (comp(*first1, *first2)) {
51 ++first1;
52 } else {
53 return false;
54 }
55 }
56 return true;
57}
58
77template<class InputIt1, class InputIt2>
78bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
79 return excludes(first1, last1, first2, last2, std::less<decltype(*first1)>());
80}
81
92template < class InputIterator1, class InputIterator2, class OutputIterator1, class OutputIterator2, class Compare >
93void set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator1 result1, OutputIterator2 result2, Compare comp ) {
94 while ( first1 != last1 && first2 != last2 ) {
95 if ( comp ( * first1, * first2 ) ) {
96 * result1 = * first1;
97 ++ result1;
98 ++ first1;
99 } else if ( comp ( * first2, * first1 ) ) {
100 * result2 = * first2;
101 ++ result2;
102 ++ first2;
103 } else {
104 ++first1;
105 ++first2;
106 }
107 }
108
109 if ( first1 == last1 ) {
110 std::copy ( first2, last2, result2 );
111 }
112 if ( first2 == last2 ) {
113 std::copy ( first1, last1, result1 );
114 }
115}
116
127template < class InputIterator1, class InputIterator2, class OutputIterator1, class OutputIterator2 >
128void set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator1 result1, OutputIterator2 result2 ) {
129 set_symmetric_difference ( first1, last1, first2, last2, result1, result2, std::less < decltype ( * first1 ) > ( ) );
130}
131
149template < class ResType, class InType, typename ... Ts, template < typename ... > class ContainerType, class Callback >
150ContainerType < ResType > transform(const ContainerType < InType, Ts ... > & in, Callback transform ) {
151 ContainerType<ResType> res;
152 std::transform ( in.begin ( ), in.end ( ), std::inserter ( res, res.begin ( ) ), transform );
153 return res;
154}
155
169template<class InputIt, class Element>
170bool contains(InputIt first, InputIt last, const Element& elem) {
171 return find(first, last, elem) != last;
172}
173
187template<class InputIt, class Element>
188bool binary_contains(InputIt first, InputIt last, const Element& elem) {
189 return binary_search(first, last, elem) != last;
190}
191
208template < class Iterator, class Value >
209std::pair < Iterator, Iterator > find_range_internal ( Iterator begin, Iterator end, const Value & open, const Value & close ) {
210 for ( ; begin != end && * begin != open && * begin != close ; ++ begin );
211
212 if ( begin == end || * begin == close )
213 return std::make_pair ( begin, begin );
214
215 Iterator openPos = begin;
216
217 for ( ; ; ) {
218 std::pair < Iterator, Iterator > subRange = find_range_internal ( begin + 1, end, open, close );
219 if ( subRange.first == subRange.second )
220 break;
221 begin = subRange.second;
222 }
223
224 if ( begin == end )
225 return std::make_pair ( begin, begin );
226
227 ++ begin;
228
229 for ( ; begin != end && * begin != close ; ++ begin );
230
231 if ( begin == end )
232 return std::make_pair ( begin, begin );
233
234 Iterator closePos = begin;
235
236 return std::make_pair ( openPos, closePos );
237}
238
255template < class Iterator, class Value >
256std::pair < Iterator, Iterator > find_range ( Iterator begin, Iterator end, const Value & open, const Value & close ) {
257 std::pair < Iterator, Iterator > res = find_range_internal ( begin, end, open, close );
258
259 if ( res.first == res.second )
260 return res;
261
262 ++ res.second;
263
264 return res;
265}
266
277template < typename T >
278constexpr const T & max ( const T & a ) {
279 return a;
280}
281
295template < typename T, typename ... Args >
296constexpr const T & max ( const T & a, const T & b, const Args & ... args ) {
297 return max ( b < a ? a : b, args ... );
298}
299
309template < typename T >
310constexpr const T & min ( const T & a) {
311 return a;
312}
313
327template < typename T, typename ... Args >
328constexpr const T & min ( const T & a, const T & b, const Args & ... args) {
329 return min ( b > a ? a : b, args ... );
330}
331
336template < typename ForwardIterator, typename BinaryPredicate >
337ForwardIterator adjacent_find_internal ( ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred ) {
338 if ( first == last )
339 return last;
340 ForwardIterator next = first;
341 while ( ++ next != last ) {
342 if ( binary_pred ( * first, * next ) )
343 return first;
344 first = next;
345 }
346 return last;
347}
348
353template < typename ForwardIterator, typename BinaryPredicate >
354ForwardIterator unique_internal ( ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred ) {
355 // Skip the beginning, if already unique.
356 first = ext::adjacent_find_internal ( first, last, binary_pred );
357 if ( first == last )
358 return last;
359
360 // Do the real copy work.
361 ForwardIterator dest = first;
362 ++ first;
363 while ( ++ first != last )
364 if ( ! binary_pred ( * dest, * first ) )
365 std::swap ( * ++ dest, * first );
366 return ++ dest;
367}
368
383template < typename ForwardIterator >
384inline ForwardIterator unique ( ForwardIterator first, ForwardIterator last ) {
385 return ext::unique_internal(first, last, [] ( const auto & a, const auto & b ) { return a == b; } );
386}
387
403template < typename ForwardIterator, typename BinaryPredicate >
404inline ForwardIterator unique ( ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred ) {
405 return ext::unique_internal(first, last, binary_pred );
406}
407
416template < class ForwardIteratorBegin, class ForwardIteratorEnd, class ForwardCandidateIterator >
417inline bool range_contains_iterator ( ForwardIteratorBegin from, const ForwardIteratorEnd & end, const ForwardCandidateIterator & candidate ) {
418 for ( ; from != end; ++ from )
419 if ( static_cast < const void * > ( std::addressof ( * from ) ) == static_cast < const void * > ( std::addressof ( * candidate ) ) )
420 return true;
421 return false;
422}
423
424} /* namespace ext */
425
return res
Definition: MinimizeByPartitioning.h:145
Definition: sigHandler.cpp:20
auto end(Container &&cont) -> decltype(std::forward(cont).end())
Definition: iterator.hpp:912
std::pair< Iterator, Iterator > find_range_internal(Iterator begin, Iterator end, const Value &open, const Value &close)
Function to locate pair of iterators (openPos, closePos), i.e. both openPos and closePos are included...
Definition: algorithm.hpp:209
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
bool range_contains_iterator(ForwardIteratorBegin from, const ForwardIteratorEnd &end, const ForwardCandidateIterator &candidate)
determines if iterator candidate is in range [from, to).
Definition: algorithm.hpp:417
ForwardIterator adjacent_find_internal(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)
Internal function of standard library.
Definition: algorithm.hpp:337
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
constexpr const T & min(const T &a)
Definition: algorithm.hpp:310
bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Tests two sorted ranges wheter all elements from the second are not present in the first.
Definition: algorithm.hpp:46
std::pair< Iterator, Iterator > find_range(Iterator begin, Iterator end, const Value &open, const Value &close)
Function to locate pair of iterators (openPos, closePos] where * openPos == open and closePos == clos...
Definition: algorithm.hpp:256
bool contains(InputIt first, InputIt last, const Element &elem)
Linear version of search in a range of values.
Definition: algorithm.hpp:170
ContainerType< ResType > transform(const ContainerType< InType, Ts ... > &in, Callback transform)
In container tranformation of all elements according to the tranform.
Definition: algorithm.hpp:150
void set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator1 result1, OutputIterator2 result2, Compare comp)
Constructs sorted ranges beginning in the location pointed by result1 and result2 with the set differ...
Definition: algorithm.hpp:93
bool binary_contains(InputIt first, InputIt last, const Element &elem)
Logaritmic version of search in a range of sorted values.
Definition: algorithm.hpp:188
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
ForwardIterator unique_internal(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)
Internal function of standard library tuned to handle swapping of pointers.
Definition: algorithm.hpp:354
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
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