|
AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization 00005 // Copyright (2003) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef SPARSE_VECTOR_CLASS_DECL_H 00030 #define SPARSE_VECTOR_CLASS_DECL_H 00031 00032 #include <assert.h> 00033 00034 #include <vector> 00035 #include <sstream> 00036 00037 #include "AbstractLinAlgPack_SpVecIndexLookupClass.hpp" 00038 00039 namespace AbstractLinAlgPack { 00040 00041 namespace SparseVectorUtilityPack { 00042 00043 void assert_is_sorted(bool is_sorted); 00044 00046 class DoesNotExistException : public std::logic_error 00047 {public: DoesNotExistException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00049 class NotSortedException : public std::logic_error 00050 {public: NotSortedException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00052 class DuplicateIndexesException : public std::logic_error 00053 {public: DuplicateIndexesException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00055 class OutOfRoomException : public std::logic_error 00056 {public: OutOfRoomException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00058 class UnsizedException : public std::logic_error 00059 {public: UnsizedException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00061 class NoNonZeroElementsException : public std::logic_error 00062 {public: NoNonZeroElementsException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00063 00064 } // end namespace SparseVectorUtilityPack 00065 00066 // ///////////////////////////////////////////////////////////////////////////////////// 00069 00081 template<class T_Element> 00082 SparseVectorSlice<T_Element> create_slice( 00083 const SparseVectorUtilityPack::SpVecIndexLookup<T_Element>& index_lookup 00084 , size_type size, Range1D rng); 00085 00087 00088 template <class T_Element> 00089 class SparseVectorSlice; 00090 00091 // /////////////////////////////////////////////////////////////////////// 00113 template <class T_Element, class T_Alloc = std::allocator<T_Element> > 00114 class SparseVector { 00115 public: 00118 00120 typedef T_Alloc allocator_type; 00122 typedef T_Element element_type; 00124 typedef AbstractLinAlgPack::size_type size_type; 00126 typedef ptrdiff_t difference_type; 00128 typedef element_type* iterator; 00130 typedef const element_type* const_iterator; 00131 00132 #if 0 /* defined(_WINDOWS) || defined(_INTEL_CXX) */ 00133 00134 typedef std::reverse_iterator<iterator, element_type 00135 , element_type&, element_type*, difference_type> reverse_iterator; 00136 00137 typedef std::reverse_iterator<const_iterator 00138 , element_type, const element_type& 00139 , const element_type*, difference_type> const_reverse_iterator; 00140 00141 #else 00142 00144 typedef std::reverse_iterator<iterator> reverse_iterator; 00146 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00147 00148 #endif 00149 00151 typedef SparseVectorUtilityPack::DoesNotExistException DoesNotExistException; 00153 typedef SparseVectorUtilityPack::NotSortedException NotSortedException; 00155 typedef SparseVectorUtilityPack::DuplicateIndexesException DuplicateIndexesException; 00157 typedef SparseVectorUtilityPack::OutOfRoomException OutOfRoomException; 00159 typedef SparseVectorUtilityPack::UnsizedException UnsizedException; 00161 typedef SparseVectorUtilityPack::NoNonZeroElementsException NoNonZeroElementsException; 00162 00164 00167 00171 SparseVector(const allocator_type& alloc = allocator_type()); 00172 00174 SparseVector(bool assume_sorted, const allocator_type& alloc = allocator_type()); 00175 00177 SparseVector(size_type size, size_type max_nz, difference_type offset = 0 00178 , bool assume_sorted = false, const allocator_type& alloc = allocator_type()); 00179 00185 SparseVector(const SparseVector<T_Element,T_Alloc>& sp_vec); 00186 00188 SparseVector( SparseVectorSlice<T_Element> sp_vec_slc 00189 , const allocator_type& alloc = allocator_type()); 00190 00192 ~SparseVector(); 00193 00195 00201 SparseVector<T_Element,T_Alloc>& operator=(const SparseVector<T_Element,T_Alloc>& sp_vec); 00202 00208 SparseVector<T_Element,T_Alloc>& operator=(const SparseVectorSlice<T_Element>& sp_vec_slc); 00209 00211 00220 EOverLap overlap(const SparseVectorSlice<T_Element>& sv) const; 00221 00224 00226 size_type dim() const; 00227 00229 size_type nz() const; 00230 00234 difference_type offset() const; 00235 00242 bool is_sorted() const; 00243 00251 00257 iterator begin(); 00258 00260 const_iterator begin() const; 00261 00263 iterator end(); 00264 00266 const_iterator end() const; 00267 00273 reverse_iterator rbegin(); 00274 00276 const_reverse_iterator rbegin() const; 00277 00279 reverse_iterator rend(); 00280 00282 const_reverse_iterator rend() const; 00283 00284 // end Iterator access to elements 00286 00287 // end SparseVectorTemplateInterface 00289 00292 00299 void resize(size_type size, size_type max_nz, difference_type offset = 0); 00300 00308 void uninitialized_resize(size_type size, size_type nz, size_type max_nz, difference_type offset = 0); 00309 00311 size_type max_nz() const; 00312 00322 void add_element(element_type ele); 00323 00333 void insert_element(element_type ele); 00334 00339 void assume_sorted(bool assume_is_sorted); 00340 00342 void sort(); 00343 00355 void assert_valid_and_sorted() const; 00356 00358 00373 00375 element_type* lookup_element(size_type i); 00377 const element_type* lookup_element(size_type i) const; 00378 00380 00389 00394 operator SparseVectorSlice<T_Element>(); 00395 00397 operator const SparseVectorSlice<T_Element>() const; 00398 00405 SparseVectorSlice<T_Element> operator()(); 00406 00408 const SparseVectorSlice<T_Element> operator()() const; 00409 00411 00427 SparseVectorSlice<T_Element> operator()(const Range1D& rng); 00428 00430 const SparseVectorSlice<T_Element> operator()(const Range1D& rng) const; 00431 00433 00449 SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound); 00450 00452 const SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound) const; 00453 00455 00456 private: 00457 00458 // ///////////////////////////////////////////////////////////////////////// 00459 // Private types 00460 00462 typedef SparseVectorUtilityPack::SpVecIndexLookup<element_type> SpVecIndexLookup; 00463 00464 // ///////////////////////////////////////////////////////////////////////// 00465 // Private data members 00466 00467 allocator_type alloc_; // allocator used to allocate memory 00468 size_type size_; // the number of elements in the full vector 00469 size_type max_nz_; // the amount of storage that has been allocated 00470 // commented out because of problems with MS Visual C++ 5.0 00471 // std::vector<element_type, allocator_type> ele_; 00472 SpVecIndexLookup index_lookup_; // Acts as storage for elements and caching of searches. 00473 bool assume_sorted_; // true if the client said that you can assume sorted. 00474 bool know_is_sorted_; // true if it has been varified that is sorted. 00475 00476 // ////////////////////////// 00477 // Private member functions 00478 00479 // Throw a NotSortedException of is_sorted() == false 00480 void assert_is_sorted() const { 00481 SparseVectorUtilityPack::assert_is_sorted(is_sorted()); 00482 } 00483 00485 void assert_space(size_type n) const { 00486 #ifdef LINALGPACK_CHECK_SLICE_SETUP 00487 if(index_lookup_.nz() + n > max_nz_) 00488 throw OutOfRoomException("SparseVector<T_Element,T_Alloc>::assert_space(): There is not storage for this many elements"); 00489 #endif 00490 } 00491 00493 void assert_sized_with_mem_set() const { 00494 if(!dim()) 00495 throw UnsizedException("SparseVector<...>::assert_sized_with_mem_set() : " 00496 "Error: The sparse vector is unsized"); 00497 if(!index_lookup_.ele()) { 00498 throw NoNonZeroElementsException("SparseVector<...>::assert_sized_with_mem_set() : " 00499 "Error: There is no memory set."); 00500 } 00501 } 00502 00504 SparseVectorSlice<T_Element> get_whole_sp_vec() { 00505 return SparseVectorSlice<T_Element>(index_lookup_.ele(), index_lookup_.nz() 00506 , index_lookup_.offset(), size_, is_sorted()); 00507 } 00508 00510 const SparseVectorSlice<T_Element> get_whole_sp_vec() const { 00511 return SparseVectorSlice<T_Element>(index_lookup_.ele(), index_lookup_.nz() 00512 , index_lookup_.offset(), size_, is_sorted()); 00513 } 00514 00516 SparseVectorSlice<T_Element> get_slice(const Range1D& rng) const { 00517 assert_is_sorted(); 00518 return create_slice(index_lookup_, size_, rng); 00519 } 00520 00521 }; // end class SparseVector 00522 00523 // /////////////////////////////////////////////////////////////////////// 00541 template <class T_Element> 00542 class SparseVectorSlice { 00543 public: 00546 00548 typedef T_Element element_type; 00550 typedef AbstractLinAlgPack::size_type size_type; 00552 typedef ptrdiff_t difference_type; 00554 typedef element_type* iterator; 00556 typedef const element_type* const_iterator; 00557 00558 #if 0 /* defined(_WINDOWS) || defined(_INTEL_CXX) */ 00559 00560 typedef std::reverse_iterator<iterator, element_type 00561 , element_type&, element_type*, difference_type> reverse_iterator; 00562 00563 typedef std::reverse_iterator<const_iterator 00564 , element_type, const element_type& 00565 , const element_type*, difference_type> const_reverse_iterator; 00566 00567 #else 00568 00570 typedef std::reverse_iterator<iterator> reverse_iterator; 00572 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00573 00574 #endif 00575 00577 typedef SparseVectorUtilityPack::DoesNotExistException DoesNotExistException; 00579 typedef SparseVectorUtilityPack::NotSortedException NotSortedException; 00580 00582 00588 00609 SparseVectorSlice(element_type ele[], size_type nz, difference_type offset, size_type size 00610 , bool assume_sorted = false); 00611 00613 00616 void bind(SparseVectorSlice svs); 00617 00619 00628 EOverLap overlap(const SparseVectorSlice<T_Element>& sv) const; 00629 00632 00634 size_type dim() const; 00635 00637 size_type nz() const; 00638 00642 difference_type offset() const; 00643 00646 bool is_sorted() const; 00647 00649 iterator begin(); 00650 00652 const_iterator begin() const; 00653 00655 iterator end(); 00656 00658 const_iterator end() const; 00659 00661 reverse_iterator rbegin(); 00662 00664 const_reverse_iterator rbegin() const; 00665 00667 reverse_iterator rend(); 00668 00670 const_reverse_iterator rend() const; 00671 00673 00688 00690 element_type* lookup_element(size_type i); 00692 const element_type* lookup_element(size_type i) const; 00693 00695 00698 00700 00704 SparseVectorSlice<T_Element>& operator()(); 00705 00707 const SparseVectorSlice<T_Element>& operator()() const; 00708 00710 SparseVectorSlice* operator&() 00711 { return this; } 00712 00713 const SparseVectorSlice* operator&() const 00714 { return this; } 00715 00717 00733 SparseVectorSlice<T_Element> operator()(const Range1D& rng); 00734 00736 const SparseVectorSlice<T_Element> operator()(const Range1D& rng) const; 00737 00739 00755 SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound); 00756 00758 const SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound) const; 00759 00761 00762 private: 00763 // ///////////////////////////////////////////////////////////////////////// 00764 // Private types 00765 00767 typedef SparseVectorUtilityPack::SpVecIndexLookup<element_type> index_lookup_type; 00768 00769 // ///////////////////////////////////////////////////////////////////////// 00770 // Private data members 00771 00772 index_lookup_type index_lookup_; // Acts as storage and cacheing 00773 size_type size_; // size of the full vector 00774 bool assume_sorted_; // true if the client said that you can assume sorted. 00775 00776 00777 // ///////////////////////////////////////////////////////////////////////// 00778 // Private member functions 00779 00780 // Throw a NotSortedException of is_sorted() == false 00781 void assert_is_sorted() const { 00782 SparseVectorUtilityPack::assert_is_sorted(is_sorted()); 00783 } 00784 00786 SparseVectorSlice<T_Element> get_slice(const Range1D& rng) const { 00787 assert_is_sorted(); 00788 return create_slice(index_lookup_, size_, rng); 00789 } 00790 00792 SparseVectorSlice(); 00794 SparseVectorSlice<element_type>& operator=(const SparseVectorSlice<element_type>&); 00795 00796 }; // end class SparseVectorSlice 00797 00798 // //////////////////////////////////////////////// 00799 // Non-member non-public utility functions 00800 00801 namespace SparseVectorUtilityPack { 00802 00807 template< class T_Element > 00808 inline const T_Element* lookup_element( const SpVecIndexLookup<T_Element>& index_lookup 00809 , typename SpVecIndexLookup<T_Element>::index_type index, bool is_sorted ) 00810 { 00811 size_type poss; 00812 return ( ( poss = index_lookup.find_element(index,is_sorted) ) < index_lookup.nz() ) 00813 ? index_lookup.ele() + poss 00814 : NULL; 00815 } 00816 00817 } // end namespace SparseVectorUtilityPack 00818 00819 // ///////////////////////////////////////////////////////////////////////////////////// 00820 // Inline members for SparseVector<> 00821 00822 // constructors 00823 00824 template <class T_Element, class T_Alloc> 00825 inline SparseVector<T_Element,T_Alloc>::SparseVector(const allocator_type& alloc) 00826 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(false), know_is_sorted_(false) 00827 {} 00828 00829 template <class T_Element, class T_Alloc> 00830 inline SparseVector<T_Element,T_Alloc>::SparseVector(bool assume_sorted,const allocator_type& alloc) 00831 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false) 00832 {} 00833 00834 template <class T_Element, class T_Alloc> 00835 inline SparseVector<T_Element,T_Alloc>::SparseVector(size_type size, size_type max_nz 00836 , difference_type offset, bool assume_sorted, const allocator_type& alloc) 00837 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false) 00838 { 00839 resize(size,max_nz,offset); 00840 } 00841 00842 template <class T_Element, class T_Alloc> 00843 inline SparseVector<T_Element,T_Alloc>::~SparseVector() { 00844 resize(0,0); 00845 } 00846 00847 // SparseVectorTemplateInterface for linear algebra operations 00848 00849 template <class T_Element, class T_Alloc> 00850 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::dim() const { 00851 return size_; 00852 } 00853 00854 template <class T_Element, class T_Alloc> 00855 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::nz() const { 00856 return index_lookup_.nz(); 00857 } 00858 00859 template <class T_Element, class T_Alloc> 00860 inline typename SparseVector<T_Element,T_Alloc>::difference_type SparseVector<T_Element,T_Alloc>::offset() const { 00861 return index_lookup_.offset(); 00862 } 00863 00864 template <class T_Element, class T_Alloc> 00865 inline bool SparseVector<T_Element,T_Alloc>::is_sorted() const { 00866 return nz() <= 1 || assume_sorted_ || know_is_sorted_; 00867 } 00868 00869 template <class T_Element, class T_Alloc> 00870 inline typename SparseVector<T_Element,T_Alloc>::iterator SparseVector<T_Element,T_Alloc>::begin() { 00871 return index_lookup_.nz() ? index_lookup_.ele() : NULL; 00872 } 00873 00874 template <class T_Element, class T_Alloc> 00875 inline typename SparseVector<T_Element,T_Alloc>::const_iterator SparseVector<T_Element,T_Alloc>::begin() const { 00876 return index_lookup_.nz() ? index_lookup_.ele() : NULL; 00877 } 00878 00879 template <class T_Element, class T_Alloc> 00880 inline typename SparseVector<T_Element,T_Alloc>::iterator SparseVector<T_Element,T_Alloc>::end() { 00881 return index_lookup_.nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL; 00882 } 00883 00884 template <class T_Element, class T_Alloc> 00885 inline typename SparseVector<T_Element,T_Alloc>::const_iterator SparseVector<T_Element,T_Alloc>::end() const { 00886 return index_lookup_.nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL; 00887 } 00888 00889 template <class T_Element, class T_Alloc> 00890 inline typename SparseVector<T_Element,T_Alloc>::reverse_iterator SparseVector<T_Element,T_Alloc>::rbegin() { 00891 return reverse_iterator(end()); 00892 } 00893 00894 template <class T_Element, class T_Alloc> 00895 inline typename SparseVector<T_Element,T_Alloc>::const_reverse_iterator SparseVector<T_Element,T_Alloc>::rbegin() const { 00896 return const_reverse_iterator(end()); 00897 } 00898 00899 template <class T_Element, class T_Alloc> 00900 inline typename SparseVector<T_Element,T_Alloc>::reverse_iterator SparseVector<T_Element,T_Alloc>::rend() { 00901 return reverse_iterator(begin()); 00902 } 00903 00904 template <class T_Element, class T_Alloc> 00905 inline typename SparseVector<T_Element,T_Alloc>::const_reverse_iterator SparseVector<T_Element,T_Alloc>::rend() const { 00906 return const_reverse_iterator(begin()); 00907 } 00908 00909 // Element setup and modification 00910 00911 template <class T_Element, class T_Alloc> 00912 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::max_nz() const { 00913 return max_nz_; 00914 } 00915 00916 template <class T_Element, class T_Alloc> 00917 inline void SparseVector<T_Element,T_Alloc>::add_element(element_type ele) { 00918 assert_space(1); 00919 assume_sorted_ = know_is_sorted_ = false; 00920 #ifdef _PG_CXX 00921 new (index_lookup_.ele() + index_lookup_.nz()) element_type; 00922 #else 00923 alloc_.construct(index_lookup_.ele() + index_lookup_.nz(), ele); 00924 #endif 00925 index_lookup_.incr_nz(); 00926 } 00927 00928 template <class T_Element, class T_Alloc> 00929 inline void SparseVector<T_Element,T_Alloc>::assume_sorted(bool assume_is_sorted) { 00930 assume_sorted_ = assume_is_sorted; 00931 } 00932 00933 // Lookup an element 00934 00935 template <class T_Element, class T_Alloc> 00936 inline 00937 typename SparseVector<T_Element,T_Alloc>::element_type* 00938 SparseVector<T_Element,T_Alloc>::lookup_element(size_type i) 00939 { 00940 return const_cast<element_type*>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_)); 00941 } 00942 00943 template <class T_Element, class T_Alloc> 00944 inline 00945 const typename SparseVector<T_Element,T_Alloc>::element_type* 00946 SparseVector<T_Element,T_Alloc>::lookup_element(size_type i) const 00947 { 00948 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_); 00949 } 00950 00951 // Creating a slice (subregion) of the sparse vector 00952 00953 template <class T_Element, class T_Alloc> 00954 inline SparseVector<T_Element,T_Alloc>::operator SparseVectorSlice<T_Element>() { 00955 return get_whole_sp_vec(); 00956 } 00957 00958 template <class T_Element, class T_Alloc> 00959 inline SparseVector<T_Element,T_Alloc>::operator const SparseVectorSlice<T_Element>() const { 00960 return get_whole_sp_vec(); 00961 } 00962 00963 template <class T_Element, class T_Alloc> 00964 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()() { 00965 return get_whole_sp_vec(); 00966 } 00967 00968 template <class T_Element, class T_Alloc> 00969 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()() const { 00970 return get_whole_sp_vec(); 00971 } 00972 00973 template <class T_Element, class T_Alloc> 00974 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(const Range1D& rng) { 00975 return get_slice(rng); 00976 } 00977 00978 template <class T_Element, class T_Alloc> 00979 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(const Range1D& rng) const { 00980 return get_slice(rng); 00981 } 00982 00983 template <class T_Element, class T_Alloc> 00984 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(size_type lbound, size_type ubound) { 00985 return get_slice(Range1D(lbound,ubound)); 00986 } 00987 00988 template <class T_Element, class T_Alloc> 00989 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(size_type lbound, size_type ubound) const { 00990 return get_slice(Range1D(lbound,ubound)); 00991 } 00992 00993 // ///////////////////////////////////////////////////////////////////////////////////// 00994 // Inline members for SparseVectorSlice<> 00995 00996 // Constuctors 00997 00998 template <class T_Element> 00999 inline SparseVectorSlice<T_Element>::SparseVectorSlice(element_type ele[], size_type nz 01000 , difference_type offset, size_type size, bool assume_sorted) 01001 : index_lookup_(ele,nz,offset), size_(size), assume_sorted_(assume_sorted) 01002 {} 01003 01004 template <class T_Element> 01005 inline void SparseVectorSlice<T_Element>::bind(SparseVectorSlice svs) 01006 { 01007 index_lookup_ = svs.index_lookup_; 01008 size_ = svs.size_; 01009 assume_sorted_ = svs.assume_sorted_; 01010 } 01011 01012 // Sparse Vector Templated interface for linear algebra operations 01013 01014 template <class T_Element> 01015 inline typename SparseVectorSlice<T_Element>::size_type SparseVectorSlice<T_Element>::dim() const { 01016 return size_; 01017 } 01018 01019 template <class T_Element> 01020 inline typename SparseVectorSlice<T_Element>::size_type SparseVectorSlice<T_Element>::nz() const { 01021 return index_lookup_.nz(); 01022 } 01023 01024 template <class T_Element> 01025 inline typename SparseVectorSlice<T_Element>::difference_type SparseVectorSlice<T_Element>::offset() const { 01026 return index_lookup_.offset(); 01027 } 01028 01029 template <class T_Element> 01030 inline bool SparseVectorSlice<T_Element>::is_sorted() const { 01031 return nz() <= 1 || assume_sorted_; 01032 } 01033 01034 template <class T_Element> 01035 inline typename SparseVectorSlice<T_Element>::iterator SparseVectorSlice<T_Element>::begin() { 01036 return index_lookup_.ele(); 01037 } 01038 01039 template <class T_Element> 01040 inline typename SparseVectorSlice<T_Element>::const_iterator SparseVectorSlice<T_Element>::begin() const { 01041 return index_lookup_.ele(); 01042 } 01043 01044 template <class T_Element> 01045 inline typename SparseVectorSlice<T_Element>::iterator SparseVectorSlice<T_Element>::end() { 01046 return index_lookup_.ele() + index_lookup_.nz(); 01047 } 01048 01049 template <class T_Element> 01050 inline typename SparseVectorSlice<T_Element>::const_iterator SparseVectorSlice<T_Element>::end() const { 01051 return index_lookup_.ele() + index_lookup_.nz(); 01052 } 01053 01054 template <class T_Element> 01055 inline typename SparseVectorSlice<T_Element>::reverse_iterator SparseVectorSlice<T_Element>::rbegin() { 01056 return reverse_iterator(end()); 01057 } 01058 01059 template <class T_Element> 01060 inline typename SparseVectorSlice<T_Element>::const_reverse_iterator SparseVectorSlice<T_Element>::rbegin() const { 01061 return const_reverse_iterator(end()); 01062 } 01063 01064 template <class T_Element> 01065 inline typename SparseVectorSlice<T_Element>::reverse_iterator SparseVectorSlice<T_Element>::rend() { 01066 return reverse_iterator(begin()); 01067 } 01068 01069 template <class T_Element> 01070 inline typename SparseVectorSlice<T_Element>::const_reverse_iterator SparseVectorSlice<T_Element>::rend() const { 01071 return const_reverse_iterator(begin()); 01072 } 01073 01074 // Lookup an element 01075 01076 template <class T_Element> 01077 inline 01078 typename SparseVectorSlice<T_Element>::element_type* 01079 SparseVectorSlice<T_Element>::lookup_element(size_type i) 01080 { 01081 return const_cast<element_type*>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_)); 01082 } 01083 01084 template <class T_Element> 01085 inline 01086 const typename SparseVectorSlice<T_Element>::element_type* 01087 SparseVectorSlice<T_Element>::lookup_element(size_type i) const 01088 { 01089 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_); 01090 } 01091 01092 // Creating a slice (subregion) of the sparse vector 01093 01094 template <class T_Element> 01095 inline SparseVectorSlice<T_Element>& SparseVectorSlice<T_Element>::operator()() { 01096 return *this; 01097 } 01098 01099 template <class T_Element> 01100 inline const SparseVectorSlice<T_Element>& SparseVectorSlice<T_Element>::operator()() const { 01101 return *this; 01102 } 01103 01104 template <class T_Element> 01105 inline SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(const Range1D& rng) { 01106 return get_slice(rng); 01107 } 01108 01109 template <class T_Element> 01110 inline const SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(const Range1D& rng) const { 01111 return get_slice(rng); 01112 } 01113 01114 template <class T_Element> 01115 inline SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(size_type lbound, size_type ubound) { 01116 return get_slice(Range1D(lbound,ubound)); 01117 } 01118 01119 template <class T_Element> 01120 inline const SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(size_type lbound, size_type ubound) const { 01121 return get_slice(Range1D(lbound,ubound)); 01122 } 01123 01124 } // end namespace AbstractLinAlgPack 01125 01126 #endif // SPARSE_VECTOR_CLASS_DECL_H
1.7.4