GJK_nD
 All Classes Files Functions Typedefs Friends Pages
Array2D.hpp
1 /*
2  * Copyright (c) 2012 Laurent Provot <provot.research@gmail.com>,
3  * Yan Gerard <yan.gerard@free.fr> and Fabien Feschet <research@feschet.fr>
4  * All rights reserved.
5  *
6  * This is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef UTILS_ARRAY2D_HPP
21 #define UTILS_ARRAY2D_HPP
22 
23 #include <algorithm>
24 
25 namespace Utils {
26 
41 template <typename T>
42 class Array2D
43 {
44 public:
49  : myWidth (0), myHeight(0), myArray (0)
50  {}
51 
55  Array2D(size_t width, size_t height)
56  : myWidth (width), myHeight(height), myArray (0)
57  {
58  if(width > 0 && height > 0)
59  myArray = new T[width * height];
60  }
61 
65  Array2D(const Array2D<T> & srcArray)
66  : myWidth(srcArray.myWidth), myHeight(srcArray.myHeight), myArray(0)
67  {
68  if(myWidth > 0 && myHeight > 0) {
69  myArray = new T[myWidth * myHeight];
70  std::copy(srcArray.myArray, srcArray.myArray + myWidth * myHeight,
71  myArray);
72  }
73  }
74 
79  {
80  if (myArray)
81  delete[] myArray;
82  }
83 
87  Array2D & operator=(const Array2D<T> & srcArray)
88  {
89  Array2D copy(srcArray);
90 
91  myWidth = copy.myWidth;
92  myHeight = copy.myHeight;
93  std::swap(myArray, copy.myArray);
94 
95  return *this;
96  }
97 
101  const T & operator () (size_t x, size_t y) const
102  {
103  return myArray[y * myWidth + x];
104  }
105 
110  T & operator () (size_t x, size_t y)
111  {
112  return myArray[y * myWidth + x];
113  }
114 
118  size_t width() const { return myWidth; }
119 
123  size_t height() const { return myHeight; }
124 
125 private:
126  size_t myWidth;
127  size_t myHeight;
128  T * myArray;
129 };
130 
131 } // namespace Utils
132 
133 #endif // UTILS_ARRAY2D_HPP