📄 storage.hpp
字号:
array_adaptor (const array_adaptor &a):
storage_array<self_type> (),
size_ (a.size_), own_ (true), data_ (new value_type [a.size_]) {
*this = a;
}
BOOST_UBLAS_INLINE
~array_adaptor () {
if (own_) {
delete [] data_;
}
}
// Resizing
private:
BOOST_UBLAS_INLINE
void resize_internal (size_type size, value_type init, bool preserve = true) {
if (size != size_) {
pointer data = new value_type [size];
if (preserve) {
std::copy (data_, data_ + (std::min) (size, size_), data);
std::fill (data + (std::min) (size, size_), data + size, init);
}
if (own_)
delete [] data_;
size_ = size;
own_ = true;
data_ = data;
}
}
BOOST_UBLAS_INLINE
void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
if (data != data_) {
if (preserve) {
std::copy (data_, data_ + (std::min) (size, size_), data);
std::fill (data + (std::min) (size, size_), data + size, init);
}
if (own_)
delete [] data_;
own_ = false;
data_ = data;
}
else {
std::fill (data + (std::min) (size, size_), data + size, init);
}
size_ = size;
}
public:
BOOST_UBLAS_INLINE
void resize (size_type size) {
resize_internal (size, value_type (), false);
}
BOOST_UBLAS_INLINE
void resize (size_type size, value_type init) {
resize_internal (size, init, true);
}
BOOST_UBLAS_INLINE
void resize (size_type size, pointer data) {
resize_internal (size, data, value_type (), false);
}
BOOST_UBLAS_INLINE
void resize (size_type size, pointer data, value_type init) {
resize_internal (size, data, init, true);
}
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) {
if (this != &a) {
resize (a.size_);
std::copy (a.data_, a.data_ + a.size_, data_);
}
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) {
if (this != &a) {
std::swap (size_, a.size_);
std::swap (own_, a.own_);
std::swap (data_, a.data_);
}
}
BOOST_UBLAS_INLINE
friend void swap (array_adaptor &a1, array_adaptor &a2) {
a1.swap (a2);
}
// Iterators simply are pointers.
typedef const_pointer const_iterator;
BOOST_UBLAS_INLINE
const_iterator begin () const {
return data_;
}
BOOST_UBLAS_INLINE
const_iterator end () const {
return data_ + size_;
}
typedef pointer iterator;
BOOST_UBLAS_INLINE
iterator begin () {
return data_;
}
BOOST_UBLAS_INLINE
iterator end () {
return data_ + size_;
}
// Reverse iterators
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<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:
size_type size_;
bool own_;
pointer data_;
};
#ifdef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
// Array adaptor with shallow (reference) copy semantics of elements.
// shared_array is used to maintain reference counts.
// This class breaks the normal copy semantics for a storage container and is very dangerous!
template<class T>
class shallow_array_adaptor:
public storage_array<shallow_array_adaptor<T> > {
typedef shallow_array_adaptor<T> self_type;
template<class TT>
struct leaker {
typedef void result_type;
typedef TT *argument_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type x) {}
};
public:
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 T *const_pointer;
typedef T *pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
shallow_array_adaptor ():
size_ (0), own_ (true), data_ (new value_type [0]) {
}
explicit BOOST_UBLAS_INLINE
shallow_array_adaptor (size_type size):
size_ (size), own_ (true), data_ (new value_type [size]) {
}
BOOST_UBLAS_INLINE
shallow_array_adaptor (size_type size, const value_type &init):
size_ (size), own_ (true), data_ (new value_type [size]) {
std::fill (data_.get (), data_.get () + size_, init);
}
BOOST_UBLAS_INLINE
shallow_array_adaptor (size_type size, pointer data):
size_ (size), own_ (false), data_ (data, leaker<value_type> ()) {}
BOOST_UBLAS_INLINE
shallow_array_adaptor (const shallow_array_adaptor &a):
storage_array<self_type> (),
size_ (a.size_), own_ (a.own_), data_ (a.data_) {}
BOOST_UBLAS_INLINE
~shallow_array_adaptor () {
}
// Resizing
private:
BOOST_UBLAS_INLINE
void resize_internal (size_type size, value_type init, bool preserve = true) {
if (size != size_) {
shared_array<value_type> data (new value_type [size]);
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, init);
}
size_ = size;
data_ = data;
}
}
BOOST_UBLAS_INLINE
void resize_internal (size_type size, pointer data, value_type init, bool preserve = true) {
if (preserve) {
std::copy (data_.get (), data_.get () + (std::min) (size, size_), data);
std::fill (data + (std::min) (size, size_), data + size, init);
}
size_ = size;
data_ = data;
}
public:
BOOST_UBLAS_INLINE
void resize (size_type size) {
resize_internal (size, value_type (), false);
}
BOOST_UBLAS_INLINE
void resize (size_type size, value_type init) {
resize_internal (size, init, true);
}
BOOST_UBLAS_INLINE
void resize (size_type size, pointer data) {
resize_internal (size, data, value_type (), false);
}
BOOST_UBLAS_INLINE
void resize (size_type size, pointer data, value_type init) {
resize_internal (size, data, init, true);
}
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
shallow_array_adaptor &operator = (const shallow_array_adaptor &a) {
if (this != &a) {
resize (a.size_);
std::copy (a.data_.get (), a.data_.get () + a.size_, data_.get ());
}
return *this;
}
BOOST_UBLAS_INLINE
shallow_array_adaptor &assign_temporary (shallow_array_adaptor &a) {
if (own_ && a.own_)
swap (a);
else
*this = a;
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (shallow_array_adaptor &a) {
if (this != &a) {
std::swap (size_, a.size_);
std::swap (own_, a.own_);
std::swap (data_, a.data_);
}
}
BOOST_UBLAS_INLINE
friend void swap (shallow_array_adaptor &a1, shallow_array_adaptor &a2) {
a1.swap (a2);
}
// 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
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<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:
size_type size_;
bool own_;
shared_array<value_type> data_;
};
#endif
// Range class
template <class Z, class D>
class basic_range {
typedef basic_range<Z, D> self_type;
public:
typedef Z size_type;
typedef D difference_type;
typedef size_type value_type;
typedef value_type const_reference;
typedef const_reference reference;
typedef const value_type *const_pointer;
typedef value_type *pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
basic_range ():
start_ (0), size_ (0) {}
BOOST_UBLAS_INLINE
basic_range (size_type start, size_type stop):
start_ (start), size_ (stop - start) {
BOOST_UBLAS_CHECK (start_ <= stop, bad_index ());
}
BOOST_UBLAS_INLINE
size_type start () const {
return start_;
}
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
// Assignment
basic_range operator=( basic_range const& r ) {
start_ = r.start_ ;
size_ = r.size_ ;
return *this ;
}
// 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
basic_range compose (const basic_range &r) const {
return basic_range (start_ + r.start_, start_ + r.start_ + r.size_);
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const basic_range &r) const {
return start_ == r.start_ && size_ == r.size_;
}
BOOST_UBLAS_INLINE
bool operator != (const basic_range &r) const {
return ! (*this == r);
}
// Iterator types
private:
// Use and index
typedef size_type const_subiterator_type;
public:
#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
typedef indexed_const_iterator<self_type, std::random_access_iterator_tag> const_iterator;
#else
class const_iterator:
public container_const_reference<basic_range>,
public random_access_iterator_base<std::random_access_iterator_tag,
const_iterator, value_type> {
public:
typedef typename basic_range::value_type value_type;
typedef typename basic_range::difference_type difference_type;
typedef typename basic_range::const_reference reference;
typedef typename basic_range::const_pointer pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
const_iterator ():
container_const_reference<basic_range> (), it_ () {}
BOOST_UBLAS_INLINE
const_iterator (const basic_range &r, const const_subiterator_type &it):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -