diff --git a/README.md b/README.md new file mode 100644 index 000000000..87f7923ad --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +#![Boost.Range](doc/logo/logo_bkg.png) + +Boost.Range, part of the [Boost C++ Libraries](http://github.com/boostorg), a collection of concepts and utilities, range-based algorithms, as well as range adaptors that allow for efficient and expressive code. + +### Directories + +* **doc** - QuickBook documentation sources +* **include** - the sourcecode of Boost.Range +* **test** - Boost.Range unit tests + +### More information + +* [Documentation](http://boost.org/libs/range) diff --git a/doc/logo/logo.svg b/doc/logo/logo.svg new file mode 100644 index 000000000..abae4ebec --- /dev/null +++ b/doc/logo/logo.svg @@ -0,0 +1,9025 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + RANGE + + + + boost + + + [..) + + + + + + + + + + + + + + + + + + + + + + + + + + + Range + + + + boost + + + [..) + + diff --git a/doc/logo/logo_bkg.png b/doc/logo/logo_bkg.png new file mode 100644 index 000000000..cead7c1e5 Binary files /dev/null and b/doc/logo/logo_bkg.png differ diff --git a/include/boost/range/adaptor/moved.hpp b/include/boost/range/adaptor/moved.hpp new file mode 100755 index 000000000..735cbe862 --- /dev/null +++ b/include/boost/range/adaptor/moved.hpp @@ -0,0 +1,94 @@ +// Boost.Range library +// +// Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. +// Copyright Adam Wulkiewicz 2014. +// +// 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) + +#ifndef BOOST_RANGE_ADAPTOR_MOVED_HPP +#define BOOST_RANGE_ADAPTOR_MOVED_HPP + +#include +#include + +#include + +namespace boost { + +namespace range_detail { + +template< class R > +struct moved_range : + boost::iterator_range< + boost::move_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > + > +{ +private: + typedef boost::iterator_range< + boost::move_iterator< + BOOST_DEDUCED_TYPENAME range_iterator::type + > + > base; +public: + moved_range( R & r ) + : base( boost::make_move_iterator(boost::begin(r)), + boost::make_move_iterator(boost::end(r)) ) + { } +}; + +struct move_forwarder {}; + +template< class InputRange > +inline moved_range +operator|( InputRange & r, move_forwarder ) +{ + return moved_range( r ); +} + +template< class InputRange > +inline moved_range +operator|( InputRange const& r, move_forwarder ) +{ + return moved_range( r ); +} + +} // namespace range_detail + +// Unusual use of 'using' is intended to bring filter_range into the boost namespace +// while leaving the mechanics of the '|' operator in range_detail and maintain +// argument dependent lookup. +// filter_range logically needs to be in the boost namespace to allow user of +// the library to define the return type for filter() +using range_detail::moved_range; + +namespace adaptors { + +namespace +{ + const range_detail::move_forwarder moved = + range_detail::move_forwarder(); +} + +template +inline moved_range +move(InputRange & rng) +{ + return moved_range(rng); +} + +template +inline moved_range +move(InputRange const& rng) +{ + return moved_range(rng); +} + +} // namespace adaptors + +} // namespace boost + +#endif // BOOST_RANGE_ADAPTOR_MOVED_HPP diff --git a/include/boost/range/algorithm/move.hpp b/include/boost/range/algorithm/move.hpp new file mode 100644 index 000000000..50911e2f3 --- /dev/null +++ b/include/boost/range/algorithm/move.hpp @@ -0,0 +1,57 @@ +// Boost.Range library +// +// Copyright Neil Groves 2009. +// Copyright Adam Wulkiewicz 2014. +// +// 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) + +#ifndef BOOST_RANGE_ALGORITHM_MOVE_HPP +#define BOOST_RANGE_ALGORITHM_MOVE_HPP + +#include +#include +#include +#include +//#include + +#include + +namespace boost { + +namespace range { + +/// \brief template function move +/// +/// range-based version of the move std algorithm +/// +/// \pre SinglePassRange is a model of the SinglePassRangeConcept +/// \pre OutputIterator is a model of the OutputIteratorConcept +template< class SinglePassRange, class OutputIterator > +inline OutputIterator move(SinglePassRange & rng, OutputIterator out) +{ + BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept )); + return ::boost::move(::boost::begin(rng), ::boost::end(rng), out); +} + +/// \brief template function move +/// +/// range-based version of the move std algorithm +/// +/// \pre SinglePassRange is a model of the SinglePassRangeConcept +/// \pre OutputIterator is a model of the OutputIteratorConcept +template< class SinglePassRange, class OutputIterator > +inline OutputIterator move(SinglePassRange const& rng, OutputIterator out) +{ + BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept )); + return ::boost::move(::boost::begin(rng), ::boost::end(rng), out); +} + +} // namespace range + +using range::move; + +} // namespace boost + +#endif // BOOST_RANGE_ALGORITHM_MOVE_HPP diff --git a/include/boost/range/algorithm/move_backward.hpp b/include/boost/range/algorithm/move_backward.hpp new file mode 100644 index 000000000..644d82639 --- /dev/null +++ b/include/boost/range/algorithm/move_backward.hpp @@ -0,0 +1,62 @@ +// Boost.Range library +// +// Copyright Neil Groves 2009. +// Copyright Adam Wulkiewicz 2014. +// +// 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) + +#ifndef BOOST_RANGE_ALGORITHM_MOVE_BACKWARD_HPP +#define BOOST_RANGE_ALGORITHM_MOVE_BACKWARD_HPP + +#include +#include +#include +#include + +#include + +namespace boost { + +namespace range { + +/// \brief template function copy_backward +/// +/// range-based version of the move_backwards std algorithm +/// +/// \pre BidirectionalRange is a model of the BidirectionalRangeConcept +/// \pre BidirectionalTraversalWriteableIterator is a model of the BidirectionalIteratorConcept +/// \pre BidirectionalTraversalWriteableIterator is a model of the WriteableIteratorConcept +template< class BidirectionalRange, class BidirectionalTraversalWriteableIterator > +inline BidirectionalTraversalWriteableIterator +move_backward(BidirectionalRange & rng, + BidirectionalTraversalWriteableIterator out) +{ + BOOST_RANGE_CONCEPT_ASSERT(( BidirectionalRangeConcept )); + return ::boost::move_backward(::boost::begin(rng), ::boost::end(rng), out); +} + +/// \brief template function copy_backward +/// +/// range-based version of the move_backwards std algorithm +/// +/// \pre BidirectionalRange is a model of the BidirectionalRangeConcept +/// \pre BidirectionalTraversalWriteableIterator is a model of the BidirectionalIteratorConcept +/// \pre BidirectionalTraversalWriteableIterator is a model of the WriteableIteratorConcept +template< class BidirectionalRange, class BidirectionalTraversalWriteableIterator > +inline BidirectionalTraversalWriteableIterator +move_backward(BidirectionalRange const& rng, + BidirectionalTraversalWriteableIterator out) +{ + BOOST_RANGE_CONCEPT_ASSERT(( BidirectionalRangeConcept )); + return ::boost::move_backward(::boost::begin(rng), ::boost::end(rng), out); +} + +} // namespace range + +using range::move_backward; + +} // namespace boost + +#endif // BOOST_RANGE_ALGORITHM_MOVE_BACKWARD_HPP diff --git a/test/adaptor_test/moved.cpp b/test/adaptor_test/moved.cpp new file mode 100644 index 000000000..357f53027 --- /dev/null +++ b/test/adaptor_test/moved.cpp @@ -0,0 +1,105 @@ +// Boost.Range library +// +// Copyright Neil Groves 2009. +// Copyright Adam Wulkiewicz 2014. +// +// 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/libs/range/ +// +#include + +#include +#include + +#include +#include + +#include +#include +#include + +#include "../copymovable.hpp" + +namespace boost +{ + template + bool check_moved(It first, It last, bool expected) + { + for ( ; first != last ; ++first ) + if ( first->moved() != expected ) + return false; + return true; + } + + template< class Container > + void moved_test_impl( Container& c) + { + using namespace boost::adaptors; + + { + Container source(c.begin(), c.end()); + check_moved(source.begin(), source.end(), false); + std::vector< copy_movable > test_result1; + boost::push_back(test_result1, source | moved); + check_moved(source.begin(), source.end(), true); + + BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(), + test_result1.begin(), test_result1.end() ); + } + + { + Container source(c.begin(), c.end()); + check_moved(source.begin(), source.end(), false); + std::vector< copy_movable > test_result2; + boost::push_back(test_result2, adaptors::move(source)); + check_moved(source.begin(), source.end(), true); + + BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(), + test_result2.begin(), test_result2.end() ); + } + } + + template< class Container > + void moved_test_impl() + { + using namespace boost::assign; + + Container c; + + // Test empty + moved_test_impl(c); + + // Test one + copy_movable c1(1); + c += c1; + moved_test_impl(c); + + // Test many + copy_movable c2(2); + copy_movable c3(3); + c += c2, c3; + moved_test_impl(c); + } + + void moved_test() + { + moved_test_impl< std::vector< copy_movable > >(); + moved_test_impl< std::list< copy_movable > >(); + } +} + + +boost::unit_test::test_suite* +init_unit_test_suite(int argc, char* argv[]) +{ + boost::unit_test::test_suite* test + = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.moved" ); + + test->add( BOOST_TEST_CASE( &boost::moved_test ) ); + + return test; +} diff --git a/test/algorithm_test/move.cpp b/test/algorithm_test/move.cpp new file mode 100644 index 000000000..422a95bf1 --- /dev/null +++ b/test/algorithm_test/move.cpp @@ -0,0 +1,114 @@ +// Boost.Range library +// +// Copyright Neil Groves 2009. +// Copyright Adam Wulkiewicz 2014. +// +// 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/libs/range/ +// +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../copymovable.hpp" + +namespace boost +{ + namespace + { + template + bool check_moved(It first, It last, bool expected) + { + for ( ; first != last ; ++first ) + if ( first->moved() != expected ) + return false; + return true; + } + + template< class Container > + void test_move_impl(bool expect_move) + { + typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; + + std::vector input; + input.push_back(copy_movable(1)); + input.push_back(copy_movable(2)); + input.push_back(copy_movable(3)); + + { + Container source(input.begin(), input.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), false)); + + std::vector target; + target.resize(source.size()); + + typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector >::type iterator_t; + + iterator_t it = boost::move(source, target.begin()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), expect_move)); + + BOOST_CHECK( it == target.end() ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + target.begin(), target.end(), + input.begin(), input.end() + ); + } + + { + Container source(input.begin(), input.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), false)); + + std::vector target; + target.resize(source.size()); + + typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector >::type iterator_t; + + iterator_t it = boost::move(boost::make_iterator_range(source), target.begin()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), expect_move)); + + BOOST_CHECK( it == target.end() ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + target.begin(), target.end(), + input.begin(), input.end() + ); + } + } + + void test_move() + { + test_move_impl< std::vector >(true); + test_move_impl< std::list >(true); + test_move_impl< std::set >(false); + test_move_impl< std::multiset >(false); + } + } +} + + +boost::unit_test::test_suite* +init_unit_test_suite(int argc, char* argv[]) +{ + boost::unit_test::test_suite* test + = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.move" ); + + test->add( BOOST_TEST_CASE( &boost::test_move ) ); + + return test; +} diff --git a/test/algorithm_test/move_backward.cpp b/test/algorithm_test/move_backward.cpp new file mode 100644 index 000000000..ee33a0a7a --- /dev/null +++ b/test/algorithm_test/move_backward.cpp @@ -0,0 +1,114 @@ +// Boost.Range library +// +// Copyright Neil Groves 2009. +// Copyright Adam Wulkiewicz 2014. +// +// 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/libs/range/ +// +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../copymovable.hpp" + +namespace boost +{ + namespace + { + template + bool check_moved(It first, It last, bool expected) + { + for ( ; first != last ; ++first ) + if ( first->moved() != expected ) + return false; + return true; + } + + template< class Container > + void test_move_backward_impl(bool expect_move) + { + typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; + + std::vector input; + input.push_back(copy_movable(1)); + input.push_back(copy_movable(2)); + input.push_back(copy_movable(3)); + + { + Container source(input.begin(), input.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), false)); + + std::vector target; + target.resize(source.size()); + + typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector >::type iterator_t; + + iterator_t it = boost::move_backward(source, target.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), expect_move)); + + BOOST_CHECK( it == target.begin() ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + target.begin(), target.end(), + input.begin(), input.end() + ); + } + + { + Container source(input.begin(), input.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), false)); + + std::vector target; + target.resize(source.size()); + + typedef BOOST_DEDUCED_TYPENAME range_iterator< std::vector >::type iterator_t; + + iterator_t it = boost::move_backward(boost::make_iterator_range(source), target.end()); + + BOOST_CHECK(check_moved(source.begin(), source.end(), expect_move)); + + BOOST_CHECK( it == target.begin() ); + + BOOST_CHECK_EQUAL_COLLECTIONS( + target.begin(), target.end(), + input.begin(), input.end() + ); + } + } + + void test_move_backward() + { + test_move_backward_impl< std::vector >(true); + test_move_backward_impl< std::list >(true); + test_move_backward_impl< std::set >(false); + test_move_backward_impl< std::multiset >(false); + } + } +} + + +boost::unit_test::test_suite* +init_unit_test_suite(int argc, char* argv[]) +{ + boost::unit_test::test_suite* test + = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.move_backward" ); + + test->add( BOOST_TEST_CASE( &boost::test_move_backward ) ); + + return test; +} diff --git a/test/copymovable.hpp b/test/copymovable.hpp new file mode 100644 index 000000000..215918689 --- /dev/null +++ b/test/copymovable.hpp @@ -0,0 +1,64 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2009. +// (C) Copyright Adam Wulkiewicz 2014. +// +// 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) +// +////////////////////////////////////////////////////////////////////////////// +#ifndef BOOST_MOVE_TEST_COPYMOVABLE_HPP +#define BOOST_MOVE_TEST_COPYMOVABLE_HPP + +#include + +//[movable_definition +//header file "copy_movable.hpp" +#include + +#include + +//A copy_movable class +class copy_movable +{ + BOOST_COPYABLE_AND_MOVABLE(copy_movable) + int value_; + + public: + copy_movable(int v = 1) : value_(v) { BOOST_ASSERT(v > 0); } + + //Move constructor and assignment + copy_movable(BOOST_RV_REF(copy_movable) m) + { value_ = m.value_; m.value_ = 0; } + + copy_movable(const copy_movable & m) + { value_ = m.value_; } + + copy_movable & operator=(BOOST_RV_REF(copy_movable) m) + { value_ = m.value_; m.value_ = 0; return *this; } + + copy_movable & operator=(BOOST_COPY_ASSIGN_REF(copy_movable) m) + { value_ = m.value_; return *this; } + + bool moved() const //Observer + { return value_ == 0; } + + friend bool operator==(copy_movable const& l, copy_movable const& r) + { return l.value_ == r.value_; } + + friend bool operator!=(copy_movable const& l, copy_movable const& r) + { return l.value_ != r.value_; } + + friend bool operator<(copy_movable const& l, copy_movable const& r) + { return l.value_ < r.value_; } + + friend std::ostream & operator<<(std::ostream & os, copy_movable const& r) + { return os << r.value_; } +}; + +//] + +#include + +#endif //BOOST_MOVE_TEST_COPYMOVABLE_HPP