Boost logo

Boost Users :

From: Nasatyr, Ben (London) (emile_nasatyr_at_[hidden])
Date: 2003-11-06 04:35:20


Can anyone help shed light on the following?

Suppose I have a 1d-array of size 10 (index base is 0). If I create a view
on [0,10), strided by 3, shouldn't the size of the result be 4,
corresponding to indices 0, 3, 6 and 9? Instead I get 3.

Please forgive me if I'm off the mark here, as I'm new to boost in general
and
MultiArray in particular.

The relevant line (assuming len => size) in boost/multi_array/base.hpp seems
to be

      index len = (finish - start) / index_factor;

(boost::detail::multi_array::multi_array_impl_base<>::generate_array_view()
line 427).
I would have written

      index len = 1 + (finish - 1 - start) / index_factor;

The following code sample should make all clear. Any clarification greatly
appreciated.

        Cheers,

                Ben

typedef boost::multi_array<double, 1> array_type;
typedef array_type::index index;
typedef array_type::index_range range;

index i;
int values = 0;

// Setup our base array and data.
// A is a 1-d array with size 10, indices 0,1,...,9 and values
0,1,...,9.
array_type A(boost::extents[10]);
        for(i = 0; i != 10; ++i)
                     A[i] = values++;

        for(i = 0; i != 10; ++i)
                     assert(A[i]==i);

        assert(A.size()==10);

// The following views of A should all be effectively the same,
// corresponding to indices 0,3,6,9 of A, so having
// size 4, indices 0,1,2,3 and values 0,3,6,9?
array_type::array_view<1>::type view1 =
        A[boost::indices[range(0,12,3)] ];
array_type::array_view<1>::type view2 =
        A[boost::indices[range(0,11,3)] ];
array_type::array_view<1>::type view3 =
        A[boost::indices[range(0,10,3)] ];

for(i = 0; i != 4; ++i)
{
        assert(A[i*3]==view1[i]);
        assert(A[i*3]==view2[i]);
        assert(A[i*3]==view3[i]);
}

assert(view1.size()==4);
assert(view2.size()==4); // Assert fails!?
assert(view3.size()==4); // Assert fails!?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net