brainrat-private  5.1.2
array.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef BIOPROCESSING_OBJECT_ARRAY_H
12 #define BIOPROCESSING_OBJECT_ARRAY_H
13 
14 #include <string>
15 #include <vector>
16 #include <cartobase/object/object.h>
17 
18 namespace carto
19 {
20 
21  template <typename T>
22  class Array: public virtual Interface,
23  public DynArrayInterface,
24  public std::vector<T>
25  {
26  private :
27  typedef typename std::vector<T>::const_iterator ArrayIterator;
28 
29  public :
30  Array() : std::vector<T>() {}
31 
32  virtual ~Array() {
33 // std::cout << "In array destructor" << std::endl << std::flush;
34 // typename std::vector<T>::const_iterator it, et = std::vector<T>::end();
35 // for(it = std::vector<T>::begin(); it < et; it++)
36 // {
37 // //cout << "*it : " << *it << endl << flush;
38 // std::cout << "it : " << typeid(*it).name() << std::endl << std::flush;
39 // }
40  }
41 
42  class interfaced_iterator;
43 
45  {
46  return interfaced_iterator( std::vector<T>::begin(), std::vector<T>::end() );
47  }
48 
50  virtual Object objectIterator() const
51  {
52  return Object::value( getIterator() );
53  }
54 
55  virtual size_t size() const { return std::vector<T>::size(); }
56 
57  virtual bool isContiguous() const { return true; }
58  virtual bool hasItem( int index ) const
59  { return index >= 0 && index < size(); }
60  virtual Object getArrayItem( int index ) const { return Object::value(std::vector<T>::at( index )); }
61  virtual void setArrayItem( int index, Object o ) { std::vector<T>::at( index ) = o->GenericObject::value<T>(); }
62 
65  virtual void reserveArray( size_t s ) { std::vector<T>::reserve(s); }
69  virtual void resizeArray( size_t s ) { std::vector<T>::resize(s); }
72  virtual void removeArrayItem( int i ) { std::vector<T>::erase( std::vector<T>::begin() + i ); }
75  virtual void insertArrayItem( int i, Object o ) { std::vector<T>::insert( std::vector<T>::begin() + i, o->GenericObject::value<T>() ); }
76 
77  class interfaced_iterator : public IteratorInterface
78  {
79 
80  public:
82  {
83  }
84 
85 
86  //-----------------------------------------------------------------------------
87  inline interfaced_iterator( const ArrayIterator &begin,
88  const ArrayIterator &end ) :
89  _iterator( begin ), _end( end )
90  {
91  }
92 
95  virtual bool isValid() const
96  {
97  return _iterator != _end;
98  }
99 
101  virtual Object currentValue() const
102  {
103  return Object::value(*_iterator);
104  }
105 
106 
108  virtual void next()
109  {
110  ++_iterator;
111  }
112 
113 
114  private:
115 
117 
118  ArrayIterator _iterator;
119  ArrayIterator _end;
120  };
121 
122  };
123 
124 }
125 
126 #endif
interfaced_iterator(const ArrayIterator &begin, const ArrayIterator &end)
Definition: array.h:87
virtual bool isValid() const
true if the iterator points to a valid value, false when the end of the iterable container has been r...
Definition: array.h:95
virtual void next()
Point to the next element of the iterable container.
Definition: array.h:108
virtual Object currentValue() const
Access the value of the element pointed to by the iterator.
Definition: array.h:101
virtual Object objectIterator() const
returns an object implementing the IteratorIntrerface
Definition: array.h:50
virtual bool hasItem(int index) const
Definition: array.h:58
virtual void resizeArray(size_t s)
resize the array.
Definition: array.h:69
virtual void reserveArray(size_t s)
like the STL std::vector::reserve(), memory is reserved but no element is stored
Definition: array.h:65
virtual size_t size() const
Definition: array.h:55
virtual void setArrayItem(int index, Object o)
Definition: array.h:61
interfaced_iterator getIterator() const
Definition: array.h:44
virtual void insertArrayItem(int i, Object o)
inserts an element into the array.
Definition: array.h:75
virtual Object getArrayItem(int index) const
Definition: array.h:60
virtual ~Array()
Definition: array.h:32
virtual bool isContiguous() const
Definition: array.h:57
virtual void removeArrayItem(int i)
removes an element from the array.
Definition: array.h:72