Bridges-C++  3.4.5-dev1-6-g935685a
Bridges(C++ API)
Array1D.h
Go to the documentation of this file.
1 #ifndef ARRAY_1D_H
2 #define ARRAY_1D_H
3 
4 #include "Array.h"
5 #include "Element.h"
6 
7 namespace bridges {
8  namespace datastructure {
52  template <typename E>
53  class Array1D : public Array<E> {
54 
55  private:
56  int size;
57 
59  Array1D() {}
60 
61  public:
62  virtual ~Array1D() {}
63 
67  Array1D(int sz) {
68  int dim[] = {sz, 1, 1};
69  this->setSize(1, dim);
70  }
71 
73  E & operator[](int indx) {
74  return Array<E>::getElement(indx).getValue();
75  }
76 
78  E const & operator[](int indx) const {
79  return Array<E>::getElement(indx).getValue();
80  }
81 
85  Element<E>& getElement(int indx) {
86  return Array<E>::getElement(indx);
87  }
88 
92  Element<E> const & getElement(int indx) const {
93  return Array<E>::getElement(indx);
94  }
95 
100  void setElement(int indx, const Element<E>& e ) {
101  return Array<E>::setElement(indx, e);
102  }
103 
105  class iterator {
106  Array1D<E>& arr;
107  int index;
108  public:
109  iterator (Array1D<E>& a, int ind) : arr(a), index(ind) {}
110  E const & operator* () const {
111  return arr[index];
112  }
113  E & operator* () {
114  return arr[index];
115  }
117  ++index;
118  return *this;
119  }
121  --index;
122  return *this;
123  }
125  ++index;
126  return *this;
127  }
129  --index;
130  return *this;
131  }
132  bool operator == (const iterator& it) const {
133  return (&arr) == &(it.arr)
134  && index == it.index;
135  }
136  bool operator != (const iterator& it) const {
137  return !(operator==(it));
138  }
139  };
140 
143  return iterator(*this, 0);
144  }
147  return iterator(*this, Array<E>::getDimensions()[0]);
148  }
149 
152  Array1D<E> const & arr;
153  int index;
154  public:
155  const_iterator (Array1D<E> const& a, int ind) : arr(a), index(ind) {}
156  E const & operator* () const {
157  return arr[index];
158  }
160  ++index;
161  return *this;
162  }
164  --index;
165  return *this;
166  }
168  ++index;
169  return *this;
170  }
172  --index;
173  return *this;
174  }
175  bool operator == (const const_iterator& it) const {
176  return (&arr) == &(it.arr)
177  && index == it.index;
178  }
179  bool operator != (const const_iterator& it) const {
180  return !(operator==(it));
181  }
182  };
183 
186  return const_iterator(*this, 0);
187  }
189  const_iterator end() const {
190  return iterator(*this, Array<E>::getDimensions()[0]);
191  }
192 
193  }; // Array1D
194  // use some aliases for accessing iterators
195  template <class E>
197  }
198 }// end namespace bridges
199 
200 #endif
enabling iterator loops in const contexts
Definition: Array1D.h:151
bool operator==(const const_iterator &it) const
Definition: Array1D.h:175
const_iterator(Array1D< E > const &a, int ind)
Definition: Array1D.h:155
const_iterator & operator--()
Definition: Array1D.h:163
const_iterator & operator++()
Definition: Array1D.h:159
bool operator!=(const const_iterator &it) const
Definition: Array1D.h:179
E const & operator*() const
Definition: Array1D.h:156
enabling range for loops
Definition: Array1D.h:105
iterator & operator++()
Definition: Array1D.h:116
bool operator==(const iterator &it) const
Definition: Array1D.h:132
iterator(Array1D< E > &a, int ind)
Definition: Array1D.h:109
E const & operator*() const
Definition: Array1D.h:110
bool operator!=(const iterator &it) const
Definition: Array1D.h:136
iterator & operator--()
Definition: Array1D.h:120
A BRIDGES 1D array type.
Definition: Array1D.h:53
Array1D(int sz)
builds an array given size
Definition: Array1D.h:67
void setElement(int indx, const Element< E > &e)
change the element that stores Array[indx]
Definition: Array1D.h:100
iterator end()
enables range for loops
Definition: Array1D.h:146
E const & operator[](int indx) const
access Array[indx]
Definition: Array1D.h:78
Element< E > & getElement(int indx)
access the element that stores Array[indx]
Definition: Array1D.h:85
const_iterator begin() const
enables range for loops
Definition: Array1D.h:185
virtual ~Array1D()
Definition: Array1D.h:62
Element< E > const & getElement(int indx) const
access the element that stores Array[indx]
Definition: Array1D.h:92
E & operator[](int indx)
access Array[indx]
Definition: Array1D.h:73
iterator begin()
enables range for loops
Definition: Array1D.h:142
const_iterator end() const
enables range for loops
Definition: Array1D.h:189
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
void setElement(int ind, Element< E > el)
Set the Element at index ind - 1D array.
Definition: Array.h:129
void setSize(int nd, int *dim)
Set the size of the array.
Definition: Array.h:57
Element< E > & getElement(int index)
Definition: Array.h:108
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:51
typename Array1D< E >::iterator Array1DIterator
Definition: Array1D.h:196
Support for drawing Bar charts.
Definition: alltypes.h:4