📄 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_H
#define BOOST_UBLAS_MATRIX_PROXY_H
#include <boost/numeric/ublas/config.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
#include <boost/numeric/ublas/matrix_expression.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> > {
public:
#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS
BOOST_UBLAS_USING vector_expression<matrix_row<M> >::operator ();
#endif
typedef const M const_matrix_type;
typedef M matrix_type;
typedef typename M::simd_category simd_category;
typedef typename M::size_type size_type;
typedef typename M::difference_type difference_type;
typedef typename M::value_type value_type;
#ifndef BOOST_UBLAS_CT_PROXY_BASE_TYPEDEFS
typedef typename M::const_reference const_reference;
typedef typename M::reference reference;
typedef typename M::const_pointer const_pointer;
typedef typename M::pointer pointer;
#else
typedef typename M::const_reference const_reference;
typedef typename boost::mpl::if_c<boost::is_const<M>::value,
typename M::const_reference,
typename M::reference>::type reference;
typedef typename M::const_pointer const_pointer;
typedef typename boost::mpl::if_c<boost::is_const<M>::value,
typename M::const_pointer,
typename M::pointer>::type pointer;
#endif
#ifndef BOOST_UBLAS_CT_PROXY_CLOSURE_TYPEDEFS
typedef typename M::closure_type matrix_closure_type;
#else
typedef typename boost::mpl::if_c<boost::is_const<M>::value,
typename M::const_closure_type,
typename M::closure_type>::type matrix_closure_type;
#endif
typedef const matrix_row<matrix_type> const_self_type;
typedef matrix_row<matrix_type> self_type;
typedef const_self_type const_closure_type;
typedef self_type closure_type;
#ifndef BOOST_UBLAS_CT_PROXY_BASE_TYPEDEFS
typedef typename M::const_iterator2 const_iterator_type;
typedef typename M::iterator2 iterator_type;
#else
typedef typename M::const_iterator2 const_iterator_type;
typedef typename boost::mpl::if_c<boost::is_const<M>::value,
typename M::const_iterator2,
typename M::iterator2>::type iterator_type;
#endif
typedef typename storage_restrict_traits<typename M::storage_category,
dense_proxy_tag>::storage_category storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
matrix_row ():
data_ (nil_), i_ () {}
BOOST_UBLAS_INLINE
matrix_row (matrix_type &data, size_type i):
data_ (data), i_ (i) {
// Early checking of preconditions.
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_;
}
BOOST_UBLAS_INLINE
const matrix_closure_type &data () const {
return data_;
}
BOOST_UBLAS_INLINE
matrix_closure_type &data () {
return data_;
}
#ifdef BOOST_UBLAS_DEPRECATED
// Resetting
BOOST_UBLAS_INLINE
void reset (matrix_type &data) {
// data_ = data;
data_.reset (data);
}
BOOST_UBLAS_INLINE
void reset (matrix_type &data, size_type i) {
// data_ = data;
data_.reset (data);
i_ = i;
}
#endif
// 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) {
// FIXME: the rows could be differently sized.
// std::copy (mr.begin (), mr.end (), begin ());
vector_assign (scalar_assign<reference, value_type> (), *this, vector<value_type> (mr));
return *this;
}
BOOST_UBLAS_INLINE
matrix_row &assign_temporary (matrix_row &mr) {
// FIXME: this is suboptimal.
// return *this = mr;
vector_assign (scalar_assign<reference, value_type> (), *this, mr);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator = (const vector_expression<AE> &ae) {
vector_assign (scalar_assign<reference, value_type> (), *this, vector<value_type> (ae));
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &assign (const vector_expression<AE> &ae) {
vector_assign (scalar_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator += (const vector_expression<AE> &ae) {
vector_assign (scalar_assign<reference, value_type> (), *this, vector<value_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<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
matrix_row &operator -= (const vector_expression<AE> &ae) {
vector_assign (scalar_assign<reference, value_type> (), *this, vector<value_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<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
matrix_row &operator *= (const AT &at) {
vector_assign_scalar (scalar_multiplies_assign<reference, AT> (), *this, at);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
matrix_row &operator /= (const AT &at) {
vector_assign_scalar (scalar_divides_assign<reference, AT> (), *this, at);
return *this;
}
// 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) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (this != &mr, external_logic ());
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<reference, reference> (), *this, mr);
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (matrix_row mr1, matrix_row mr2) {
mr1.swap (mr2);
}
#endif
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
typedef indexed_iterator<matrix_row<matrix_type>,
BOOST_UBLAS_TYPENAME iterator_type::iterator_category> iterator;
typedef indexed_const_iterator<matrix_row<matrix_type>,
BOOST_UBLAS_TYPENAME const_iterator_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_iterator_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) {
iterator_type it2 (data ().find2 (1, i_, j));
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
return iterator (*this, it2.index2 ());
#else
return iterator (*this, it2);
#endif
}
// Iterators simply are pointers.
#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
class const_iterator:
public container_const_reference<matrix_row>,
#ifdef BOOST_UBLAS_USE_ITERATOR_BASE_TRAITS
public iterator_base_traits<typename const_iterator_type::iterator_category>::template
iterator_base<const_iterator, value_type>::type {
#else
public random_access_iterator_base<typename const_iterator_type::iterator_category,
const_iterator, value_type> {
#endif
public:
typedef typename const_iterator_type::iterator_category iterator_category;
typedef typename const_iterator_type::difference_type difference_type;
typedef typename const_iterator_type::value_type value_type;
typedef typename const_iterator_type::reference reference;
typedef typename const_iterator_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_iterator_type &it):
container_const_reference<self_type> (mr), it_ (it) {}
#ifndef BOOST_UBLAS_QUALIFIED_TYPENAME
BOOST_UBLAS_INLINE
const_iterator (const iterator &it):
container_const_reference<self_type> (it ()), it_ (it.it_) {}
#else
BOOST_UBLAS_INLINE
const_iterator (const typename self_type::iterator &it):
container_const_reference<self_type> (it ()), it_ (it.it_) {}
#endif
// 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) () == it (), external_logic ());
return it_ - it.it_;
}
// Dereference
BOOST_UBLAS_INLINE
reference operator * () const {
BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
return *it_;
}
// Index
BOOST_UBLAS_INLINE
size_type index () const {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -