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>
21 #include <ecl/utilities/blueprints.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;
143  typedef formatters::ArrayFormatter<Type,DynamicStorage> Formatter;
146  typedef BluePrintFactory< Array<Type,DynamicStorage> > Factory;
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  };
175  Array(const Array<Type,DynamicStorage>& array) :
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)
211  ecl_compile_time_concept_check(BluePrintConcept<T>);
212  blueprint.implementApply(*this);
213  }
219  ~Array() {
220  if ( buffer != NULL ) {
221  delete[] buffer;
222  }
223  }
224  /*********************
225  ** Assignment
226  **********************/
240  containers::BoundedListInitialiser<value_type,iterator,DynamicStorage> operator<<(const Type &value) {
241  return containers::BoundedListInitialiser<value_type,iterator,DynamicStorage>(value,buffer,buffer_size);
242  }
243 
244  void operator=(const Array<Type,DynamicStorage>& array) {
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() ecl_assert_throw_decl(StandardException) {
263  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
264  return buffer;
265  }
272  const_iterator begin() const ecl_assert_throw_decl(StandardException) {
273  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
274  return buffer;
275  }
282  iterator end() ecl_assert_throw_decl(StandardException) {
283  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
284  return buffer+buffer_size;
285  }
292  const_iterator end() const ecl_assert_throw_decl(StandardException) {
293  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
294  return buffer+buffer_size;
295  }
302  reverse_iterator rbegin() ecl_assert_throw_decl(StandardException) {
303  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
304  return reverse_iterator(end());
305  }
312  const_reverse_iterator rbegin() const ecl_assert_throw_decl(StandardException) {
313  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
314  return const_reverse_iterator(end());
315  }
322  reverse_iterator rend() ecl_assert_throw_decl(StandardException) {
323  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
324  return reverse_iterator(begin());
325  }
332  const_reverse_iterator rend() const ecl_assert_throw_decl(StandardException) {
333  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
334  return const_reverse_iterator(begin());
335  }
336 
337  /*********************
338  ** Front/Back
339  **********************/
346  reference front() ecl_assert_throw_decl(StandardException) {
347  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
348  return buffer[0];
349  }
356  const_reference front() const ecl_assert_throw_decl(StandardException) {
357  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
358  return buffer[0];
359  }
366  reference back() ecl_assert_throw_decl(StandardException) {
367  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
368  return buffer[buffer_size-1];
369  }
376  const_reference back() const ecl_assert_throw_decl(StandardException) {
377  ecl_assert_throw( buffer != NULL, StandardException(LOC,OutOfRangeError) );
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) ecl_assert_throw_decl(StandardException) {
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) ecl_assert_throw_decl(StandardException) {
410  ecl_assert_throw( i<buffer_size, StandardException(LOC,OutOfRangeError));
411  return buffer[i];
412  }
424  const_reference operator[](size_type i) const ecl_assert_throw_decl(StandardException) {
425  ecl_assert_throw( i<buffer_size, StandardException(LOC,OutOfRangeError));
426  return buffer[i];
427  }
438  reference at(size_type i) throw(StandardException) {
439  if ( i>=buffer_size ) {
440  throw StandardException(LOC,OutOfRangeError);
441  }
442  return buffer[i];
443  }
456  const_reference at(size_type i) const throw(StandardException) {
457  if ( i>=buffer_size ) {
458  throw StandardException(LOC,OutOfRangeError);
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:
563  typedef ecl::Array<Type,ecl::DynamicStorage> base_type;
569  ConstantDynamicArray() {};
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.
ecl_geometry_PUBLIC void resize(Trajectory2D &trajectory, const int &size)
#define LOC
Stringify the line of code you are at.
OutputStream & operator<<(OutputStream &ostream, const Void void_object)
Output stream operator for Void objects.
Definition: void.hpp:76
#define ecl_assert_throw(expression, exception)
Debug mode throw with a logical condition check.
OutOfRangeError
ecl_geometry_PUBLIC int size(const Trajectory2D &trajectory)
#define ECL_PUBLIC
#define ecl_assert_throw_decl(exception)
Assure throw exception declaration.
DynamicStorage
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
MatrixFormatter< Derived, Scalar > Formatter
Definition: plugin.hpp:28
Type


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13