array_dynamic_no_mem_check.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_
13 #define ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <algorithm>
20 #include <ecl/config/macros.hpp>
22 #include "../definitions.hpp"
23 #include "../initialiser.hpp"
24 #include "../stencil.hpp"
25 #include "array_no_mem_check.hpp"
26 
27 /*****************************************************************************
28 ** Namespaces
29 *****************************************************************************/
30 
31 namespace ecl {
32 
33 /*****************************************************************************
34 ** Forward Declarations
35 *****************************************************************************/
36 
37 template <typename Type> class Array<Type,DynamicStorage>;
38 
39 namespace blueprints {
40 
41 template <typename Type> class ConstantDynamicArray;
42 
43 } // namespace blueprints
44 
45 namespace formatters {
46 
47 template <typename Type, size_t N> class ArrayFormatter;
48 
49 } // namespace formatters
50 
51 /*****************************************************************************
52 ** BluePrintFactory
53 *****************************************************************************/
62 template<typename Type>
63 class ECL_PUBLIC BluePrintFactory< Array<Type,DynamicStorage> > {
64  public:
73  static blueprints::ConstantDynamicArray<Type> Constant(size_t size, const Type &value) {
74  return blueprints::ConstantDynamicArray<Type>(size,value);
75  }
76  virtual ~BluePrintFactory() {};
77 };
78 
79 /*****************************************************************************
80 ** Interface [Array][Dynamic]
81 *****************************************************************************/
128 template<typename Type>
129 class ECL_PUBLIC Array<Type,DynamicStorage> : public BluePrintFactory< Array<Type,DynamicStorage> > {
130  public:
131  /*********************
132  ** Typedefs
133  **********************/
134  typedef Type value_type;
135  typedef Type* iterator;
136  typedef const Type* const_iterator;
137  typedef Type& reference;
138  typedef const Type& const_reference;
139  typedef std::size_t size_type;
140  typedef std::ptrdiff_t difference_type;
141  typedef std::reverse_iterator<iterator> reverse_iterator;
142  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
147 
148  /*********************
149  ** Constructors
150  **********************/
156  explicit Array() : buffer_size(0), buffer(NULL) {}
165  explicit Array(const unsigned int reserve_size) : buffer_size(reserve_size), buffer(NULL) {
166  buffer = new Type[reserve_size];
167  };
176  Factory(),
177  buffer_size(0),
178  buffer(NULL)
179  {
180  if ( array.size() != 0 ) {
181  resize(array.size());
182  std::copy(array.begin(),array.end(),begin());
183  }
184  }
205  template<typename T>
206  Array(const blueprints::ArrayBluePrint< T > &blueprint) : buffer_size(0), buffer(NULL) {
207  // Note we're using a partially specialised parent interface here otherwise the
208  // constructor that reserves sizes as well as the comma initialiser won't
209  // conveniently convert types correctly
210  // (e.g. Array<double> array(4); array = 1,2,3,4; will fail)
212  blueprint.implementApply(*this);
213  }
219  ~Array() {
220  if ( buffer != NULL ) {
221  delete[] buffer;
222  }
223  }
224  /*********************
225  ** Assignment
226  **********************/
242  }
243 
245  if ( array.size() == 0 ) {
246  clear();
247  } else {
248  resize(array.size());
249  std::copy(array.begin(),array.end(),begin());
250  }
251  }
252 
253  /*********************
254  ** Iterators
255  **********************/
262  iterator begin() {
264  return buffer;
265  }
272  const_iterator begin() const {
274  return buffer;
275  }
282  iterator end() {
284  return buffer+buffer_size;
285  }
292  const_iterator end() const {
294  return buffer+buffer_size;
295  }
302  reverse_iterator rbegin() {
304  return reverse_iterator(end());
305  }
312  const_reverse_iterator rbegin() const {
314  return const_reverse_iterator(end());
315  }
322  reverse_iterator rend() {
324  return reverse_iterator(begin());
325  }
332  const_reverse_iterator rend() const {
334  return const_reverse_iterator(begin());
335  }
336 
337  /*********************
338  ** Front/Back
339  **********************/
346  reference front() {
348  return buffer[0];
349  }
356  const_reference front() const {
358  return buffer[0];
359  }
366  reference back() {
368  return buffer[buffer_size-1];
369  }
376  const_reference back() const {
378  return buffer[buffer_size-1];
379  }
380 
381  /*********************
382  ** Accessors
383  **********************/
395  Stencil< Array<Type,DynamicStorage> > stencil(const unsigned int& start_index, const unsigned int& n) {
396  ecl_assert_throw(start_index < size(), StandardException(LOC, OutOfRangeError, "Start index provided is larger than the underlying array size."));
397  ecl_assert_throw(start_index+n <= size(), StandardException(LOC, OutOfRangeError, "Finish index provided is larger than the underlying array size."));
398  return Stencil< Array<Type,DynamicStorage> >(*this, begin()+start_index, begin()+start_index+n);
399  }
409  reference operator[](size_type i) {
411  return buffer[i];
412  }
424  const_reference operator[](size_type i) const {
426  return buffer[i];
427  }
438  reference at(size_type i) {
439  if ( i>=buffer_size ) {
441  }
442  return buffer[i];
443  }
456  const_reference at(size_type i) const {
457  if ( i>=buffer_size ) {
459  }
460  return buffer[i];
461  }
462 
463  /*********************
464  ** Utilities
465  **********************/
473  size_type size() const { return buffer_size; }
474 
483  void resize( size_t n ) {
484  if ( buffer != NULL ) {
485  delete[] buffer;
486  }
487  buffer = new Type[n];
488  buffer_size = n;
489  }
495  void clear() {
496  if ( buffer != NULL ) {
497  delete[] buffer;
498  buffer = NULL;
499  }
500  buffer_size = 0;
501  }
502 
503  /*********************
504  ** Streaming
505  **********************/
513  template <typename OutputStream, typename ElementType>
514  friend OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array);
515 
516  private:
517  unsigned int buffer_size;
518  Type *buffer;
519 
520 };
521 
522 /*****************************************************************************
523 ** Implementation [Array]
524 *****************************************************************************/
525 
526 template <typename OutputStream, typename ElementType>
527 OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array) {
528 
529  ostream << "[ ";
530  for(size_t i = 0; i < array.buffer_size; ++i )
531  {
532  ostream << array[i] << " ";
533  }
534  ostream << "]";
535  ostream.flush();
536 
537  return ostream;
538 }
539 
540 /*****************************************************************************
541 ** BluePrints
542 *****************************************************************************/
543 
544 namespace blueprints {
545 
546 /*****************************************************************************
547 ** Interface [ConstantDynamicArray]
548 *****************************************************************************/
549 
557 template <typename Type>
558 class ConstantDynamicArray: public ArrayBluePrint< ConstantDynamicArray<Type> > {
559  public:
570 
580  ConstantDynamicArray(size_t size, const Type &value ) :
581  reserve_size(size),
582  val(value)
583  {}
584 
585  virtual ~ConstantDynamicArray() {};
586 
597  base_type instantiate() {
598  ecl::Array<Type> array(reserve_size);
599  std::fill_n(array.begin(),reserve_size,val);
600  return array;
601  }
602 
611  void apply(base_type& array) const {
612  array.resize(reserve_size);
613  std::fill_n(array.begin(),reserve_size,val);
614  }
615 
616  private:
617  size_t reserve_size;
618  Type val;
619 };
620 
621 
622 } // namespace blueprints
623 } // namespace ecl
624 
625 
626 #endif /* ECL_CONTAINERS_ARRAY_DYNAMIC_NO_MEM_CHECK_HPP_ */
Fixed size container with a few bells and whistles.
Embedded control libraries.
Parent class for array blueprints.
Array(const blueprints::ArrayBluePrint< T > &blueprint)
Blueprint constructor.
const_reference at(size_type i) const
OutputStream & operator<<(OutputStream &ostream, Format< std::string > &formatter)
ConstantDynamicArray(size_t size, const Type &value)
Constructor that properly configures/initialises the blueprint.
Fixed size containers with a few bells and whistles.
void apply(base_type &array) const
Apply the blueprint to configure an existing object.
formatters::ArrayFormatter< Type, DynamicStorage > Formatter
Formatter for this class.
#define LOC
Array(const unsigned int reserve_size)
Reserves storage for the array.
void operator=(const Array< Type, DynamicStorage > &array)
#define ecl_assert_throw(expression, exception)
void clear()
Clear the array, deleting all storage space previously allocated.
OutOfRangeError
const_reference operator[](size_type i) const
void resize(size_t n)
Resize the array, clearing whatever was in there before.
Pseudo formatter for integral type arrays.
base_type instantiate()
Instantiate a copy of the object that is blueprinted.
size_type size() const
The size of the array.
std::reverse_iterator< iterator > reverse_iterator
Convenience initialiser with bounds checking for fixed size containers.
Definition: initialiser.hpp:49
A safe windowing class that opens onto array-like containers.
#define ecl_compile_time_concept_check(Model)
Blueprint for generating a cubic spline satisfying C2 constraints.
std::reverse_iterator< const_iterator > const_reverse_iterator
static blueprints::ConstantDynamicArray< Type > Constant(size_t size, const Type &value)
Generates a constant array of the specified size.
Stencil< Array< Type, DynamicStorage > > stencil(const unsigned int &start_index, const unsigned int &n)
Open a window (stencil) onto the array.
Array(const Array< Type, DynamicStorage > &array)
Copy constructor.
Dynamic size container with a few bells and whistles.
void implementApply(BaseType &array) const
#define ECL_PUBLIC
BluePrintFactory< Array< Type, DynamicStorage > > Factory
Generates blueprints for this class.
ecl::Array< Type, ecl::DynamicStorage > base_type
Abstract representation of the class to be instantiated/configured.


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:43