mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Extended next/prior using patch from Daniel Walker (Daniel.Walker-at-bowneglobal.com)
[SVN r21382]
This commit is contained in:
parent
be43ba1569
commit
dfc320124f
@ -8,9 +8,14 @@
|
|||||||
|
|
||||||
// See http://www.boost.org/libs/utility for documentation.
|
// See http://www.boost.org/libs/utility for documentation.
|
||||||
|
|
||||||
|
// Revision History
|
||||||
|
// 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
|
||||||
|
|
||||||
#ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
|
#ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
|
||||||
#define BOOST_NEXT_PRIOR_HPP_INCLUDED
|
#define BOOST_NEXT_PRIOR_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
// Helper functions for classes like bidirectional iterators not supporting
|
// Helper functions for classes like bidirectional iterators not supporting
|
||||||
@ -19,15 +24,30 @@ namespace boost {
|
|||||||
// Usage:
|
// Usage:
|
||||||
// const std::list<T>::iterator p = get_some_iterator();
|
// const std::list<T>::iterator p = get_some_iterator();
|
||||||
// const std::list<T>::iterator prev = boost::prior(p);
|
// const std::list<T>::iterator prev = boost::prior(p);
|
||||||
|
// const std::list<T>::iterator next = boost::next(prev, 2);
|
||||||
|
|
||||||
// Contributed by Dave Abrahams
|
// Contributed by Dave Abrahams
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline T next(T x) { return ++x; }
|
inline T next(T x) { return ++x; }
|
||||||
|
|
||||||
|
template <class T, class Distance>
|
||||||
|
inline T next(T x, Distance n)
|
||||||
|
{
|
||||||
|
std::advance(x, n);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline T prior(T x) { return --x; }
|
inline T prior(T x) { return --x; }
|
||||||
|
|
||||||
|
template <class T, class Distance>
|
||||||
|
inline T prior(T x, Distance n)
|
||||||
|
{
|
||||||
|
std::advance(x, -n);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
#endif // BOOST_NEXT_PRIOR_HPP_INCLUDED
|
#endif // BOOST_NEXT_PRIOR_HPP_INCLUDED
|
||||||
|
31
test/Jamfile
31
test/Jamfile
@ -16,25 +16,28 @@ import testing ;
|
|||||||
# Make tests run by default.
|
# Make tests run by default.
|
||||||
DEPENDS all : test ;
|
DEPENDS all : test ;
|
||||||
|
|
||||||
|
local test_monitor = <lib>@boost/libs/test/build/boost_test_exec_monitor ;
|
||||||
|
|
||||||
test-suite utility
|
test-suite utility
|
||||||
:
|
:
|
||||||
[ run ../iterator_traits_test.cpp ]
|
[ run ../iterator_traits_test.cpp ]
|
||||||
[ run ../iterators_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../iterators_test.cpp $(test_monitor) ]
|
||||||
[ compile-fail ../noncopyable_test.cpp ]
|
[ compile-fail ../noncopyable_test.cpp ]
|
||||||
[ run ../numeric_traits_test.cpp ]
|
[ run ../numeric_traits_test.cpp ]
|
||||||
[ run ../operators_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../operators_test.cpp $(test_monitor) ]
|
||||||
[ run ../binary_search_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../binary_search_test.cpp $(test_monitor) ]
|
||||||
[ run ../call_traits_test.cpp : -u ]
|
[ run ../call_traits_test.cpp : -u ]
|
||||||
[ compile-fail ../checked_delete_test.cpp ]
|
[ compile-fail ../checked_delete_test.cpp ]
|
||||||
[ run ../compressed_pair_test.cpp <lib>../../test/build/boost_test_exec_monitor : -u ]
|
[ run ../compressed_pair_test.cpp $(test_monitor) : -u ]
|
||||||
[ run ../addressof_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../addressof_test.cpp $(test_monitor) ]
|
||||||
[ run ../ref_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../ref_test.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_constructors.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_constructors.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_dummy_arg_disambiguation.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_dummy_arg_disambiguation.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_lazy.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_lazy.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_lazy_test.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_lazy_test.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_member_templates.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_member_templates.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_namespace_disambiguation.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_namespace_disambiguation.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_no_disambiguation.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_no_disambiguation.cpp $(test_monitor) ]
|
||||||
[ run ../enable_if_partial_specializations.cpp <lib>../../test/build/boost_test_exec_monitor ]
|
[ run ../enable_if_partial_specializations.cpp $(test_monitor) ]
|
||||||
|
[ run ../next_prior_test.cpp $(test_monitor) ]
|
||||||
;
|
;
|
||||||
|
79
test/next_prior_test.cpp
Executable file
79
test/next_prior_test.cpp
Executable file
@ -0,0 +1,79 @@
|
|||||||
|
// Boost test program for next() and prior() utilities.
|
||||||
|
|
||||||
|
// Copyright 2003 Daniel Walker. Use, modification, and distribution
|
||||||
|
// are subject to the Boost Software License, Version 1.0. (See
|
||||||
|
// accompanying file LICENSE_1_0.txt or a copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt.)
|
||||||
|
|
||||||
|
// See http://www.boost.org/libs/utility for documentation.
|
||||||
|
|
||||||
|
// Revision History 13 Dec 2003 Initial Version (Daniel Walker)
|
||||||
|
|
||||||
|
// next() and prior() are replacements for operator+ and operator- for
|
||||||
|
// non-random-access iterators. The semantics of these operators are
|
||||||
|
// such that after executing j = i + n, std::distance(i, j) equals
|
||||||
|
// n. Tests are provided to ensure next() has the same
|
||||||
|
// result. Parallel tests are provided for prior(). The tests call
|
||||||
|
// next() and prior() several times. next() and prior() are very
|
||||||
|
// simple functions, though, and it would be very strange if these
|
||||||
|
// tests were to fail.
|
||||||
|
|
||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <boost/next_prior.hpp>
|
||||||
|
|
||||||
|
template<class RandomAccessIterator, class ForwardIterator>
|
||||||
|
bool plus_one_test(RandomAccessIterator first, RandomAccessIterator last, ForwardIterator first2)
|
||||||
|
{
|
||||||
|
RandomAccessIterator i = first;
|
||||||
|
ForwardIterator j = first2;
|
||||||
|
while(i != last)
|
||||||
|
i = i + 1, j = boost::next(j);
|
||||||
|
return std::distance(first, i) == std::distance(first2, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class RandomAccessIterator, class ForwardIterator>
|
||||||
|
bool plus_n_test(RandomAccessIterator first, RandomAccessIterator last, ForwardIterator first2)
|
||||||
|
{
|
||||||
|
RandomAccessIterator i = first;
|
||||||
|
ForwardIterator j = first2;
|
||||||
|
for(int n = 0; i != last; ++n)
|
||||||
|
i = first + n, j = boost::next(first2, n);
|
||||||
|
return std::distance(first, i) == std::distance(first2, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class RandomAccessIterator, class BidirectionalIterator>
|
||||||
|
bool minus_one_test(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator last2)
|
||||||
|
{
|
||||||
|
RandomAccessIterator i = last;
|
||||||
|
BidirectionalIterator j = last2;
|
||||||
|
while(i != first)
|
||||||
|
i = i - 1, j = boost::prior(j);
|
||||||
|
return std::distance(i, last) == std::distance(j, last2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class RandomAccessIterator, class BidirectionalIterator>
|
||||||
|
bool minus_n_test(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator last2)
|
||||||
|
{
|
||||||
|
RandomAccessIterator i = last;
|
||||||
|
BidirectionalIterator j = last2;
|
||||||
|
for(int n = 0; i != first; ++n)
|
||||||
|
i = last - n, j = boost::prior(last2, n);
|
||||||
|
return std::distance(i, last) == std::distance(j, last2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
std::vector<int> x(8);
|
||||||
|
std::list<int> y(x.begin(), x.end());
|
||||||
|
|
||||||
|
BOOST_REQUIRE(plus_one_test(x.begin(), x.end(), y.begin()));
|
||||||
|
BOOST_REQUIRE(plus_n_test(x.begin(), x.end(), y.begin()));
|
||||||
|
BOOST_REQUIRE(minus_one_test(x.begin(), x.end(), y.end()));
|
||||||
|
BOOST_REQUIRE(minus_n_test(x.begin(), x.end(), y.end()));
|
||||||
|
return 0;
|
||||||
|
}
|
28
utility.htm
28
utility.htm
@ -40,15 +40,33 @@
|
|||||||
<pre>template <class T>
|
<pre>template <class T>
|
||||||
T next(T x) { return ++x; }
|
T next(T x) { return ++x; }
|
||||||
|
|
||||||
template <class X>
|
template <class T, class Distance>
|
||||||
T prior(T x) { return --x; }</pre>
|
T next(T x, Distance n)
|
||||||
|
{
|
||||||
|
std::advance(x, n);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T prior(T x) { return --x; }
|
||||||
|
|
||||||
|
template <class T, class Distance>
|
||||||
|
T prior(T x, Distance n)
|
||||||
|
{
|
||||||
|
std::advance(x, -n);
|
||||||
|
return x;
|
||||||
|
}</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<p>Usage is simple:</p>
|
<p>Usage is simple:</p>
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<pre>const std::list<T>::iterator p = get_some_iterator();
|
<pre>const std::list<T>::iterator p = get_some_iterator();
|
||||||
const std::list<T>::iterator prev = boost::prior(p);</pre>
|
const std::list<T>::iterator prev = boost::prior(p);
|
||||||
|
const std::list<T>::iterator next = boost::next(prev, 2);</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.</p>
|
<p>The distance from the given iterator should be supplied as an absolute value. For
|
||||||
|
example, the iterator four iterators prior to the given iterator <code>p</code>
|
||||||
|
may be obtained by <code>prior(p, 4)</code>.</p>
|
||||||
|
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>. Two-argument versions by Daniel Walker.</p>
|
||||||
<h2><a name="Class_noncopyable">Class noncopyable</a></h2>
|
<h2><a name="Class_noncopyable">Class noncopyable</a></h2>
|
||||||
<p>Class <strong>noncopyable</strong> is a base class. Derive your own class
|
<p>Class <strong>noncopyable</strong> is a base class. Derive your own class
|
||||||
from <strong>noncopyable</strong> when you want to prohibit copy construction
|
from <strong>noncopyable</strong> when you want to prohibit copy construction
|
||||||
@ -122,7 +140,7 @@ void f() {
|
|||||||
<p>See <a href="base_from_member.html">separate documentation</a>.</p>
|
<p>See <a href="base_from_member.html">separate documentation</a>.</p>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
|
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
|
||||||
-->01 September, 2003<!--webbot bot="Timestamp" endspan i-checksum="38582"
|
-->23 December, 2003<!--webbot bot="Timestamp" endspan i-checksum="38582"
|
||||||
-->
|
-->
|
||||||
</p>
|
</p>
|
||||||
<p>© Copyright boost.org 1999-2003. Permission to copy, use, modify, sell and distribute
|
<p>© Copyright boost.org 1999-2003. Permission to copy, use, modify, sell and distribute
|
||||||
|
Loading…
x
Reference in New Issue
Block a user