📄 storage.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_STORAGE_H
#define BOOST_UBLAS_STORAGE_H
#include <algorithm>
#include <valarray>
#include <vector>
// #define BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#ifndef BOOST_UBLAS_SIMPLE_ARRAY_ADAPTOR
#include <boost/shared_array.hpp>
#endif
#include <boost/numeric/ublas/config.hpp>
#include <boost/numeric/ublas/exception.hpp>
#include <boost/numeric/ublas/iterator.hpp>
#include <boost/numeric/ublas/traits.hpp>
namespace boost { namespace numeric { namespace ublas {
#ifndef BOOST_UBLAS_USE_FAST_SAME
// FIXME: for performance reasons we better use macros
// template<class T>
// BOOST_UBLAS_INLINE
// const T &same_impl (const T &size1, const T &size2) {
// BOOST_UBLAS_CHECK (size1 == size2, bad_argument ());
// return std::min (size1, size2);
// }
// #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
template<class T>
BOOST_UBLAS_INLINE
// Kresimir Fresl and Dan Muller reported problems with COMO.
// We better change the signature instead of libcomo ;-)
// const T &same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
T same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ());
return std::min (size1, size2);
}
#define BOOST_UBLAS_SAME(size1, size2) same_impl_ex ((size1), (size2), __FILE__, __LINE__)
#else
// FIXME: for performance reasons we better use macros
// template<class T>
// BOOST_UBLAS_INLINE
// const T &same_impl (const T &size1, const T &size2) {
// return size1;
// }
// #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
#define BOOST_UBLAS_SAME(size1, size2) (size1)
#endif
struct no_init {};
// Unbounded array
template<class T>
class unbounded_array {
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
unbounded_array ():
size_ (0), data_ (new value_type [0]) {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
std::fill (data_, data_ + size_, value_type ());
}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
unbounded_array (no_init):
size_ (0), data_ (new value_type [0]) {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
unbounded_array (size_type size):
size_ (size), data_ (new value_type [size]) {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
std::fill (data_, data_ + size_, value_type ());
}
BOOST_UBLAS_INLINE
unbounded_array (size_type size, no_init):
size_ (size), data_ (new value_type [size]) {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
}
BOOST_UBLAS_INLINE
unbounded_array (const unbounded_array &a):
size_ (a.size_), data_ (new value_type [a.size_]) {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
*this = a;
}
BOOST_UBLAS_INLINE
~unbounded_array () {
// Assuming std compliant allocator as requested during review.
// if (! data_)
// throw std::bad_alloc ();
delete [] data_;
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size, bool preserve = true) {
if (size != size_) {
pointer data = new value_type [size];
// Assuming std compliant allocator as requested during review.
// if (! data)
// throw std::bad_alloc ();
// if (! data_)
// throw std::bad_alloc ();
if (preserve) {
std::copy (data_, data_ + std::min (size, size_), data);
std::fill (data + std::min (size, size_), data + size, value_type ());
}
delete [] data_;
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
unbounded_array &operator = (const unbounded_array &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_, a.data_ + a.size_, data_);
}
return *this;
}
BOOST_UBLAS_INLINE
unbounded_array &assign_temporary (unbounded_array &a) {
swap (a);
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (unbounded_array &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 (data_, a.data_);
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (unbounded_array &a1, unbounded_array &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_;
}
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
#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_;
pointer data_;
};
// Bounded array
template<class T, std::size_t N>
class bounded_array {
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_EXPLICIT BOOST_UBLAS_INLINE
bounded_array ():
// Kresimir Fresl suggested to change the default back to the template argument.
// size_ (0) /* , data_ () */ {
size_ (N) /* , data_ () */ {
std::fill (data_, data_ + size_, value_type ());
}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
bounded_array (no_init):
// Kresimir Fresl suggested to change the default back to the template argument.
// size_ (0) /* , data_ () */ {
size_ (N) /* , data_ () */ {}
BOOST_UBLAS_EXPLICIT BOOST_UBLAS_INLINE
bounded_array (size_type size):
size_ (size) /* , data_ () */ {
if (size_ > N)
// Raising exceptions abstracted as requested during review.
// throw std::bad_alloc ();
bad_size ().raise ();
std::fill (data_, data_ + size_, value_type ());
}
BOOST_UBLAS_INLINE
bounded_array (size_type size, no_init):
size_ (size) /* , data_ () */ {
if (size_ > N)
// Raising exceptions abstracted as requested during review.
// throw std::bad_alloc ();
bad_size ().raise ();
}
BOOST_UBLAS_INLINE
bounded_array (const bounded_array &a):
size_ (a.size_) /* , data_ () */ {
if (size_ > N)
// Raising exceptions abstracted as requested during review.
// throw std::bad_alloc ();
bad_size ().raise ();
*this = a;
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size, bool preserve = true) {
if (size > N)
// Raising exceptions abstracted as requested during review.
// throw std::bad_alloc ();
bad_size ().raise ();
if (preserve)
std::fill (data_ + std::min (size, size_), data_ + size, value_type ());
size_ = size;
}
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];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -