Choreonoid  1.8
Array2D.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_ARRAY_2D_H
7 #define CNOID_UTIL_ARRAY_2D_H
8 
9 #include <vector>
10 #include <iterator>
11 
12 namespace cnoid {
13 
14 template <typename ElementType, typename Allocator = std::allocator<ElementType> >
15 class Array2D
16 {
18 
19 public:
20  typedef ElementType Element;
21  typedef typename std::vector<ElementType, Allocator> Container;
22  typedef typename Container::iterator iterator;
23 
25  class Row
26  {
27  typename Container::iterator top;
28  typename Container::iterator end_;
29  int size_;
30  public:
31 
32  typedef typename Container::iterator iterator;
33 
34  Row() {
35  size_ = 0;
36  }
37 
38  Row(const Array2D<ElementType, Allocator>& owner, int frame) {
39  size_ = owner.colSize_;
40  top = (const_cast<Array2D<ElementType, Allocator>&>(owner)).container.begin() + frame * size_;
41  end_ = top + size_;
42  }
43 
44 
45  Row& operator=(const Row& rhs) {
46  top = rhs.top;
47  end_ = rhs.end_;
48  size_ = rhs.size_;
49  return *this;
50  }
51 
52  bool empty() const {
53  return (size_ == 0);
54  }
55 
56  int size() const {
57  return size_;
58  }
59 
60  ElementType& operator[](int index){
61  return top[index];
62  }
63 
64  const ElementType& operator[](int index) const {
65  return top[index];
66  }
67 
68  ElementType& at(int index) {
69  return top[index];
70  }
71 
73  return top;
74  }
75 
77  return end_;
78  }
79  };
80 
82  class Column
83  {
84  public:
85 
86  class iterator : public std::iterator<std::bidirectional_iterator_tag, ElementType, int> {
87  typename Container::iterator current;
88  int colSize;
89  public:
90  iterator() { }
91 
92  iterator(typename Container::iterator current, int colSize)
93  : current(current), colSize(colSize) {
94  }
95  ElementType& operator*() {
96  return *current;
97  }
98  void operator++() {
99  current += colSize;
100  }
101  void operator--() {
102  current -= colSize;
103  }
104  bool operator==(iterator other) const {
105  return (current == other.current);
106  }
107  bool operator!=(iterator other) const {
108  return (current != other.current);
109  }
110  };
111 
112  private:
113  typename Container::iterator top;
114  iterator end_;
115  int part;
116  int colSize;
117  int size_;
118 
119  public:
120 
121  Column(const Array2D<ElementType, Allocator>& owner, int part) {
122  top = (const_cast<Array2D<ElementType, Allocator>&>(owner)).container.begin() + part;
123  this->part = part;
124  colSize = owner.colSize_;
125  size_ = owner.rowSize_;
126  end_ = iterator(top + colSize * size_, colSize);
127  }
128 
129  bool empty() const {
130  return (size_ == 0);
131  }
132 
133  int size() const {
134  return size_;
135  }
136 
137  ElementType& operator[](int index){
138  return top[index * colSize];
139  }
140 
141  const ElementType& operator[](int index) const {
142  return top[index * colSize];
143  }
144 
145  ElementType& at(int index) {
146  return top[index * colSize];
147  }
148 
150  return iterator(top, colSize);
151  }
153  return end_;
154  }
155  };
156 
158  rowSize_ = 0;
159  colSize_ = 0;
160  }
161 
163  : container(rowSize * colSize) {
164  rowSize_ = rowSize;
165  colSize_ = colSize;
166  }
167 
168  Array2D(const Array2DType& org)
169  : container(org.container) {
170  rowSize_ = org.rowSize_;
171  colSize_ = org.colSize_;
172  }
173 
175  if(this != &rhs){
176  container = rhs.container;
177  rowSize_ = rhs.rowSize_;
178  colSize_ = rhs.colSize_;
179  }
180  return *this;
181  }
182 
183  virtual ~Array2D() { }
184 
185  bool empty() const {
186  return container.empty();
187  }
188 
189  void resize(int newRowSize, int newColSize) {
190  container.resize(newRowSize * newColSize);
191  rowSize_ = newRowSize;
192  colSize_ = newColSize;
193  }
194 
195  void clear(){
196  resize(0, 0);
197  }
198 
199  void resizeColumn(int newColSize){
200  resize(rowSize_, newColSize);
201  }
202 
203  int rowSize() const {
204  return rowSize_;
205  }
206 
207  void resizeRow(int newRowSize){
208  resize(newRowSize, colSize_);
209  }
210 
211  int colSize() const {
212  return colSize_;
213  }
214 
215  const ElementType& operator()(int rowIndex, int colIndex) const {
216  return container[rowIndex * colSize_ + colIndex];
217  }
218 
219  ElementType& operator()(int rowIndex, int colIndex) {
220  return container[rowIndex * colSize_ + colIndex];
221  }
222 
223  const ElementType& at(int rowIndex, int colIndex) const {
224  return container[rowIndex * colSize_ + colIndex];
225  }
226 
227  ElementType& at(int rowIndex, int colIndex) {
228  return container[rowIndex * colSize_ + colIndex];
229  }
230 
231  Row operator[](int rowIndex) {
232  return Row(*this, rowIndex);
233  }
234 
235  const Row operator[](int rowIndex) const {
236  return Row(*this, rowIndex);
237  }
238 
239  Row row(int rowIndex) {
240  return Row(*this, rowIndex);
241  }
242 
243  const Row row(int rowIndex) const {
244  return Row(*this, rowIndex);
245  }
246 
247  Column column(int colIndex) {
248  return Column(*this, colIndex);
249  }
250 
251  const Column column(int colIndex) const {
252  return Column(*this, colIndex);
253  }
254 
256  return container.begin();
257  }
258 
260  return container.end();
261  }
262 
263 private:
264 
265  Container container;
266  int rowSize_;
267  int colSize_;
268 };
269 
270 }
271 
272 #endif
cnoid::Array2D::Array2D
Array2D(int rowSize, int colSize)
Definition: Array2D.h:162
cnoid::Array2D::Column::size
int size() const
Definition: Array2D.h:133
cnoid::Array2D::Column::at
ElementType & at(int index)
Definition: Array2D.h:145
cnoid::Array2D::colSize
int colSize() const
Definition: Array2D.h:211
cnoid::Array2D::Row::empty
bool empty() const
Definition: Array2D.h:52
cnoid::Array2D::~Array2D
virtual ~Array2D()
Definition: Array2D.h:183
cnoid::Array2D::operator[]
Row operator[](int rowIndex)
Definition: Array2D.h:231
cnoid::Array2D::Column::begin
iterator begin()
Definition: Array2D.h:149
cnoid::Array2D::Column::end
iterator end()
Definition: Array2D.h:152
cnoid::Array2D::Row::operator[]
const ElementType & operator[](int index) const
Definition: Array2D.h:64
cnoid::Array2D::Column::operator[]
const ElementType & operator[](int index) const
Definition: Array2D.h:141
cnoid::Array2D::resizeColumn
void resizeColumn(int newColSize)
Definition: Array2D.h:199
cnoid::Array2D::Column::iterator
Definition: Array2D.h:86
cnoid::Array2D::Column::iterator::operator--
void operator--()
Definition: Array2D.h:101
cnoid::Array2D::operator=
Array2DType & operator=(const Array2DType &rhs)
Definition: Array2D.h:174
cnoid::Array2D::begin
iterator begin()
Definition: Array2D.h:255
cnoid::Array2D::Column::iterator::iterator
iterator(typename Container::iterator current, int colSize)
Definition: Array2D.h:92
cnoid::Array2D::column
Column column(int colIndex)
Definition: Array2D.h:247
cnoid::Array2D::Column::empty
bool empty() const
Definition: Array2D.h:129
cnoid::Array2D::Column::iterator::operator!=
bool operator!=(iterator other) const
Definition: Array2D.h:107
cnoid::Array2D
Definition: Array2D.h:15
cnoid::Array2D::operator[]
const Row operator[](int rowIndex) const
Definition: Array2D.h:235
cnoid::Array2D::Column::operator[]
ElementType & operator[](int index)
Definition: Array2D.h:137
cnoid::Array2D::Column::iterator::iterator
iterator()
Definition: Array2D.h:90
cnoid::Array2D::Row::iterator
Container::iterator iterator
Definition: Array2D.h:32
cnoid::Array2D::at
ElementType & at(int rowIndex, int colIndex)
Definition: Array2D.h:227
cnoid::Array2D::Row::end
iterator end()
Definition: Array2D.h:76
cnoid::Array2D::resize
void resize(int newRowSize, int newColSize)
Definition: Array2D.h:189
cnoid::Array2D::Container
std::vector< ElementType, Allocator > Container
Definition: Array2D.h:21
cnoid::Array2D::at
const ElementType & at(int rowIndex, int colIndex) const
Definition: Array2D.h:223
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Array2D::Column::iterator::operator==
bool operator==(iterator other) const
Definition: Array2D.h:104
cnoid::Array2D::Column::Column
Column(const Array2D< ElementType, Allocator > &owner, int part)
Definition: Array2D.h:121
cnoid::Array2D::Array2D
Array2D(const Array2DType &org)
Definition: Array2D.h:168
cnoid::Array2D::Column
Definition: Array2D.h:82
cnoid::Array2D::resizeRow
void resizeRow(int newRowSize)
Definition: Array2D.h:207
cnoid::Array2D::iterator
Container::iterator iterator
Definition: Array2D.h:22
cnoid::Array2D::operator()
const ElementType & operator()(int rowIndex, int colIndex) const
Definition: Array2D.h:215
cnoid::Array2D::empty
bool empty() const
Definition: Array2D.h:185
cnoid::Array2D::Column::iterator::operator++
void operator++()
Definition: Array2D.h:98
cnoid::Array2D::Row::Row
Row(const Array2D< ElementType, Allocator > &owner, int frame)
Definition: Array2D.h:38
cnoid::Array2D::Array2D
Array2D()
Definition: Array2D.h:157
cnoid::Array2D::column
const Column column(int colIndex) const
Definition: Array2D.h:251
cnoid::Array2D::row
Row row(int rowIndex)
Definition: Array2D.h:239
cnoid::Array2D::Column::iterator::operator*
ElementType & operator*()
Definition: Array2D.h:95
cnoid::Array2D::Row
Definition: Array2D.h:25
cnoid::Array2D::Row::size
int size() const
Definition: Array2D.h:56
cnoid::Array2D::Row::operator[]
ElementType & operator[](int index)
Definition: Array2D.h:60
cnoid::Array2D::rowSize
int rowSize() const
Definition: Array2D.h:203
cnoid::Array2D::row
const Row row(int rowIndex) const
Definition: Array2D.h:243
cnoid::Array2D::Element
ElementType Element
Definition: Array2D.h:20
cnoid::Array2D::Row::Row
Row()
Definition: Array2D.h:34
cnoid::Array2D::operator()
ElementType & operator()(int rowIndex, int colIndex)
Definition: Array2D.h:219
cnoid::Array2D::end
iterator end()
Definition: Array2D.h:259
cnoid::Array2D::Row::begin
iterator begin()
Definition: Array2D.h:72
cnoid::Array2D::Row::at
ElementType & at(int index)
Definition: Array2D.h:68
cnoid::Array2D::clear
void clear()
Definition: Array2D.h:195
cnoid::Array2D::Row::operator=
Row & operator=(const Row &rhs)
Definition: Array2D.h:45