12 #ifndef ECL_CONTAINERS_ARRAY_DYNAMIC_MEM_CHECK_HPP_ 
   13 #define ECL_CONTAINERS_ARRAY_DYNAMIC_MEM_CHECK_HPP_ 
   24 #include "../definitions.hpp" 
   25 #include "../initialiser.hpp" 
   26 #include "../stencil.hpp" 
   41 namespace blueprints {
 
   43 template <
typename Type> 
class ConstantDynamicArray;
 
   47 namespace formatters {
 
   49 template <
typename Type, 
size_t N> 
class ArrayFormatter;
 
   61 template<
typename Type>
 
  127 template<
typename Type>
 
  133         typedef Type           value_type; 
 
  135         typedef const Type*    const_iterator;  
 
  137         typedef const Type&    const_reference; 
 
  155         explicit Array() : buffer_size(0), underrun(NULL), buffer(NULL), overrun(NULL) {}
 
  164         explicit Array(
const unsigned int reserve_size) :
 
  165                         buffer_size(reserve_size),
 
  170                 underrun = (
char*) malloc(buffer_size*
sizeof(Type)+bufferUnderrunLength()+bufferOverrunLength());
 
  172                 buffer = (Type*) (underrun+bufferUnderrunLength());
 
  173                 overrun = (
char*) underrun+bufferUnderrunLength()+buffer_size*
sizeof(Type);
 
  178                         initialiseMagicSections();
 
  188                 if ( array.
size() != 0 ) {
 
  189                                 resize(array.
size()); 
 
  190                                 std::copy(array.
begin(),array.
end(),begin());
 
  214         Array(
const blueprints::ArrayBluePrint< T > &blueprint) : buffer_size(0), underrun(NULL), buffer(NULL), overrun(NULL) {
 
  220             blueprint.implementApply(*
this);
 
  221             initialiseMagicSections();
 
  230             if ( underrun != NULL ) {
 
  257         void operator=(
const Array<Type,DynamicStorage>& array) {
 
  258                 if ( array.size() == 0 ) {
 
  261                                 resize(array.size()); 
 
  262                                 std::copy(array.begin(),array.end(),begin());
 
  285         const_iterator begin()
 const {
 
  297             return buffer+buffer_size;
 
  305         const_iterator end()
 const {
 
  307             return buffer+buffer_size;
 
  315         reverse_iterator rbegin() {
 
  317             return reverse_iterator(end());
 
  325         const_reverse_iterator rbegin()
 const {
 
  327             return const_reverse_iterator(end());
 
  335         reverse_iterator rend() {
 
  337             return reverse_iterator(begin());
 
  345         const_reverse_iterator rend()
 const {
 
  347             return const_reverse_iterator(begin());
 
  369         const_reference front()
 const {
 
  381             return buffer[buffer_size-1];
 
  389         const_reference back()
 const {
 
  391             return buffer[buffer_size-1];
 
  408         Stencil< Array<Type,DynamicStorage> > stencil(
const unsigned int& start_index, 
const unsigned int& n) {
 
  411                 return Stencil< Array<Type,DynamicStorage> >(*
this, begin()+start_index, begin()+start_index+n);
 
  422         reference operator[](size_type i) {
 
  437         const_reference operator[](size_type i)
 const {
 
  451         reference at(size_type i) {
 
  452             if ( i>=buffer_size ) {
 
  469         const_reference at(size_type i)
 const {
 
  470             if ( i>=buffer_size ) {
 
  486         size_type size()
 const { 
return buffer_size; }
 
  496         void resize( 
size_t n )  {
 
  497                 if ( underrun != NULL ) {
 
  501                 underrun = (
char*) malloc(buffer_size*
sizeof(Type)+bufferUnderrunLength()+bufferOverrunLength());
 
  503                 buffer = (Type*) (underrun+bufferUnderrunLength());
 
  504                 overrun = (
char*) underrun+bufferUnderrunLength()+buffer_size*
sizeof(Type);
 
  509             initialiseMagicSections();
 
  517                 if ( underrun != NULL ) {
 
  536         template <
typename OutputStream, 
typename ElementType>
 
  542         bool bufferUnderRun();
 
  543         bool bufferOverRun();
 
  544         bool bufferOverFlow();
 
  547         void initialiseMagicSections();
 
  549          static const unsigned int& bufferOverrunLength() {
 
  550             static const unsigned int buffer_overrun_length = 4;
 
  551             return buffer_overrun_length;
 
  553         static const unsigned int& bufferUnderrunLength() {
 
  554             static const unsigned int buffer_underrun_length = 4;
 
  555             return buffer_underrun_length;
 
  557         static const char& magicChar() {
 
  558             static const char magic_char = 0xCC;
 
  561         unsigned int buffer_size;
 
  576 template<
typename Type>
 
  580         for ( 
unsigned int i = 0; i < bufferUnderrunLength(); ++i ) {
 
  581                 *(underrun+i) = magicChar();
 
  583         for ( 
unsigned int i = 0; i < bufferOverrunLength(); ++i ) {
 
  584                 *(overrun+i) = magicChar();
 
  595 template<
typename Type>
 
  597         if ( underrun != NULL ) {
 
  598                 for ( 
unsigned int i = 0; i < bufferUnderrunLength(); ++i ) {
 
  599                         if ( *(underrun+i) != magicChar() ) {
 
  615 template<
typename Type>
 
  618         if ( overrun != NULL ) {
 
  619                 for ( 
unsigned int i = 0; i < bufferOverrunLength(); ++i ) {
 
  620                         if ( *(overrun+i) != magicChar() ) {
 
  637 template<
typename Type>
 
  639         return ( bufferOverRun() || bufferUnderRun() );
 
  647 template <
typename OutputStream, 
typename ElementType>
 
  651     for(
size_t i = 0; i < array.buffer_size; ++i )
 
  653         ostream << array[i] << 
" ";
 
  665 namespace blueprints {
 
  678 template <
typename Type>
 
  679 class ConstantDynamicArray: 
public ArrayBluePrint< ConstantDynamicArray<Type> >  {