GteArray2.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <GTEngineDEF.h>
11 #include <cstddef>
12 #include <vector>
13 
14 // The Array2 class represents a 2-dimensional array that minimizes the number
15 // of new and delete calls. The T objects are stored in a contiguous array.
16 
17 namespace gte
18 {
19 
20 template <typename T>
21 class Array2
22 {
23 public:
24  // Construction. The first constructor generates an array of objects that
25  // are owned by Array2. The second constructor is given an array of
26  // objects that are owned by the caller. The array has bound0 columns and
27  // bound1 rows.
28  Array2(size_t bound0, size_t bound1);
29  Array2(size_t bound0, size_t bound1, T* objects);
30 
31  // Support for dynamic resizing, copying, or moving. If 'other' does
32  // not own the original 'objects', they are not copied by the assignment
33  // operator.
34  Array2();
35  Array2(Array2 const& other);
36  Array2& operator=(Array2 const& other);
37  Array2(Array2&& other);
38  Array2& operator=(Array2&& other);
39 
40  // Access to the array. Sample usage is
41  // Array2<T> myArray(3, 2);
42  // T* row1 = myArray[1];
43  // T row1Col2 = myArray[1][2];
44  inline size_t GetBound0() const;
45  inline size_t GetBound1() const;
46  inline T const* operator[] (int row) const;
47  inline T* operator[] (int row);
48 
49 private:
50  void SetPointers(T* objects);
51  void SetPointers(Array2 const& other);
52 
53  size_t mBound0, mBound1;
54  std::vector<T> mObjects;
55  std::vector<T*> mIndirect1;
56 };
57 
58 template <typename T>
59 Array2<T>::Array2(size_t bound0, size_t bound1)
60  :
61  mBound0(bound0),
62  mBound1(bound1),
63  mObjects(bound0 * bound1),
64  mIndirect1(bound1)
65 {
66  SetPointers(mObjects.data());
67 }
68 
69 template <typename T>
70 Array2<T>::Array2(size_t bound0, size_t bound1, T* objects)
71  :
72  mBound0(bound0),
73  mBound1(bound1),
74  mIndirect1(bound1)
75 {
76  SetPointers(objects);
77 }
78 
79 template <typename T>
81  :
82  mBound0(0),
83  mBound1(0)
84 {
85 }
86 
87 template <typename T>
89 {
90  *this = other;
91 }
92 
93 template <typename T>
95 {
96  // The copy is valid whether or not other.mObjects has elements.
97  mObjects = other.mObjects;
98  SetPointers(other);
99  return *this;
100 }
101 
102 template <typename T>
104 {
105  *this = std::move(other);
106 }
107 
108 template <typename T>
110 {
111  // The move is valid whether or not other.mObjects has elements.
112  mObjects = std::move(other.mObjects);
113  SetPointers(other);
114  return *this;
115 }
116 
117 template <typename T> inline
118 size_t Array2<T>::GetBound0() const
119 {
120  return mBound0;
121 }
122 
123 template <typename T> inline
124 size_t Array2<T>::GetBound1() const
125 {
126  return mBound1;
127 }
128 
129 template <typename T> inline
130 T const* Array2<T>::operator[] (int row) const
131 {
132  return mIndirect1[row];
133 }
134 
135 template <typename T> inline
137 {
138  return mIndirect1[row];
139 }
140 
141 template <typename T>
142 void Array2<T>::SetPointers(T* objects)
143 {
144  for (size_t i1 = 0; i1 < mBound1; ++i1)
145  {
146  size_t j0 = mBound0 * i1; // = bound0 * (i1 + j1) where j1 = 0
147  mIndirect1[i1] = &objects[j0];
148  }
149 }
150 
151 template <typename T>
152 void Array2<T>::SetPointers(Array2 const& other)
153 {
154  mBound0 = other.mBound0;
155  mBound1 = other.mBound1;
156  mIndirect1.resize(mBound1);
157 
158  if (mBound0 > 0)
159  {
160  // The objects are owned.
161  SetPointers(mObjects.data());
162  }
163  else if (mIndirect1.size() > 0)
164  {
165  // The objects are not owned.
166  SetPointers(other.mIndirect1[0]);
167  }
168  // else 'other' is an empty Array2.
169 }
170 
171 }
size_t GetBound1() const
Definition: GteArray2.h:124
size_t mBound0
Definition: GteArray2.h:53
std::vector< T * > mIndirect1
Definition: GteArray2.h:55
size_t mBound1
Definition: GteArray2.h:53
Array2 & operator=(Array2 const &other)
Definition: GteArray2.h:94
GLenum GLenum GLsizei void * row
Definition: glext.h:2725
void SetPointers(T *objects)
Definition: GteArray2.h:142
T const * operator[](int row) const
Definition: GteArray2.h:130
std::vector< T > mObjects
Definition: GteArray2.h:54
size_t GetBound0() const
Definition: GteArray2.h:118


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59