dune-fem  2.4.1-rc
array.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_ARRAY_HH
2 #define DUNE_FEM_ARRAY_HH
3 
4 #include <cassert>
5 
6 #include <dune/common/iteratorfacades.hh>
7 
9 #include <dune/fem/misc/metaprogramming.hh>
10 
12 
13 namespace Dune
14 {
15  namespace Fem
16  {
17 
22  template< class AT >
24  : public BartonNackmanInterface< ArrayInterface< AT >, typename AT::ArrayType >
25  {
28 
29  public:
31  typedef AT Traits;
32 
34  typedef typename Traits::ArrayType ArrayType;
35 
37  typedef ThisType ArrayInterfaceType;
38 
40  typedef typename Traits::ElementType ElementType;
41 
43  typedef ElementType value_type ;
44 
47 
50 
52  typedef ConstIteratorType const_iterator;
53 
55  typedef IteratorType iterator;
56 
58  typedef unsigned int size_type;
59 
60  protected:
61  using BaseType::asImp;
62 
63  public:
70  const ElementType &operator[] ( unsigned int index ) const
71  {
72  CHECK_INTERFACE_IMPLEMENTATION( asImp()[ index ] );
73  return asImp()[ index ];
74  }
75 
82  ElementType &operator[] ( unsigned int index )
83  {
84  CHECK_INTERFACE_IMPLEMENTATION( asImp()[ index ] );
85  return asImp()[ index ];
86  }
87 
93  void assign ( const ElementType &element )
94  {
95  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().assign( element ) );
96  }
97 
105  template< class T >
106  void assign( const ArrayInterface< T > &other )
107  {
108  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().assign( other ) );
109  }
110 
115  ConstIteratorType begin () const
116  {
117  CHECK_INTERFACE_IMPLEMENTATION( asImp().begin() );
118  return asImp().begin();
119  }
120 
125  IteratorType begin ()
126  {
127  CHECK_INTERFACE_IMPLEMENTATION( asImp().begin() );
128  return asImp().begin();
129  }
130 
135  ConstIteratorType end () const
136  {
137  CHECK_INTERFACE_IMPLEMENTATION( asImp().end() );
138  return asImp().end();
139  }
140 
145  IteratorType end ()
146  {
147  CHECK_INTERFACE_IMPLEMENTATION( asImp().end() );
148  return asImp().end();
149  }
150 
155  unsigned int size () const
156  {
157  CHECK_INTERFACE_IMPLEMENTATION( asImp().size() );
158  return asImp().size();
159  }
160  };
161 
162 
163  template< class Array >
165  {
167  static const bool v = Conversion< Array, ArrayInterfaceType >::exists;
168  };
169 
170 
171  template< class Element, class Array >
173  : public ForwardIteratorFacade< ArrayDefaultIterator< Element, Array >, Element >
174  {
176 
177  public:
178  typedef Element ElementType;
179 
180  typedef Array ArrayType;
181 
182  protected:
183  ArrayType &array_;
184  unsigned int index_;
185 
186  public:
187  ArrayDefaultIterator ( ArrayType &array, unsigned int index )
188  : array_( array ),
189  index_( index )
190  {
191  assert( index <= array.size() );
192  }
193 
194  ArrayDefaultIterator( const ThisType &other )
195  : array_( other.array_ ),
196  index_( other.index_ )
197  {}
198 
199  ThisType &operator= ( const ThisType &other )
200  {
201  assert( &(other.array_) == &array_ );
202  index_ = other.index_;
203  }
204 
205  ElementType& dereference() const
206  {
207  assert( index_ < array_.size() );
208  return array_[ index_ ];
209  }
210 
211  void increment()
212  {
213  assert( index_ < array_.size() );
214  ++index_;
215  }
216 
217  bool equals( const ThisType &other ) const
218  {
219  assert( &(other.array_) == &array_ );
220  return index_ == other.index_;
221  }
222 
223  unsigned int index () const
224  {
225  return index_;
226  }
227  };
228 
229 
230  template< class ElementImp, class ArrayImp >
232  {
233  typedef ElementImp ElementType;
234 
235  typedef ArrayImp ArrayType;
236 
238 
240  };
241 
242 
247  template< class ElementImp, class ArrayImp >
249  : public ArrayInterface< ArrayDefaultTraits< ElementImp, ArrayImp > >
250  {
251  public:
253  typedef ElementImp ElementType;
254 
256  typedef ArrayImp ArrayType;
257 
260 
261  private:
264 
265  public:
266  using BaseType :: size;
267 
268  protected:
269  using BaseType :: asImp;
270 
271  public:
274 
277 
278  public:
280  inline void assign ( const ElementType &element )
281  {
282  ArrayType &imp = asImp();
283  const unsigned int size = imp.size();
284  for( unsigned int i = 0; i < size; ++i )
285  imp[ i ] = element;
286  }
287 
289  template< class T >
290  inline void assign( const ArrayInterface< T > &other )
291  {
292  ArrayType &imp = asImp();
293  const unsigned int size = imp.size();
294  assert( size == other.size() );
295  for( unsigned int i = 0; i < size; ++i )
296  imp[ i ] = other[ i ];
297  }
298 
300  inline ConstIteratorType begin () const
301  {
302  return ConstIteratorType( asImp(), 0 );
303  }
304 
306  inline IteratorType begin ()
307  {
308  return IteratorType( asImp(), 0 );
309  }
310 
312  inline ConstIteratorType end () const
313  {
314  return ConstIteratorType( asImp(), size() );
315  }
316 
318  inline IteratorType end ()
319  {
320  return IteratorType( asImp(), size() );
321  }
322  };
323 
324 
330  template< class ElementImp >
332  : public ArrayDefault< ElementImp, ArrayWrapper< ElementImp > >
333  {
334  public:
336  typedef ElementImp ElementType;
337 
338  private:
341 
342  private:
343  const unsigned int size_;
344  ElementType *elements_;
345 
346  public:
348  inline ArrayWrapper ( unsigned int size, ElementType *elements )
349  : size_( size ),
350  elements_( elements )
351  {
352  assert( elements_ != NULL );
353  }
354 
356  inline const ElementType &operator[] ( unsigned int index ) const
357  {
358  assert( index < size_ );
359  return elements_[ index ];
360  }
361 
363  inline ElementType &operator[] ( unsigned int index )
364  {
365  assert( index < size_ );
366  return elements_[ index ];
367  }
368 
370  inline unsigned int size () const
371  {
372  return size_;
373  }
374  };
375 
376 
397  template< class Element, unsigned int Size >
399  : public ArrayDefault< Element, FixedSizeArray< Element, Size > >
400  {
403 
404  public:
406  typedef Element ElementType;
407 
409  static const unsigned int fixedSize = Size;
410 
411  protected:
412  ElementType elements_[ fixedSize ];
413 
414  public:
419  inline FixedSizeArray ()
420  {}
421 
428  inline explicit FixedSizeArray ( const ElementType &element )
429  {
430  assign( element );
431  }
432 
437  inline FixedSizeArray ( const ThisType &other )
438  {
439  assign( other );
440  }
441 
443  inline const ElementType &operator[] ( unsigned int index ) const
444  {
445  assert( index < fixedSize );
446  return elements_[ index ];
447  }
448 
450  inline ElementType &operator[] ( unsigned int index )
451  {
452  assert( index < fixedSize );
453  return elements_[ index ];
454  }
455 
457  inline unsigned int size () const
458  {
459  return fixedSize;
460  }
461  };
462 
463 
464  template< class Element,
465  template< class > class ArrayAllocator = DefaultArrayAllocator >
467  : public ArrayDefault< Element, DynamicArray< Element, ArrayAllocator > >
468  {
471 
472  public:
473  typedef Element ElementType;
474  typedef ElementType FieldType ;
475  typedef ElementType value_type ;
476 
477  protected:
478  typedef ArrayAllocator< ElementType > ArrayAllocatorType;
479 
480  typedef typename ArrayAllocatorType :: ElementPtrType ElementPtrType;
481 
482  protected:
483  ArrayAllocatorType allocator_;
484 
485  unsigned int size_;
486  ElementPtrType elements_;
487 
488  public:
489  using BaseType :: assign;
490 
491  public:
492  inline explicit DynamicArray ( unsigned int size = 0 )
493  : allocator_()
494  {
495  size_ = size;
496  allocator_.allocate( size_, elements_ );
497  }
498 
499  inline explicit DynamicArray ( const ArrayAllocatorType &arrayAllocator,
500  unsigned int size = 0 )
501  : allocator_( arrayAllocator )
502  {
503  size_ = size;
504  allocator_.allocate( size_, elements_ );
505  }
506 
507  inline DynamicArray ( unsigned int size,
508  const ElementType &element )
509  : allocator_()
510  {
511  size_ = size;
512  allocator_.allocate( size_, elements_ );
513  assign( element );
514  }
515 
516  inline DynamicArray ( const ArrayAllocatorType &arrayAllocator,
517  unsigned int size,
518  const ElementType defaultElement )
519  : allocator_( arrayAllocator )
520  {
521  size_ = size;
522  allocator_.allocate( size_, elements_ );
523  assign( defaultElement );
524  }
525 
526  inline DynamicArray ( const ThisType &other )
527  : allocator_( other.allocator_ )
528  {
529  size_ = other.size_;
530  allocator_.allocate( size_, elements_ );
531  assign( other );
532  }
533 
534  inline ~DynamicArray ()
535  {
536  allocator_.free( elements_ );
537  }
538 
539  inline const ElementType& operator[] ( unsigned int index ) const
540  {
541  assert( index < size_ );
542  return elements_[ index ];
543  }
544 
545  inline ElementType& operator[] ( unsigned int index )
546  {
547  assert( index < size_ );
548  return elements_[ index ];
549  }
550 
551  inline void append ( const ElementType &element )
552  {
553  const unsigned int oldSize = size_;
554  resize( oldSize + 1 );
555  elements_[ oldSize ] = element;
556  }
557 
558  template< class T >
559  inline void append ( const ArrayInterface< T > &array )
560  {
561  const unsigned int arraySize = array.size();
562 
563  const unsigned int oldSize = size_;
564  resize( oldSize + arraySize );
565 
566  for( unsigned int i = 0; i < arraySize; ++i )
567  elements_[ oldSize + i ] = array[ i ];
568  }
569 
571  template< class T >
572  inline void assign( const ArrayInterface< T > &other )
573  {
574  resize( other.size() );
575  for( unsigned int i = 0; i < size_; ++i )
576  elements_[ i ] = other[ i ];
577  }
578 
579  inline ElementType *leakPointer ()
580  {
581  return (ElementType *)elements_;
582  }
583 
584  inline const ElementType *leakPointer () const
585  {
586  return (const ElementType *)elements_;
587  }
588 
589  inline void reserve ( unsigned int newSize )
590  {
591  allocator_.reserve( newSize, elements_ );
592  }
593 
594  inline void resize ( unsigned int newSize )
595  {
596  const unsigned int oldSize = size_;
597  if( newSize == oldSize )
598  return;
599 
600  allocator_.reallocate( oldSize, newSize, elements_ );
601  size_ = newSize;
602  }
603 
604  inline void resize ( unsigned int newSize,
605  const ElementType &defaultElement )
606  {
607  const unsigned int oldSize = size_;
608  if( newSize == oldSize )
609  return;
610 
611  allocator_.reallocate( oldSize, newSize, elements_ );
612  size_ = newSize;
613  for( unsigned int i = oldSize; i < newSize; ++i )
614  elements_[ i ] = defaultElement;
615  }
616 
617  inline unsigned int size () const
618  {
619  return size_;
620  }
621  };
622 
623  // Capabilities
624  // ------------
625 
626  namespace Capabilities
627  {
628 
629  template< class Array >
631  : public Fem::MetaBool< false >
632  {};
633 
634  template< class Element, template< class > class ArrayAllocator >
635  struct HasLeakPointer< Fem :: DynamicArray< Element, ArrayAllocator > >
636  : public Fem::MetaBool< true >
637  {};
638 
639  }
640 
641  } // namespace Fem
642 
643 } // namespace Dune
644 
645 #include "array_inline.hh"
646 
647 #endif // #ifndef DUNE_FEM_ARRAY_HH
ElementPtrType elements_
Definition: array.hh:486
ConstIteratorType end() const
obtain end iterator
Definition: array.hh:135
const ElementType * leakPointer() const
Definition: array.hh:584
ArrayAllocator< ElementType > ArrayAllocatorType
Definition: array.hh:478
Traits::ConstIteratorType ConstIteratorType
type of constant iterator
Definition: array.hh:273
Traits::ElementType ElementType
type of the array elements
Definition: array.hh:40
void assign(const ArrayInterface< T > &other)
copy another array to this one
Definition: array.hh:106
ElementType value_type
Definition: array.hh:475
unsigned int size() const
Definition: array.hh:617
FixedSizeArray(const ThisType &other)
copy constructor
Definition: array.hh:437
Array ArrayType
Definition: array.hh:180
unsigned int index_
Definition: array.hh:184
AT Traits
type of the traits
Definition: array.hh:31
ConstIteratorType end() const
obtain end iterator
Definition: array.hh:312
unsigned int size() const
Definition: array.hh:457
IteratorType begin()
obtain begin iterator
Definition: array.hh:125
ElementType * leakPointer()
Definition: array.hh:579
void assign(const ElementType &element)
fill the array with copies of an element
Definition: array.hh:280
default implementation of the ArrayInterface
Definition: array.hh:248
void reallocate(unsigned int oldSize, unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:80
IteratorType iterator
type of (non-constant) iterator
Definition: array.hh:55
ConstIteratorType const_iterator
type of constant iterator
Definition: array.hh:52
IteratorType end()
obtain end iterator
Definition: array.hh:145
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
void reserve(unsigned int newSize)
Definition: array.hh:589
Element ElementType
Definition: array.hh:178
IteratorType end()
obtain end iterator
Definition: array.hh:318
~DynamicArray()
Definition: array.hh:534
ArrayAllocatorType allocator_
Definition: array.hh:483
DynamicArray(const ArrayAllocatorType &arrayAllocator, unsigned int size, const ElementType defaultElement)
Definition: array.hh:516
void append(const ArrayInterface< T > &array)
Definition: array.hh:559
ThisType ArrayInterfaceType
type of this interface
Definition: array.hh:37
ArrayDefaultIterator< ElementType, ArrayType > IteratorType
Definition: array.hh:237
DynamicArray(unsigned int size, const ElementType &element)
Definition: array.hh:507
ArrayInterface< typename Array::Traits > ArrayInterfaceType
Definition: array.hh:166
ElementType FieldType
Definition: array.hh:474
Definition: array.hh:164
void append(const ElementType &element)
Definition: array.hh:551
standard array with fixed size
Definition: array.hh:398
ArrayDefaultTraits< ElementType, ArrayType > Traits
type of the traits
Definition: array.hh:259
FixedSizeArray(const ElementType &element)
initializing constructor
Definition: array.hh:428
ElementImp ElementType
type of the array elements
Definition: array.hh:336
unsigned int index() const
Definition: array.hh:223
Definition: coordinate.hh:4
IteratorType begin()
obtain begin iterator
Definition: array.hh:306
DynamicArray(unsigned int size=0)
Definition: array.hh:492
Definition: array.hh:466
Element ElementType
Definition: array.hh:473
ArrayType & array_
Definition: array.hh:183
ElementType & dereference() const
Definition: array.hh:205
ArrayAllocatorType::ElementPtrType ElementPtrType
Definition: array.hh:480
Traits::ConstIteratorType ConstIteratorType
type of constant iterator
Definition: array.hh:46
const ElementType & operator[](unsigned int index) const
access an array element
Definition: array.hh:70
implementation of the ArrayInterface wrapping a pointer to an array of elements
Definition: array.hh:331
ArrayImp ArrayType
type of the implementation (Barton-Nackman)
Definition: array.hh:256
Traits::ArrayType ArrayType
type of the implementation (Barton-Nackman)
Definition: array.hh:34
Definition: array.hh:231
ElementImp ElementType
type of the array elements
Definition: array.hh:253
bool equals(const ThisType &other) const
Definition: array.hh:217
ArrayDefaultIterator(const ThisType &other)
Definition: array.hh:194
ConstIteratorType begin() const
obtain begin iterator
Definition: array.hh:300
ElementType value_type
make consistent with std::vector
Definition: array.hh:43
ElementImp ElementType
Definition: array.hh:233
unsigned int size_
Definition: array.hh:485
ArrayImp ArrayType
Definition: array.hh:235
unsigned int size() const
Definition: array.hh:155
ArrayDefaultIterator(ArrayType &array, unsigned int index)
Definition: array.hh:187
void reserve(unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:96
void free(ElementPtrType &array) const
Definition: arrayallocator.hh:158
void assign(const ArrayInterface< T > &other)
copy another array to this one
Definition: array.hh:572
void assign(const ElementType &element)
fill the array with copies of an element
Definition: array.hh:93
ConstIteratorType begin() const
obtain begin iterator
Definition: array.hh:115
void allocate(unsigned int size, ElementPtrType &array) const
Definition: arrayallocator.hh:146
Traits::IteratorType IteratorType
type of (non-constant) iterator
Definition: array.hh:49
ArrayDefaultIterator< const ElementType, const ArrayType > ConstIteratorType
Definition: array.hh:239
unsigned int size_type
type of unsigned integral type of indexing
Definition: array.hh:58
ArrayWrapper(unsigned int size, ElementType *elements)
create an ArrayWrapper from a size and a pointer
Definition: array.hh:348
abstract array interface
Definition: array.hh:23
Definition: array.hh:172
void resize(unsigned int newSize, const ElementType &defaultElement)
Definition: array.hh:604
void resize(unsigned int newSize)
Definition: array.hh:594
Traits::IteratorType IteratorType
type of (non-constant) iterator
Definition: array.hh:276
FixedSizeArray()
default constructor
Definition: array.hh:419
unsigned int size() const
Definition: array.hh:370
DynamicArray(const ThisType &other)
Definition: array.hh:526
void assign(const ArrayInterface< T > &other)
copy another array to this one
Definition: array.hh:290
Definition: bartonnackmaninterface.hh:15
void increment()
Definition: array.hh:211
Element ElementType
type of the array elements
Definition: array.hh:406
DynamicArray(const ArrayAllocatorType &arrayAllocator, unsigned int size=0)
Definition: array.hh:499
Definition: arrayallocator.hh:112