AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | List of all members
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc > Class Template Reference

Sparse Vector class template. More...

#include <AbstractLinAlgPack_SparseVectorClassDecl.hpp>

Inheritance diagram for AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >:
Inheritance graph
[legend]

Public Member Functions

SparseVector< T_Element,
T_Alloc > & 
operator= (const SparseVector< T_Element, T_Alloc > &sp_vec)
 Assignment operator. More...
 
SparseVector< T_Element,
T_Alloc > & 
operator= (const SparseVectorSlice< T_Element > &sp_vec_slc)
 Assignment operator. More...
 
EOverLap overlap (const SparseVectorSlice< T_Element > &sv) const
 

Public Types.

typedef T_Alloc allocator_type
 
typedef T_Element element_type
 
typedef
AbstractLinAlgPack::size_type 
size_type
 
typedef ptrdiff_t difference_type
 
typedef element_typeiterator
 
typedef const element_typeconst_iterator
 
typedef std::reverse_iterator
< iterator
reverse_iterator
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 
typedef
SparseVectorUtilityPack::DoesNotExistException 
DoesNotExistException
 
typedef
SparseVectorUtilityPack::NotSortedException 
NotSortedException
 
typedef
SparseVectorUtilityPack::DuplicateIndexesException 
DuplicateIndexesException
 
typedef
SparseVectorUtilityPack::OutOfRoomException 
OutOfRoomException
 
typedef
SparseVectorUtilityPack::UnsizedException 
UnsizedException
 
typedef
SparseVectorUtilityPack::NoNonZeroElementsException 
NoNonZeroElementsException
 

Constuctors

 SparseVector (const allocator_type &alloc=allocator_type())
 Constructs a sparse vector with no elements (nz() == dim() == 0#) and assumes the elements are not sorted. More...
 
 SparseVector (bool assume_sorted, const allocator_type &alloc=allocator_type())
 Constructs a sparse vector with no elements (nz() == dim() == 0#). More...
 
 SparseVector (size_type size, size_type max_nz, difference_type offset=0, bool assume_sorted=false, const allocator_type &alloc=allocator_type())
 Constructs a sparse vector of size #size# with storage for max_nz# elements (nz() == 0#) More...
 
 SparseVector (const SparseVector< T_Element, T_Alloc > &sp_vec)
 Constructs a sparse vector from another sparse vector. More...
 
 SparseVector (SparseVectorSlice< T_Element > sp_vec_slc, const allocator_type &alloc=allocator_type())
 Constructs a sparse vector of from a sparse vector slice. More...
 
 ~SparseVector ()
 Destructor (frees storage for elements). More...
 

SparseVectorTemplateInterface for linear algebra operations

size_type dim () const
 Return the number of elements in the full vector. More...
 
size_type nz () const
 Return the number of non-zero elements. More...
 
difference_type offset () const
 Return the offset for the indexes (ith real index = begin()[i-1]->index() + offset()# , for i = 1,,,nz()#) More...
 
bool is_sorted () const
 Return true if the sequence is sorted. More...
 

Iterator access to elements.

These functions return random access iterators that yield SparseElementTemplateInterface objects when dereferenced. This is required for the template argument.

iterator begin ()
 Returns iterator that iterates forward through the nonzero elements. More...
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
reverse_iterator rbegin ()
 Returns iterator that iterates backward through the nonzero elements. More...
 
const_reverse_iterator rbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 

Element setup and modification

void resize (size_type size, size_type max_nz, difference_type offset=0)
 Resize to #size# with a maximum of max_nz# non-zero elements. More...
 
void uninitialized_resize (size_type size, size_type nz, size_type max_nz, difference_type offset=0)
 Resize to #size# with a max_nz# uninitialized non-zero elements. More...
 
size_type max_nz () const
 Return the max number of elements that can be held without resizing. More...
 
void add_element (element_type ele)
 Add an unsorted element. More...
 
void insert_element (element_type ele)
 Add an element into a sorted sequence. More...
 
void assume_sorted (bool assume_is_sorted)
 Called by the client to inform this sparse vector object that the elements be assumed to be in sequence and it is the clients responcibiliy to make sure that it is. More...
 
void sort ()
 Sort the elements into assending order by index. More...
 
void assert_valid_and_sorted () const
 Assert that sparse vector is sorted. More...
 

Lookup an element.

If element v(i) exists, then a pointer to the element will be returned. If v(i) does not exist, then the NULL pointer will be returned.

If i is out of range then a std::out_of_range exception will be thrown.

If the elements are sored then this operation is O(log(nz)) for a binary search. Otherwise, it requries a O(nz) linear search.

element_typelookup_element (size_type i)
 
const element_typelookup_element (size_type i) const
 

Creating a slice (subregion) of the sparse vector.

If the vector is not sorted (is_sorted() == false#) then all of these functions will throw an exception (NotSortedException#).

** Say something about the cost of these operations! **

 operator SparseVectorSlice< T_Element > ()
 Allow an implicit conversion to a SparseVectorSlice<> object. More...
 
 operator const SparseVectorSlice< T_Element > () const
 
SparseVectorSlice< T_Element > operator() ()
 Returns a SparseVectorSlice representing the entire sparse vector. More...
 
const SparseVectorSlice
< T_Element > 
operator() () const
 
SparseVectorSlice< T_Element > operator() (const Range1D &rng)
 
const SparseVectorSlice
< T_Element > 
operator() (const Range1D &rng) const
 
SparseVectorSlice< T_Element > operator() (size_type lbound, size_type ubound)
 
const SparseVectorSlice
< T_Element > 
operator() (size_type lbound, size_type ubound) const
 Same as above. More...
 

Detailed Description

template<class T_Element, class T_Alloc = std::allocator<T_Element>>
class AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >

Sparse Vector class template.

This is a class for abstracting a sparse vector of a templated element type. All of the operations are based on the element type. Access to the elements is provided by iterators. It is also templated by an allocator that is that is used to allocate memory for the nonzero elements.

The templated type T_Element must support the following interface (SparseElementTemplateInterface): {description} [value_type] public typedef for the stored value of the element [index_type] public typedef for the stored index of the element [value_type& value()] function returning a lvalue for the value of the element [value_type value() const] const function returning a rvalue for the value of the element [index_type index() const] const function returning a rvalue for the index of the element. [T_Element& operator=(const T_Element&)] assignment operator [T_Element(const T_Element&)] copy constructor {description}

Definition at line 127 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

Member Typedef Documentation

template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef T_Alloc AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::allocator_type
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef T_Element AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::element_type
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef AbstractLinAlgPack::size_type AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::size_type
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef ptrdiff_t AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::difference_type
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef element_type* AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::iterator
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef const element_type* AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::const_iterator
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef std::reverse_iterator<iterator> AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::reverse_iterator
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef std::reverse_iterator<const_iterator> AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::const_reverse_iterator
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::DoesNotExistException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::DoesNotExistException
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::NotSortedException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::NotSortedException
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::DuplicateIndexesException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::DuplicateIndexesException
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::OutOfRoomException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::OutOfRoomException
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::UnsizedException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::UnsizedException
template<class T_Element, class T_Alloc = std::allocator<T_Element>>
typedef SparseVectorUtilityPack::NoNonZeroElementsException AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::NoNonZeroElementsException

Constructor & Destructor Documentation

template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::SparseVector ( const allocator_type alloc = allocator_type())
inline

Constructs a sparse vector with no elements (nz() == dim() == 0#) and assumes the elements are not sorted.

Definition at line 838 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::SparseVector ( bool  assume_sorted,
const allocator_type alloc = allocator_type() 
)
inline

Constructs a sparse vector with no elements (nz() == dim() == 0#).

Definition at line 843 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::SparseVector ( size_type  size,
size_type  max_nz,
difference_type  offset = 0,
bool  assume_sorted = false,
const allocator_type alloc = allocator_type() 
)
inline

Constructs a sparse vector of size #size# with storage for max_nz# elements (nz() == 0#)

Definition at line 848 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element, class T_Alloc>
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::SparseVector ( const SparseVector< T_Element, T_Alloc > &  sp_vec)

Constructs a sparse vector from another sparse vector.

Copies the complete state including the same max_nz() but a fresh copy of the elements are made.

Definition at line 103 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element, class T_Alloc>
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::SparseVector ( SparseVectorSlice< T_Element >  sp_vec_slc,
const allocator_type alloc = allocator_type() 
)

Constructs a sparse vector of from a sparse vector slice.

Definition at line 131 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::~SparseVector ( )
inline

Destructor (frees storage for elements).

Definition at line 856 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

Member Function Documentation

template<class T_Element, class T_Alloc>
SparseVector< T_Element, T_Alloc > & AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator= ( const SparseVector< T_Element, T_Alloc > &  sp_vec)

Assignment operator.

If max_nz() > sp_vec.nz()# then no new allocation takes place otherwise #this# will will be resized to #sp_vec.nz()#.

Definition at line 162 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element, class T_Alloc>
SparseVector< T_Element, T_Alloc > & AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator= ( const SparseVectorSlice< T_Element > &  sp_vec_slc)

Assignment operator.

If max_nz() > sp_vec_slc.nz()# then no new allocation takes place otherwise #this# will will be resized to #sp_vec_slc.nz()#.

Definition at line 210 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element, class T_Alloc >
EOverLap AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::overlap ( const SparseVectorSlice< T_Element > &  sv) const

Returns the degree of memory overlap of this SparseVector and a SparseVectorSlice.

Returns
{description} [NO_OVERLAP] There is no memory overlap between this and sv [SOME_OVERLAP] There is some memory locations that this and sv share [SAME_MEM] The DVectorSlice objects this and sv share the exact same memory locations. {description}

Definition at line 256 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::size_type AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::dim ( ) const
inline

Return the number of elements in the full vector.

Definition at line 863 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::size_type AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::nz ( ) const
inline

Return the number of non-zero elements.

Definition at line 868 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::difference_type AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::offset ( ) const
inline

Return the offset for the indexes (ith real index = begin()[i-1]->index() + offset()# , for i = 1,,,nz()#)

Definition at line 873 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
bool AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::is_sorted ( ) const
inline

Return true if the sequence is sorted.

If sorted() was called prior to this then it is garrented to be sorted and if assume_sorted(true) was called then a client is assumed to be responcible for it being sorted by it can not be garrented to be sorted.

Definition at line 878 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::begin ( )
inline

Returns iterator that iterates forward through the nonzero elements.

If is_sorted() == true# then the elements will be forward iterated in accending indexes.

Definition at line 883 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::const_iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::begin ( ) const
inline
template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::const_iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::end ( ) const
inline
template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::reverse_iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::rbegin ( )
inline

Returns iterator that iterates backward through the nonzero elements.

If is_sorted() == true# then the elements will be forward iterated in deaccending indexes.

Definition at line 903 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::const_reverse_iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::rbegin ( ) const
inline
template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::const_reverse_iterator AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::rend ( ) const
inline
template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::resize ( size_type  size,
size_type  max_nz,
difference_type  offset = 0 
)

Resize to #size# with a maximum of max_nz# non-zero elements.

This does not preserve the existing elements already in the sparse vector. If you pass in #size == 0# or max_nz == 0# then the storage will be deallocated and no storage will be reallocated.

Definition at line 278 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::uninitialized_resize ( size_type  size,
size_type  nz,
size_type  max_nz,
difference_type  offset = 0 
)

Resize to #size# with a max_nz# uninitialized non-zero elements.

This function has the same basic behavior as resize(...)# accept on return nz()# will equal nz#. The elements are initialized to garbage so it is imparative that the client initialize the elements before the sparse vector is used.

Definition at line 318 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::size_type AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::max_nz ( ) const
inline

Return the max number of elements that can be held without resizing.

Definition at line 925 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::add_element ( element_type  ele)
inline

Add an unsorted element.

If nz() = max_nz()#) then the exception OutOfRoomException will be thrown.

If you want to add more elements than you have reserved space for in the construction or resize operation then you have to mannually copy the elements in the sparse vector, resize the sparse vector, and then readd the elements including the extra ones you want to add.

Definition at line 930 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::insert_element ( element_type  ele)

Add an element into a sorted sequence.

If nz() = max_nz()#) then the exception OutOfRoomException will be thrown.

If you want to add more elements than you have reserved space for in the construction or resize operation then you have to mannually copy the elements in the sparse vector, resize the sparse vector, and then readd the elements including the extra ones you want to add.

Definition at line 332 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::assume_sorted ( bool  assume_is_sorted)
inline

Called by the client to inform this sparse vector object that the elements be assumed to be in sequence and it is the clients responcibiliy to make sure that it is.

Definition at line 942 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::sort ( )

Sort the elements into assending order by index.

Definition at line 369 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
void AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::assert_valid_and_sorted ( ) const

Assert that sparse vector is sorted.

This function will throw an exception if any of the following are not true: {enumerate} The sequence is not sorted by index (NotSortedException#) There are duplicate indexes (DuplicateIndexesException#) The indexes are out of range (#std::out_of_range#) {enumerate}

This function will throw an exception for the first error it finds.

Definition at line 376 of file AbstractLinAlgPack_SparseVectorClassDef.hpp.

template<class T_Element , class T_Alloc >
SparseVector< T_Element, T_Alloc >::element_type * AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::lookup_element ( size_type  i)
inline
template<class T_Element , class T_Alloc >
const SparseVector< T_Element, T_Alloc >::element_type * AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::lookup_element ( size_type  i) const
inline
template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator SparseVectorSlice< T_Element > ( )
inline

Allow an implicit conversion to a SparseVectorSlice<> object.

This is a very cheap operation.

Definition at line 967 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator const SparseVectorSlice< T_Element > ( ) const
inline
template<class T_Element , class T_Alloc >
SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( )
inline

Returns a SparseVectorSlice representing the entire sparse vector.

It is used to provide a quick, explicit conversion so that the SparseVector object can be used in functions that expect a SparseVectorSlice object.

Definition at line 977 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
const SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( ) const
inline
template<class T_Element , class T_Alloc >
SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( const Range1D rng)
inline

Returns a continous subregion of the SparseVector object.

The returned SparseVectorSlice object represents the range of the rng argument.

Preconditions:

Postconditions:

  • returned#.dim() == rng.ubound() - rng.lbound() + 1#
  • contains all of the elements in the range.
Parameters
rngIndex range [lbound,ubound] of the region being returned.

Definition at line 987 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
const SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( const Range1D rng) const
inline
template<class T_Element , class T_Alloc >
SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( size_type  lbound,
size_type  ubound 
)
inline

Returns a SparseVectorSlice object for the continous subregion [ubound, lbound].

Preconditions:

  • #lbound > 1# (throw #out_of_range#)
  • #lbound < ubound# (throw #out_of_range#)
  • #ubound <= this->dim()# (throw #out_of_range#)

Postconditions:

  • returned#.dim() == ubound() - lbound() + 1#
  • contains all of the elements in the range.
Parameters
lboundLower bound of range [lbound,ubound] of the region being returned.
uboundUpper bound of range [lbound,ubound] of the region being returned.

Definition at line 997 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.

template<class T_Element , class T_Alloc >
const SparseVectorSlice< T_Element > AbstractLinAlgPack::SparseVector< T_Element, T_Alloc >::operator() ( size_type  lbound,
size_type  ubound 
) const
inline

Same as above.

Definition at line 1002 of file AbstractLinAlgPack_SparseVectorClassDecl.hpp.


The documentation for this class was generated from the following files: