|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #ifndef SPARSE_VECTOR_CLASS_DECL_H 00043 #define SPARSE_VECTOR_CLASS_DECL_H 00044 00045 #include <assert.h> 00046 00047 #include <vector> 00048 #include <sstream> 00049 00050 #include "AbstractLinAlgPack_SpVecIndexLookupClass.hpp" 00051 00052 namespace AbstractLinAlgPack { 00053 00054 namespace SparseVectorUtilityPack { 00055 00056 void assert_is_sorted(bool is_sorted); 00057 00059 class DoesNotExistException : public std::logic_error 00060 {public: DoesNotExistException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00062 class NotSortedException : public std::logic_error 00063 {public: NotSortedException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00065 class DuplicateIndexesException : public std::logic_error 00066 {public: DuplicateIndexesException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00068 class OutOfRoomException : public std::logic_error 00069 {public: OutOfRoomException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00071 class UnsizedException : public std::logic_error 00072 {public: UnsizedException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00074 class NoNonZeroElementsException : public std::logic_error 00075 {public: NoNonZeroElementsException(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00076 00077 } // end namespace SparseVectorUtilityPack 00078 00079 // ///////////////////////////////////////////////////////////////////////////////////// 00082 00094 template<class T_Element> 00095 SparseVectorSlice<T_Element> create_slice( 00096 const SparseVectorUtilityPack::SpVecIndexLookup<T_Element>& index_lookup 00097 , size_type size, Range1D rng); 00098 00100 00101 template <class T_Element> 00102 class SparseVectorSlice; 00103 00104 // /////////////////////////////////////////////////////////////////////// 00126 template <class T_Element, class T_Alloc = std::allocator<T_Element> > 00127 class SparseVector { 00128 public: 00131 00133 typedef T_Alloc allocator_type; 00135 typedef T_Element element_type; 00137 typedef AbstractLinAlgPack::size_type size_type; 00139 typedef ptrdiff_t difference_type; 00141 typedef element_type* iterator; 00143 typedef const element_type* const_iterator; 00144 00145 #if 0 /* defined(_WINDOWS) || defined(_INTEL_CXX) */ 00146 00147 typedef std::reverse_iterator<iterator, element_type 00148 , element_type&, element_type*, difference_type> reverse_iterator; 00149 00150 typedef std::reverse_iterator<const_iterator 00151 , element_type, const element_type& 00152 , const element_type*, difference_type> const_reverse_iterator; 00153 00154 #else 00155 00157 typedef std::reverse_iterator<iterator> reverse_iterator; 00159 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00160 00161 #endif 00162 00164 typedef SparseVectorUtilityPack::DoesNotExistException DoesNotExistException; 00166 typedef SparseVectorUtilityPack::NotSortedException NotSortedException; 00168 typedef SparseVectorUtilityPack::DuplicateIndexesException DuplicateIndexesException; 00170 typedef SparseVectorUtilityPack::OutOfRoomException OutOfRoomException; 00172 typedef SparseVectorUtilityPack::UnsizedException UnsizedException; 00174 typedef SparseVectorUtilityPack::NoNonZeroElementsException NoNonZeroElementsException; 00175 00177 00180 00184 SparseVector(const allocator_type& alloc = allocator_type()); 00185 00187 SparseVector(bool assume_sorted, const allocator_type& alloc = allocator_type()); 00188 00190 SparseVector(size_type size, size_type max_nz, difference_type offset = 0 00191 , bool assume_sorted = false, const allocator_type& alloc = allocator_type()); 00192 00198 SparseVector(const SparseVector<T_Element,T_Alloc>& sp_vec); 00199 00201 SparseVector( SparseVectorSlice<T_Element> sp_vec_slc 00202 , const allocator_type& alloc = allocator_type()); 00203 00205 ~SparseVector(); 00206 00208 00214 SparseVector<T_Element,T_Alloc>& operator=(const SparseVector<T_Element,T_Alloc>& sp_vec); 00215 00221 SparseVector<T_Element,T_Alloc>& operator=(const SparseVectorSlice<T_Element>& sp_vec_slc); 00222 00224 00233 EOverLap overlap(const SparseVectorSlice<T_Element>& sv) const; 00234 00237 00239 size_type dim() const; 00240 00242 size_type nz() const; 00243 00247 difference_type offset() const; 00248 00255 bool is_sorted() const; 00256 00264 00270 iterator begin(); 00271 00273 const_iterator begin() const; 00274 00276 iterator end(); 00277 00279 const_iterator end() const; 00280 00286 reverse_iterator rbegin(); 00287 00289 const_reverse_iterator rbegin() const; 00290 00292 reverse_iterator rend(); 00293 00295 const_reverse_iterator rend() const; 00296 00297 // end Iterator access to elements 00299 00300 // end SparseVectorTemplateInterface 00302 00305 00312 void resize(size_type size, size_type max_nz, difference_type offset = 0); 00313 00321 void uninitialized_resize(size_type size, size_type nz, size_type max_nz, difference_type offset = 0); 00322 00324 size_type max_nz() const; 00325 00335 void add_element(element_type ele); 00336 00346 void insert_element(element_type ele); 00347 00352 void assume_sorted(bool assume_is_sorted); 00353 00355 void sort(); 00356 00368 void assert_valid_and_sorted() const; 00369 00371 00386 00388 element_type* lookup_element(size_type i); 00390 const element_type* lookup_element(size_type i) const; 00391 00393 00402 00407 operator SparseVectorSlice<T_Element>(); 00408 00410 operator const SparseVectorSlice<T_Element>() const; 00411 00418 SparseVectorSlice<T_Element> operator()(); 00419 00421 const SparseVectorSlice<T_Element> operator()() const; 00422 00424 00440 SparseVectorSlice<T_Element> operator()(const Range1D& rng); 00441 00443 const SparseVectorSlice<T_Element> operator()(const Range1D& rng) const; 00444 00446 00462 SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound); 00463 00465 const SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound) const; 00466 00468 00469 private: 00470 00471 // ///////////////////////////////////////////////////////////////////////// 00472 // Private types 00473 00475 typedef SparseVectorUtilityPack::SpVecIndexLookup<element_type> SpVecIndexLookup; 00476 00477 // ///////////////////////////////////////////////////////////////////////// 00478 // Private data members 00479 00480 allocator_type alloc_; // allocator used to allocate memory 00481 size_type size_; // the number of elements in the full vector 00482 size_type max_nz_; // the amount of storage that has been allocated 00483 // commented out because of problems with MS Visual C++ 5.0 00484 // std::vector<element_type, allocator_type> ele_; 00485 SpVecIndexLookup index_lookup_; // Acts as storage for elements and caching of searches. 00486 bool assume_sorted_; // true if the client said that you can assume sorted. 00487 bool know_is_sorted_; // true if it has been varified that is sorted. 00488 00489 // ////////////////////////// 00490 // Private member functions 00491 00492 // Throw a NotSortedException of is_sorted() == false 00493 void assert_is_sorted() const { 00494 SparseVectorUtilityPack::assert_is_sorted(is_sorted()); 00495 } 00496 00498 void assert_space(size_type n) const { 00499 #ifdef LINALGPACK_CHECK_SLICE_SETUP 00500 if(index_lookup_.nz() + n > max_nz_) 00501 throw OutOfRoomException("SparseVector<T_Element,T_Alloc>::assert_space(): There is not storage for this many elements"); 00502 #endif 00503 } 00504 00506 void assert_sized_with_mem_set() const { 00507 if(!dim()) 00508 throw UnsizedException("SparseVector<...>::assert_sized_with_mem_set() : " 00509 "Error: The sparse vector is unsized"); 00510 if(!index_lookup_.ele()) { 00511 throw NoNonZeroElementsException("SparseVector<...>::assert_sized_with_mem_set() : " 00512 "Error: There is no memory set."); 00513 } 00514 } 00515 00517 SparseVectorSlice<T_Element> get_whole_sp_vec() { 00518 return SparseVectorSlice<T_Element>(index_lookup_.ele(), index_lookup_.nz() 00519 , index_lookup_.offset(), size_, is_sorted()); 00520 } 00521 00523 const SparseVectorSlice<T_Element> get_whole_sp_vec() const { 00524 return SparseVectorSlice<T_Element>(index_lookup_.ele(), index_lookup_.nz() 00525 , index_lookup_.offset(), size_, is_sorted()); 00526 } 00527 00529 SparseVectorSlice<T_Element> get_slice(const Range1D& rng) const { 00530 assert_is_sorted(); 00531 return create_slice(index_lookup_, size_, rng); 00532 } 00533 00534 }; // end class SparseVector 00535 00536 // /////////////////////////////////////////////////////////////////////// 00554 template <class T_Element> 00555 class SparseVectorSlice { 00556 public: 00559 00561 typedef T_Element element_type; 00563 typedef AbstractLinAlgPack::size_type size_type; 00565 typedef ptrdiff_t difference_type; 00567 typedef element_type* iterator; 00569 typedef const element_type* const_iterator; 00570 00571 #if 0 /* defined(_WINDOWS) || defined(_INTEL_CXX) */ 00572 00573 typedef std::reverse_iterator<iterator, element_type 00574 , element_type&, element_type*, difference_type> reverse_iterator; 00575 00576 typedef std::reverse_iterator<const_iterator 00577 , element_type, const element_type& 00578 , const element_type*, difference_type> const_reverse_iterator; 00579 00580 #else 00581 00583 typedef std::reverse_iterator<iterator> reverse_iterator; 00585 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00586 00587 #endif 00588 00590 typedef SparseVectorUtilityPack::DoesNotExistException DoesNotExistException; 00592 typedef SparseVectorUtilityPack::NotSortedException NotSortedException; 00593 00595 00601 00622 SparseVectorSlice(element_type ele[], size_type nz, difference_type offset, size_type size 00623 , bool assume_sorted = false); 00624 00626 00629 void bind(SparseVectorSlice svs); 00630 00632 00641 EOverLap overlap(const SparseVectorSlice<T_Element>& sv) const; 00642 00645 00647 size_type dim() const; 00648 00650 size_type nz() const; 00651 00655 difference_type offset() const; 00656 00659 bool is_sorted() const; 00660 00662 iterator begin(); 00663 00665 const_iterator begin() const; 00666 00668 iterator end(); 00669 00671 const_iterator end() const; 00672 00674 reverse_iterator rbegin(); 00675 00677 const_reverse_iterator rbegin() const; 00678 00680 reverse_iterator rend(); 00681 00683 const_reverse_iterator rend() const; 00684 00686 00701 00703 element_type* lookup_element(size_type i); 00705 const element_type* lookup_element(size_type i) const; 00706 00708 00711 00713 00717 SparseVectorSlice<T_Element>& operator()(); 00718 00720 const SparseVectorSlice<T_Element>& operator()() const; 00721 00723 SparseVectorSlice* operator&() 00724 { return this; } 00725 00726 const SparseVectorSlice* operator&() const 00727 { return this; } 00728 00730 00746 SparseVectorSlice<T_Element> operator()(const Range1D& rng); 00747 00749 const SparseVectorSlice<T_Element> operator()(const Range1D& rng) const; 00750 00752 00768 SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound); 00769 00771 const SparseVectorSlice<T_Element> operator()(size_type lbound, size_type ubound) const; 00772 00774 00775 private: 00776 // ///////////////////////////////////////////////////////////////////////// 00777 // Private types 00778 00780 typedef SparseVectorUtilityPack::SpVecIndexLookup<element_type> index_lookup_type; 00781 00782 // ///////////////////////////////////////////////////////////////////////// 00783 // Private data members 00784 00785 index_lookup_type index_lookup_; // Acts as storage and cacheing 00786 size_type size_; // size of the full vector 00787 bool assume_sorted_; // true if the client said that you can assume sorted. 00788 00789 00790 // ///////////////////////////////////////////////////////////////////////// 00791 // Private member functions 00792 00793 // Throw a NotSortedException of is_sorted() == false 00794 void assert_is_sorted() const { 00795 SparseVectorUtilityPack::assert_is_sorted(is_sorted()); 00796 } 00797 00799 SparseVectorSlice<T_Element> get_slice(const Range1D& rng) const { 00800 assert_is_sorted(); 00801 return create_slice(index_lookup_, size_, rng); 00802 } 00803 00805 SparseVectorSlice(); 00807 SparseVectorSlice<element_type>& operator=(const SparseVectorSlice<element_type>&); 00808 00809 }; // end class SparseVectorSlice 00810 00811 // //////////////////////////////////////////////// 00812 // Non-member non-public utility functions 00813 00814 namespace SparseVectorUtilityPack { 00815 00820 template< class T_Element > 00821 inline const T_Element* lookup_element( const SpVecIndexLookup<T_Element>& index_lookup 00822 , typename SpVecIndexLookup<T_Element>::index_type index, bool is_sorted ) 00823 { 00824 size_type poss; 00825 return ( ( poss = index_lookup.find_element(index,is_sorted) ) < index_lookup.nz() ) 00826 ? index_lookup.ele() + poss 00827 : NULL; 00828 } 00829 00830 } // end namespace SparseVectorUtilityPack 00831 00832 // ///////////////////////////////////////////////////////////////////////////////////// 00833 // Inline members for SparseVector<> 00834 00835 // constructors 00836 00837 template <class T_Element, class T_Alloc> 00838 inline SparseVector<T_Element,T_Alloc>::SparseVector(const allocator_type& alloc) 00839 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(false), know_is_sorted_(false) 00840 {} 00841 00842 template <class T_Element, class T_Alloc> 00843 inline SparseVector<T_Element,T_Alloc>::SparseVector(bool assume_sorted,const allocator_type& alloc) 00844 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false) 00845 {} 00846 00847 template <class T_Element, class T_Alloc> 00848 inline SparseVector<T_Element,T_Alloc>::SparseVector(size_type size, size_type max_nz 00849 , difference_type offset, bool assume_sorted, const allocator_type& alloc) 00850 : alloc_(alloc), size_(0), max_nz_(0), assume_sorted_(assume_sorted), know_is_sorted_(false) 00851 { 00852 resize(size,max_nz,offset); 00853 } 00854 00855 template <class T_Element, class T_Alloc> 00856 inline SparseVector<T_Element,T_Alloc>::~SparseVector() { 00857 resize(0,0); 00858 } 00859 00860 // SparseVectorTemplateInterface for linear algebra operations 00861 00862 template <class T_Element, class T_Alloc> 00863 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::dim() const { 00864 return size_; 00865 } 00866 00867 template <class T_Element, class T_Alloc> 00868 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::nz() const { 00869 return index_lookup_.nz(); 00870 } 00871 00872 template <class T_Element, class T_Alloc> 00873 inline typename SparseVector<T_Element,T_Alloc>::difference_type SparseVector<T_Element,T_Alloc>::offset() const { 00874 return index_lookup_.offset(); 00875 } 00876 00877 template <class T_Element, class T_Alloc> 00878 inline bool SparseVector<T_Element,T_Alloc>::is_sorted() const { 00879 return nz() <= 1 || assume_sorted_ || know_is_sorted_; 00880 } 00881 00882 template <class T_Element, class T_Alloc> 00883 inline typename SparseVector<T_Element,T_Alloc>::iterator SparseVector<T_Element,T_Alloc>::begin() { 00884 return index_lookup_.nz() ? index_lookup_.ele() : NULL; 00885 } 00886 00887 template <class T_Element, class T_Alloc> 00888 inline typename SparseVector<T_Element,T_Alloc>::const_iterator SparseVector<T_Element,T_Alloc>::begin() const { 00889 return index_lookup_.nz() ? index_lookup_.ele() : NULL; 00890 } 00891 00892 template <class T_Element, class T_Alloc> 00893 inline typename SparseVector<T_Element,T_Alloc>::iterator SparseVector<T_Element,T_Alloc>::end() { 00894 return index_lookup_.nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL; 00895 } 00896 00897 template <class T_Element, class T_Alloc> 00898 inline typename SparseVector<T_Element,T_Alloc>::const_iterator SparseVector<T_Element,T_Alloc>::end() const { 00899 return index_lookup_.nz() ? index_lookup_.ele() + index_lookup_.nz() : NULL; 00900 } 00901 00902 template <class T_Element, class T_Alloc> 00903 inline typename SparseVector<T_Element,T_Alloc>::reverse_iterator SparseVector<T_Element,T_Alloc>::rbegin() { 00904 return reverse_iterator(end()); 00905 } 00906 00907 template <class T_Element, class T_Alloc> 00908 inline typename SparseVector<T_Element,T_Alloc>::const_reverse_iterator SparseVector<T_Element,T_Alloc>::rbegin() const { 00909 return const_reverse_iterator(end()); 00910 } 00911 00912 template <class T_Element, class T_Alloc> 00913 inline typename SparseVector<T_Element,T_Alloc>::reverse_iterator SparseVector<T_Element,T_Alloc>::rend() { 00914 return reverse_iterator(begin()); 00915 } 00916 00917 template <class T_Element, class T_Alloc> 00918 inline typename SparseVector<T_Element,T_Alloc>::const_reverse_iterator SparseVector<T_Element,T_Alloc>::rend() const { 00919 return const_reverse_iterator(begin()); 00920 } 00921 00922 // Element setup and modification 00923 00924 template <class T_Element, class T_Alloc> 00925 inline typename SparseVector<T_Element,T_Alloc>::size_type SparseVector<T_Element,T_Alloc>::max_nz() const { 00926 return max_nz_; 00927 } 00928 00929 template <class T_Element, class T_Alloc> 00930 inline void SparseVector<T_Element,T_Alloc>::add_element(element_type ele) { 00931 assert_space(1); 00932 assume_sorted_ = know_is_sorted_ = false; 00933 #ifdef _PG_CXX 00934 new (index_lookup_.ele() + index_lookup_.nz()) element_type; 00935 #else 00936 alloc_.construct(index_lookup_.ele() + index_lookup_.nz(), ele); 00937 #endif 00938 index_lookup_.incr_nz(); 00939 } 00940 00941 template <class T_Element, class T_Alloc> 00942 inline void SparseVector<T_Element,T_Alloc>::assume_sorted(bool assume_is_sorted) { 00943 assume_sorted_ = assume_is_sorted; 00944 } 00945 00946 // Lookup an element 00947 00948 template <class T_Element, class T_Alloc> 00949 inline 00950 typename SparseVector<T_Element,T_Alloc>::element_type* 00951 SparseVector<T_Element,T_Alloc>::lookup_element(size_type i) 00952 { 00953 return const_cast<element_type*>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_)); 00954 } 00955 00956 template <class T_Element, class T_Alloc> 00957 inline 00958 const typename SparseVector<T_Element,T_Alloc>::element_type* 00959 SparseVector<T_Element,T_Alloc>::lookup_element(size_type i) const 00960 { 00961 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_); 00962 } 00963 00964 // Creating a slice (subregion) of the sparse vector 00965 00966 template <class T_Element, class T_Alloc> 00967 inline SparseVector<T_Element,T_Alloc>::operator SparseVectorSlice<T_Element>() { 00968 return get_whole_sp_vec(); 00969 } 00970 00971 template <class T_Element, class T_Alloc> 00972 inline SparseVector<T_Element,T_Alloc>::operator const SparseVectorSlice<T_Element>() const { 00973 return get_whole_sp_vec(); 00974 } 00975 00976 template <class T_Element, class T_Alloc> 00977 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()() { 00978 return get_whole_sp_vec(); 00979 } 00980 00981 template <class T_Element, class T_Alloc> 00982 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()() const { 00983 return get_whole_sp_vec(); 00984 } 00985 00986 template <class T_Element, class T_Alloc> 00987 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(const Range1D& rng) { 00988 return get_slice(rng); 00989 } 00990 00991 template <class T_Element, class T_Alloc> 00992 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(const Range1D& rng) const { 00993 return get_slice(rng); 00994 } 00995 00996 template <class T_Element, class T_Alloc> 00997 inline SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(size_type lbound, size_type ubound) { 00998 return get_slice(Range1D(lbound,ubound)); 00999 } 01000 01001 template <class T_Element, class T_Alloc> 01002 inline const SparseVectorSlice<T_Element> SparseVector<T_Element,T_Alloc>::operator()(size_type lbound, size_type ubound) const { 01003 return get_slice(Range1D(lbound,ubound)); 01004 } 01005 01006 // ///////////////////////////////////////////////////////////////////////////////////// 01007 // Inline members for SparseVectorSlice<> 01008 01009 // Constuctors 01010 01011 template <class T_Element> 01012 inline SparseVectorSlice<T_Element>::SparseVectorSlice(element_type ele[], size_type nz 01013 , difference_type offset, size_type size, bool assume_sorted) 01014 : index_lookup_(ele,nz,offset), size_(size), assume_sorted_(assume_sorted) 01015 {} 01016 01017 template <class T_Element> 01018 inline void SparseVectorSlice<T_Element>::bind(SparseVectorSlice svs) 01019 { 01020 index_lookup_ = svs.index_lookup_; 01021 size_ = svs.size_; 01022 assume_sorted_ = svs.assume_sorted_; 01023 } 01024 01025 // Sparse Vector Templated interface for linear algebra operations 01026 01027 template <class T_Element> 01028 inline typename SparseVectorSlice<T_Element>::size_type SparseVectorSlice<T_Element>::dim() const { 01029 return size_; 01030 } 01031 01032 template <class T_Element> 01033 inline typename SparseVectorSlice<T_Element>::size_type SparseVectorSlice<T_Element>::nz() const { 01034 return index_lookup_.nz(); 01035 } 01036 01037 template <class T_Element> 01038 inline typename SparseVectorSlice<T_Element>::difference_type SparseVectorSlice<T_Element>::offset() const { 01039 return index_lookup_.offset(); 01040 } 01041 01042 template <class T_Element> 01043 inline bool SparseVectorSlice<T_Element>::is_sorted() const { 01044 return nz() <= 1 || assume_sorted_; 01045 } 01046 01047 template <class T_Element> 01048 inline typename SparseVectorSlice<T_Element>::iterator SparseVectorSlice<T_Element>::begin() { 01049 return index_lookup_.ele(); 01050 } 01051 01052 template <class T_Element> 01053 inline typename SparseVectorSlice<T_Element>::const_iterator SparseVectorSlice<T_Element>::begin() const { 01054 return index_lookup_.ele(); 01055 } 01056 01057 template <class T_Element> 01058 inline typename SparseVectorSlice<T_Element>::iterator SparseVectorSlice<T_Element>::end() { 01059 return index_lookup_.ele() + index_lookup_.nz(); 01060 } 01061 01062 template <class T_Element> 01063 inline typename SparseVectorSlice<T_Element>::const_iterator SparseVectorSlice<T_Element>::end() const { 01064 return index_lookup_.ele() + index_lookup_.nz(); 01065 } 01066 01067 template <class T_Element> 01068 inline typename SparseVectorSlice<T_Element>::reverse_iterator SparseVectorSlice<T_Element>::rbegin() { 01069 return reverse_iterator(end()); 01070 } 01071 01072 template <class T_Element> 01073 inline typename SparseVectorSlice<T_Element>::const_reverse_iterator SparseVectorSlice<T_Element>::rbegin() const { 01074 return const_reverse_iterator(end()); 01075 } 01076 01077 template <class T_Element> 01078 inline typename SparseVectorSlice<T_Element>::reverse_iterator SparseVectorSlice<T_Element>::rend() { 01079 return reverse_iterator(begin()); 01080 } 01081 01082 template <class T_Element> 01083 inline typename SparseVectorSlice<T_Element>::const_reverse_iterator SparseVectorSlice<T_Element>::rend() const { 01084 return const_reverse_iterator(begin()); 01085 } 01086 01087 // Lookup an element 01088 01089 template <class T_Element> 01090 inline 01091 typename SparseVectorSlice<T_Element>::element_type* 01092 SparseVectorSlice<T_Element>::lookup_element(size_type i) 01093 { 01094 return const_cast<element_type*>(SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_)); 01095 } 01096 01097 template <class T_Element> 01098 inline 01099 const typename SparseVectorSlice<T_Element>::element_type* 01100 SparseVectorSlice<T_Element>::lookup_element(size_type i) const 01101 { 01102 return SparseVectorUtilityPack::lookup_element(index_lookup_,i,assume_sorted_); 01103 } 01104 01105 // Creating a slice (subregion) of the sparse vector 01106 01107 template <class T_Element> 01108 inline SparseVectorSlice<T_Element>& SparseVectorSlice<T_Element>::operator()() { 01109 return *this; 01110 } 01111 01112 template <class T_Element> 01113 inline const SparseVectorSlice<T_Element>& SparseVectorSlice<T_Element>::operator()() const { 01114 return *this; 01115 } 01116 01117 template <class T_Element> 01118 inline SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(const Range1D& rng) { 01119 return get_slice(rng); 01120 } 01121 01122 template <class T_Element> 01123 inline const SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(const Range1D& rng) const { 01124 return get_slice(rng); 01125 } 01126 01127 template <class T_Element> 01128 inline SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(size_type lbound, size_type ubound) { 01129 return get_slice(Range1D(lbound,ubound)); 01130 } 01131 01132 template <class T_Element> 01133 inline const SparseVectorSlice<T_Element> SparseVectorSlice<T_Element>::operator()(size_type lbound, size_type ubound) const { 01134 return get_slice(Range1D(lbound,ubound)); 01135 } 01136 01137 } // end namespace AbstractLinAlgPack 01138 01139 #endif // SPARSE_VECTOR_CLASS_DECL_H
1.7.6.1