GteArray3.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 Array3 class represents a 3-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 Array3
22 {
23 public:
24  // Construction. The first constructor generates an array of objects that
25  // are owned by Array3. The second constructor is given an array of
26  // objects that are owned by the caller. The array has bound0 columns,
27  // bound1 rows, and bound2 slices.
28  Array3(size_t bound0, size_t bound1, size_t bound2);
29  Array3(size_t bound0, size_t bound1, size_t bound2, 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  Array3();
35  Array3(Array3 const&);
36  Array3& operator=(Array3 const&);
37  Array3(Array3&&);
39 
40  // Access to the array. Sample usage is
41  // Array3<T> myArray(4, 3, 2);
42  // T** slice1 = myArray[1];
43  // T* slice1row2 = myArray[1][2];
44  // T slice1Row2Col3 = myArray[1][2][3];
45  inline size_t GetBound0() const;
46  inline size_t GetBound1() const;
47  inline size_t GetBound2() const;
48  inline T* const* operator[] (int slice) const;
49  inline T** operator[] (int slice);
50 
51 private:
52  void SetPointers(T* objects);
53  void SetPointers(Array3 const& other);
54 
56  std::vector<T> mObjects;
57  std::vector<T*> mIndirect1;
58  std::vector<T**> mIndirect2;
59 };
60 
61 template <typename T>
62 Array3<T>::Array3(size_t bound0, size_t bound1, size_t bound2)
63  :
64  mBound0(bound0),
65  mBound1(bound1),
66  mBound2(bound2),
67  mObjects(bound0 * bound1 * bound2),
68  mIndirect1(bound1 * bound2),
69  mIndirect2(bound2)
70 {
71  SetPointers(mObjects.data());
72 }
73 
74 template <typename T>
75 Array3<T>::Array3(size_t bound0, size_t bound1, size_t bound2, T* objects)
76  :
77  mBound0(bound0),
78  mBound1(bound1),
79  mBound2(bound2),
80  mIndirect1(bound1 * bound2),
81  mIndirect2(bound2)
82 {
83  SetPointers(objects);
84 }
85 
86 template <typename T>
88  :
89  mBound0(0),
90  mBound1(0),
91  mBound2(0)
92 {
93 }
94 
95 template <typename T>
97 {
98  *this = other;
99 }
100 
101 template <typename T>
103 {
104  // The copy is valid whether or not other.mObjects has elements.
105  mObjects = other.mObjects;
106  SetPointers(other);
107  return *this;
108 }
109 
110 template <typename T>
112 {
113  *this = std::move(other);
114 }
115 
116 template <typename T>
118 {
119  // The move is valid whether or not other.mObjects has elements.
120  mObjects = std::move(other.mObjects);
121  SetPointers(other);
122  return *this;
123 }
124 
125 template <typename T> inline
126 size_t Array3<T>::GetBound0() const
127 {
128  return mBound0;
129 }
130 
131 template <typename T> inline
132 size_t Array3<T>::GetBound1() const
133 {
134  return mBound1;
135 }
136 
137 template <typename T> inline
138 size_t Array3<T>::GetBound2() const
139 {
140  return mBound2;
141 }
142 
143 template <typename T> inline
144 T* const* Array3<T>::operator[] (int slice) const
145 {
146  return mIndirect2[slice];
147 }
148 
149 template <typename T> inline
151 {
152  return mIndirect2[slice];
153 }
154 
155 template <typename T>
156 void Array3<T>::SetPointers(T* objects)
157 {
158  for (size_t i2 = 0; i2 < mBound2; ++i2)
159  {
160  size_t j1 = mBound1 * i2; // = bound1 * (i2 + j2) where j2 = 0
161  mIndirect2[i2] = &mIndirect1[j1];
162  for (size_t i1 = 0; i1 < mBound1; ++i1)
163  {
164  size_t j0 = mBound0 * (i1 + j1);
165  mIndirect2[i2][i1] = &objects[j0];
166  }
167  }
168 }
169 
170 template <typename T>
171 void Array3<T>::SetPointers(Array3 const& other)
172 {
173  mBound0 = other.mBound0;
174  mBound1 = other.mBound1;
175  mBound2 = other.mBound2;
176  mIndirect1.resize(mBound1 * mBound2);
177  mIndirect2.resize(mBound2);
178 
179  if (mBound0 > 0)
180  {
181  // The objects are owned.
182  SetPointers(mObjects.data());
183  }
184  else if (mIndirect1.size() > 0)
185  {
186  // The objects are not owned.
187  SetPointers(other.mIndirect2[0][0]);
188  }
189  // else 'other' is an empty Array3.
190 }
191 
192 }
size_t GetBound1() const
Definition: GteArray3.h:132
std::vector< T > mObjects
Definition: GteArray3.h:56
Array3 & operator=(Array3 const &)
Definition: GteArray3.h:102
size_t mBound2
Definition: GteArray3.h:55
std::vector< T * > mIndirect1
Definition: GteArray3.h:57
void SetPointers(T *objects)
Definition: GteArray3.h:156
size_t mBound0
Definition: GteArray3.h:55
T *const * operator[](int slice) const
Definition: GteArray3.h:144
size_t mBound1
Definition: GteArray3.h:55
size_t GetBound2() const
Definition: GteArray3.h:138
std::vector< T ** > mIndirect2
Definition: GteArray3.h:58
size_t GetBound0() const
Definition: GteArray3.h:126


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