Bridges-C++  3.2.0
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 
104 
106  class iterator {
107  Array1D<E>& arr;
108  int index;
109  public:
110  iterator (Array1D<E>& a, int ind) : arr(a), index(ind) {}
111  E const & operator* () const {
112  return arr[index];
113  }
114  E & operator* () {
115  return arr[index];
116  }
118  ++index;
119  return *this;
120  }
122  --index;
123  return *this;
124  }
126  ++index;
127  return *this;
128  }
130  --index;
131  return *this;
132  }
133  bool operator == (const iterator& it) const {
134  return (&arr) == &(it.arr)
135  && index == it.index;
136  }
137  bool operator != (const iterator& it) const {
138  return !(operator==(it));
139  }
140  };
141 
144  return iterator(*this, 0);
145  }
148  return iterator(*this, Array<E>::getDimensions()[0]);
149  }
150 
153  Array1D<E> const & arr;
154  int index;
155  public:
156  const_iterator (Array1D<E> const& a, int ind) : arr(a), index(ind) {}
157  E const & operator* () const {
158  return arr[index];
159  }
161  ++index;
162  return *this;
163  }
165  --index;
166  return *this;
167  }
169  ++index;
170  return *this;
171  }
173  --index;
174  return *this;
175  }
176  bool operator == (const const_iterator& it) const {
177  return (&arr) == &(it.arr)
178  && index == it.index;
179  }
180  bool operator != (const const_iterator& it) const {
181  return !(operator==(it));
182  }
183  };
184 
187  return const_iterator(*this, 0);
188  }
190  const_iterator end() const {
191  return iterator(*this, Array<E>::getDimensions()[0]);
192  }
193 
194  }; // Array1D
195  // use some aliases for accessing iterators
196  template <class E>
198  }
199 }// end namespace bridges
200 
201 #endif
void setElement(int indx, const Element< E > &e)
change the element that stores Array[indx]
Definition: Array1D.h:100
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:52
iterator(Array1D< E > &a, int ind)
Definition: Array1D.h:110
E & operator[](int indx)
access Array[indx]
Definition: Array1D.h:73
iterator begin()
enables range for loops
Definition: Array1D.h:143
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
iterator & operator++()
Definition: Array1D.h:117
enabling range for loops
Definition: Array1D.h:106
const_iterator begin() const
enables range for loops
Definition: Array1D.h:186
const_iterator(Array1D< E > const &a, int ind)
Definition: Array1D.h:156
typename Array1D< E >::iterator Array1DIterator
Definition: Array1D.h:197
Element< E > & getElement(int indx)
access the element that stores Array[indx]
Definition: Array1D.h:85
bool operator!=(const iterator &it) const
Definition: Array1D.h:137
iterator end()
enables range for loops
Definition: Array1D.h:147
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
virtual ~Array1D()
Definition: Array1D.h:62
void setSize(int nd, int *dim)
Set the size of the array.
Definition: Array.h:57
E const & operator[](int indx) const
access Array[indx]
Definition: Array1D.h:78
bool operator==(const iterator &it) const
Definition: Array1D.h:133
A BRIDGES 1D array type.
Definition: Array1D.h:53
Array1D(int sz)
builds an array given size
Definition: Array1D.h:67
Element< E > & getElement(int index)
Definition: Array.h:108
const_iterator end() const
enables range for loops
Definition: Array1D.h:190
iterator & operator--()
Definition: Array1D.h:121
Element< E > const & getElement(int indx) const
access the element that stores Array[indx]
Definition: Array1D.h:92
E const & operator*() const
Definition: Array1D.h:111
enabling iterator loops in const contexts
Definition: Array1D.h:152