diff --git a/doc/algorithm.qbk b/doc/algorithm.qbk index f5e4d0d..10098c0 100644 --- a/doc/algorithm.qbk +++ b/doc/algorithm.qbk @@ -58,6 +58,7 @@ Thanks to all the people who have reviewed this library and made suggestions for [section:Misc Other Algorithms] [include clamp-hpp.qbk] +[include gather.qbk] [include hex.qbk] [endsect] diff --git a/doc/gather.qbk b/doc/gather.qbk new file mode 100644 index 0000000..f46429a --- /dev/null +++ b/doc/gather.qbk @@ -0,0 +1,79 @@ +[/ File gather.qbk] + +[section:gather gather] + +[/license +Copyright (c) 2013 Marshall Clow + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +] + +The header file 'boost/algorithm/gather.hpp' contains two variants of a single algorithm, `gather`. + +`gather()` takes a collection of elements defined by a pair of iterators and moves the ones satisfying a predicate to them to a position (called the pivot) within the sequence. The algorithm is stable. The result is a pair of iterators that contains the items that satisfy the predicate. + +[heading Interface] + +The function `gather` returns a `std::pair` of iterators that denote the elements that satisfy the predicate. + +There are two versions; one takes two iterators, and the other takes a range. + +`` +namespace boost { namespace algorithm { + +template +std::pair +gather ( ForwardIterator first, ForwardIterator last, ForwardIterator pivot, Pred pred ); + +template +std::pair::type, typename boost::range_iterator::type> +gather ( ForwardRange &range, typename boost::range_iterator::type pivot, Pred pred ); + +}} +`` + +[heading Examples] + +Given an sequence containing: +`` +0 1 2 3 4 5 6 7 8 9 +`` + +a call to gather ( arr, arr + 10, arr + 4, IsEven ) will result in: + +`` +1 3 0 2 4 6 8 5 7 9 + |---|-----| + first | second + pivot +`` +where `first` and `second` are the fields of the pair that is returned by the call. + + +[heading Iterator Requirements] + +`gather` work on all iterators except input or output iterators. + +[heading Storage Requirements] + +`gather` uses stable_partition, which will attempt to allocate temporary memory, but will work in-situ if there is none available. + +[heading Complexity] + +If there is sufficient memory available, the run time is linear: `O(N)` + +If there is not any memory available, then the run time is `O(N log N)`. + +[heading Exception Safety] + +[heading Notes] + +[endsect] + +[/ File gather.qbk +Copyright 2013 Marshall Clow +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). +] + diff --git a/include/boost/algorithm/gather.hpp b/include/boost/algorithm/gather.hpp new file mode 100644 index 0000000..3624d33 --- /dev/null +++ b/include/boost/algorithm/gather.hpp @@ -0,0 +1,122 @@ +/* + Copyright 2008 Adobe Systems Incorporated + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + Revision history: + January 2008 mtc Version for Adobe Source Library + January 2013 mtc Version for Boost.Algorithm + +*/ + +/**************************************************************************************************/ + +/*! +\author Marshall Clow +\date January 2008 +*/ + +#ifndef BOOST_ALGORITHM_GATHER_HPP +#define ADOBE_ALGORITHM_GATHER_HPP + +#include // for std::stable_partition +#include + +#include // for boost::bind +#include // for boost::begin(range) +#include // for boost::end(range) + + +/**************************************************************************************************/ +/*! + \defgroup gather gather + \ingroup mutating_algorithm + + \c gather() takes a collection of elements defined by a pair of iterators and moves + the ones satisfying a predicate to them to a position (called the pivot) within + the sequence. The algorithm is stable. The result is a pair of iterators that + contains the items that satisfy the predicate. + + Given an sequence containing: +
+    0 1 2 3 4 5 6 7 8 9
+    
+ + a call to gather ( arr, arr + 10, arr + 4, IsEven ()) will result in: + +
+    1 3 0 2 4 6 8 5 7 9
+        |---|-----|
+      first |  second
+          pivot
+    
+ + + The problem is broken down into two basic steps, namely, moving the items before the pivot + and then moving the items from the pivot to the end. These "moves" are done with calls to + stable_partition. + + \par Storage Requirements: + + The algorithm uses stable_partition, which will attempt to allocate temporary memory, + but will work in-situ if there is none available. + + \par Time Complexity: + + If there is sufficient memory available, the run time is linear in N. + If there is not any memory available, then the run time is O(N log N). +*/ + +/**************************************************************************************************/ + +namespace boost { namespace algorithm { + +/**************************************************************************************************/ + +/*! + \ingroup gather + \brief iterator-based gather implementation +*/ + +template < + typename ForwardIterator, // Iter models ForwardIterator + typename Pred> // Pred models UnaryPredicate +std::pair gather ( ForwardIterator first, ForwardIterator last, ForwardIterator pivot, Pred pred ) +{ +// The first call partitions everything up to (but not including) the pivot element, +// while the second call partitions the rest of the sequence. + return std::make_pair ( + std::stable_partition ( first, pivot, !boost::bind ( pred, _1 )), + std::stable_partition ( pivot, last, boost::bind ( pred, _1 ))); +} + +/**************************************************************************************************/ + +/*! + \ingroup gather + \brief range-based gather implementation +*/ + +template < + typename ForwardRange, // + typename Pred> // Pred models UnaryPredicate +std::pair< + typename boost::range_iterator::type, + typename boost::range_iterator::type> +gather ( + ForwardRange &range, + typename boost::range_iterator::type pivot, + Pred pred ) +{ + return boost::algorithm::gather ( boost::begin ( range ), boost::end ( range ), pivot, pred ); +} + +/**************************************************************************************************/ + +}} // namespace + +/**************************************************************************************************/ + +#endif + diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 1d13cec..f079d56 100755 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -46,10 +46,14 @@ import testing ; [ run hex_test2.cpp : : : : hex_test2 ] [ run hex_test3.cpp : : : : hex_test3 ] [ run hex_test4.cpp : : : : hex_test4 ] - [ compile-fail hex_fail1.cpp ] + [ compile-fail hex_fail1.cpp ] # Wrapper tests [ run wrapper_test1.cpp : : : : wrapper_test1 ] + +# Gather tests + [ run gather_test1.cpp : : : : gather_test1 ] + [ compile-fail gather_fail1.cpp ] ; } diff --git a/test/gather_fail1.cpp b/test/gather_fail1.cpp new file mode 100644 index 0000000..2c20c8e --- /dev/null +++ b/test/gather_fail1.cpp @@ -0,0 +1,39 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +#include "iterator_test.hpp" + +namespace ba = boost::algorithm; + +bool is_ten ( int i ) { return i == 10; } + +void test_sequence1 () { + std::vector v; + typedef input_iterator::iterator> II; + +// This should fail to compile, since gather doesn't work with input iterators + (void) ba::gather ( II( v.begin ()), II( v.end ()), II( v.begin ()), is_even ); + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/gather_test1.cpp b/test/gather_test1.cpp new file mode 100644 index 0000000..1eff308 --- /dev/null +++ b/test/gather_test1.cpp @@ -0,0 +1,139 @@ +/* + Copyright (c) Marshall Clow 2011-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include + +#include +#include +#include + +#include +#include +#include + +#include "iterator_test.hpp" + +namespace ba = boost::algorithm; + +template +void print ( const char *prompt, const Container &c ) { + std::cout << prompt << " { "; + std::copy ( c.begin (), c.end (), std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + } + +template +void test_iterators ( Iterator first, Iterator last, Predicate comp, std::size_t offset ) { +// Create the pivot point + Iterator off = first; + std::advance(off, offset); + +// Gather the elements + std::pair res = ba::gather ( first, last, off, comp ); + +// We should now have three sequences, any of which may be empty: +// * [begin .. result.first) - items that do not satisfy the predicate +// * [result.first .. result.second) - items that do satisfy the predicate +// * [result.second .. end) - items that do not satisfy the predicate + Iterator iter = first; + for ( ; iter != res.first; ++iter ) + BOOST_CHECK ( !comp ( *iter )); + for ( ; iter != res.second; ++iter) + BOOST_CHECK ( comp ( *iter )); + for ( ; iter != last; ++iter ) + BOOST_CHECK ( !comp ( *iter )); + } + +template +void test_iterator_types ( const Container &c, Predicate comp, std::size_t offset ) { + typedef std::vector vec; + typedef forward_iterator FI; + typedef bidirectional_iterator BDI; + typedef random_access_iterator RAI; + + vec v; + v.assign ( c.begin (), c.end ()); + test_iterators ( FI ( v.begin ()), FI ( v.end ()), comp, offset ); + v.assign ( c.begin (), c.end ()); + test_iterators ( BDI ( v.begin ()), BDI ( v.end ()), comp, offset ); + v.assign ( c.begin (), c.end ()); + test_iterators ( RAI ( v.begin ()), RAI ( v.end ()), comp, offset ); + } + + +template +struct less_than { +public: +// typedef T argument_type; +// typedef bool result_type; + + less_than ( T foo ) : val ( foo ) {} + less_than ( const less_than &rhs ) : val ( rhs.val ) {} + + bool operator () ( const T &v ) const { return v < val; } +private: + less_than (); + less_than operator = ( const less_than &rhs ); + T val; + }; + +bool is_even ( int i ) { return i % 2 == 0; } +bool is_ten ( int i ) { return i == 10; } + +void test_sequence1 () { + std::vector v; + + for ( int i = 5; i < 15; ++i ) + v.push_back ( i ); + test_iterator_types ( v, less_than(10), 0 ); // at beginning + test_iterator_types ( v, less_than(10), 5 ); + test_iterator_types ( v, less_than(10), v.size () - 1 ); // at end + + test_iterator_types ( v, is_even, 0 ); + test_iterator_types ( v, is_even, 5 ); + test_iterator_types ( v, is_even, v.size () - 1 ); + +// Exactly one element in the sequence matches + test_iterator_types ( v, is_ten, 0 ); + test_iterator_types ( v, is_ten, 5 ); + test_iterator_types ( v, is_ten, v.size () - 1 ); + +// Everything in the sequence matches + test_iterator_types ( v, less_than(99), 0 ); + test_iterator_types ( v, less_than(99), 5 ); + test_iterator_types ( v, less_than(99), v.size () - 1 ); + +// Nothing in the sequence matches + test_iterator_types ( v, less_than(0), 0 ); + test_iterator_types ( v, less_than(0), 5 ); + test_iterator_types ( v, less_than(0), v.size () - 1 ); + +// All the elements in the sequence are the same + v.clear (); + for ( int i = 0; i < 11; ++i ) + v.push_back ( 10 ); + +// Everything in the sequence matches + test_iterator_types ( v, is_ten, 0 ); + test_iterator_types ( v, is_ten, 5 ); + test_iterator_types ( v, is_ten, v.size () - 1 ); + +// Nothing in the sequence matches + test_iterator_types ( v, less_than(5), 0 ); + test_iterator_types ( v, less_than(5), 5 ); + test_iterator_types ( v, less_than(5), v.size () - 1 ); + + } + + +int test_main( int , char* [] ) +{ + test_sequence1 (); + return 0; +} diff --git a/test/iterator_test.hpp b/test/iterator_test.hpp new file mode 100644 index 0000000..cd77fb6 --- /dev/null +++ b/test/iterator_test.hpp @@ -0,0 +1,296 @@ +#ifndef ITERATOR_TEST_H +#define ITERATOR_TEST_H + +/* + A set of iterator adapters for constructing test cases + From an iterator (or a pointer), you can make any class of iterator. + Assuming you want to degrade the capabilities. + + Modeled closely on work that Howard Hinnant did for libc++. +*/ + +#include + +// == Input Iterator == +template +class input_iterator { +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + input_iterator() : it_() {} + explicit input_iterator(It it) : it_(it) {} + + template + input_iterator(const input_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + input_iterator& operator++() {++it_; return *this;} + input_iterator operator++(int) {input_iterator tmp(*this); ++(*this); return tmp;} + + friend bool operator==(const input_iterator& x, const input_iterator& y) + {return x.it_ == y.it_;} + friend bool operator!=(const input_iterator& x, const input_iterator& y) + {return !(x == y);} + +private: + It it_; + template friend class input_iterator; +}; + +template +inline bool +operator==(const input_iterator& x, const input_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const input_iterator& x, const input_iterator& y) +{ + return !(x == y); +} + + +// == Forward Iterator == +template +class forward_iterator { +public: + typedef std::forward_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + forward_iterator() : it_() {} + explicit forward_iterator(It it) : it_(it) {} + template + forward_iterator(const forward_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + forward_iterator& operator++() {++it_; return *this;} + forward_iterator operator++(int) {forward_iterator tmp(*this); ++(*this); return tmp;} + + friend bool operator==(const forward_iterator& x, const forward_iterator& y) + {return x.it_ == y.it_;} + friend bool operator!=(const forward_iterator& x, const forward_iterator& y) + {return !(x == y);} +private: + It it_; + + template friend class forward_iterator; +}; + +template +inline bool +operator==(const forward_iterator& x, const forward_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const forward_iterator& x, const forward_iterator& y) +{ + return !(x == y); +} + +// == Bidirectional Iterator == +template +class bidirectional_iterator +{ +public: + typedef std::bidirectional_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + bidirectional_iterator() : it_() {} + explicit bidirectional_iterator(It it) : it_(it) {} + template + bidirectional_iterator(const bidirectional_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + bidirectional_iterator& operator++() {++it_; return *this;} + bidirectional_iterator operator++(int) {bidirectional_iterator tmp(*this); ++(*this); return tmp;} + + bidirectional_iterator& operator--() {--it_; return *this;} + bidirectional_iterator operator--(int) {bidirectional_iterator tmp(*this); --(*this); return tmp;} +private: + It it_; + template friend class bidirectional_iterator; +}; + +template +inline bool +operator==(const bidirectional_iterator& x, const bidirectional_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const bidirectional_iterator& x, const bidirectional_iterator& y) +{ + return !(x == y); +} + + +// == Random Access Iterator == +template +class random_access_iterator { +public: + typedef std::random_access_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + random_access_iterator() : it_() {} + explicit random_access_iterator(It it) : it_(it) {} + template + random_access_iterator(const random_access_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + random_access_iterator& operator++() {++it_; return *this;} + random_access_iterator operator++(int) {random_access_iterator tmp(*this); ++(*this); return tmp;} + + random_access_iterator& operator--() {--it_; return *this;} + random_access_iterator operator--(int) {random_access_iterator tmp(*this); --(*this); return tmp;} + + random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;} + random_access_iterator operator+ (difference_type n) const {random_access_iterator tmp(*this); tmp += n; return tmp;} + friend random_access_iterator operator+(difference_type n, random_access_iterator x) {x += n; return x;} + + random_access_iterator& operator-=(difference_type n) {return *this += -n;} + random_access_iterator operator- (difference_type n) const {random_access_iterator tmp(*this); tmp -= n; return tmp;} + + reference operator[](difference_type n) const {return it_[n];} +private: + It it_; + + template friend class random_access_iterator; +}; + +template +inline bool +operator==(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline bool +operator!=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(x == y); +} + +template +inline bool +operator<(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() < y.base(); +} + +template +inline bool +operator<=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(y < x); +} + +template +inline bool +operator>(const random_access_iterator& x, const random_access_iterator& y) +{ + return y < x; +} + +template +inline bool +operator>=(const random_access_iterator& x, const random_access_iterator& y) +{ + return !(x < y); +} + +template +inline typename std::iterator_traits::difference_type +operator-(const random_access_iterator& x, const random_access_iterator& y) +{ + return x.base() - y.base(); +} + + +// == Output Iterator == +template +class output_iterator { +public: + typedef std::output_iterator_tag iterator_category; + typedef void value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + output_iterator () {} + explicit output_iterator(It it) : it_(it) {} + + template + output_iterator(const output_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + + output_iterator& operator++() {++it_; return *this;} + output_iterator operator++(int) {output_iterator tmp(*this); ++(*this); return tmp;} + +private: + It it_; + template friend class output_iterator; + }; + +// No comparison operators for output iterators + + +// == Get the base of an iterator; used for comparisons == +template +inline Iter base(output_iterator i) { return i.base(); } + +template +inline Iter base(input_iterator i) { return i.base(); } + +template +inline Iter base(forward_iterator i) { return i.base(); } + +template +inline Iter base(bidirectional_iterator i) { return i.base(); } + +template +inline Iter base(random_access_iterator i) { return i.base(); } + +template // everything else +inline Iter base(Iter i) { return i; } + +#endif // ITERATORS_H