mirror of
https://github.com/boostorg/iterator.git
synced 2025-07-23 09:27:15 +02:00
Merge branch 'develop'
# Conflicts: # include/boost/iterator/reverse_iterator.hpp
This commit is contained in:
40
.travis.yml
Normal file
40
.travis.yml
Normal file
@ -0,0 +1,40 @@
|
||||
# Copyright 2016 Peter Dimov
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
|
||||
language: cpp
|
||||
|
||||
sudo: false
|
||||
|
||||
python: "2.7"
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- develop
|
||||
|
||||
install:
|
||||
- cd ..
|
||||
- git clone -b $TRAVIS_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
|
||||
- cd boost-root
|
||||
- git submodule update --init --depth 1 tools/build
|
||||
- git submodule update --init --depth 1 libs/config
|
||||
- git submodule update --init --depth 1 tools/boostdep
|
||||
- cp -r $TRAVIS_BUILD_DIR/* libs/iterator
|
||||
- python tools/boostdep/depinst/depinst.py iterator
|
||||
- ./bootstrap.sh
|
||||
- ./b2 headers
|
||||
|
||||
script:
|
||||
- TOOLSET=gcc,clang
|
||||
- if [ $TRAVIS_OS_NAME == osx ]; then TOOLSET=clang; fi
|
||||
- ./b2 --verbose-test libs/config/test//config_info toolset=$TOOLSET || true
|
||||
- ./b2 libs/iterator/test toolset=$TOOLSET
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: always
|
84
include/boost/iterator/advance.hpp
Normal file
84
include/boost/iterator/advance.hpp
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright (C) 2017 Michel Morin.
|
||||
//
|
||||
// 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_ITERATOR_ADVANCE_HPP
|
||||
#define BOOST_ITERATOR_ADVANCE_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/iterator/iterator_categories.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace iterators {
|
||||
|
||||
namespace detail {
|
||||
template <typename InputIterator, typename Distance>
|
||||
inline BOOST_CXX14_CONSTEXPR void
|
||||
advance_impl(
|
||||
InputIterator& it
|
||||
, Distance n
|
||||
, incrementable_traversal_tag
|
||||
)
|
||||
{
|
||||
while (n > 0) {
|
||||
++it;
|
||||
--n;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename BidirectionalIterator, typename Distance>
|
||||
inline BOOST_CXX14_CONSTEXPR void
|
||||
advance_impl(
|
||||
BidirectionalIterator& it
|
||||
, Distance n
|
||||
, bidirectional_traversal_tag
|
||||
)
|
||||
{
|
||||
if (n >= 0) {
|
||||
while (n > 0) {
|
||||
++it;
|
||||
--n;
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (n < 0) {
|
||||
--it;
|
||||
++n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename RandomAccessIterator, typename Distance>
|
||||
inline BOOST_CXX14_CONSTEXPR void
|
||||
advance_impl(
|
||||
RandomAccessIterator& it
|
||||
, Distance n
|
||||
, random_access_traversal_tag
|
||||
)
|
||||
{
|
||||
it += n;
|
||||
}
|
||||
}
|
||||
|
||||
namespace advance_adl_barrier {
|
||||
template <typename InputIterator, typename Distance>
|
||||
inline BOOST_CXX14_CONSTEXPR void
|
||||
advance(InputIterator& it, Distance n)
|
||||
{
|
||||
detail::advance_impl(
|
||||
it, n, typename iterator_traversal<InputIterator>::type()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
using namespace advance_adl_barrier;
|
||||
|
||||
} // namespace iterators
|
||||
|
||||
using iterators::advance;
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif
|
65
include/boost/iterator/distance.hpp
Normal file
65
include/boost/iterator/distance.hpp
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2017 Michel Morin.
|
||||
//
|
||||
// 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_ITERATOR_DISTANCE_HPP
|
||||
#define BOOST_ITERATOR_DISTANCE_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/iterator/iterator_categories.hpp>
|
||||
#include <boost/iterator/iterator_traits.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace iterators {
|
||||
|
||||
namespace detail {
|
||||
template <typename SinglePassIterator>
|
||||
inline BOOST_CXX14_CONSTEXPR typename iterator_difference<SinglePassIterator>::type
|
||||
distance_impl(
|
||||
SinglePassIterator first
|
||||
, SinglePassIterator last
|
||||
, single_pass_traversal_tag
|
||||
)
|
||||
{
|
||||
typename iterator_difference<SinglePassIterator>::type n = 0;
|
||||
while (first != last) {
|
||||
++first;
|
||||
++n;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
template <typename RandomAccessIterator>
|
||||
inline BOOST_CXX14_CONSTEXPR typename iterator_difference<RandomAccessIterator>::type
|
||||
distance_impl(
|
||||
RandomAccessIterator first
|
||||
, RandomAccessIterator last
|
||||
, random_access_traversal_tag
|
||||
)
|
||||
{
|
||||
return last - first;
|
||||
}
|
||||
}
|
||||
|
||||
namespace distance_adl_barrier {
|
||||
template <typename SinglePassIterator>
|
||||
inline BOOST_CXX14_CONSTEXPR typename iterator_difference<SinglePassIterator>::type
|
||||
distance(SinglePassIterator first, SinglePassIterator last)
|
||||
{
|
||||
return detail::distance_impl(
|
||||
first, last, typename iterator_traversal<SinglePassIterator>::type()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
using namespace distance_adl_barrier;
|
||||
|
||||
} // namespace iterators
|
||||
|
||||
using iterators::distance;
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif
|
@ -7,7 +7,6 @@
|
||||
#ifndef BOOST_REVERSE_ITERATOR_23022003THW_HPP
|
||||
#define BOOST_REVERSE_ITERATOR_23022003THW_HPP
|
||||
|
||||
#include <boost/next_prior.hpp>
|
||||
#include <boost/iterator.hpp>
|
||||
#include <boost/iterator/iterator_adaptor.hpp>
|
||||
|
||||
@ -40,14 +39,19 @@ namespace iterators {
|
||||
{}
|
||||
|
||||
private:
|
||||
typename super_t::reference dereference() const { return *boost::prior(this->base()); }
|
||||
typename super_t::reference dereference() const
|
||||
{
|
||||
Iterator it = this->base_reference();
|
||||
--it;
|
||||
return *it;
|
||||
}
|
||||
|
||||
void increment() { --this->base_reference(); }
|
||||
void decrement() { ++this->base_reference(); }
|
||||
|
||||
void advance(typename super_t::difference_type n)
|
||||
{
|
||||
this->base_reference() += -n;
|
||||
this->base_reference() -= n;
|
||||
}
|
||||
|
||||
template <class OtherIterator>
|
||||
|
@ -55,4 +55,7 @@ test-suite iterator
|
||||
|
||||
[ run minimum_category.cpp ]
|
||||
[ compile-fail minimum_category_compile_fail.cpp ]
|
||||
|
||||
[ run advance_test.cpp ]
|
||||
[ run distance_test.cpp ]
|
||||
;
|
||||
|
91
test/advance_test.cpp
Normal file
91
test/advance_test.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright (C) 2017 Michel Morin.
|
||||
//
|
||||
// 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)
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <boost/container/slist.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/iterator/advance.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
|
||||
int twice(int x) { return x + x; }
|
||||
|
||||
template <typename Iterator>
|
||||
void test_advance(Iterator it_from, Iterator it_to, int n)
|
||||
{
|
||||
boost::advance(it_from, n);
|
||||
BOOST_TEST(it_from == it_to);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int array[3] = {1, 2, 3};
|
||||
int* ptr1 = array;
|
||||
int* ptr2 = array + 3;
|
||||
|
||||
{
|
||||
test_advance(ptr1, ptr2, 3);
|
||||
test_advance(ptr2, ptr1, -3);
|
||||
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ptr1, twice)
|
||||
, boost::make_transform_iterator(ptr2, twice)
|
||||
, 3
|
||||
);
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ptr2, twice)
|
||||
, boost::make_transform_iterator(ptr1, twice)
|
||||
, -3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> ints(ptr1, ptr2);
|
||||
test_advance(ints.begin(), ints.end(), 3);
|
||||
test_advance(ints.end(), ints.begin(), -3);
|
||||
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ints.end(), twice)
|
||||
, boost::make_transform_iterator(ints.begin(), twice)
|
||||
, -3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
std::list<int> ints(ptr1, ptr2);
|
||||
test_advance(ints.begin(), ints.end(), 3);
|
||||
test_advance(ints.end(), ints.begin(), -3);
|
||||
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ints.end(), twice)
|
||||
, boost::make_transform_iterator(ints.begin(), twice)
|
||||
, -3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
boost::container::slist<int> ints(ptr1, ptr2);
|
||||
test_advance(ints.begin(), ints.end(), 3);
|
||||
|
||||
test_advance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
84
test/distance_test.cpp
Normal file
84
test/distance_test.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright (C) 2017 Michel Morin.
|
||||
//
|
||||
// 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)
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <boost/container/slist.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/iterator/distance.hpp>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
|
||||
int twice(int x) { return x + x; }
|
||||
|
||||
template <typename Iterator>
|
||||
void test_distance(Iterator it_from, Iterator it_to, int n)
|
||||
{
|
||||
BOOST_TEST(boost::distance(it_from, it_to) == n);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int array[3] = {1, 2, 3};
|
||||
int* ptr1 = array;
|
||||
int* ptr2 = array + 3;
|
||||
|
||||
{
|
||||
test_distance(ptr1, ptr2, 3);
|
||||
test_distance(ptr2, ptr1, -3);
|
||||
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ptr1, twice)
|
||||
, boost::make_transform_iterator(ptr2, twice)
|
||||
, 3
|
||||
);
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ptr2, twice)
|
||||
, boost::make_transform_iterator(ptr1, twice)
|
||||
, -3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> ints(ptr1, ptr2);
|
||||
test_distance(ints.begin(), ints.end(), 3);
|
||||
test_distance(ints.end(), ints.begin(), -3);
|
||||
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ints.end(), twice)
|
||||
, boost::make_transform_iterator(ints.begin(), twice)
|
||||
, -3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
std::list<int> ints(ptr1, ptr2);
|
||||
test_distance(ints.begin(), ints.end(), 3);
|
||||
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
boost::container::slist<int> ints(ptr1, ptr2);
|
||||
test_distance(ints.begin(), ints.end(), 3);
|
||||
|
||||
test_distance(
|
||||
boost::make_transform_iterator(ints.begin(), twice)
|
||||
, boost::make_transform_iterator(ints.end(), twice)
|
||||
, 3
|
||||
);
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user