📄 vector.hpp
字号:
BOOST_UBLAS_INLINE
iterator &operator -= (difference_type n) {
it_ -= n;
return *this;
}
BOOST_UBLAS_INLINE
difference_type operator - (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 (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ());
return *it_;
}
BOOST_UBLAS_INLINE
reference operator [] (difference_type n) const {
return *(it_ + n);
}
// Index
BOOST_UBLAS_INLINE
size_type index () const {
BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ());
return it_ - (*this) ().begin ().it_;
}
// Assignment
BOOST_UBLAS_INLINE
iterator &operator = (const iterator &it) {
container_reference<self_type>::assign (&it ());
it_ = it.it_;
return *this;
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const iterator &it) const {
BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
return it_ == it.it_;
}
BOOST_UBLAS_INLINE
bool operator < (const iterator &it) const {
BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
return it_ < it.it_;
}
private:
subiterator_type it_;
friend class const_iterator;
};
#endif
BOOST_UBLAS_INLINE
iterator begin () {
return find (0);
}
BOOST_UBLAS_INLINE
iterator end () {
return find (data_.size ());
}
// Reverse iterator
typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
typedef reverse_iterator_base<iterator> reverse_iterator;
BOOST_UBLAS_INLINE
const_reverse_iterator rbegin () const {
return const_reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator rend () const {
return const_reverse_iterator (begin ());
}
BOOST_UBLAS_INLINE
reverse_iterator rbegin () {
return reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
reverse_iterator rend () {
return reverse_iterator (begin ());
}
private:
array_type data_;
};
// Bounded vector class
template<class T, std::size_t N>
class bounded_vector:
public vector<T, bounded_array<T, N> > {
typedef vector<T, bounded_array<T, N> > vector_type;
public:
typedef typename vector_type::size_type size_type;
static const size_type max_size = N;
// Construction and destruction
BOOST_UBLAS_INLINE
bounded_vector ():
vector_type (N) {}
BOOST_UBLAS_INLINE
bounded_vector (size_type size):
vector_type (size) {}
BOOST_UBLAS_INLINE
bounded_vector (const bounded_vector &v):
vector_type (v) {}
template<class A2> // Allow vector<T,bounded_array<N> construction
BOOST_UBLAS_INLINE
bounded_vector (const vector<T, A2> &v):
vector_type (v) {}
template<class AE>
BOOST_UBLAS_INLINE
bounded_vector (const vector_expression<AE> &ae):
vector_type (ae) {}
BOOST_UBLAS_INLINE
~bounded_vector () {}
// Assignment
BOOST_UBLAS_INLINE
bounded_vector &operator = (const bounded_vector &v) {
vector_type::operator = (v);
return *this;
}
template<class A2> // Generic vector assignment
BOOST_UBLAS_INLINE
bounded_vector &operator = (const vector<T, A2> &v) {
vector_type::operator = (v);
return *this;
}
template<class C> // Container assignment without temporary
BOOST_UBLAS_INLINE
bounded_vector &operator = (const vector_container<C> &v) {
vector_type::operator = (v);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
bounded_vector &operator = (const vector_expression<AE> &ae) {
vector_type::operator = (ae);
return *this;
}
};
// Zero vector class
template<class T>
class zero_vector:
public vector_container<zero_vector<T> > {
typedef const T *const_pointer;
typedef zero_vector<T> self_type;
public:
#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
using vector_container<self_type>::operator ();
#endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
typedef const T &const_reference;
typedef T &reference;
typedef const vector_reference<const self_type> const_closure_type;
typedef vector_reference<self_type> closure_type;
typedef sparse_tag storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
zero_vector ():
vector_container<self_type> (),
size_ (0) {}
explicit BOOST_UBLAS_INLINE
zero_vector (size_type size):
vector_container<self_type> (),
size_ (size) {}
BOOST_UBLAS_INLINE
zero_vector (const zero_vector &v):
vector_container<self_type> (),
size_ (v.size_) {}
// Accessors
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size, bool /*preserve*/ = true) {
size_ = size;
}
// Element support
BOOST_UBLAS_INLINE
const_pointer find_element (size_type i) const {
return & zero_;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator () (size_type /* i */) const {
return zero_;
}
BOOST_UBLAS_INLINE
const_reference operator [] (size_type i) const {
return (*this) (i);
}
// Assignment
BOOST_UBLAS_INLINE
zero_vector &operator = (const zero_vector &v) {
size_ = v.size_;
return *this;
}
BOOST_UBLAS_INLINE
zero_vector &assign_temporary (zero_vector &v) {
swap (v);
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (zero_vector &v) {
if (this != &v) {
std::swap (size_, v.size_);
}
}
BOOST_UBLAS_INLINE
friend void swap (zero_vector &v1, zero_vector &v2) {
v1.swap (v2);
}
// Iterator types
public:
class const_iterator;
// Element lookup
BOOST_UBLAS_INLINE
const_iterator find (size_type /*i*/) const {
return const_iterator (*this);
}
class const_iterator:
public container_const_reference<zero_vector>,
public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
const_iterator, value_type> {
public:
typedef typename zero_vector::difference_type difference_type;
typedef typename zero_vector::value_type value_type;
typedef typename zero_vector::const_reference reference;
typedef typename zero_vector::const_pointer pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
const_iterator ():
container_const_reference<self_type> () {}
BOOST_UBLAS_INLINE
const_iterator (const self_type &v):
container_const_reference<self_type> (v) {}
// Arithmetic
BOOST_UBLAS_INLINE
const_iterator &operator ++ () {
BOOST_UBLAS_CHECK_FALSE (bad_index ());
return *this;
}
BOOST_UBLAS_INLINE
const_iterator &operator -- () {
BOOST_UBLAS_CHECK_FALSE (bad_index ());
return *this;
}
// Dereference
BOOST_UBLAS_INLINE
const_reference operator * () const {
BOOST_UBLAS_CHECK_FALSE (bad_index ());
return zero_; // arbitary return value
}
// Index
BOOST_UBLAS_INLINE
size_type index () const {
BOOST_UBLAS_CHECK_FALSE (bad_index ());
return 0; // arbitary return value
}
// Assignment
BOOST_UBLAS_INLINE
const_iterator &operator = (const const_iterator &it) {
container_const_reference<self_type>::assign (&it ());
return *this;
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const const_iterator &it) const {
BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
detail::ignore_unused_variable_warning(it);
return true;
}
};
typedef const_iterator iterator;
BOOST_UBLAS_INLINE
const_iterator begin () const {
return const_iterator (*this);
}
BOOST_UBLAS_INLINE
const_iterator end () const {
return const_iterator (*this);
}
// Reverse iterator
typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
BOOST_UBLAS_INLINE
const_reverse_iterator rbegin () const {
return const_reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator rend () const {
return const_reverse_iterator (begin ());
}
private:
size_type size_;
typedef const value_type const_value_type;
static const_value_type zero_;
};
template<class T>
typename zero_vector<T>::const_value_type zero_vector<T>::zero_ (0);
// Unit vector class
template<class T>
class unit_vector:
public vector_container<unit_vector<T> > {
typedef const T *const_pointer;
typedef unit_vector<T> self_type;
public:
#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
using vector_container<self_type>::operator ();
#endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
typedef const T &const_reference;
typedef T &reference;
typedef const vector_reference<const self_type> const_closure_type;
typedef vector_reference<self_type> closure_type;
typedef sparse_tag storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
unit_vector ():
vector_container<self_type> (),
size_ (0), index_ (0) {}
BOOST_UBLAS_INLINE
explicit unit_vector (size_type size, size_type index = 0):
vector_container<self_type> (),
size_ (size), index_ (index) {}
BOOST_UBLAS_INLINE
unit_vector (const unit_vector &v):
vector_container<self_type> (),
size_ (v.size_), index_ (v.index_) {}
// Accessors
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
BOOST_UBLAS_INLINE
size_type index () const {
return index_;
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size, bool /*preserve*/ = true) {
size_ = size;
}
// Element support
BOOST_UBLAS_INLINE
const_pointer find_element (size_type i) const {
if (i == index_)
return & one_;
else
return & zero_;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator () (size_type i) const {
if (i == index_)
return one_;
else
return zero_;
}
BOOST_UBLAS_INLINE
const_reference operator [] (size_type i) const {
return (*this) (i);
}
// Assignment
BOOST_UBLAS_INLINE
unit_vector &operator = (const unit_vector &v) {
size_ = v.size_;
index_ = v.index_;
return *this;
}
BOOST_UBLAS_INLINE
unit_vector &assign_temporary (unit_vector &v) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -