Boost logo

Boost :

From: Serge Barral (sbarral_at_[hidden])
Date: 2001-03-27 06:23:13


On Tue, 27 Mar 2001 00:23:39 Jeremy Siek wrote:
>
> Ron Garcia and I have been hard at work on the multi-dimensional array.
> The implementation is still a little ways off, but we'd like to present
> the interface we're aiming for:
>
> http://www.lsc.nd.edu/~jsiek/tlc/multi_array/libs/numeric/doc/multi_array.html
>
> Comments expected :)

Looks nice.

To improve safety, a constructor that takes a "shape_data"
object which is parametrized with the number of dimensions could be
provided. I mean something like:

multi_array( const shape_data< NumDims >& )

An array could then be instantiated in this way:

multi_array< double, 3 > a( shape[3][2][10] );

to make sure at compile-time that the shape is consistant with the rank of
the array (I also find the notation convenient).

Here is a quick and dirty implementation :

template< std::size_t rank > class shape_data {
public:
    shape_data( std::size_t first_dims[rank-1] , std::size_t last_dim )
    {
        for ( std::size_t i=0; i!=(rank-1); ++i )
            _dim[i] = first_dims[i];
        _dim[rank-1] = last_dim;
    }
    
    shape_data<rank+1> operator[] ( std::size_t n )
    {
        return shape_data<rank+1>( _dim, n );
    }
private:
    std::size_t _dim[rank];
};

struct shape_builder {
    shape_data<1> operator[] ( std::size_t n ) {
        return shape_data<1>( 0, n );
    }
};

shape_builder shape;

       Serge


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk