📄 storage_sparse.hpp
字号:
// if (! data)
// throw std::bad_alloc ();
// if (! data_)
// throw std::bad_alloc ();
std::copy (data_, data_ + std::min (size, size_), data);
std::fill (data + std::min (size, size_), data + size, value_type ());
delete [] data_;
capacity_ = size << 1;
data_ = data;
}
size_ = size;
BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
}
// Reserving
BOOST_UBLAS_INLINE
void reserve (size_type capacity) {
BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
if (capacity > capacity_) {
pointer data = new value_type [capacity];
// Assuming std compliant allocator as requested during review.
// if (! data)
// throw std::bad_alloc ();
// if (! data_)
// throw std::bad_alloc ();
std::copy (data_, data_ + size_, data);
delete [] data_;
capacity_ = capacity;
data_ = data;
}
BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
}
BOOST_UBLAS_INLINE
size_type size () const {
return size_;
}
// Element access
BOOST_UBLAS_INLINE
data_reference operator [] (index_type i) {
#ifndef BOOST_UBLAS_STRICT_STORAGE_SPARSE
pointer it = find (i);
if (it == end ())
it = insert (end (), value_type (i, data_value_type ()));
BOOST_UBLAS_CHECK (it != end (), internal_logic ());
return it->second;
#else
return data_reference (*this, i);
#endif
}
// Assignment
BOOST_UBLAS_INLINE
map_array &operator = (const map_array &a) {
// Too unusual semantic.
// Thanks to Michael Stevens for spotting this.
// BOOST_UBLAS_CHECK (this != &a, external_logic ());
if (this != &a) {
resize (a.size_);
std::copy (a.data_, a.data_ + a.size_, data_);
}
return *this;
}
BOOST_UBLAS_INLINE
map_array &assign_temporary (map_array &a) {
swap (a);
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (map_array &a) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (this != &a, external_logic ());
if (this != &a) {
std::swap (capacity_, a.capacity_);
std::swap (data_, a.data_);
std::swap (size_, a.size_);
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (map_array &a1, map_array &a2) {
a1.swap (a2);
}
#endif
// Element insertion and deletion
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
pointer push_back (pointer it, const value_type &p) {
if (size () == 0 || (it = end () - 1)->first < p.first) {
resize (size () + 1);
*(it = end () - 1) = p;
return it;
}
// Raising exceptions abstracted as requested during review.
// throw external_logic ();
external_logic ().raise ();
return it;
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
pointer insert (pointer it, const value_type &p) {
it = detail::lower_bound (begin (), end (), p, detail::less_pair<value_type> ());
difference_type n = it - begin ();
BOOST_UBLAS_CHECK (size () == 0 || size () == size_type (n) || it->first != p.first, external_logic ());
resize (size () + 1);
it = begin () + n;
std::copy_backward (it, end () - 1, end ());
*it = p;
return it;
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
void insert (pointer it, pointer it1, pointer it2) {
#ifdef BOOST_UBLAS_BOUNDS_CHECK
while (it1 != it2) {
insert (it, *it1);
++ it1;
}
#else
difference_type n = it - begin ();
resize (size () + it2 - it1);
it = begin () + n;
std::copy (it1, it2, it);
std::sort (begin (), end (), detail::less_pair<value_type> ());
#endif
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
void erase (pointer it) {
BOOST_UBLAS_CHECK (begin () <= it && it < end (), bad_index ());
// Fixed by George Katsirelos.
// (*it).second = data_value_type ();
std::copy (it + 1, end (), it);
resize (size () - 1);
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
void erase (pointer it1, pointer it2) {
BOOST_UBLAS_CHECK (begin () <= it1 && it1 < it2 && it2 <= end (), bad_index ());
// Fixed by George Katsirelos.
// while (it1 != it2) {
// BOOST_UBLAS_CHECK (begin () <= it1 && it1 < end (), bad_index ());
// (*it1).second = data_value_type ();
// ++ it1;
// }
std::copy (it2, end (), it1);
resize (size () - (it2 - it1));
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
void clear () {
resize (0);
}
// Element lookup
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
const_pointer find (index_type i) const {
#ifdef BOOST_UBLAS_DEPRECATED
std::pair<const_pointer, const_pointer> pit;
pit = std::equal_range (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
if (pit.first->first == i)
return pit.first;
else if (pit.second->first == i)
return pit.second;
else
return end ();
#else
const_pointer it (detail::lower_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ()));
if (it == end () || it->first != i)
it = end ();
return it;
#endif
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
pointer find (index_type i) {
#ifdef BOOST_UBLAS_DEPRECATED
std::pair<pointer, pointer> pit;
pit = std::equal_range (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
if (pit.first->first == i)
return pit.first;
else if (pit.second->first == i)
return pit.second;
else
return end ();
#else
pointer it (detail::lower_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ()));
if (it == end () || it->first != i)
it = end ();
return it;
#endif
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
const_pointer lower_bound (index_type i) const {
return detail::lower_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
pointer lower_bound (index_type i) {
return detail::lower_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
}
#ifdef BOOST_UBLAS_DEPRECATED
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
const_pointer upper_bound (index_type i) const {
return detail::upper_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
}
// This function seems to be big. So we do not let the compiler inline it.
// BOOST_UBLAS_INLINE
pointer upper_bound (index_type i) {
return detail::upper_bound (begin (), end (), value_type (i, data_value_type ()), detail::less_pair<value_type> ());
}
#endif
// 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 capacity_;
pointer data_;
size_type size_;
};
namespace detail {
using namespace boost::numeric::ublas;
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class A>
struct map_traits {};
template<class I, class T>
struct map_traits<map_array<I, T> > {
typedef typename map_array<I, T>::data_reference reference;
};
template<class I, class T>
struct map_traits<std::map<I, T> > {
typedef typename std::map<I, T>::mapped_type &reference;
};
#endif
// Some helpers for map_array
template<class I, class T>
BOOST_UBLAS_INLINE
void reserve (map_array<I, T> &a, typename map_array<I, T>::size_type capacity) {
a.reserve (capacity);
}
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class I, class T>
BOOST_UBLAS_INLINE
typename map_array<I, T>::data_reference make_reference (map_array<I, T> &a, typename map_array<I, T>::iterator it) {
return reference (a, it);
}
#endif
// Some helpers for std::map
template<class I, class T>
BOOST_UBLAS_INLINE
void reserve (std::map<I, T> &/* a */, typename std::map<I, T>::size_type /* capacity */) {}
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class I, class T>
BOOST_UBLAS_INLINE
typename std::map<I, T>::mapped_type &make_reference (std::map<I, T> &/* a */, typename std::map<I, T>::iterator it) {
return (*it).second;
}
#endif
}
// This specialization is missing in Dinkumware's STL?!
template<class I, class T, class F>
BOOST_UBLAS_INLINE
void swap (std::map<I, T, F> &a1, std::map<I, T, F> &a2) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (&a1 != &a2, external_logic ());
if (&a1 != &a2)
a1.swap (a2);
}
// Set array
template<class I>
class set_array {
public:
typedef std::size_t size_type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -