Boost logo

Boost-Commit :

From: garcia_at_[hidden]
Date: 2007-08-06 15:40:46


Author: garcia
Date: 2007-08-06 15:40:43 EDT (Mon, 06 Aug 2007)
New Revision: 38482
URL: http://svn.boost.org/trac/boost/changeset/38482

Log:
Fixes #1127
array views were not handling negative strides properly. Thanks to Phil Richards for pointing that out.

Text files modified:
   trunk/boost/multi_array/base.hpp | 7 +++++--
   trunk/libs/multi_array/test/slice.cpp | 22 ++++++++++++++++++++++
   2 files changed, 27 insertions(+), 2 deletions(-)

Modified: trunk/boost/multi_array/base.hpp
==============================================================================
--- trunk/boost/multi_array/base.hpp (original)
+++ trunk/boost/multi_array/base.hpp 2007-08-06 15:40:43 EDT (Mon, 06 Aug 2007)
@@ -437,13 +437,16 @@
       index start = current_range.get_start(default_start);
       index finish = current_range.get_finish(default_finish);
       index index_factor = current_range.stride();
- index len = (finish - start + (index_factor - 1)) / index_factor;
+
+ // integral trick for ceiling((finish-start) / index_factor)
+ index shrinkage = index_factor > 0 ? 1 : -1;
+ index len = (finish - start + (index_factor - shrinkage)) / index_factor;
 
       BOOST_ASSERT(index_bases[n] <= start &&
                    start <= index_bases[n]+index(extents[n]));
       BOOST_ASSERT(index_bases[n] <= finish &&
                    finish <= index_bases[n]+index(extents[n]));
- BOOST_ASSERT(index_factor > 0);
+ BOOST_ASSERT(index_factor != 0);
 
       // the array data pointer is modified to account for non-zero
       // bases during slicing (see [Garcia] for the math involved)

Modified: trunk/libs/multi_array/test/slice.cpp
==============================================================================
--- trunk/libs/multi_array/test/slice.cpp (original)
+++ trunk/libs/multi_array/test/slice.cpp 2007-08-06 15:40:43 EDT (Mon, 06 Aug 2007)
@@ -90,6 +90,28 @@
         BOOST_CHECK(B(elmts) == A[idx0+i][idx1+1][idx2+j*2]);
       }
   }
+
+ // Flip the third dimension
+ {
+ typename ViewTraits::array_view3 B = A[
+ indices[range(idx0+0,idx0+2)]
+ [range(idx1+0,idx1+2)]
+ [range(idx2+2,idx2+0,-1)]
+ ];
+
+ // typename ViewTraits::array_view3 B =
+ // A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]];
+
+ for (index i = 0; i != 2; ++i)
+ for (index j = 0; j != 2; ++j)
+ for (index k = 0; k != 2; ++k) {
+ BOOST_CHECK(B[i][j][k] == A[idx0+i][idx1+j][idx2+2-k]);
+ boost::array<index,3> elmts;
+ elmts[0]=i; elmts[1]=j; elmts[2]=k;
+ BOOST_CHECK(B(elmts) == A[idx0+i][idx1+j][idx2+2-k]);
+ }
+ }
+
   ++tests_run;
 }
 


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