42 #ifndef SPARSE_VECTOR_CLASS_DECL_H
43 #define SPARSE_VECTOR_CLASS_DECL_H
52 namespace AbstractLinAlgPack {
54 namespace SparseVectorUtilityPack {
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> >
499 #ifdef LINALGPACK_CHECK_SLICE_SETUP
501 throw OutOfRoomException(
"SparseVector<T_Element,T_Alloc>::assert_space(): There is not storage for this many elements");
509 "Error: The sparse vector is unsized");
512 "Error: There is no memory set.");
554 template <
class T_Element>
555 class SparseVectorSlice {
623 ,
bool assume_sorted =
false);
814 namespace SparseVectorUtilityPack {
820 template<
class T_Element >
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>
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>
956 template <
class T_Element,
class T_Alloc>
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>
1082 template <
class T_Element>
1089 template <
class T_Element>
1097 template <
class T_Element>
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
const SparseVectorSlice * operator&() const
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
OutOfRoomException(const std::string &what_arg)
SparseVectorUtilityPack::SpVecIndexLookup< element_type > index_lookup_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).
SparseVectorSlice()
Not defined and not to be called.
DuplicateIndexesException(const std::string &what_arg)
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.
void assert_is_sorted() const
SparseVectorSlice< T_Element > get_slice(const Range1D &rng) const
Return a SparseVectorSlice (inplementation for indexing operators)
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.
SpVecIndexLookup index_lookup_
reverse_iterator rbegin()
NotSortedException(const std::string &what_arg)
const element_type * const_iterator
void assert_valid_and_sorted() const
Assert that sparse vector is sorted.
SparseVectorUtilityPack::UnsizedException UnsizedException
element_type::index_type index_type
AbstractLinAlgPack::size_type size_type
UnsizedException(const std::string &what_arg)
SparseVectorUtilityPack::DoesNotExistException DoesNotExistException
. One-based subregion index range class.
SparseVectorUtilityPack::DuplicateIndexesException DuplicateIndexesException
RTOp_index_type size_type
std::reverse_iterator< iterator > reverse_iterator
size_type dim() const
Return the number of elements in the full vector.
SparseVectorUtilityPack::NoNonZeroElementsException NoNonZeroElementsException
DoesNotExistException(const std::string &what_arg)
NoNonZeroElementsException(const std::string &what_arg)
size_type nz() const
Return the number of non-zero elements.
SparseVectorSlice< element_type > & operator=(const SparseVectorSlice< element_type > &)
Not defined and not to be called.
SparseVectorSlice< T_Element > get_whole_sp_vec()
Return the entire vector slice.
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...
index_lookup_type index_lookup_
SparseVectorSlice< T_Element > create_slice(const SparseVectorUtilityPack::SpVecIndexLookup< T_Element > &index_lookup, size_type size, Range1D rng)
Return a sparse vector slice.
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.
const SparseVectorSlice< T_Element > get_whole_sp_vec() const
const T_Element * lookup_element(const SpVecIndexLookup< T_Element > &index_lookup, typename SpVecIndexLookup< T_Element >::index_type index, bool is_sorted)
Lookup an element.
EOverLap overlap(const SparseVectorSlice< T_Element > &sv) const
SparseVectorSlice< T_Element > get_slice(const Range1D &rng) const
Return a SparseVectorSlice (inplementation for indexing operators)
Sparse Vector class template.
EOverLap
Enumeration for returning the amount of overlap between two objects.
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.
SparseVectorUtilityPack::NotSortedException NotSortedException
void assert_sized_with_mem_set() const
Assert dim() > 0# (UnsizedException#) and #index_lookup_.ele() != 0# (NoNonZeroElementsException#) ...
Sparse storage element type.
SparseVectorUtilityPack::SpVecIndexLookup< element_type > SpVecIndexLookup
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
size_type find_element(index_type index, bool is_sorted) const
Lookup an element.
void assert_space(size_type n) const
Assert (#OutOfRoom#) that there is room for n elements.
AbstractLinAlgPack::size_type size_type
SparseVectorSlice< T_Element > operator()()
Returns a SparseVectorSlice representing the entire sparse vector.
RangePack::Range1D Range1D
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.
void assert_is_sorted(bool is_sorted)
reverse_iterator rbegin()
Returns iterator that iterates backward through the nonzero elements.
void assert_is_sorted() const