#ifndef glas_dense_vector_hpp #define glas_dense_vector_hpp #include #include #include #include #include #include #include #include #include namespace glas { template < class T, class Alloc = std::allocator< T >, class S = typename Alloc::size_type > class dense_vector : public detail::concepts { public: typedef S size_type ; /// VectorExpression typedef T value_type ; /// VectorExpression typedef value_type const & const_reference ; /// VectorExpression typedef value_type& reference ; /// VectorCollection typedef T const* const_iterator ; /// RandomAccessContainer typedef T* iterator ; /// RandomAccessContainer typedef boost::mpl::vector< strided_vector_collection_concept, dense_vector_collection_concept > concept_type ; public: /// DefaultConstructible dense_vector() : begin_( 0 ) , size_( 0 ) {} /// SizeConstructible dense_vector(size_type size) : size_( size ) { begin_ = alloc_.allocate( size ) ; utilities::construct_array( begin_, size ) ; } /// Assignable, Expression dense_vector(dense_vector const & that) : size_( that.size_ ) { begin_ = alloc_.allocate( size_ ) ; utilities::construct_array( begin_, size_ ) ; std::copy( that.begin_, that.begin_ + size_, begin_ ) ; } // Assignable dense_vector& operator=(dense_vector const & that) { assert( size() == that.size() ) ; std::copy( that.begin_, that.begin_ + size_, begin_ ) ; } /// Expression ~dense_vector() { utilities::destruct_array( begin_, size_ ) ; if (size_!=0) alloc_.deallocate( begin_, size_ ) ; } /// EqualityComparable bool operator==(dense_vector const & that) { assert( that.size_ == size_ ) ; for(size_type i = 0 ; i < size_ ; ++i ) { if ( (*this)[i] != that[i] ) return false ; } return true ; } /// EqualityComparable bool operator!=(dense_vector const & that) { return ! ( *this == that ) ; } /// VectorExpression, SizeConstructible inline size_type size() const { return size_ ; } /// SizeConstructible void resize(size_type n) { if ( n != size_ ) { if (size_!=0) alloc_.deallocate( begin_, size_ ) ; begin_ = alloc_.allocate( n ) ; size_ = n ; } } /// VectorExpression const_reference operator[](size_type i) const { assert( i < size_ ) ; return *(begin_ + i) ; } /// VectorExpression const_reference operator()(size_type i) const { return (*this)[i] ; } /// VectorCollection reference operator[](size_type i) { assert( i < size_ ) ; return *(begin_ + i) ; } /// VectorCollection reference operator()(size_type i) { return (*this)[i] ; } /// StridedVectorExpression ptrdiff_t stride() const { return 1 ; } /// RandomAccessContainer const_iterator begin() const { return begin_ ; } /// RandomAccessContainer iterator begin() { return begin_ ; } /// RandomAccessContainer const_iterator end() const { return begin_+size_ ; } /// RandomAccessContainer iterator end() { return begin_+size_ ; } private: T* begin_ ; size_type size_ ; // The allocator is stored as a member variable to guarantee that deallocation // is done with the same allocator that was used to perform the allocation. // Storing the allocator increases the memory footprint however but eventually // allocation will become part of the storage that will be a template argument // to the vector and storage models will not *have* to store an allocator. Alloc alloc_ ; } ; } #endif // glas_dense_vector_hpp