|
Boost : |
From: Fernando Luis Cacciola Carballal (fcacciola_at_[hidden])
Date: 2000-05-30 08:23:48
Hi everyone!
I've been listening to this mailing list and peering inside boost
stuff for some while.
I think it's great and I even started to use some of the general
stuff, such as types_traits, operators, etc...
I'd like to contribute to class array<> with something I found:
I developed a very similiar class (which I called fixedarray<>).
I'was particularly concerned about filling and copy operations, so I
used the following technique:
template<int size,typename T>
struct arrayops
{
static inline void fill ( T * a , const T & val )
{
arrayops<size-1,T>::fill(a,val);
a[size-1]=val;
}
static inline void copy ( const T * src , T * dest )
{
arrayops<size-1,T>::copy(src,dest);
dest[size-1]=src[size-1];
}
static inline void add ( const T * a , const T * b , T * c )
{
arrayops<size-1,T>::add(a,b,c);
c[size-1]=a[size-1] + b[size-1];
}
static inline void sub ( const T * a , const T * b , T * c )
{
arrayops<size-1,T>::sub(a,b,c);
c[size-1]=a[size-1] - b[size-1];
}
static inline void mul ( const T * a , const T * b , T * c )
{
arrayops<size-1,T>::mul(a,b,c);
c[size-1]=a[size-1] * b[size-1];
}
static inline void div ( const T * a , const T * b , T * c )
{
arrayops<size-1,T>::div(a,b,c);
c[size-1]=a[size-1] / b[size-1];
}
} ;
template<typename T>
struct arrayops<0,T>
{
static inline void fill ( T * , const T & ) {}
static inline void copy ( const T * , T * ) {}
static inline void add ( const T * , const T * , T * ) {}
static inline void sub ( const T * , const T * , T * ) {}
static inline void mul ( const T * , const T * , T * ) {}
static inline void div ( const T * , const T * , T * ) {}
} ;
fixedarray ( const T & value )
{ arrayops<_size,T>::fill ( data , value ) ; }
fixedarray ( const T * array )
{ arrayops<_size,T>::copy ( array , data ) ; }
fixedarray ( const fixedarray & rhs )
{ arrayops<_size,T>::copy ( rhs.data , data ) ; }
fixedarray& operator = ( const fixedarray & rhs )
{ arrayops<_size,T>::copy ( rhs.data , data ) ; return * this ; }
The idea is simple, the compiler will generate N lines of the
particular form of the operation, so no loop is used. I've tested
this and its really faster than any kind of iteration. The drawback,
though, is that it might get too larger for big Ns.
Hope this helps.
Fernando Cacciola
fcacciola_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk