📄 matrix_proxy.hpp
字号:
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. The authors make no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//
// The authors gratefully acknowledge the support of
// GeNeSys mbH & Co. KG in producing this work.
//
#ifndef _BOOST_UBLAS_MATRIX_PROXY_
#define _BOOST_UBLAS_MATRIX_PROXY_
#include <boost/numeric/ublas/matrix_expression.hpp>
#include <boost/numeric/ublas/detail/vector_assign.hpp>
#include <boost/numeric/ublas/detail/matrix_assign.hpp>
#include <boost/numeric/ublas/detail/temporary.hpp>
// Iterators based on ideas of Jeremy Siek
namespace boost { namespace numeric { namespace ublas {
// Matrix based row vector class
template<class M>
class matrix_row:
public vector_expression<matrix_row<M> > {
typedef matrix_row<M> self_type;
public:
#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
using vector_expression<self_type>::operator ();
#endif
typedef M matrix_type;
typedef typename M::size_type size_type;
typedef typename M::difference_type difference_type;
typedef typename M::value_type value_type;
typedef typename M::const_reference const_reference;
typedef typename boost::mpl::if_<boost::is_const<M>,
typename M::const_reference,
typename M::reference>::type reference;
typedef typename boost::mpl::if_<boost::is_const<M>,
typename M::const_closure_type,
typename M::closure_type>::type matrix_closure_type;
typedef const self_type const_closure_type;
typedef self_type closure_type;
typedef typename storage_restrict_traits<typename M::storage_category,
dense_proxy_tag>::storage_category storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
matrix_row (matrix_type &data, size_type i):
data_ (data), i_ (i) {
// Early checking of preconditions here.
// BOOST_UBLAS_CHECK (i_ < data_.size1 (), bad_index ());
}
// Accessors
BOOST_UBLAS_INLINE
size_type size () const {
return data_.size2 ();
}
BOOST_UBLAS_INLINE
size_type index () const {
return i_;
}
// Storage accessors
BOOST_UBLAS_INLINE
const matrix_closure_type &data () const {
return data_;
}
BOOST_UBLAS_INLINE
matrix_closure_type &data () {
return data_;
}
// Element access
#ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
BOOST_UBLAS_INLINE
const_reference operator () (size_type j) const {
return data_ (i_, j);
}
BOOST_UBLAS_INLINE
reference operator () (size_type j) {
return data_ (i_, j);
}
BOOST_UBLAS_INLINE
const_reference operator [] (size_type j) const {
return (*this) (j);
}
BOOST_UBLAS_INLINE
reference operator [] (size_type j) {
return (*this) (j);
}
#else
BOOST_UBLAS_INLINE
reference operator () (size_type j) const {
return data_ (i_, j);
}
BOOST_UBLAS_INLINE
reference operator [] (size_type j) const {
return (*this) (j);
}
#endif
// Assignment
BOOST_UBLAS_INLINE
matrix_row &operator = (const matrix_row &mr) {
// ISSUE need a temporary, proxy can be overlaping alias
vector_assign<scalar_assign> (*this, typename vector_temporary_traits<M>::type (mr));
return *this;
}
BOOST_UBLAS_INLINE
matrix_row &assign_temporary (matrix_row &mr) {
// assign elements, proxied container remains the same
vector_assign<scalar_assign> (*this, mr);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator = (const vector_expression<AE> &ae) {
vector_assign<scalar_assign> (*this, typename vector_temporary_traits<M>::type (ae));
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &assign (const vector_expression<AE> &ae) {
vector_assign<scalar_assign> (*this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator += (const vector_expression<AE> &ae) {
vector_assign<scalar_assign> (*this, typename vector_temporary_traits<M>::type (*this + ae));
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &plus_assign (const vector_expression<AE> &ae) {
vector_assign<scalar_plus_assign> (*this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator -= (const vector_expression<AE> &ae) {
vector_assign<scalar_assign> (*this, typename vector_temporary_traits<M>::type (*this - ae));
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &minus_assign (const vector_expression<AE> &ae) {
vector_assign<scalar_minus_assign> (*this, ae);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
matrix_row &operator *= (const AT &at) {
vector_assign_scalar<scalar_multiplies_assign> (*this, at);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
matrix_row &operator /= (const AT &at) {
vector_assign_scalar<scalar_divides_assign> (*this, at);
return *this;
}
// Closure comparison
BOOST_UBLAS_INLINE
bool same_closure (const matrix_row &mr) const {
return (*this).data_.same_closure (mr.data_);
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const matrix_row &mr) const {
return (*this).data_ == mr.data_ && index () == mr.index ();
}
// Swapping
BOOST_UBLAS_INLINE
void swap (matrix_row mr) {
if (this != &mr) {
BOOST_UBLAS_CHECK (size () == mr.size (), bad_size ());
// Sparse ranges may be nonconformant now.
// std::swap_ranges (begin (), end (), mr.begin ());
vector_swap<scalar_swap> (*this, mr);
}
}
BOOST_UBLAS_INLINE
friend void swap (matrix_row mr1, matrix_row mr2) {
mr1.swap (mr2);
}
// Iterator types
private:
typedef typename M::const_iterator2 const_subiterator_type;
typedef typename boost::mpl::if_<boost::is_const<M>,
typename M::const_iterator2,
typename M::iterator2>::type subiterator_type;
public:
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
typedef indexed_iterator<matrix_row<matrix_type>,
typename subiterator_type::iterator_category> iterator;
typedef indexed_const_iterator<matrix_row<matrix_type>,
typename const_subiterator_type::iterator_category> const_iterator;
#else
class const_iterator;
class iterator;
#endif
// Element lookup
BOOST_UBLAS_INLINE
const_iterator find (size_type j) const {
const_subiterator_type it2 (data_.find2 (1, i_, j));
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
return const_iterator (*this, it2.index2 ());
#else
return const_iterator (*this, it2);
#endif
}
BOOST_UBLAS_INLINE
iterator find (size_type j) {
subiterator_type it2 (data_.find2 (1, i_, j));
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
return iterator (*this, it2.index2 ());
#else
return iterator (*this, it2);
#endif
}
#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
class const_iterator:
public container_const_reference<matrix_row>,
public iterator_base_traits<typename const_subiterator_type::iterator_category>::template
iterator_base<const_iterator, value_type>::type {
public:
typedef typename const_subiterator_type::value_type value_type;
typedef typename const_subiterator_type::difference_type difference_type;
typedef typename const_subiterator_type::reference reference;
typedef typename const_subiterator_type::pointer pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
const_iterator ():
container_const_reference<self_type> (), it_ () {}
BOOST_UBLAS_INLINE
const_iterator (const self_type &mr, const const_subiterator_type &it):
container_const_reference<self_type> (mr), it_ (it) {}
BOOST_UBLAS_INLINE
const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
container_const_reference<self_type> (it ()), it_ (it.it_) {}
// Arithmetic
BOOST_UBLAS_INLINE
const_iterator &operator ++ () {
++ it_;
return *this;
}
BOOST_UBLAS_INLINE
const_iterator &operator -- () {
-- it_;
return *this;
}
BOOST_UBLAS_INLINE
const_iterator &operator += (difference_type n) {
it_ += n;
return *this;
}
BOOST_UBLAS_INLINE
const_iterator &operator -= (difference_type n) {
it_ -= n;
return *this;
}
BOOST_UBLAS_INLINE
difference_type operator - (const const_iterator &it) const {
BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
return it_ - it.it_;
}
// Dereference
BOOST_UBLAS_INLINE
const_reference operator * () const {
BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
return *it_;
}
BOOST_UBLAS_INLINE
const_reference operator [] (difference_type n) const {
return *(*this + n);
}
// Index
BOOST_UBLAS_INLINE
size_type index () const {
return it_.index2 ();
}
// Assignment
BOOST_UBLAS_INLINE
const_iterator &operator = (const const_iterator &it) {
container_const_reference<self_type>::assign (&it ());
it_ = it.it_;
return *this;
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const const_iterator &it) const {
BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
return it_ == it.it_;
}
BOOST_UBLAS_INLINE
bool operator < (const const_iterator &it) const {
BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
return it_ < it.it_;
}
private:
const_subiterator_type it_;
};
#endif
BOOST_UBLAS_INLINE
const_iterator begin () const {
return find (0);
}
BOOST_UBLAS_INLINE
const_iterator end () const {
return find (size ());
}
#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
class iterator:
public container_reference<matrix_row>,
public iterator_base_traits<typename subiterator_type::iterator_category>::template
iterator_base<iterator, value_type>::type {
public:
typedef typename subiterator_type::value_type value_type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -