42 #ifndef SPARSE_VECTOR_CLASS_DECL_H 43 #define SPARSE_VECTOR_CLASS_DECL_H 50 #include "AbstractLinAlgPack_SpVecIndexLookupClass.hpp" 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> >
147 typedef std::reverse_iterator<iterator, element_type
150 typedef std::reverse_iterator<const_iterator
151 , element_type,
const element_type&
184 SparseVector(
const allocator_type& alloc = allocator_type());
187 SparseVector(
bool assume_sorted,
const allocator_type& alloc = allocator_type());
190 SparseVector(size_type size, size_type max_nz, difference_type offset = 0
191 ,
bool assume_sorted =
false,
const allocator_type& alloc = allocator_type());
202 ,
const allocator_type& alloc = allocator_type());
239 size_type dim()
const;
242 size_type nz()
const;
247 difference_type offset()
const;
255 bool is_sorted()
const;
273 const_iterator begin()
const;
279 const_iterator end()
const;
286 reverse_iterator rbegin();
289 const_reverse_iterator rbegin()
const;
292 reverse_iterator rend();
295 const_reverse_iterator rend()
const;
312 void resize(size_type size, size_type max_nz, difference_type offset = 0);
321 void uninitialized_resize(size_type size, size_type nz, size_type max_nz, difference_type offset = 0);
324 size_type max_nz()
const;
335 void add_element(element_type ele);
346 void insert_element(element_type ele);
352 void assume_sorted(
bool assume_is_sorted);
368 void assert_valid_and_sorted()
const;
388 element_type* lookup_element(size_type i);
390 const element_type* lookup_element(size_type i)
const;
480 allocator_type alloc_;
485 SpVecIndexLookup index_lookup_;
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 {
508 throw UnsizedException(
"SparseVector<...>::assert_sized_with_mem_set() : " 509 "Error: The sparse vector is unsized");
510 if(!index_lookup_.
ele()) {
511 throw NoNonZeroElementsException(
"SparseVector<...>::assert_sized_with_mem_set() : " 512 "Error: There is no memory set.");
519 , index_lookup_.
offset(), size_, is_sorted());
525 , index_lookup_.
offset(), size_, is_sorted());
531 return create_slice(index_lookup_, size_, rng);
554 template <
class T_Element>
573 typedef std::reverse_iterator<iterator, element_type
574 , element_type&, element_type*, difference_type> reverse_iterator;
576 typedef std::reverse_iterator<const_iterator
577 , element_type,
const element_type&
578 ,
const element_type*, difference_type> const_reverse_iterator;
622 SparseVectorSlice(element_type ele[], size_type nz, difference_type offset, size_type size
623 ,
bool assume_sorted =
false);
647 size_type dim()
const;
650 size_type nz()
const;
655 difference_type offset()
const;
659 bool is_sorted()
const;
665 const_iterator begin()
const;
671 const_iterator end()
const;
674 reverse_iterator rbegin();
677 const_reverse_iterator rbegin()
const;
680 reverse_iterator rend();
683 const_reverse_iterator rend()
const;
703 element_type* lookup_element(size_type i);
705 const element_type* lookup_element(size_type i)
const;
785 index_lookup_type index_lookup_;
794 void assert_is_sorted()
const {
795 SparseVectorUtilityPack::assert_is_sorted(is_sorted());
801 return create_slice(index_lookup_, size_, rng);
814 namespace SparseVectorUtilityPack {
820 template<
class T_Element >
821 inline const T_Element* lookup_element(
const SpVecIndexLookup<T_Element>& index_lookup
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>
849 , difference_type
offset,
bool assume_sorted,
const allocator_type& 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);
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>
1013 , difference_type
offset, size_type size,
bool assume_sorted)
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>
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 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.
void incr_nz()
Increment nz only.
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
SparseVectorUtilityPack::UnsizedException UnsizedException
element_type::index_type index_type
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.
Sparse Vector class template.
difference_type offset() const
Return the offset for the indexes (ith real index = begin()[i-1]->index() + offset()# ...
SparseVectorUtilityPack::NotSortedException NotSortedException
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.
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.
reverse_iterator rbegin()
Returns iterator that iterates backward through the nonzero elements.