📄 storage.hpp
字号:
#else
typedef std::reverse_iterator<iterator> reverse_iterator;
#endif
BOOST_UBLAS_INLINE
reverse_iterator rbegin () {
return reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
reverse_iterator rend () {
return reverse_iterator (begin ());
}
private:
size_type size_;
bool own_;
pointer data_;
};
#else
template<class T>
struct leaker {
typedef void result_type;
typedef T *argument_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type x) {}
};
// Array adaptor
template<class T>
class array_adaptor {
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
// typedef const T &const_reference;
typedef typename type_traits<T>::const_reference const_reference;
typedef T &reference;
typedef const T *const_pointer;
typedef T *pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
array_adaptor ():
size_ (0), own_ (true), data_ (new value_type [0]) {
std::fill (data_.get (), data_.get () + size_, value_type ());
}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
array_adaptor (no_init):
size_ (0), own_ (true), data_ (new value_type [0]) {}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
array_adaptor (size_type size):
size_ (size), own_ (true), data_ (new value_type [size]) {
// Assuming std compliant allocator as requested during review.
// if (! data_.get ())
// throw std::bad_alloc ();
std::fill (data_.get (), data_.get () + size_, value_type ());
}
BOOST_UBLAS_INLINE
array_adaptor (size_type size, no_init):
size_ (size), own_ (true), data_ (new value_type [size]) {
// Assuming std compliant allocator as requested during review.
// if (! data_.get ())
// throw std::bad_alloc ();
}
BOOST_UBLAS_INLINE
array_adaptor (size_type size, pointer data):
size_ (size), own_ (false), data_ (data, leaker<value_type> ()) {}
#ifdef BOOST_UBLAS_DEEP_COPY
BOOST_UBLAS_INLINE
array_adaptor (const array_adaptor &a):
size_ (a.size_), own_ (true), data_ (new value_type [a.size_]) {
// Assuming std compliant allocator as requested during review.
// if (! data_.get ())
// throw std::bad_alloc ();
*this = a;
}
#else
BOOST_UBLAS_INLINE
array_adaptor (const array_adaptor &a):
size_ (a.size_), own_ (a.own_), data_ (a.data_) {}
#endif
BOOST_UBLAS_INLINE
~array_adaptor () {
// Assuming std compliant allocator as requested during review.
// if (! data_.get ())
// throw std::bad_alloc ();
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size, bool preserve = true) {
if (size != size_) {
shared_array<value_type> data (new value_type [size]);
// Assuming std compliant allocator as requested during review.
// if (! data.get ())
// throw std::bad_alloc ();
// if (! data_.get ())
// throw std::bad_alloc ();
if (preserve) {
std::copy (data_.get (), data_.get () + std::min (size, size_), data.get ());
std::fill (data.get () + std::min (size, size_), data.get () + size, value_type ());
}
size_ = size;
data_ = data;
}
}
BOOST_UBLAS_INLINE
void resize (size_type size, pointer data, bool preserve = true) {
// Assuming std compliant allocator as requested during review.
// if (! data_.get ())
// throw std::bad_alloc ();
if (preserve) {
std::copy (data_.get (), data_.get () + std::min (size, size_), data);
std::fill (data + std::min (size, size_), data + size, value_type ());
}
size_ = size;
data_ = data;
}
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator [] (size_type i) const {
BOOST_UBLAS_CHECK (i < size_, bad_index ());
return data_ [i];
}
BOOST_UBLAS_INLINE
reference operator [] (size_type i) {
BOOST_UBLAS_CHECK (i < size_, bad_index ());
return data_ [i];
}
// Assignment
BOOST_UBLAS_INLINE
array_adaptor &operator = (const array_adaptor &a) {
// Too unusual semantic.
// Thanks to Michael Stevens for spotting this.
// BOOST_UBLAS_CHECK (this != &a, external_logic ());
if (this != &a) {
// Precondition for container relaxed as requested during review.
// BOOST_UBLAS_CHECK (size_ == a.size_, bad_size ());
resize (a.size_, false);
std::copy (a.data_.get (), a.data_.get () + a.size_, data_.get ());
}
return *this;
}
BOOST_UBLAS_INLINE
array_adaptor &assign_temporary (array_adaptor &a) {
if (own_ && a.own_)
swap (a);
else
*this = a;
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (array_adaptor &a) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (this != &a, external_logic ());
if (this != &a) {
// Precondition for container relaxed as requested during review.
// BOOST_UBLAS_CHECK (size_ == a.size_, bad_size ());
std::swap (size_, a.size_);
std::swap (own_, a.own_);
std::swap (data_, a.data_);
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (array_adaptor &a1, array_adaptor &a2) {
a1.swap (a2);
}
#endif
// Element insertion and deletion
BOOST_UBLAS_INLINE
pointer insert (pointer it, const value_type &t) {
BOOST_UBLAS_CHECK (begin () <= it && it < end (), bad_index ());
BOOST_UBLAS_CHECK (*it == value_type (), external_logic ());
*it = t;
return it;
}
BOOST_UBLAS_INLINE
void insert (pointer it, pointer it1, pointer it2) {
while (it1 != it2) {
BOOST_UBLAS_CHECK (begin () <= it && it < end (), bad_index ());
BOOST_UBLAS_CHECK (*it == value_type (), external_logic ());
*it = *it1;
++ it, ++ it1;
}
}
BOOST_UBLAS_INLINE
void erase (pointer it) {
BOOST_UBLAS_CHECK (begin () <= it && it < end (), bad_index ());
*it = value_type ();
}
BOOST_UBLAS_INLINE
void erase (pointer it1, pointer it2) {
while (it1 != it2) {
BOOST_UBLAS_CHECK (begin () <= it1 && it1 < end (), bad_index ());
*it1 = value_type ();
++ it1;
}
}
BOOST_UBLAS_INLINE
void clear () {
erase (begin (), end ());
}
// Iterators simply are pointers.
typedef const_pointer const_iterator;
BOOST_UBLAS_INLINE
const_iterator begin () const {
return data_.get ();
}
BOOST_UBLAS_INLINE
const_iterator end () const {
return data_.get () + size_;
}
typedef pointer iterator;
BOOST_UBLAS_INLINE
iterator begin () {
return data_.get ();
}
BOOST_UBLAS_INLINE
iterator end () {
return data_.get () + size_;
}
// Reverse iterators
#ifdef BOOST_MSVC_STD_ITERATOR
typedef std::reverse_iterator<const_iterator, value_type, const_reference> const_reverse_iterator;
#else
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#endif
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 ());
}
#ifdef BOOST_MSVC_STD_ITERATOR
typedef std::reverse_iterator<iterator, value_type, reference> reverse_iterator;
#else
typedef std::reverse_iterator<iterator> reverse_iterator;
#endif
BOOST_UBLAS_INLINE
reverse_iterator rbegin () {
return reverse_iterator (end ());
}
BOOST_UBLAS_INLINE
reverse_iterator rend () {
return reverse_iterator (begin ());
}
private:
size_type size_;
bool own_;
shared_array<value_type> data_;
};
#endif
namespace detail {
using namespace boost::numeric::ublas;
// Some helpers for unbounded_array
template<class T>
BOOST_UBLAS_INLINE
void resize (unbounded_array<T> &a, typename unbounded_array<T>::size_type size, bool preserve) {
a.resize (size, preserve);
}
// Some helpers for bounded_array
template<class T, std::size_t N>
BOOST_UBLAS_INLINE
void resize (bounded_array<T, N> &a, typename bounded_array<T, N>::size_type size, bool preserve) {
a.resize (size, preserve);
}
// Some helpers for array_adaptor
template<class T>
BOOST_UBLAS_INLINE
void resize (array_adaptor<T> &a, typename array_adaptor<T>::size_type size, bool preserve) {
a.resize (size, preserve);
}
// Some helpers for std::vector
template<class T>
BOOST_UBLAS_INLINE
void resize (std::vector<T> &a, typename std::vector<T>::size_type size, bool /* preserve */) {
a.resize (size);
}
}
// Range class
class range {
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef difference_type value_type;
typedef value_type const_reference;
typedef const_reference reference;
typedef const difference_type *const_pointer;
typedef difference_type *pointer;
typedef size_type const_iterator_type;
// Construction and destruction
BOOST_UBLAS_INLINE
range ():
start_ (), size_ () {}
BOOST_UBLAS_INLINE
range (size_type start, size_type stop):
start_ (start), size_ (stop - start) {
BOOST_UBLAS_CHECK (start <= stop, bad_size ());
}
BOOST_UBLAS_INLINE
size_type start () const {
return start_;
}
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator () (size_type i) const {
BOOST_UBLAS_CHECK (i < size_, bad_index ());
return start_ + i;
}
// Composition
BOOST_UBLAS_INLINE
range compose (const range &r) const {
BOOST_UBLAS_CHECK (r.start_ + r.size_ <= size_, bad_size ());
return range (start_ + r.start_, start_ + r.start_ + r.size_);
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const range &r) const {
return start_ == r.start_ && size_ == r.size_;
}
BOOST_UBLAS_INLINE
bool operator != (const range &r) const {
return ! (*this == r);
}
// Iterator simply is a index.
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
typedef indexed_const_iterator<range, std::random_access_iterator_tag> const_iterator;
#else
class const_iterator:
public container_const_reference<range>,
public random_access_iterator_base<std::random_access_iterator_tag,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -