|
Ublas : |
From: Vardan Akopian (vardan.akopian_at_[hidden])
Date: 2007-05-30 07:45:24
On 5/30/07, Gunter Winkler <guwi17_at_[hidden]> wrote:
> On Tuesday 29 May 2007 09:46, Langeland, Stian (GE Healthcare) wrote:
> > Hi,
> >
> > I have been trying to create a vector of 3 elements (a point) without
> > the size element stored in the object. The reason for this is that I
> > need to be able to have all the elements consecutive in memory when
> > gathering several points in a std::vector. In this way all points in the
> > std::vector can be uploaded to a graphics card using a single memcopy.
> > Can this be acheived using ublas? I have not been able to find a way.
>
> Did you already have a look at Example3? There is a "point.h" header that
> exactly implements such a type.
> http://freenet-homepage.de/guwi17/ublas/examples/
> This type behaves nearly a an ublas vector. Alternatively you should take a
> look at tvmet http://tvmet.sourceforge.net/ which might be much faster than
> ublas (for small vectors and matrices)
>
Wouldn't it be better to have a simple fixed_size_array class in
storage.hpp, which is pretty much the same as bounded_array, except it
does not allow any resizing (and therefore doesn't need the size_
member)? A working example below (can be added to storage.hpp as is).
-Vardan
<code>
// Fixed size array
template<class T, std::size_t N>
class fixed_size_array:
public storage_array<fixed_size_array<T, N> > {
typedef fixed_size_array<T, N> self_type;
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
typedef const T &const_reference;
typedef T &reference;
typedef const T *const_pointer;
typedef T *pointer;
typedef const_pointer const_iterator;
typedef pointer iterator;
// Construction and destruction
BOOST_UBLAS_INLINE
fixed_size_array () {
}
explicit BOOST_UBLAS_INLINE
fixed_size_array (size_type size) {
BOOST_UBLAS_CHECK (size == N, bad_size ());
}
BOOST_UBLAS_INLINE
fixed_size_array (size_type size, const value_type &init) {
BOOST_UBLAS_CHECK (size == N, bad_size ());
std::fill (data_, data_ + N, init) ;
}
BOOST_UBLAS_INLINE
fixed_size_array (const fixed_size_array &c) {
std::copy (c.data_, c.data_ + N, data_);
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size) {
BOOST_UBLAS_CHECK (size == N, bad_size ());
}
BOOST_UBLAS_INLINE
void resize (size_type size, value_type init) {
BOOST_UBLAS_CHECK (size == N, bad_size ());
std::fill (data_, data_ + N, init);
}
// Random Access Container
BOOST_UBLAS_INLINE
size_type max_size () const {
return N;
}
BOOST_UBLAS_INLINE
bool empty () const {
return N == 0;
}
BOOST_UBLAS_INLINE
size_type size () const {
return N;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator [] (size_type i) const {
BOOST_UBLAS_CHECK (i < N, bad_index ());
return data_ [i];
}
BOOST_UBLAS_INLINE
reference operator [] (size_type i) {
BOOST_UBLAS_CHECK (i < N, bad_index ());
return data_ [i];
}
// Assignment
BOOST_UBLAS_INLINE
fixed_size_array &operator = (const fixed_size_array &a) {
if (this != &a) {
std::copy (a.data_, a.data_ + N, data_);
}
return *this;
}
BOOST_UBLAS_INLINE
fixed_size_array &assign_temporary (fixed_size_array &a) {
*this = a;
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (fixed_size_array &a) {
if (this != &a) {
std::swap_ranges (data_, data_ + N, a.data_);
}
}
BOOST_UBLAS_INLINE
friend void swap (fixed_size_array &a1, fixed_size_array &a2) {
a1.swap (a2);
}
BOOST_UBLAS_INLINE
const_iterator begin () const {
return data_;
}
BOOST_UBLAS_INLINE
const_iterator end () const {
return data_ + N;
}
BOOST_UBLAS_INLINE
iterator begin () {
return data_;
}
BOOST_UBLAS_INLINE
iterator end () {
return data_ + N;
}
// Reverse iterators
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
BOOST_UBLAS_INLINE
const_reverse_iterator rbegin () const {
return const_reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator rend () const {
return const_reverse_iterator (begin ());
}
BOOST_UBLAS_INLINE
reverse_iterator rbegin () {
return reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
reverse_iterator rend () {
return reverse_iterator (begin ());
}
private:
value_type data_ [N];
};
</code>