Boost logo

Boost Users :

Subject: [Boost-users] [MultiArray] Postcondition of assignment
From: dariomt_at_[hidden]
Date: 2009-06-26 04:22:39


Hi list,

I see in the MultiArray concept documentation that the operator= has the
postcondition (*.this) == x;

But if I use overlapping memory this postcondition does not hold.

The attached program asserts trying to verify the postcondition. In this
program I have a 2D square matrix represented as a multi_array. I then
assign column j to row i. Depending on the relationship of i and j and how
the implementation of operator= does the traversal over the elements to be
copied, the program may not satisfy the mentioned postcondition.

May I suggest some note in the documentation about this.

Is it possible that the traversal can be done forward or backward to
guarantee the said postcondition even in the face of overlapping memory?

Thanks.

PS: Maybe I'm just using multi_array incorrectly. It that's the case, please
do help me to use it correctly.

#include <boost/multi_array.hpp>
#include <iostream>
#include <iterator>

typedef boost::multi_array<double, 2> array_type;

array_type::array_view<1>::type row(array_type & m, size_t i)
{
    typedef array_type::index_range range;
    array_type::index_gen indices;
    return m[ indices[i][range(0,m.shape()[1])] ];
}
array_type::array_view<1>::type col(array_type & m, size_t i)
{
    typedef array_type::index_range range;
    array_type::index_gen indices;
    return m[ indices[range(0,m.shape()[0])][i] ];
}

void print(array_type & m)
{
    typedef array_type::index_range range;
    array_type::index_gen indices;

    for (size_t i = 0; i < m.size(); ++i)
    {
        array_type::array_view<1>::type r = row(m, i);

        std::copy( r.begin(), r.end(),
std::ostream_iterator<double>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main()
{
    typedef array_type::index index;
    array_type A(boost::extents[3][3]);

    int values = 1;
    for(index i = 0; i < 3; ++i)
        for(index j = 0; j < 3; ++j)
            A[i][j] = values++;
    print(A);

    array_type::array_view<1>::type r = row(A, 2);
    array_type::array_view<1>::type c = col(A, 1);
    c = r;
    print(A);

    // this assert fires because the contents of the row
    // and the column are not equal after the assigment
    // the postcondition does not hold
    assert( c == r );

    return 0;
}



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