📄 linalg_vec.h
字号:
} inline subrange_type operator()(size_type s, size_type f) MTL_THROW_ASSERTION { return subrange_type(data() + s, f - s); } //: Return a const reference to the element with the ith index //!wheredef: Vector inline const_reference operator[](size_type i) const MTL_THROW_ASSERTION { MTL_ASSERT(i < size(), "linalg_vec::operator[]"); return (*rep)[i - first]; } /**@name Size Methods */ //: The size of the vector //!wheredef: Container inline size_type size() const { return rep->size(); } //: The number of non-zeroes in the vector inline size_type nnz() const { return rep->size(); } //: Resize the vector to n inline void resize(size_type n) { rep->resize(n); } //: Resize the vector to n, and assign x to the new positions inline void resize(size_type n, const value_type& x) { rep->resize(n, x); } //: Return the total capacity of the vector size_type capacity() const { return rep->capacity(); } //: Reserve more space in the vector void reserve(size_type n) { rep->reserve(n); } //: Raw Memory Access inline const value_type* data() const { return &(*rep)[0]; } //: Raw Memory Access inline value_type* data() { return &(*rep)[0]; } //: Insert x at the indicated position in the vector //!wheredef: Container inline iterator insert (iterator position, const value_type& x = value_type()) { return iterator(rep->insert(position.base(), x), position.index()+1); /* JGS, not sure about what to do with the index here */ } inline IndexArrayRef nz_struct() const { return IndexArrayRef(*this); } inline self& adjust_index(size_type i) { first += i; return *this; }protected: rep_ptr rep; size_type first;};//: External 1-D Container//!category: containers//!component: type//// This is similar to dense1D, except that the memory is provided// by the user. This allows for interoperability with other array// packages and even with Fortran.////!definition: linalg_vec.h//!tparam: T - The element type.//!tparam: NN - The static size of the Vector, 0 if dynamic size//!tparam: SizeT - The size type to use - size_t//!tparam: DiffT - ptrdiff_t//!models: Vector//!example: dot_prod.cc, apply_givens.cc, euclid_norm.cc, max_index.cctemplate <class T, int NN = 0, class SizeType=unsigned int>class external_vec { typedef external_vec self;public: enum { N = NN }; typedef external_vec<int> IndexArray; /* JGS */ /* Type Definitions */ //: The vector is dense typedef dense_tag sparsity; //: Scaled type for the vector typedef scaled1D< self > scaled_type; typedef SizeType size_type; typedef int difference_type; //: The element type typedef T value_type; //: The reference to the value type typedef T& reference; //: The pointer ot the value type typedef T* pointer; //: The const reference type typedef const T& const_reference; //: The const pointer to the value type typedef const T* const_pointer;#if defined( _MSVCPP_ ) typedef dense_iterator<T, 0, 0, size_type> iterator; typedef dense_iterator<T, 1, 0, size_type> const_iterator;#elif defined ( _MSVCPP7_ ) /// used std::_Ptrit in order to support iterator_traits for /// pointers masquerading as iterators as per std::vector and std::basic_string - BEL // typedef std::_Ptrit<value_type, difference_type, pointer, reference, pointer, reference> ptr_iterator; typedef std::_Ptrit<value_type, difference_type, const_pointer, const_reference, pointer, reference> ptr_const_iterator; typedef dense_iterator<ptr_iterator,0,size_type> iterator; typedef dense_iterator<ptr_const_iterator,0,size_type> const_iterator;#else typedef dense_iterator<T*,0,size_type> iterator; typedef dense_iterator<const T*,0,size_type> const_iterator;#endif //: The reverse iterator type typedef reverse_iter<iterator> reverse_iterator; //: The const reverse iterator type typedef reverse_iter<const_iterator> const_reverse_iterator; //: //!wheredef: Vector typedef self IndexArrayRef; //: The type for the subrange vector //!wheredef: Vector typedef self subrange_type; typedef std::pair<size_type, size_type> range; //: This is a 1D container typedef oned_tag dimension; /* Constructors */ //: Default Constructor inline external_vec() : rep(0), size_(0), first(0) { } //: External Data Contructor inline external_vec(T* data) : rep(data), size_(N), first(0) { } //: Preallocated Memory Constructor with optional non-zero starting index inline external_vec(T* data, size_type n, size_type start = 0) : rep(data), size_(n), first(start) { } //: Copy Constructor inline external_vec(const self& x) : rep(x.rep), size_(x.size_), first(x.first) { } //: Assignment inline self& operator=(const self& x) { rep = x.rep; size_ = x.size_; first = x.first; return *this; } //: Destructor inline ~external_vec() { } /* Access Methods */ /* Iterator Access Methods */ //: Return an iterator pointing to the beginning of the vector //!wheredef: Container inline iterator begin() { return iterator(rep, 0, first); } //: Return an iterator pointing past the end of the vector //!wheredef: Container inline iterator end() { return iterator(rep, size(), first); } //: Return a const iterator pointing to the begining of the vector //!wheredef: Container inline const_iterator begin() const { return const_iterator(rep, 0, first); } //: Return a const iterator pointing past the end of the vector //!wheredef: Container inline const_iterator end() const{ return const_iterator(rep, size(), first); } //: Return a reverse iterator pointing to the last element of the vector //!wheredef: Reversible Container inline reverse_iterator rbegin() { return reverse_iterator(end()); } //: Return a reverse iterator pointing past the end of the vector //!wheredef: Reversible Container inline reverse_iterator rend() { return reverse_iterator(begin()); } //: Return a const reverse iterator pointing to the last element of the vector //!wheredef: Reversible Container inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } //: Return a const reverse iterator pointing past the end of the vector //!wheredef: Reversible Container inline const_reverse_iterator rend() const{ return const_reverse_iterator(begin()); } /* Element Access Methods */ //: Return a reference to the element with the ith index //!wheredef: Vector inline reference operator[](size_type i) { return rep[i - first]; } //: Return a const reference to the element with the ith index //!wheredef: Vector inline const_reference operator[](size_type i) const { return rep[i - first]; } //: Return a subrange vector with start at s and finish at f //!wheredef: Vector inline subrange_type operator()(size_type s, size_type f) const { return subrange_type(rep + s - first, f - s, 0); } inline subrange_type operator()(range r) MTL_THROW_ASSERTION { return subrange_type(data() + r.first, r.second - r.first, 0); } /* Size Methods */ //: The size of the vector //!wheredef: Container inline size_type size() const { return N ? N : size_; } //: The number of non-zeroes in the vector //!wheredef: Vector inline size_type nnz() const { return size(); } //: Resize the vector to size n#if 0 inline void resize(size_type n) { if (rep) delete [] rep; size_ = n; rep = new T[size_]; }#else inline void resize(size_type n) { size_ = n; } inline void clear() { size_ = 0; }#endif //: Raw Memory Access inline value_type* data() const { return rep; } inline self& adjust_index(size_type i) { first += i; return *this; } //: Push x onto the end of the vector, increasing the size // This function does not allocation memory. // Better hope enough memory is already there! void push_back(const T& x) { rep[size_] = x; ++size_; }protected: T* rep; size_type size_; size_type first;};//: blah//!noindex:template <int N>struct __make_external { template <class T> inline external_vec<T,N> operator()(T* x) { return external_vec<T,N>(x); }};/* For converting static arrays into MTL vectors */#define array_to_vec(x) mtl::__make_external<sizeof(x)/sizeof(*x)>()(x)template <class Container>inline linalg_vec<Container>vec(const Container& x){ return linalg_vec<Container>(x);}} /* namespace mtl */#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -