OVR_Array.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /************************************************************************************
  2. PublicHeader: OVR_Kernel.h
  3. Filename : OVR_Array.h
  4. Content : Template implementation for Array
  5. Created : September 19, 2012
  6. Notes :
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_Array_h
  21. #define OVR_Array_h
  22. #include "OVR_ContainerAllocator.h"
  23. namespace OVR {
  24. //-----------------------------------------------------------------------------------
  25. // ***** ArrayDefaultPolicy
  26. //
  27. // Default resize behavior. No minimal capacity, Granularity=4,
  28. // Shrinking as needed. ArrayConstPolicy actually is the same as
  29. // ArrayDefaultPolicy, but parametrized with constants.
  30. // This struct is used only in order to reduce the template "matroska".
  31. struct ArrayDefaultPolicy
  32. {
  33. ArrayDefaultPolicy() : Capacity(0) {}
  34. ArrayDefaultPolicy(const ArrayDefaultPolicy&) : Capacity(0) {}
  35. size_t GetMinCapacity() const { return 0; }
  36. size_t GetGranularity() const { return 4; }
  37. bool NeverShrinking() const { return 1; }
  38. size_t GetCapacity() const { return Capacity; }
  39. void SetCapacity(size_t capacity) { Capacity = capacity; }
  40. private:
  41. size_t Capacity;
  42. };
  43. //-----------------------------------------------------------------------------------
  44. // ***** ArrayConstPolicy
  45. //
  46. // Statically parametrized resizing behavior:
  47. // MinCapacity, Granularity, and Shrinking flag.
  48. template<int MinCapacity=0, int Granularity=4, bool NeverShrink=false>
  49. struct ArrayConstPolicy
  50. {
  51. typedef ArrayConstPolicy<MinCapacity, Granularity, NeverShrink> SelfType;
  52. ArrayConstPolicy() : Capacity(0) {}
  53. ArrayConstPolicy(const SelfType&) : Capacity(0) {}
  54. size_t GetMinCapacity() const { return MinCapacity; }
  55. size_t GetGranularity() const { return Granularity; }
  56. bool NeverShrinking() const { return NeverShrink; }
  57. size_t GetCapacity() const { return Capacity; }
  58. void SetCapacity(size_t capacity) { Capacity = capacity; }
  59. private:
  60. size_t Capacity;
  61. };
  62. //-----------------------------------------------------------------------------------
  63. // ***** ArrayDataBase
  64. //
  65. // Basic operations with array data: Reserve, Resize, Free, ArrayPolicy.
  66. // For internal use only: ArrayData,ArrayDataCC and others.
  67. template<class T, class Allocator, class SizePolicy>
  68. struct ArrayDataBase
  69. {
  70. typedef T ValueType;
  71. typedef Allocator AllocatorType;
  72. typedef SizePolicy SizePolicyType;
  73. typedef ArrayDataBase<T, Allocator, SizePolicy> SelfType;
  74. ArrayDataBase()
  75. : Data(0), Size(0), Policy() {}
  76. ArrayDataBase(const SizePolicy& p)
  77. : Data(0), Size(0), Policy(p) {}
  78. ~ArrayDataBase()
  79. {
  80. if (Data)
  81. {
  82. Allocator::DestructArray(Data, Size);
  83. Allocator::Free(Data);
  84. }
  85. }
  86. size_t GetCapacity() const
  87. {
  88. return Policy.GetCapacity();
  89. }
  90. void ClearAndRelease()
  91. {
  92. if (Data)
  93. {
  94. Allocator::DestructArray(Data, Size);
  95. Allocator::Free(Data);
  96. Data = 0;
  97. }
  98. Size = 0;
  99. Policy.SetCapacity(0);
  100. }
  101. void Reserve(size_t newCapacity)
  102. {
  103. if (Policy.NeverShrinking() && newCapacity < GetCapacity())
  104. return;
  105. if (newCapacity < Policy.GetMinCapacity())
  106. newCapacity = Policy.GetMinCapacity();
  107. // Resize the buffer.
  108. if (newCapacity == 0)
  109. {
  110. if (Data)
  111. {
  112. Allocator::Free(Data);
  113. Data = 0;
  114. }
  115. Policy.SetCapacity(0);
  116. }
  117. else
  118. {
  119. size_t gran = Policy.GetGranularity();
  120. newCapacity = (newCapacity + gran - 1) / gran * gran;
  121. if (Data)
  122. {
  123. if (Allocator::IsMovable())
  124. {
  125. Data = (T*)Allocator::Realloc(Data, sizeof(T) * newCapacity);
  126. }
  127. else
  128. {
  129. T* newData = (T*)Allocator::Alloc(sizeof(T) * newCapacity);
  130. size_t i, s;
  131. s = (Size < newCapacity) ? Size : newCapacity;
  132. for (i = 0; i < s; ++i)
  133. {
  134. Allocator::Construct(&newData[i], Data[i]);
  135. Allocator::Destruct(&Data[i]);
  136. }
  137. for (i = s; i < Size; ++i)
  138. {
  139. Allocator::Destruct(&Data[i]);
  140. }
  141. Allocator::Free(Data);
  142. Data = newData;
  143. }
  144. }
  145. else
  146. {
  147. Data = (T*)Allocator::Alloc(sizeof(T) * newCapacity);
  148. //memset(Buffer, 0, (sizeof(ValueType) * newSize)); // Do we need this?
  149. }
  150. Policy.SetCapacity(newCapacity);
  151. // OVR_ASSERT(Data); // need to throw (or something) on alloc failure!
  152. }
  153. }
  154. // This version of Resize DOES NOT construct the elements.
  155. // It's done to optimize PushBack, which uses a copy constructor
  156. // instead of the default constructor and assignment
  157. void ResizeNoConstruct(size_t newSize)
  158. {
  159. size_t oldSize = Size;
  160. if (newSize < oldSize)
  161. {
  162. Allocator::DestructArray(Data + newSize, oldSize - newSize);
  163. if (newSize < (Policy.GetCapacity() >> 1))
  164. {
  165. Reserve(newSize);
  166. }
  167. }
  168. else if(newSize >= Policy.GetCapacity())
  169. {
  170. Reserve(newSize + (newSize >> 2));
  171. }
  172. //! IMPORTANT to modify Size only after Reserve completes, because garbage collectable
  173. // array may use this array and may traverse it during Reserve (in the case, if
  174. // collection occurs because of heap limit exceeded).
  175. Size = newSize;
  176. }
  177. ValueType* Data;
  178. size_t Size;
  179. SizePolicy Policy;
  180. };
  181. //-----------------------------------------------------------------------------------
  182. // ***** ArrayData
  183. //
  184. // General purpose array data.
  185. // For internal use only in Array, ArrayLH, ArrayPOD and so on.
  186. template<class T, class Allocator, class SizePolicy>
  187. struct ArrayData : ArrayDataBase<T, Allocator, SizePolicy>
  188. {
  189. typedef T ValueType;
  190. typedef Allocator AllocatorType;
  191. typedef SizePolicy SizePolicyType;
  192. typedef ArrayDataBase<T, Allocator, SizePolicy> BaseType;
  193. typedef ArrayData <T, Allocator, SizePolicy> SelfType;
  194. ArrayData()
  195. : BaseType() { }
  196. ArrayData(size_t size)
  197. : BaseType() { Resize(size); }
  198. ArrayData(const SelfType& a)
  199. : BaseType(a.Policy) { Append(a.Data, a.Size); }
  200. void Resize(size_t newSize)
  201. {
  202. size_t oldSize = this->Size;
  203. BaseType::ResizeNoConstruct(newSize);
  204. if(newSize > oldSize)
  205. Allocator::ConstructArray(this->Data + oldSize, newSize - oldSize);
  206. }
  207. void PushBack(const ValueType& val)
  208. {
  209. BaseType::ResizeNoConstruct(this->Size + 1);
  210. OVR_ASSERT(this->Data != NULL);
  211. Allocator::Construct(this->Data + this->Size - 1, val);
  212. }
  213. template<class S>
  214. void PushBackAlt(const S& val)
  215. {
  216. BaseType::ResizeNoConstruct(this->Size + 1);
  217. Allocator::ConstructAlt(this->Data + this->Size - 1, val);
  218. }
  219. // Append the given data to the array.
  220. void Append(const ValueType other[], size_t count)
  221. {
  222. if (count)
  223. {
  224. size_t oldSize = this->Size;
  225. BaseType::ResizeNoConstruct(this->Size + count);
  226. Allocator::ConstructArray(this->Data + oldSize, count, other);
  227. }
  228. }
  229. };
  230. //-----------------------------------------------------------------------------------
  231. // ***** ArrayDataCC
  232. //
  233. // A modification of ArrayData that always copy-constructs new elements
  234. // using a specified DefaultValue. For internal use only in ArrayCC.
  235. template<class T, class Allocator, class SizePolicy>
  236. struct ArrayDataCC : ArrayDataBase<T, Allocator, SizePolicy>
  237. {
  238. typedef T ValueType;
  239. typedef Allocator AllocatorType;
  240. typedef SizePolicy SizePolicyType;
  241. typedef ArrayDataBase<T, Allocator, SizePolicy> BaseType;
  242. typedef ArrayDataCC <T, Allocator, SizePolicy> SelfType;
  243. ArrayDataCC(const ValueType& defval)
  244. : BaseType(), DefaultValue(defval) { }
  245. ArrayDataCC(const ValueType& defval, size_t size)
  246. : BaseType(), DefaultValue(defval) { Resize(size); }
  247. ArrayDataCC(const SelfType& a)
  248. : BaseType(a.Policy), DefaultValue(a.DefaultValue) { Append(a.Data, a.Size); }
  249. void Resize(size_t newSize)
  250. {
  251. size_t oldSize = this->Size;
  252. BaseType::ResizeNoConstruct(newSize);
  253. if(newSize > oldSize)
  254. Allocator::ConstructArray(this->Data + oldSize, newSize - oldSize, DefaultValue);
  255. }
  256. void PushBack(const ValueType& val)
  257. {
  258. BaseType::ResizeNoConstruct(this->Size + 1);
  259. Allocator::Construct(this->Data + this->Size - 1, val);
  260. }
  261. template<class S>
  262. void PushBackAlt(const S& val)
  263. {
  264. BaseType::ResizeNoConstruct(this->Size + 1);
  265. Allocator::ConstructAlt(this->Data + this->Size - 1, val);
  266. }
  267. // Append the given data to the array.
  268. void Append(const ValueType other[], size_t count)
  269. {
  270. if (count)
  271. {
  272. size_t oldSize = this->Size;
  273. BaseType::ResizeNoConstruct(this->Size + count);
  274. Allocator::ConstructArray(this->Data + oldSize, count, other);
  275. }
  276. }
  277. ValueType DefaultValue;
  278. };
  279. //-----------------------------------------------------------------------------------
  280. // ***** ArrayBase
  281. //
  282. // Resizable array. The behavior can be POD (suffix _POD) and
  283. // Movable (no suffix) depending on the allocator policy.
  284. // In case of _POD the constructors and destructors are not called.
  285. //
  286. // Arrays can't handle non-movable objects! Don't put anything in here
  287. // that can't be moved around by bitwise copy.
  288. //
  289. // The addresses of elements are not persistent! Don't keep the address
  290. // of an element; the array contents will move around as it gets resized.
  291. template<class ArrayData>
  292. class ArrayBase
  293. {
  294. public:
  295. typedef typename ArrayData::ValueType ValueType;
  296. typedef typename ArrayData::AllocatorType AllocatorType;
  297. typedef typename ArrayData::SizePolicyType SizePolicyType;
  298. typedef ArrayBase<ArrayData> SelfType;
  299. #undef new
  300. OVR_MEMORY_REDEFINE_NEW(ArrayBase)
  301. // Redefine operator 'new' if necessary.
  302. #if defined(OVR_DEFINE_NEW)
  303. #define new OVR_DEFINE_NEW
  304. #endif
  305. ArrayBase()
  306. : Data() {}
  307. ArrayBase(size_t size)
  308. : Data(size) {}
  309. ArrayBase(const SelfType& a)
  310. : Data(a.Data) {}
  311. ArrayBase(const ValueType& defval)
  312. : Data(defval) {}
  313. ArrayBase(const ValueType& defval, size_t size)
  314. : Data(defval, size) {}
  315. SizePolicyType* GetSizePolicy() const { return Data.Policy; }
  316. void SetSizePolicy(const SizePolicyType& p) { Data.Policy = p; }
  317. bool NeverShrinking()const { return Data.Policy.NeverShrinking(); }
  318. size_t GetSize() const { return Data.Size; }
  319. int GetSizeI() const { return (int)Data.Size; }
  320. bool IsEmpty() const { return Data.Size == 0; }
  321. size_t GetCapacity() const { return Data.GetCapacity(); }
  322. size_t GetNumBytes() const { return Data.GetCapacity() * sizeof(ValueType); }
  323. void ClearAndRelease() { Data.ClearAndRelease(); }
  324. void Clear() { Data.Resize(0); }
  325. void Resize(size_t newSize) { Data.Resize(newSize); }
  326. // Reserve can only increase the capacity
  327. void Reserve(size_t newCapacity)
  328. {
  329. if (newCapacity > Data.GetCapacity())
  330. Data.Reserve(newCapacity);
  331. }
  332. // Basic access.
  333. ValueType& At(size_t index)
  334. {
  335. OVR_ASSERT((Data.Data) && (index < Data.Size)); // Asserting that Data.Data is valid helps static analysis tools.
  336. return Data.Data[index];
  337. }
  338. const ValueType& At(size_t index) const
  339. {
  340. OVR_ASSERT((Data.Data) && (index < Data.Size));
  341. return Data.Data[index];
  342. }
  343. ValueType ValueAt(size_t index) const
  344. {
  345. OVR_ASSERT((Data.Data) && (index < Data.Size));
  346. return Data.Data[index];
  347. }
  348. // Basic access.
  349. ValueType& operator [] (size_t index)
  350. {
  351. OVR_ASSERT((Data.Data) && (index < Data.Size));
  352. return Data.Data[index];
  353. }
  354. const ValueType& operator [] (size_t index) const
  355. {
  356. OVR_ASSERT((Data.Data) && (index < Data.Size));
  357. return Data.Data[index];
  358. }
  359. // Raw pointer to the data. Use with caution!
  360. const ValueType* GetDataPtr() const { return Data.Data; }
  361. ValueType* GetDataPtr() { return Data.Data; }
  362. // Insert the given element at the end of the array.
  363. void PushBack(const ValueType& val)
  364. {
  365. // DO NOT pass elements of your own vector into
  366. // push_back()! Since we're using references,
  367. // resize() may munge the element storage!
  368. // OVR_ASSERT(&val < &Buffer[0] || &val > &Buffer[BufferSize]);
  369. Data.PushBack(val);
  370. }
  371. template<class S>
  372. void PushBackAlt(const S& val)
  373. {
  374. Data.PushBackAlt(val);
  375. }
  376. // Remove the last element.
  377. void PopBack(size_t count = 1)
  378. {
  379. OVR_ASSERT(Data.Size >= count);
  380. Data.Resize(Data.Size - count);
  381. }
  382. ValueType& PushDefault()
  383. {
  384. Data.PushBack(ValueType());
  385. return Back();
  386. }
  387. ValueType Pop()
  388. {
  389. OVR_ASSERT((Data.Data) && (Data.Size > 0));
  390. ValueType t = Back();
  391. PopBack();
  392. return t;
  393. }
  394. // Access the first element.
  395. ValueType& Front() { return At(0); }
  396. const ValueType& Front() const { return At(0); }
  397. // Access the last element.
  398. ValueType& Back() { return At(Data.Size - 1); }
  399. const ValueType& Back() const { return At(Data.Size - 1); }
  400. // Array copy. Copies the contents of a into this array.
  401. const SelfType& operator = (const SelfType& a)
  402. {
  403. Resize(a.GetSize());
  404. OVR_ASSERT((Data.Data != NULL) || (Data.Size == 0));
  405. for (size_t i = 0; i < Data.Size; i++) {
  406. *(Data.Data + i) = a[i];
  407. }
  408. return *this;
  409. }
  410. // Removing multiple elements from the array.
  411. void RemoveMultipleAt(size_t index, size_t num)
  412. {
  413. OVR_ASSERT(index + num <= Data.Size);
  414. if (Data.Size == num)
  415. {
  416. Clear();
  417. }
  418. else
  419. {
  420. AllocatorType::DestructArray(Data.Data + index, num);
  421. AllocatorType::CopyArrayForward(
  422. Data.Data + index,
  423. Data.Data + index + num,
  424. Data.Size - num - index);
  425. Data.Size -= num;
  426. }
  427. }
  428. // Removing an element from the array is an expensive operation!
  429. // It compacts only after removing the last element.
  430. // If order of elements in the array is not important then use
  431. // RemoveAtUnordered, that could be much faster than the regular
  432. // RemoveAt.
  433. void RemoveAt(size_t index)
  434. {
  435. OVR_ASSERT((Data.Data) && (index < Data.Size));
  436. if (Data.Size == 1)
  437. {
  438. Clear();
  439. }
  440. else
  441. {
  442. AllocatorType::Destruct(Data.Data + index);
  443. AllocatorType::CopyArrayForward(
  444. Data.Data + index,
  445. Data.Data + index + 1,
  446. Data.Size - 1 - index);
  447. --Data.Size;
  448. }
  449. }
  450. // Removes an element from the array without respecting of original order of
  451. // elements for better performance. Do not use on array where order of elements
  452. // is important, otherwise use it instead of regular RemoveAt().
  453. void RemoveAtUnordered(size_t index)
  454. {
  455. OVR_ASSERT((Data.Data) && (index < Data.Size));
  456. if (Data.Size == 1)
  457. {
  458. Clear();
  459. }
  460. else
  461. {
  462. // copy the last element into the 'index' position
  463. // and decrement the size (instead of moving all elements
  464. // in [index + 1 .. size - 1] range).
  465. const size_t lastElemIndex = Data.Size - 1;
  466. if (index < lastElemIndex)
  467. {
  468. AllocatorType::Destruct(Data.Data + index);
  469. AllocatorType::Construct(Data.Data + index, Data.Data[lastElemIndex]);
  470. }
  471. AllocatorType::Destruct(Data.Data + lastElemIndex);
  472. --Data.Size;
  473. }
  474. }
  475. // Insert the given object at the given index shifting all the elements up.
  476. void InsertAt(size_t index, const ValueType& val = ValueType())
  477. {
  478. OVR_ASSERT(index <= Data.Size);
  479. Data.Resize(Data.Size + 1);
  480. if (index < Data.Size - 1)
  481. {
  482. AllocatorType::CopyArrayBackward(
  483. Data.Data + index + 1,
  484. Data.Data + index,
  485. Data.Size - 1 - index);
  486. }
  487. AllocatorType::Construct(Data.Data + index, val);
  488. }
  489. // Insert the given object at the given index shifting all the elements up.
  490. void InsertMultipleAt(size_t index, size_t num, const ValueType& val = ValueType())
  491. {
  492. OVR_ASSERT(index <= Data.Size);
  493. Data.Resize(Data.Size + num);
  494. if (index < Data.Size - num)
  495. {
  496. AllocatorType::CopyArrayBackward(
  497. Data.Data + index + num,
  498. Data.Data + index,
  499. Data.Size - num - index);
  500. }
  501. for (size_t i = 0; i < num; ++i)
  502. AllocatorType::Construct(Data.Data + index + i, val);
  503. }
  504. // Append the given data to the array.
  505. void Append(const SelfType& other)
  506. {
  507. Append(other.Data.Data, other.GetSize());
  508. }
  509. // Append the given data to the array.
  510. void Append(const ValueType other[], size_t count)
  511. {
  512. Data.Append(other, count);
  513. }
  514. class Iterator
  515. {
  516. SelfType* pArray;
  517. intptr_t CurIndex;
  518. public:
  519. Iterator() : pArray(0), CurIndex(-1) {}
  520. Iterator(SelfType* parr, intptr_t idx = 0) : pArray(parr), CurIndex(idx) {}
  521. bool operator==(const Iterator& it) const { return pArray == it.pArray && CurIndex == it.CurIndex; }
  522. bool operator!=(const Iterator& it) const { return pArray != it.pArray || CurIndex != it.CurIndex; }
  523. Iterator& operator++()
  524. {
  525. if (pArray)
  526. {
  527. if (CurIndex < (intptr_t)pArray->GetSize())
  528. ++CurIndex;
  529. }
  530. return *this;
  531. }
  532. Iterator operator++(int)
  533. {
  534. Iterator it(*this);
  535. operator++();
  536. return it;
  537. }
  538. Iterator& operator--()
  539. {
  540. if (pArray)
  541. {
  542. if (CurIndex >= 0)
  543. --CurIndex;
  544. }
  545. return *this;
  546. }
  547. Iterator operator--(int)
  548. {
  549. Iterator it(*this);
  550. operator--();
  551. return it;
  552. }
  553. Iterator operator+(int delta) const
  554. {
  555. return Iterator(pArray, CurIndex + delta);
  556. }
  557. Iterator operator-(int delta) const
  558. {
  559. return Iterator(pArray, CurIndex - delta);
  560. }
  561. intptr_t operator-(const Iterator& right) const
  562. {
  563. OVR_ASSERT(pArray == right.pArray);
  564. return CurIndex - right.CurIndex;
  565. }
  566. ValueType& operator*() const { OVR_ASSERT(pArray); return (*pArray)[CurIndex]; }
  567. ValueType* operator->() const { OVR_ASSERT(pArray); return &(*pArray)[CurIndex]; }
  568. ValueType* GetPtr() const { OVR_ASSERT(pArray); return &(*pArray)[CurIndex]; }
  569. bool IsFinished() const { return !pArray || CurIndex < 0 || CurIndex >= (int)pArray->GetSize(); }
  570. void Remove()
  571. {
  572. if (!IsFinished())
  573. pArray->RemoveAt(CurIndex);
  574. }
  575. intptr_t GetIndex() const { return CurIndex; }
  576. };
  577. Iterator Begin() { return Iterator(this); }
  578. Iterator End() { return Iterator(this, (intptr_t)GetSize()); }
  579. Iterator Last() { return Iterator(this, (intptr_t)GetSize() - 1); }
  580. // C++11 ranged-based for loop support.
  581. Iterator begin() { return Begin(); }
  582. Iterator end() { return End(); }
  583. class ConstIterator
  584. {
  585. const SelfType* pArray;
  586. intptr_t CurIndex;
  587. public:
  588. ConstIterator() : pArray(0), CurIndex(-1) {}
  589. ConstIterator(const SelfType* parr, intptr_t idx = 0) : pArray(parr), CurIndex(idx) {}
  590. bool operator==(const ConstIterator& it) const { return pArray == it.pArray && CurIndex == it.CurIndex; }
  591. bool operator!=(const ConstIterator& it) const { return pArray != it.pArray || CurIndex != it.CurIndex; }
  592. ConstIterator& operator++()
  593. {
  594. if (pArray)
  595. {
  596. if (CurIndex < (int)pArray->GetSize())
  597. ++CurIndex;
  598. }
  599. return *this;
  600. }
  601. ConstIterator operator++(int)
  602. {
  603. ConstIterator it(*this);
  604. operator++();
  605. return it;
  606. }
  607. ConstIterator& operator--()
  608. {
  609. if (pArray)
  610. {
  611. if (CurIndex >= 0)
  612. --CurIndex;
  613. }
  614. return *this;
  615. }
  616. ConstIterator operator--(int)
  617. {
  618. ConstIterator it(*this);
  619. operator--();
  620. return it;
  621. }
  622. ConstIterator operator+(int delta) const
  623. {
  624. return ConstIterator(pArray, CurIndex + delta);
  625. }
  626. ConstIterator operator-(int delta) const
  627. {
  628. return ConstIterator(pArray, CurIndex - delta);
  629. }
  630. intptr_t operator-(const ConstIterator& right) const
  631. {
  632. OVR_ASSERT(pArray == right.pArray);
  633. return CurIndex - right.CurIndex;
  634. }
  635. const ValueType& operator*() const { OVR_ASSERT(pArray); return (*pArray)[CurIndex]; }
  636. const ValueType* operator->() const { OVR_ASSERT(pArray); return &(*pArray)[CurIndex]; }
  637. const ValueType* GetPtr() const { OVR_ASSERT(pArray); return &(*pArray)[CurIndex]; }
  638. bool IsFinished() const { return !pArray || CurIndex < 0 || CurIndex >= (int)pArray->GetSize(); }
  639. intptr_t GetIndex() const { return CurIndex; }
  640. };
  641. ConstIterator Begin() const { return ConstIterator(this); }
  642. ConstIterator End() const { return ConstIterator(this, (intptr_t)GetSize()); }
  643. ConstIterator Last() const { return ConstIterator(this, (intptr_t)GetSize() - 1); }
  644. protected:
  645. ArrayData Data;
  646. };
  647. //-----------------------------------------------------------------------------------
  648. // ***** Array
  649. //
  650. // General purpose array for movable objects that require explicit
  651. // construction/destruction.
  652. template<class T, class SizePolicy=ArrayDefaultPolicy>
  653. class Array : public ArrayBase<ArrayData<T, ContainerAllocator<T>, SizePolicy> >
  654. {
  655. public:
  656. typedef T ValueType;
  657. typedef ContainerAllocator<T> AllocatorType;
  658. typedef SizePolicy SizePolicyType;
  659. typedef Array<T, SizePolicy> SelfType;
  660. typedef ArrayBase<ArrayData<T, ContainerAllocator<T>, SizePolicy> > BaseType;
  661. Array() : BaseType() {}
  662. explicit Array(size_t size) : BaseType(size) {}
  663. Array(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
  664. Array(const SelfType& a) : BaseType(a) {}
  665. const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
  666. };
  667. // ***** ArrayPOD
  668. //
  669. // General purpose array for movable objects that DOES NOT require
  670. // construction/destruction. Constructors and destructors are not called!
  671. // Global heap is in use.
  672. template<class T, class SizePolicy=ArrayDefaultPolicy>
  673. class ArrayPOD : public ArrayBase<ArrayData<T, ContainerAllocator_POD<T>, SizePolicy> >
  674. {
  675. public:
  676. typedef T ValueType;
  677. typedef ContainerAllocator_POD<T> AllocatorType;
  678. typedef SizePolicy SizePolicyType;
  679. typedef ArrayPOD<T, SizePolicy> SelfType;
  680. typedef ArrayBase<ArrayData<T, ContainerAllocator_POD<T>, SizePolicy> > BaseType;
  681. ArrayPOD() : BaseType() {}
  682. explicit ArrayPOD(size_t size) : BaseType(size) {}
  683. ArrayPOD(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
  684. ArrayPOD(const SelfType& a) : BaseType(a) {}
  685. const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
  686. };
  687. // ***** ArrayCPP
  688. //
  689. // General purpose, fully C++ compliant array. Can be used with non-movable data.
  690. // Global heap is in use.
  691. template<class T, class SizePolicy=ArrayDefaultPolicy>
  692. class ArrayCPP : public ArrayBase<ArrayData<T, ContainerAllocator_CPP<T>, SizePolicy> >
  693. {
  694. public:
  695. typedef T ValueType;
  696. typedef ContainerAllocator_CPP<T> AllocatorType;
  697. typedef SizePolicy SizePolicyType;
  698. typedef ArrayCPP<T, SizePolicy> SelfType;
  699. typedef ArrayBase<ArrayData<T, ContainerAllocator_CPP<T>, SizePolicy> > BaseType;
  700. ArrayCPP() : BaseType() {}
  701. explicit ArrayCPP(size_t size) : BaseType(size) {}
  702. ArrayCPP(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
  703. ArrayCPP(const SelfType& a) : BaseType(a) {}
  704. const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
  705. };
  706. // ***** ArrayCC
  707. //
  708. // A modification of the array that uses the given default value to
  709. // construct the elements. The constructors and destructors are
  710. // properly called, the objects must be movable.
  711. template<class T, class SizePolicy=ArrayDefaultPolicy>
  712. class ArrayCC : public ArrayBase<ArrayDataCC<T, ContainerAllocator<T>, SizePolicy> >
  713. {
  714. public:
  715. typedef T ValueType;
  716. typedef ContainerAllocator<T> AllocatorType;
  717. typedef SizePolicy SizePolicyType;
  718. typedef ArrayCC<T, SizePolicy> SelfType;
  719. typedef ArrayBase<ArrayDataCC<T, ContainerAllocator<T>, SizePolicy> > BaseType;
  720. ArrayCC(const ValueType& defval) : BaseType(defval) {}
  721. ArrayCC(const ValueType& defval, size_t size) : BaseType(defval, size) {}
  722. ArrayCC(const ValueType& defval, const SizePolicyType& p) : BaseType(defval) { SetSizePolicy(p); }
  723. ArrayCC(const SelfType& a) : BaseType(a) {}
  724. const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
  725. };
  726. } // OVR
  727. #endif