42 #ifndef SPARSE_VECTOR_CLASS_DECL_H
43 #define SPARSE_VECTOR_CLASS_DECL_H
50 #include "AbstractLinAlgPack_SpVecIndexLookupClass.hpp"
52 namespace AbstractLinAlgPack {
54 namespace SparseVectorUtilityPack {
56 void assert_is_sorted(
bool is_sorted);
63 {
public:
NotSortedException(
const std::string& what_arg) : std::logic_error(what_arg) {}};
69 {
public:
OutOfRoomException(
const std::string& what_arg) : std::logic_error(what_arg) {}};
72 {
public:
UnsizedException(
const std::string& what_arg) : std::logic_error(what_arg) {}};
94 template<
class T_Element>
101 template <
class T_Element>
126 template <
class T_Element,
class T_Alloc = std::allocator<T_Element> >
487 bool know_is_sorted_;
493 void assert_is_sorted()
const {
494 SparseVectorUtilityPack::assert_is_sorted(
is_sorted());
498 void assert_space(size_type n)
const {
499 #ifdef LINALGPACK_CHECK_SLICE_SETUP
500 if(index_lookup_.
nz() + n > max_nz_)
501 throw OutOfRoomException(
"SparseVector<T_Element,T_Alloc>::assert_space(): There is not storage for this many elements");
506 void assert_sized_with_mem_set()
const {
509 "Error: The sparse vector is unsized");
510 if(!index_lookup_.
ele()) {
512 "Error: There is no memory set.");
517 SparseVectorSlice<T_Element> get_whole_sp_vec() {
518 return SparseVectorSlice<T_Element>(index_lookup_.
ele(), index_lookup_.
nz()
523 const SparseVectorSlice<T_Element> get_whole_sp_vec()
const {
524 return SparseVectorSlice<T_Element>(index_lookup_.
ele(), index_lookup_.
nz()
529 SparseVectorSlice<T_Element> get_slice(
const Range1D& rng)
const {
531 return create_slice(index_lookup_, size_, rng);
554 template <
class T_Element>
555 class SparseVectorSlice {
623 ,
bool assume_sorted =
false);
746 SparseVectorSlice<T_Element>
operator()(
const Range1D& rng);
749 const SparseVectorSlice<T_Element>
operator()(
const Range1D& rng)
const;
780 typedef SparseVectorUtilityPack::SpVecIndexLookup<element_type> index_lookup_type;
785 index_lookup_type index_lookup_;
794 void assert_is_sorted()
const {
795 SparseVectorUtilityPack::assert_is_sorted(
is_sorted());
799 SparseVectorSlice<T_Element> get_slice(
const Range1D& rng)
const {
801 return create_slice(index_lookup_, size_, rng);
807 SparseVectorSlice<element_type>& operator=(
const SparseVectorSlice<element_type>&);
814 namespace SparseVectorUtilityPack {
820 template<
class T_Element >
821 inline const T_Element* lookup_element(
const SpVecIndexLookup<T_Element>& index_lookup
822 ,
typename SpVecIndexLookup<T_Element>::index_type index,
bool is_sorted )
825 return ( ( poss = index_lookup.find_element(index,is_sorted) ) < index_lookup.nz() )
826 ? index_lookup.ele() + poss
837 template <
class T_Element,
class T_Alloc>
839 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(false), know_is_sorted_(false)
842 template <
class T_Element,
class T_Alloc>
844 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false)
847 template <
class T_Element,
class T_Alloc>
850 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false)
852 resize(size,max_nz,offset);
855 template <
class T_Element,
class T_Alloc>
862 template <
class T_Element,
class T_Alloc>
867 template <
class T_Element,
class T_Alloc>
869 return index_lookup_.nz();
872 template <
class T_Element,
class T_Alloc>
874 return index_lookup_.offset();
877 template <
class T_Element,
class T_Alloc>
879 return nz() <= 1 || assume_sorted_ || know_is_sorted_;
882 template <
class T_Element,
class T_Alloc>
884 return index_lookup_.nz() ? index_lookup_.ele() : NULL;
887 template <
class T_Element,
class T_Alloc>
889 return index_lookup_.nz() ? index_lookup_.ele() : NULL;
892 template <
class T_Element,
class T_Alloc>
894 return index_lookup_.
nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL;
897 template <
class T_Element,
class T_Alloc>
899 return index_lookup_.
nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL;
902 template <
class T_Element,
class T_Alloc>
907 template <
class T_Element,
class T_Alloc>
912 template <
class T_Element,
class T_Alloc>
914 return reverse_iterator(begin());
917 template <
class T_Element,
class T_Alloc>
924 template <
class T_Element,
class T_Alloc>
929 template <
class T_Element,
class T_Alloc>
932 assume_sorted_ = know_is_sorted_ =
false;
934 new (index_lookup_.ele() + index_lookup_.nz())
element_type;
936 alloc_.construct(index_lookup_.ele() + index_lookup_.nz(), ele);
938 index_lookup_.incr_nz();
941 template <
class T_Element,
class T_Alloc>
943 assume_sorted_ = assume_is_sorted;
948 template <
class T_Element,
class T_Alloc>
953 return const_cast<element_type*
>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_));
956 template <
class T_Element,
class T_Alloc>
961 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_);
966 template <
class T_Element,
class T_Alloc>
968 return get_whole_sp_vec();
971 template <
class T_Element,
class T_Alloc>
973 return get_whole_sp_vec();
976 template <
class T_Element,
class T_Alloc>
978 return get_whole_sp_vec();
981 template <
class T_Element,
class T_Alloc>
983 return get_whole_sp_vec();
986 template <
class T_Element,
class T_Alloc>
988 return get_slice(rng);
991 template <
class T_Element,
class T_Alloc>
993 return get_slice(rng);
996 template <
class T_Element,
class T_Alloc>
998 return get_slice(
Range1D(lbound,ubound));
1001 template <
class T_Element,
class T_Alloc>
1003 return get_slice(
Range1D(lbound,ubound));
1011 template <
class T_Element>
1014 : index_lookup_(ele,nz,offset), size_(size), assume_sorted_(assume_sorted)
1017 template <
class T_Element>
1020 index_lookup_ = svs.index_lookup_;
1022 assume_sorted_ = svs.assume_sorted_;
1027 template <
class T_Element>
1032 template <
class T_Element>
1034 return index_lookup_.nz();
1037 template <
class T_Element>
1039 return index_lookup_.offset();
1042 template <
class T_Element>
1044 return nz() <= 1 || assume_sorted_;
1047 template <
class T_Element>
1049 return index_lookup_.ele();
1052 template <
class T_Element>
1054 return index_lookup_.ele();
1057 template <
class T_Element>
1059 return index_lookup_.ele() + index_lookup_.
nz();
1062 template <
class T_Element>
1064 return index_lookup_.ele() + index_lookup_.
nz();
1067 template <
class T_Element>
1072 template <
class T_Element>
1077 template <
class T_Element>
1079 return reverse_iterator(begin());
1082 template <
class T_Element>
1089 template <
class T_Element>
1094 return const_cast<element_type*
>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_));
1097 template <
class T_Element>
1102 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_);
1107 template <
class T_Element>
1112 template <
class T_Element>
1117 template <
class T_Element>
1119 return get_slice(rng);
1122 template <
class T_Element>
1124 return get_slice(rng);
1127 template <
class T_Element>
1129 return get_slice(
Range1D(lbound,ubound));
1132 template <
class T_Element>
1134 return get_slice(
Range1D(lbound,ubound));
1139 #endif // SPARSE_VECTOR_CLASS_DECL_H
SparseVector< T_Element, T_Alloc > & operator=(const SparseVector< T_Element, T_Alloc > &sp_vec)
Assignment operator.
element_type * lookup_element(size_type i)
difference_type offset() const
Return the offset for the indexes (ith real index = begin()[i-1]->index() + offset()# ...
ptrdiff_t difference_type
const element_type * const_iterator
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.
std::reverse_iterator< iterator > reverse_iterator
size_type dim() const
Return the number of elements in the full vector.
difference_type offset() const
~SparseVector()
Destructor (frees storage for elements).
Sparse Vector Slice class template.
SparseVectorSlice< T_Element > & operator()()
bool is_sorted() const
Return true if the sequence is sorted.
element_type * lookup_element(size_type i)
SparseVectorUtilityPack::OutOfRoomException OutOfRoomException
element_type * ele() const
std::reverse_iterator< const_iterator > const_reverse_iterator
ptrdiff_t difference_type
std::reverse_iterator< const_iterator > const_reverse_iterator
void add_element(element_type ele)
Add an unsorted element.
SparseVectorUtilityPack::DoesNotExistException DoesNotExistException
bool is_sorted() const
Return true if the sequence is assumed sorted.
void bind(SparseVectorSlice svs)
Constructs a sparse vector slice view from another sparse vector slice.
reverse_iterator rbegin()
const element_type * const_iterator
void assert_valid_and_sorted() const
Assert that sparse vector is sorted.
SparseVectorUtilityPack::UnsizedException UnsizedException
AbstractLinAlgPack::size_type size_type
SparseVectorUtilityPack::DoesNotExistException DoesNotExistException
SparseVectorUtilityPack::DuplicateIndexesException DuplicateIndexesException
std::reverse_iterator< iterator > reverse_iterator
size_type dim() const
Return the number of elements in the full vector.
SparseVectorUtilityPack::NoNonZeroElementsException NoNonZeroElementsException
size_type nz() const
Return the number of non-zero elements.
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 sequen...
SparseVectorSlice * operator&()
Allow address to be taken of an rvalue of this object.
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.
EOverLap overlap(const SparseVectorSlice< T_Element > &sv) const
Sparse Vector class template.
difference_type offset() const
Return the offset for the indexes (ith real index = begin()[i-1]->index() + offset()# ...
void sort()
Sort the elements into assending order by index.
SparseVectorSlice(element_type ele[], size_type nz, difference_type offset, size_type size, bool assume_sorted=false)
Constructs a sparse vector slice from an array of elements.
SparseVectorUtilityPack::NotSortedException NotSortedException
Sparse storage element type.
SparseVector(const allocator_type &alloc=allocator_type())
Constructs a sparse vector with no elements (nz() == dim() == 0#) and assumes the elements are not so...
Sparse Vector Index Lookup and Caching class.
EOverLap overlap(const SparseVectorSlice< T_Element > &sv) const
SparseVectorUtilityPack::NotSortedException NotSortedException
AbstractLinAlgPack::size_type size_type
SparseVectorSlice< T_Element > operator()()
Returns a SparseVectorSlice representing the entire sparse vector.
size_type max_nz() const
Return the max number of elements that can be held without resizing.
iterator begin()
Returns iterator that iterates forward through the nonzero elements.
size_type nz() const
Return the number of non-zero elements.
void insert_element(element_type ele)
Add an element into a sorted sequence.
reverse_iterator rbegin()
Returns iterator that iterates backward through the nonzero elements.