3#ifndef DUNE_ISTL_BASEARRAY_HH
4#define DUNE_ISTL_BASEARRAY_HH
12#include "istlexception.hh"
48 template<
class B,
class A=std::allocator<B> >
49 class base_array_unmanaged
56 typedef B member_type;
59 typedef A allocator_type;
62 typedef typename A::size_type size_type;
68 using const_reference =
const B&;
73 reference operator[] (size_type i)
75#ifdef DUNE_ISTL_WITH_CHECKING
76 if (i>=n)
DUNE_THROW(ISTLError,
"index out of range");
82 const_reference operator[] (size_type i)
const
84#ifdef DUNE_ISTL_WITH_CHECKING
85 if (i>=n)
DUNE_THROW(ISTLError,
"index out of range");
93 :
public RandomAccessIteratorFacade<RealIterator<T>, T>
97 typedef typename std::remove_const<T>::type ValueType;
99 friend class RandomAccessIteratorFacade<RealIterator<const ValueType>, const ValueType>;
100 friend class RandomAccessIteratorFacade<RealIterator<ValueType>, ValueType>;
101 friend class RealIterator<const ValueType>;
102 friend class RealIterator<ValueType>;
109 RealIterator (
const B* _p, B* _i) : p(_p), i(_i)
112 RealIterator(
const RealIterator<ValueType>& it)
117 size_type index ()
const
123 bool equals (
const RealIterator<ValueType>& other)
const
130 bool equals (
const RealIterator<const ValueType>& other)
const
136 std::ptrdiff_t distanceTo(
const RealIterator& o)
const
155 reference
elementAt (std::ptrdiff_t offset)
const
161 reference dereference ()
const
166 void advance(std::ptrdiff_t d)
176 typedef RealIterator<B> iterator;
182 return iterator(p,p);
188 return iterator(p,p+n);
193 iterator beforeEnd ()
195 return iterator(p,p+n-1);
200 iterator beforeBegin ()
202 return iterator(p,p-1);
206 iterator find (size_type i)
208 return iterator(p,p+std::min(i,n));
212 typedef RealIterator<const B> const_iterator;
215 const_iterator begin ()
const
217 return const_iterator(p,p+0);
221 const_iterator end ()
const
223 return const_iterator(p,p+n);
228 const_iterator beforeEnd ()
const
230 return const_iterator(p,p+n-1);
235 const_iterator beforeBegin ()
const
237 return const_iterator(p,p-1);
241 const_iterator find (size_type i)
const
243 return const_iterator(p,p+std::min(i,n));
250 size_type size ()
const
256 const B* data()
const
269 base_array_unmanaged ()
273 base_array_unmanaged (size_type n_, B* p_)
297 template<
class B,
class A=std::allocator<B> >
298 class base_array_window :
public base_array_unmanaged<B,A>
305 typedef B member_type;
308 typedef A allocator_type;
311 typedef typename base_array_unmanaged<B,A>::iterator iterator;
314 typedef typename base_array_unmanaged<B,A>::const_iterator const_iterator;
317 typedef typename base_array_unmanaged<B,A>::size_type size_type;
320 typedef typename A::difference_type difference_type;
326 : base_array_unmanaged<B,A>()
330 base_array_window (B* _p, size_type _n)
331 : base_array_unmanaged<B,A>(_n ,_p)
337 void set (size_type _n, B* _p)
344 void advance (difference_type newsize)
351 void move (difference_type offset, size_type newsize)
358 void move (difference_type offset)
388 template<
class B,
class A=std::allocator<B> >
389 class base_array :
public base_array_unmanaged<B,A>
396 typedef B member_type;
399 typedef A allocator_type;
402 typedef typename base_array_unmanaged<B,A>::iterator iterator;
405 typedef typename base_array_unmanaged<B,A>::const_iterator const_iterator;
408 typedef typename base_array_unmanaged<B,A>::size_type size_type;
411 typedef typename A::difference_type difference_type;
417 : base_array_unmanaged<B,A>()
421 base_array (size_type _n)
422 : base_array_unmanaged<B,A>(_n, 0)
425 this->p = allocator_.allocate(this->n);
426 new (this->p)B[this->n];
435 base_array (
const base_array& a)
441 this->p = allocator_.allocate(this->n);
442 new (this->p)B[this->n];
450 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
460 allocator_.deallocate(this->p,this->n);
465 void resize (size_type _n)
467 if (this->n==_n)
return;
473 allocator_.deallocate(this->p,this->n);
477 this->p = allocator_.allocate(this->n);
478 new (this->p)B[this->n];
487 base_array& operator= (
const base_array& a)
498 allocator_.deallocate(this->p,this->n);
502 this->p = allocator_.allocate(this->n);
503 new (this->p)B[this->n];
511 for (size_type i=0; i<this->n; i++) this->p[i]=a.p[i];
545 template<
class B,
class A=std::allocator<B> >
546 class compressed_base_array_unmanaged
553 typedef B member_type;
556 typedef A allocator_type;
559 typedef typename A::size_type size_type;
562 using reference = B&;
565 using const_reference =
const B&;
570 reference operator[] (size_type i)
572 const size_type* lb = std::lower_bound(j, j+n, i);
573 if (lb == j+n || *lb != i)
574 DUNE_THROW(ISTLError,
"index "<<i<<
" not in compressed array");
579 const_reference operator[] (size_type i)
const
581 const size_type* lb = std::lower_bound(j, j+n, i);
582 if (lb == j+n || *lb != i)
583 DUNE_THROW(ISTLError,
"index "<<i<<
" not in compressed array");
590 :
public BidirectionalIteratorFacade<RealIterator<T>, T>
594 typedef typename std::remove_const<T>::type ValueType;
596 friend class BidirectionalIteratorFacade<RealIterator<const ValueType>, const ValueType>;
597 friend class BidirectionalIteratorFacade<RealIterator<ValueType>, ValueType>;
598 friend class RealIterator<const ValueType>;
599 friend class RealIterator<ValueType>;
607 RealIterator (B* _p, size_type* _j, size_type _i)
608 : p(_p), j(_j), i(_i)
614 RealIterator(
const RealIterator<ValueType>& it)
615 : p(it.p), j(it.j), i(it.i)
620 bool equals (
const RealIterator<ValueType>& it)
const
627 bool equals (
const RealIterator<const ValueType>& it)
const
635 size_type index ()
const
641 void setindex (size_type k)
653 size_type offset ()
const
672 reference dereference ()
const
683 typedef RealIterator<B> iterator;
688 return iterator(p,j,0);
694 return iterator(p,j,n);
699 iterator beforeEnd ()
701 return iterator(p,j,n-1);
706 iterator beforeBegin ()
708 return iterator(p,j,-1);
712 iterator find (size_type i)
714 const size_type* lb = std::lower_bound(j, j+n, i);
715 return (lb != j+n && *lb == i)
721 typedef RealIterator<const B> const_iterator;
724 const_iterator begin ()
const
726 return const_iterator(p,j,0);
730 const_iterator end ()
const
732 return const_iterator(p,j,n);
737 const_iterator beforeEnd ()
const
739 return const_iterator(p,j,n-1);
744 const_iterator beforeBegin ()
const
746 return const_iterator(p,j,-1);
750 const_iterator find (size_type i)
const
752 const size_type* lb = std::lower_bound(j, j+n, i);
753 return (lb != j+n && *lb == i)
754 ? const_iterator(p,j,lb-j)
761 size_type size ()
const
768 compressed_base_array_unmanaged ()
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:435
constexpr decltype(auto) elementAt(Container &&c, Index &&i)
Get element at given position from container.
Definition: hybridutilities.hh:133
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:10