Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84701 - in trunk: boost/range libs/range/test
From: zeratul976_at_[hidden]
Date: 2013-06-08 16:41:43


Author: nathanridge
Date: 2013-06-08 16:41:43 EDT (Sat, 08 Jun 2013)
New Revision: 84701
URL: http://svn.boost.org/trac/boost/changeset/84701

Log:
[range] Fix off-by-one error in implementation of irange() (refs #7731).

Text files modified:
   trunk/boost/range/irange.hpp | 2 +-
   trunk/libs/range/test/irange.cpp | 18 ++++++------------
   2 files changed, 7 insertions(+), 13 deletions(-)

Modified: trunk/boost/range/irange.hpp
==============================================================================
--- trunk/boost/range/irange.hpp Sat Jun 8 14:22:36 2013 (r84700)
+++ trunk/boost/range/irange.hpp 2013-06-08 16:41:43 EDT (Sat, 08 Jun 2013) (r84701)
@@ -217,7 +217,7 @@
         const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(step_size >= 0 ? step_size : -step_size);
         const Integer l = step_size >= 0 ? last : first;
         const Integer f = step_size >= 0 ? first : last;
- const std::ptrdiff_t num_steps = (l + ((l-f) % sz) - f) / sz;
+ const std::ptrdiff_t num_steps = (l - f) / sz + ((l - f) % sz ? 1 : 0);
         BOOST_ASSERT(num_steps >= 0);
        
         return strided_integer_range<Integer>(

Modified: trunk/libs/range/test/irange.cpp
==============================================================================
--- trunk/libs/range/test/irange.cpp Sat Jun 8 14:22:36 2013 (r84700)
+++ trunk/libs/range/test/irange.cpp 2013-06-08 16:41:43 EDT (Sat, 08 Jun 2013) (r84701)
@@ -35,15 +35,6 @@
                                        reference.begin(), reference.end() );
     }
     
- template<typename Integer>
- std::ptrdiff_t test_irange_calculate_num_steps(Integer first, Integer last, int step)
- {
- const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(step >= 0 ? step : -step);
- const std::ptrdiff_t l = static_cast<std::ptrdiff_t>(step >= 0 ? last : first);
- const std::ptrdiff_t f = static_cast<std::ptrdiff_t>(step >= 0 ? first : last);
- return (l + ((l-f) % sz) - f) / sz;
- }
-
     // Test an integer range with a runtime specified step size.
     template<typename Integer, typename IntegerInput>
     void test_irange_impl(IntegerInput first, IntegerInput last, int step)
@@ -58,9 +49,12 @@
         
         std::vector<Integer> reference;
 
- const std::ptrdiff_t num_steps = test_irange_calculate_num_steps(first, last, step);
- Integer current_value = first;
- for (std::ptrdiff_t i = 0; i < num_steps; ++i, current_value += step)
+ const std::ptrdiff_t first_p = static_cast<std::ptrdiff_t>(first);
+ const std::ptrdiff_t last_p = static_cast<std::ptrdiff_t>(last);
+ const std::ptrdiff_t step_p = static_cast<std::ptrdiff_t>(step);
+ for (std::ptrdiff_t current_value = first_p;
+ step_p >= 0 ? current_value < last_p : current_value > last_p;
+ current_value += step_p)
             reference.push_back(current_value);
 
         std::vector<Integer> test;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk