1#ifndef DUNE_FEM_DYNAMICARRAY_HH
2#define DUNE_FEM_DYNAMICARRAY_HH
15#include <dune/fem/common/utility.hh>
23 template<
class K >
class StaticArray;
28 :
public std::allocator< T >
30 typedef std::allocator< T > BaseType;
32#if __cplusplus <= 201703L
33 typedef typename BaseType :: pointer pointer ;
37 typedef typename BaseType :: size_type size_type;
39 pointer allocate( size_type n )
44 void deallocate( pointer p, size_type n )
49 pointer reallocate ( pointer oldMem, size_type oldSize, size_type n )
52 pointer p = allocate( n );
53 const size_type copySize = std::min( oldSize, n );
54 std::copy( oldMem, oldMem+copySize, p );
55 deallocate( oldMem, oldSize );
65 static_assert( Std::is_pod< T > :: value,
"T is not POD" );
66 typedef std::allocator< T > BaseType;
70#if __cplusplus <= 201703L
71 typedef typename BaseType :: pointer pointer ;
75 typedef typename BaseType :: size_type size_type;
76 typedef typename BaseType :: value_type value_type;
81 pointer p =
static_cast< pointer
> (std::malloc(n *
sizeof(value_type)));
94 pointer
reallocate (pointer oldMem, size_type oldSize , size_type n)
97 pointer p =
static_cast< pointer
> (std::realloc(oldMem , n*
sizeof(value_type)));
103 template <
class T,
class AllocatorType =
typename std::conditional< Std::is_pod< T > :: value,
104 PODArrayAllocator< T >,
105 StandardArrayAllocator< T > > :: type >
112 struct DenseMatVecTraits< Fem::StaticArray< K > >
114 typedef Fem::StaticArray< K > derived_type;
115 typedef K* container_type;
116 typedef K value_type;
117 typedef std::size_t size_type;
121 struct FieldTraits< Fem::StaticArray< K > >
123 typedef typename FieldTraits< K >::field_type field_type;
124 typedef typename FieldTraits< K >::real_type real_type;
129 struct DenseMatVecTraits< Fem::DynamicArray< K > > :
public DenseMatVecTraits< Fem::StaticArray< K > >
134 struct FieldTraits< Fem::DynamicArray< K > > :
public FieldTraits< Fem::StaticArray< K > >
156 typedef value_type FieldType;
158 typedef typename DenseMatVecTraits< ThisType >::container_type DofStorageType;
164 : vec_( const_cast< DofStorageType > (vec) )
192 assert(org.size_ >=
size() );
193 assert( ( size_ > 0 ) ? vec_ !=
nullptr :
true );
194 std::copy(org.vec_, org.vec_ + size_, vec_ );
201 std::fill( vec_, vec_+size_, value_type(0) );
205 void memmove(size_type length, size_type oldStartIdx, size_type newStartIdx)
207 void * dest = ((
void *) (&vec_[newStartIdx]));
208 const void * src = ((
const void *) (&vec_[oldStartIdx]));
209 std::memmove(dest, src, length *
sizeof(value_type));
216 return vec_ == other.vec_;
242 template <
class T,
class Allocator>
246 typedef Allocator AllocatorType;
257 typedef typename BaseType::size_type size_type;
265 , allocator_( other.allocator_ )
272 const value_type& value,
273 AllocatorType allocator = AllocatorType() )
277 , allocator_( allocator )
281 std::fill( vec_, vec_+size_, value );
287 AllocatorType allocator = AllocatorType() )
291 , allocator_( allocator )
298 memoryFactor_ = memFactor;
299 assert( memoryFactor_ >= 1.0 );
317 memoryFactor_ = org.memoryFactor_;
318 assert( memoryFactor_ >= 1.0 );
321 assert( ( size_ > 0 ) ? vec_ !=
nullptr :
true );
322 std::copy(org.vec_, org.vec_ + size_, vec_ );
337 doResize( nsize, ! Std::is_pod< value_type >::value );
342 void resize ( size_type nsize,
const value_type& value )
344 doResize( nsize,
true, value );
347 void doResize( size_type nsize,
bool initializeNewValues,
const value_type& value = value_type() )
350 if( (nsize <= memSize_) && (nsize >= (memSize_/2)) )
374 if( mSize <= memSize_ )
386 return memSize_ *
sizeof(value_type) +
sizeof(
ThisType);
391 void adjustMemory( size_type mSize,
bool initializeNewValues,
const value_type& value = value_type() )
393 assert( memoryFactor_ >= 1.0 );
394 const double overEstimate = memoryFactor_ * mSize;
395 const size_type nMemSize = (size_type) std::ceil( overEstimate );
396 assert( nMemSize >= mSize );
398 if( vec_ ==
nullptr )
401 vec_ = allocator_.allocate( nMemSize );
402 if( initializeNewValues )
404 std::fill( vec_, vec_+nMemSize, value );
409 assert( nMemSize > 0 );
413 vec_ = allocator_.reallocate (vec_, memSize_, nMemSize);
414 if( nMemSize > memSize_ && initializeNewValues )
416 std::fill( vec_+memSize_, vec_+nMemSize, value );
427 if( vec_ !=
nullptr )
429 allocator_.deallocate( vec_, memSize_ );
436 double memoryFactor_;
438 AllocatorType allocator_;
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:230
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:251
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:260
An implementation of DenseVector which uses a C-array of dynamic size as storage.
Definition: dynamicarray.hh:244
ThisType & operator=(const ThisType &org)
assign arrays
Definition: dynamicarray.hh:326
void setMemoryFactor(double memFactor)
set memory factor
Definition: dynamicarray.hh:296
void adjustMemory(size_type mSize, bool initializeNewValues, const value_type &value=value_type())
adjust the memory
Definition: dynamicarray.hh:391
void assign(const ThisType &org)
assign arrays
Definition: dynamicarray.hh:315
void reserve(size_type mSize)
Definition: dynamicarray.hh:371
size_type capacity() const
return number of total enties of array
Definition: dynamicarray.hh:309
void resize(size_type nsize)
Definition: dynamicarray.hh:334
void resize(size_type nsize, const value_type &value)
Definition: dynamicarray.hh:342
DynamicArray(const ThisType &other)
copy constructor
Definition: dynamicarray.hh:261
DynamicArray(size_type size=0, AllocatorType allocator=AllocatorType())
create array of length size without initializing the values
Definition: dynamicarray.hh:286
~DynamicArray()
destructor
Definition: dynamicarray.hh:303
size_type usedMemorySize() const
return size of vector in bytes
Definition: dynamicarray.hh:384
DynamicArray(size_type size, const value_type &value, AllocatorType allocator=AllocatorType())
create array of length size with initialized values
Definition: dynamicarray.hh:271
Definition: dynamicarray.hh:64
void deallocate(pointer p, size_type n)
release memory previously allocated with malloc member
Definition: dynamicarray.hh:87
pointer reallocate(pointer oldMem, size_type oldSize, size_type n)
allocate array of nmemb objects of type T
Definition: dynamicarray.hh:94
pointer allocate(size_type n)
allocate array of nmemb objects of type T
Definition: dynamicarray.hh:79
oriented to the STL Allocator funtionality
Definition: dynamicarray.hh:29
An implementation of DenseVector which uses a C-array of fixed size as storage.
Definition: dynamicarray.hh:148
value_type & operator[](size_type i)
random access operator
Definition: dynamicarray.hh:176
size_type size() const
return size of array
Definition: dynamicarray.hh:170
ThisType & operator=(const ThisType &org)
copy assignament
Definition: dynamicarray.hh:190
bool operator==(const ThisType &other) const
Definition: dynamicarray.hh:214
value_type * data()
return pointer to data
Definition: dynamicarray.hh:220
void clear()
set all entries to 0
Definition: dynamicarray.hh:199
StaticArray(size_type size, const value_type *vec)
create array of length size and store vec as pointer to memory
Definition: dynamicarray.hh:163
void memmove(size_type length, size_type oldStartIdx, size_type newStartIdx)
move memory from old to new destination
Definition: dynamicarray.hh:205
const value_type * data() const
return pointer to data
Definition: dynamicarray.hh:226
Implements the dense vector interface, with an exchangeable storage class.
Type traits to determine the type of reals (when working with complex numbers)
Dune namespace.
Definition: alignedallocator.hh:13