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


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