Array2D.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_ARRAY_2D_H_INCLUDED
24 #define ICL_CORE_ARRAY_2D_H_INCLUDED
25 
26 #include <vector>
27 #include <memory>
28 #include <algorithm>
29 
30 namespace icl_core {
31 
37 template <typename T, typename TAllocator = std::allocator<T> >
38 class Array2D
39 {
40 public:
41  typedef std::vector<T, TAllocator> container_type;
42  typedef typename container_type::value_type value_type;
43  typedef typename container_type::allocator_type allocator_type;
44  typedef typename container_type::size_type size_type;
45  typedef typename container_type::difference_type difference_type;
46  typedef typename container_type::reference reference;
47  typedef typename container_type::const_reference const_reference;
48  typedef typename container_type::pointer pointer;
49  typedef typename container_type::const_pointer const_pointer;
50  typedef typename container_type::iterator iterator;
51  typedef typename container_type::const_iterator const_iterator;
52  typedef typename container_type::reverse_iterator reverse_iterator;
53  typedef typename container_type::const_reverse_iterator const_reverse_iterator;
54 
55  explicit Array2D(const allocator_type& alloc = allocator_type())
56  : m_container(alloc),
57  m_width(0),
58  m_height(0)
59  { }
60 
61  Array2D(size_t width, size_t height,
62  const value_type& value = value_type(),
63  const allocator_type& alloc = allocator_type())
64  : m_container(width*height, value, alloc),
65  m_width(width),
66  m_height(height)
67  { }
68 
69  Array2D(const Array2D& other)
70  : m_container(other.m_container),
71  m_width(other.m_width),
72  m_height(other.m_height)
73  { }
74 
75  Array2D& operator = (const Array2D& other)
76  {
77  m_container = other.m_container;
78  m_width = other.m_width;
79  m_height = other.m_height;
80  return *this;
81  }
82 
83  allocator_type get_allocator() const { return m_container.get_allocator(); }
84 
85  void resize(size_t width, size_t height)
86  {
87  m_container.resize(width*height);
88  m_width = width;
89  m_height = height;
90  }
91 
92  void resize(size_t width, size_t height, const value_type& value)
93  {
94  m_container.resize(width*height, value);
95  m_width = width;
96  m_height = height;
97  }
98 
99  void assign(size_t width, size_t height)
100  {
101  m_container.assign(width*height, value_type());
102  m_width = width;
103  m_height = height;
104  }
105 
106  void assign(size_t width, size_t height, const value_type& value)
107  {
108  m_container.assign(width*height, value);
109  m_width = width;
110  m_height = height;
111  }
112 
113  void reset()
114  {
116  }
117 
118  void set(const value_type& value)
119  {
120  m_container.assign(m_width*m_height, value);
121  }
122 
124  const T& at(size_t i) const { return m_container[i]; }
126  T& at(size_t i) { return m_container[i]; }
127 
129  const T& operator [] (size_t i) const { return m_container[i]; }
131  T& operator [] (size_t i) { return m_container[i]; }
132 
134  const T& at(size_t column, size_t row) const { return m_container[row*m_width + column]; }
136  T& at(size_t column, size_t row) { return m_container[row*m_width + column]; }
137 
139  const T& operator () (size_t column, size_t row) const { return m_container[row*m_width + column]; }
141  T& operator () (size_t column, size_t row) { return m_container[row*m_width + column]; }
142 
144  inline size_t index1D(size_t column, size_t row) const { return row*m_width + column; }
145 
147  inline void indexTo2D(size_t i, size_t& column, size_t& row) const
148  {
149  column = i % m_width;
150  row = i / m_width;
151  }
152 
154  void swap(Array2D& other)
155  {
156  m_container.swap(other.m_container);
157  std::swap(m_width, other.m_width);
158  std::swap(m_height, other.m_height);
159  }
160 
161  size_t width() const { return m_width; }
162  size_t height() const { return m_height; }
163  size_t size() const { return m_container.size(); }
164  size_type max_size() const { return m_container.max_size(); }
165  size_type capacity() const { return m_container.capacity(); }
166  bool empty() const { return m_container.empty(); }
167 
168  iterator begin() { return m_container.begin(); }
169  iterator end() { return m_container.end(); }
170  const_iterator begin() const { return m_container.begin(); }
171  const_iterator end() const { return m_container.end(); }
172 
173 protected:
174  std::vector<T> m_container;
175  size_t m_width;
176  size_t m_height;
177 };
178 
179 }
180 
181 #endif
const_iterator begin() const
Definition: Array2D.h:170
const T & at(size_t column, size_t row) const
2D access.
Definition: Array2D.h:134
const T & at(size_t i) const
1D vector-like access.
Definition: Array2D.h:124
size_t width() const
Definition: Array2D.h:161
iterator end()
Definition: Array2D.h:169
size_type capacity() const
Definition: Array2D.h:165
Array2D(const Array2D &other)
Definition: Array2D.h:69
std::vector< T > m_container
Definition: Array2D.h:174
size_t m_width
Definition: Array2D.h:175
container_type::const_pointer const_pointer
Definition: Array2D.h:49
allocator_type get_allocator() const
Definition: Array2D.h:83
size_t size() const
Definition: Array2D.h:163
Array2D(size_t width, size_t height, const value_type &value=value_type(), const allocator_type &alloc=allocator_type())
Definition: Array2D.h:61
void resize(size_t width, size_t height)
Definition: Array2D.h:85
Array2D(const allocator_type &alloc=allocator_type())
Definition: Array2D.h:55
void assign(size_t width, size_t height)
Definition: Array2D.h:99
container_type::allocator_type allocator_type
Definition: Array2D.h:43
T & at(size_t i)
1D vector-like access.
Definition: Array2D.h:126
const T & operator()(size_t column, size_t row) const
2D access.
Definition: Array2D.h:139
void assign(size_t width, size_t height, const value_type &value)
Definition: Array2D.h:106
size_t height() const
Definition: Array2D.h:162
Array2D & operator=(const Array2D &other)
Definition: Array2D.h:75
container_type::reference reference
Definition: Array2D.h:46
const T & operator[](size_t i) const
1D vector-like access.
Definition: Array2D.h:129
container_type::pointer pointer
Definition: Array2D.h:48
T & at(size_t column, size_t row)
2D access.
Definition: Array2D.h:136
container_type::difference_type difference_type
Definition: Array2D.h:45
size_type max_size() const
Definition: Array2D.h:164
container_type::reverse_iterator reverse_iterator
Definition: Array2D.h:52
bool empty() const
Definition: Array2D.h:166
size_t index1D(size_t column, size_t row) const
Calculate the 1D index for 2D array coordinates.
Definition: Array2D.h:144
void swap(Array2D &other)
Swap this array with another one.
Definition: Array2D.h:154
void resize(size_t width, size_t height, const value_type &value)
Definition: Array2D.h:92
void indexTo2D(size_t i, size_t &column, size_t &row) const
Calculate the 2D array coordinates for a given 1D index.
Definition: Array2D.h:147
container_type::const_reference const_reference
Definition: Array2D.h:47
iterator begin()
Definition: Array2D.h:168
std::vector< T, TAllocator > container_type
Definition: Array2D.h:41
const_iterator end() const
Definition: Array2D.h:171
container_type::const_reverse_iterator const_reverse_iterator
Definition: Array2D.h:53
size_t m_height
Definition: Array2D.h:176
container_type::iterator iterator
Definition: Array2D.h:50
container_type::value_type value_type
Definition: Array2D.h:42
container_type::size_type size_type
Definition: Array2D.h:44
container_type::const_iterator const_iterator
Definition: Array2D.h:51


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58