Array.h 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. #pragma once
  2. #include "BFPlatform.h"
  3. NS_BF_BEGIN;
  4. template <typename T>
  5. class AllocatorCLib
  6. {
  7. public:
  8. T* allocate(intptr count)
  9. {
  10. return (T*)malloc(sizeof(T) * count);
  11. }
  12. void deallocate(T* ptr)
  13. {
  14. free(ptr);
  15. }
  16. void* rawAllocate(intptr size)
  17. {
  18. return malloc(size);
  19. }
  20. void rawDeallocate(void* ptr)
  21. {
  22. free(ptr);
  23. }
  24. };
  25. template <typename T, typename TAlloc = AllocatorCLib<T> >
  26. class ArrayBase : public TAlloc
  27. {
  28. public:
  29. typedef T value_type;
  30. typedef int int_cosize;
  31. T* mVals;
  32. int_cosize mSize;
  33. int_cosize mAllocSize;
  34. struct iterator
  35. {
  36. public:
  37. typedef std::random_access_iterator_tag iterator_category;
  38. typedef T value_type;
  39. typedef intptr difference_type;
  40. typedef T* pointer;
  41. typedef T& reference;
  42. public:
  43. T* mPtr;
  44. public:
  45. iterator()
  46. {
  47. mPtr = NULL;
  48. }
  49. iterator(T* ptr)
  50. {
  51. mPtr = ptr;
  52. }
  53. iterator& operator++()
  54. {
  55. mPtr++;
  56. return *this;
  57. }
  58. iterator operator++(int)
  59. {
  60. auto prevVal = *this;
  61. mPtr++;
  62. return prevVal;
  63. }
  64. iterator& operator--()
  65. {
  66. mPtr--;
  67. return *this;
  68. }
  69. iterator operator--(int)
  70. {
  71. auto prevVal = *this;
  72. mPtr--;
  73. return prevVal;
  74. }
  75. iterator& operator+=(intptr offset)
  76. {
  77. mPtr += offset;
  78. return *this;
  79. }
  80. bool operator!=(const iterator& itr) const
  81. {
  82. return itr.mPtr != mPtr;
  83. }
  84. bool operator==(const iterator& itr) const
  85. {
  86. return itr.mPtr == mPtr;
  87. }
  88. intptr operator-(const iterator& itr) const
  89. {
  90. return mPtr - itr.mPtr;
  91. }
  92. iterator operator+(intptr offset) const
  93. {
  94. iterator itr(mPtr + offset);
  95. return itr;
  96. }
  97. iterator operator-(intptr offset) const
  98. {
  99. iterator itr(mPtr - offset);
  100. return itr;
  101. }
  102. T& operator*() const
  103. {
  104. return *mPtr;
  105. }
  106. T* operator->() const
  107. {
  108. return mPtr;
  109. }
  110. bool operator<(const iterator& val2) const
  111. {
  112. return mPtr < val2.mPtr;
  113. }
  114. bool operator>(const iterator& val2) const
  115. {
  116. return mPtr > val2.mPtr;
  117. }
  118. bool operator<=(const iterator& val2) const
  119. {
  120. return mPtr <= val2.mPtr;
  121. }
  122. bool operator>=(const iterator& val2) const
  123. {
  124. return mPtr >= val2.mPtr;
  125. }
  126. };
  127. struct const_iterator
  128. {
  129. public:
  130. typedef std::random_access_iterator_tag iterator_category;
  131. typedef T value_type;
  132. typedef intptr difference_type;
  133. typedef const T* pointer;
  134. typedef const T& reference;
  135. public:
  136. const T* mPtr;
  137. public:
  138. const_iterator(const T* ptr)
  139. {
  140. mPtr = ptr;
  141. }
  142. const_iterator& operator++()
  143. {
  144. mPtr++;
  145. return *this;
  146. }
  147. const_iterator operator++(int)
  148. {
  149. auto prevVal = *this;
  150. mPtr++;
  151. return prevVal;
  152. }
  153. bool operator!=(const const_iterator& itr) const
  154. {
  155. return itr.mPtr != mPtr;
  156. }
  157. bool operator==(const const_iterator& itr) const
  158. {
  159. return itr.mPtr == mPtr;
  160. }
  161. intptr operator-(const iterator& itr) const
  162. {
  163. return mPtr - itr.mPtr;
  164. }
  165. const_iterator operator+(intptr offset) const
  166. {
  167. const_iterator itr(mPtr + offset);
  168. return itr;
  169. }
  170. const T& operator*() const
  171. {
  172. return *mPtr;
  173. }
  174. const T* operator->() const
  175. {
  176. return mPtr;
  177. }
  178. bool operator<(const const_iterator& val2) const
  179. {
  180. return mPtr < val2.mPtr;
  181. }
  182. };
  183. private:
  184. public:
  185. ArrayBase()
  186. {
  187. mVals = NULL;
  188. mSize = 0;
  189. mAllocSize = 0;
  190. }
  191. ArrayBase(ArrayBase<T, TAlloc>&& val)
  192. {
  193. mVals = val.mVals;
  194. mSize = val.mSize;
  195. mAllocSize = val.mAllocSize;
  196. val.mVals = NULL;
  197. val.mSize = 0;
  198. val.mAllocSize = 0;
  199. }
  200. T& operator[](intptr idx)
  201. {
  202. BF_ASSERT((uintptr)idx < (uintptr)mSize);
  203. return mVals[idx];
  204. }
  205. const T& operator[](intptr idx) const
  206. {
  207. BF_ASSERT((uintptr)idx < (uintptr)mSize);
  208. return mVals[idx];
  209. }
  210. bool operator==(const ArrayBase& arrB) const
  211. {
  212. if (mSize != arrB.mSize)
  213. return false;
  214. for (intptr i = 0; i < mSize; i++)
  215. if (mVals[i] != arrB.mVals[i])
  216. return false;
  217. return true;
  218. }
  219. bool operator!=(const ArrayBase& arrB) const
  220. {
  221. if (mSize != arrB.mSize)
  222. return true;
  223. for (intptr i = 0; i < mSize; i++)
  224. if (mVals[i] != arrB.mVals[i])
  225. return true;
  226. return true;
  227. }
  228. const_iterator begin() const
  229. {
  230. return mVals;
  231. }
  232. const_iterator end() const
  233. {
  234. return mVals + mSize;
  235. }
  236. iterator begin()
  237. {
  238. return mVals;
  239. }
  240. iterator end()
  241. {
  242. return mVals + mSize;
  243. }
  244. T& front() const
  245. {
  246. return mVals[0];
  247. }
  248. T& back() const
  249. {
  250. return mVals[mSize - 1];
  251. }
  252. intptr size() const
  253. {
  254. return mSize;
  255. }
  256. int Count() const
  257. {
  258. return (int)mSize;
  259. }
  260. bool empty() const
  261. {
  262. return mSize == 0;
  263. }
  264. bool IsEmpty() const
  265. {
  266. return mSize == 0;
  267. }
  268. intptr GetFreeCount()
  269. {
  270. return mAllocSize - mSize;
  271. }
  272. /*void Free()
  273. {
  274. if (mVals != NULL)
  275. {
  276. deallocate(mVals);
  277. }
  278. mVals = NULL;
  279. mAllocSize = 0;
  280. mSize = 0;
  281. }*/
  282. T GetSafe(intptr idx)
  283. {
  284. if ((idx < 0) || (idx >= mSize))
  285. return T();
  286. return mVals[idx];
  287. }
  288. T GetLastSafe()
  289. {
  290. if (mSize == 0)
  291. return T();
  292. return mVals[mSize - 1];
  293. }
  294. T GetFirstSafe()
  295. {
  296. if (mSize == 0)
  297. return T();
  298. return mVals[0];
  299. }
  300. bool Contains(const T& val)
  301. {
  302. for (intptr i = 0; i < mSize; i++)
  303. if (mVals[i] == val)
  304. return true;
  305. return false;
  306. }
  307. intptr IndexOf(const T& val)
  308. {
  309. for (intptr i = 0; i < mSize; i++)
  310. if (mVals[i] == val)
  311. return i;
  312. return -1;
  313. }
  314. intptr IndexOf(const T& val, int startIdx)
  315. {
  316. for (intptr i = startIdx; i < mSize; i++)
  317. if (mVals[i] == val)
  318. return i;
  319. return -1;
  320. }
  321. intptr LastIndexOf(const T& val)
  322. {
  323. for (intptr i = mSize - 1; i >= 0; i--)
  324. if (mVals[i] == val)
  325. return i;
  326. return -1;
  327. }
  328. void MoveTo(ArrayBase<T>& dest)
  329. {
  330. dest.mVals = this->mVals;
  331. dest.mSize = this->mSize;
  332. dest.mAllocSize = this->mAllocSize;
  333. this->mVals = NULL;
  334. this->mSize = 0;
  335. this->mAllocSize = 0;
  336. }
  337. void Sort(std::function<bool(const T&, const T&)> pred)
  338. {
  339. std::sort(this->begin(), this->end(), pred);
  340. }
  341. };
  342. // NON-POD
  343. template <typename T, typename TAlloc, bool TIsPod>
  344. class ArrayImpl : public ArrayBase<T, TAlloc>
  345. {
  346. public:
  347. typedef int int_cosize;
  348. protected:
  349. void MoveArray(T* to, T* from, intptr count)
  350. {
  351. if (to < from)
  352. {
  353. // Prefer in-order moves
  354. for (intptr i = 0; i < count; i++)
  355. new (&to[i]) T(std::move(from[i]));
  356. }
  357. else
  358. {
  359. for (intptr i = count - 1; i >= 0; i--)
  360. new (&to[i]) T(std::move(from[i]));
  361. }
  362. }
  363. void SetBufferSize(intptr newSize)
  364. {
  365. T* newVals = TAlloc::allocate(newSize);
  366. if (this->mVals != NULL)
  367. {
  368. if (this->mSize > 0)
  369. MoveArray(newVals, this->mVals, this->mSize);
  370. TAlloc::deallocate(this->mVals);
  371. }
  372. this->mVals = newVals;
  373. this->mAllocSize = (int_cosize)newSize;
  374. }
  375. void EnsureFree(intptr freeCount)
  376. {
  377. if (this->mSize + freeCount > this->mAllocSize)
  378. SetBufferSize(std::max(this->mAllocSize + this->mAllocSize / 2 + 1, this->mSize + freeCount));
  379. }
  380. public:
  381. using ArrayBase<T, TAlloc>::ArrayBase;
  382. ArrayImpl() : ArrayBase<T, TAlloc>()
  383. {
  384. }
  385. ArrayImpl(const ArrayImpl& val)
  386. {
  387. this->mVals = NULL;
  388. this->mSize = 0;
  389. this->mAllocSize = 0;
  390. *this = val;
  391. }
  392. ArrayImpl(ArrayImpl&& val) : ArrayBase<T, TAlloc>(std::move(val))
  393. {
  394. }
  395. ~ArrayImpl()
  396. {
  397. for (intptr i = 0; i < this->mSize; i++)
  398. this->mVals[i].~T(); //-V595
  399. if (this->mVals != NULL)
  400. {
  401. TAlloc::deallocate(this->mVals);
  402. }
  403. }
  404. void Resize(intptr size)
  405. {
  406. while (size < this->mSize)
  407. pop_back();
  408. if (size > this->mSize)
  409. {
  410. Reserve(size);
  411. while (size > this->mSize)
  412. new (&this->mVals[this->mSize++]) T();
  413. }
  414. }
  415. void Reserve(intptr size)
  416. {
  417. if (size > this->mAllocSize)
  418. SetBufferSize(size);
  419. }
  420. void SetSize(intptr size)
  421. {
  422. if (size > this->mAllocSize)
  423. SetBufferSize(size);
  424. this->mSize = (int_cosize)size;
  425. }
  426. void Clear()
  427. {
  428. for (intptr i = 0; i < this->mSize; i++)
  429. this->mVals[i].~T();
  430. this->mSize = 0;
  431. }
  432. void Dispose()
  433. {
  434. Clear();
  435. delete this->mVals;
  436. this->mVals = NULL;
  437. this->mAllocSize = 0;
  438. }
  439. ArrayImpl& operator=(const ArrayImpl& val)
  440. {
  441. if (&val == this)
  442. return *this;
  443. for (intptr i = 0; i < this->mSize; i++)
  444. this->mVals[i].~T();
  445. this->mSize = 0;
  446. if (val.mSize > this->mAllocSize)
  447. SetBufferSize(val.mSize);
  448. Resize(val.mSize);
  449. for (intptr i = 0; i < val.mSize; i++)
  450. new (&this->mVals[i]) T(val.mVals[i]);
  451. this->mSize = val.mSize;
  452. return *this;
  453. }
  454. ArrayImpl& operator=(ArrayImpl&& val)
  455. {
  456. if (this->mVals != NULL)
  457. {
  458. for (intptr i = 0; i < this->mSize; i++)
  459. this->mVals[i].~T();
  460. TAlloc::deallocate(this->mVals);
  461. }
  462. this->mVals = val.mVals;
  463. this->mSize = val.mSize;
  464. this->mAllocSize = val.mAllocSize;
  465. val.mVals = NULL;
  466. return *this;
  467. }
  468. void RemoveAt(intptr idx)
  469. {
  470. BF_ASSERT((uintptr)idx < (uintptr)this->mSize);
  471. this->mVals[idx].~T();
  472. // If we're removing the last element then we don't have to move anything
  473. if (idx != this->mSize - 1)
  474. {
  475. intptr moveCount = this->mSize - idx - 1;
  476. MoveArray(this->mVals + idx, this->mVals + idx + 1, moveCount);
  477. }
  478. this->mSize--;
  479. }
  480. // 'Fast' because it's allowed to change item order
  481. void RemoveAtFast(intptr idx)
  482. {
  483. BF_ASSERT((uintptr)idx < (uintptr)this->mSize);
  484. this->mVals[idx].~T();
  485. // If we're removing the last element then we don't have to move anything
  486. if (idx != this->mSize - 1)
  487. {
  488. new (&this->mVals[idx]) T(std::move(this->mVals[this->mSize - 1]));
  489. }
  490. this->mSize--;
  491. }
  492. void RemoveRange(intptr idx, intptr length)
  493. {
  494. BF_ASSERT(
  495. ((uintptr)idx < (uintptr)this->mSize) &&
  496. ((uintptr)length > 0) &&
  497. ((uintptr)(idx + length) <= (uintptr)this->mSize));
  498. for (intptr i = idx; i < idx + length; i++)
  499. this->mVals[i].~T();
  500. // If we're removing the last element then we don't have to move anything
  501. if (idx != this->mSize - length)
  502. {
  503. intptr moveCount = this->mSize - idx - length;
  504. MoveArray(this->mVals + idx, this->mVals + idx + length, moveCount);
  505. }
  506. this->mSize -= (int_cosize)length;
  507. }
  508. void Insert(intptr idx, const T& val)
  509. {
  510. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  511. if (this->mSize >= this->mAllocSize)
  512. {
  513. intptr newSize = this->mAllocSize + this->mAllocSize / 2 + 1;
  514. T* newVals = TAlloc::allocate(newSize);
  515. if (this->mVals != NULL)
  516. {
  517. if (idx > 0) // Copy left of idx
  518. MoveArray(newVals, this->mVals, idx);
  519. if (idx < this->mSize) // Copy right of idx
  520. MoveArray(newVals + idx + 1, this->mVals + idx, this->mSize - idx);
  521. TAlloc::deallocate(this->mVals);
  522. }
  523. this->mVals = newVals;
  524. this->mAllocSize = (int_cosize)newSize;
  525. }
  526. else if (idx != this->mSize)
  527. {
  528. intptr moveCount = this->mSize - idx;
  529. MoveArray(this->mVals + idx + 1, this->mVals + idx, moveCount);
  530. }
  531. new (&this->mVals[idx]) T(val);
  532. this->mSize++;
  533. }
  534. void Insert(intptr idx, T* vals, intptr size)
  535. {
  536. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  537. if (this->mSize + size > this->mAllocSize)
  538. {
  539. intptr newSize = BF_MAX(this->mSize + size, this->mAllocSize + this->mAllocSize / 2 + 1);
  540. T* newVals = TAlloc::allocate(newSize);
  541. if (this->mVals != NULL)
  542. {
  543. if (idx > 0) // Copy left of idx
  544. MoveArray(newVals, this->mVals, idx);
  545. if (idx < this->mSize) // Copy right of idx
  546. MoveArray(newVals + idx + size, this->mVals + idx, this->mSize - idx);
  547. TAlloc::deallocate(this->mVals);
  548. }
  549. this->mVals = newVals;
  550. this->mAllocSize = (int_cosize)newSize;
  551. }
  552. else if (idx != this->mSize)
  553. {
  554. intptr moveCount = this->mSize - idx;
  555. MoveArray(this->mVals + idx + size, this->mVals + idx, moveCount);
  556. }
  557. for (intptr i = 0; i < size; i++)
  558. new (&this->mVals[idx + i]) T(vals[i]);
  559. this->mSize += (int_cosize)size;
  560. }
  561. void Insert(intptr idx, const T& val, intptr count)
  562. {
  563. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  564. if (this->mSize + count > this->mAllocSize)
  565. {
  566. intptr newSize = BF_MAX(this->mSize + count, this->mAllocSize + this->mAllocSize / 2 + 1);
  567. T* newVals = TAlloc::allocate(newSize);
  568. if (this->mVals != NULL)
  569. {
  570. if (idx > 0) // Copy left of idx
  571. MoveArray(this->newVals, this->mVals, idx);
  572. if (idx < this->mSize) // Copy right of idx
  573. MoveArray(newVals + idx + count, this->mVals + idx, this->mSize - idx);
  574. TAlloc::deallocate(this->mVals);
  575. }
  576. this->mVals = newVals;
  577. this->mAllocSize = (int_cosize)newSize;
  578. }
  579. else if (idx != this->mSize)
  580. {
  581. intptr moveCount = this->mSize - idx;
  582. MoveArray(this->mVals + idx + count, this->mVals + idx, moveCount);
  583. }
  584. for (intptr i = 0; i < count; i++)
  585. new (&this->mVals[idx + i]) T(val);
  586. this->mSize += (int_cosize)count;
  587. }
  588. bool Remove(const T& val)
  589. {
  590. for (intptr i = 0; i < this->mSize; i++)
  591. {
  592. if (this->mVals[i] == val)
  593. {
  594. RemoveAt(i);
  595. return true;
  596. }
  597. }
  598. return false;
  599. }
  600. typename ArrayBase<T, TAlloc>::iterator erase(typename ArrayBase<T, TAlloc>::iterator itr)
  601. {
  602. RemoveAt(itr.mPtr - this->mVals);
  603. return itr;
  604. }
  605. void push_back(const T& val)
  606. {
  607. if (this->mSize >= this->mAllocSize)
  608. SetBufferSize(this->mAllocSize + this->mAllocSize / 2 + 1);
  609. new (&this->mVals[this->mSize++]) T(val);
  610. }
  611. void pop_back()
  612. {
  613. BF_ASSERT(this->mSize > 0);
  614. this->mVals[this->mSize - 1].~T();
  615. --this->mSize;
  616. }
  617. void Add(const T& val)
  618. {
  619. if (this->mSize >= this->mAllocSize)
  620. SetBufferSize(this->mAllocSize + this->mAllocSize / 2 + 1);
  621. new (&this->mVals[this->mSize++]) T(val);
  622. }
  623. };
  624. // POD
  625. template <typename T, typename TAlloc>
  626. class ArrayImpl<T, TAlloc, true> : public ArrayBase<T, TAlloc>
  627. {
  628. public:
  629. typedef int int_cosize;
  630. protected:
  631. void SetBufferSize(intptr newSize)
  632. {
  633. T* newVals = TAlloc::allocate(newSize);
  634. if (this->mVals != NULL)
  635. {
  636. if (this->mSize > 0)
  637. memcpy(newVals, this->mVals, this->mSize * sizeof(T));
  638. TAlloc::deallocate(this->mVals);
  639. }
  640. this->mVals = newVals;
  641. this->mAllocSize = (int_cosize)newSize;
  642. }
  643. void EnsureFree(intptr freeCount)
  644. {
  645. if (this->mSize + freeCount > this->mAllocSize)
  646. SetBufferSize(std::max(this->mAllocSize + this->mAllocSize / 2 + 1, this->mSize + freeCount));
  647. }
  648. public:
  649. using ArrayBase<T, TAlloc>::ArrayBase;
  650. ArrayImpl() : ArrayBase<T, TAlloc>::ArrayBase()
  651. {
  652. }
  653. ArrayImpl(const ArrayImpl& val)
  654. {
  655. this->mVals = NULL;
  656. this->mSize = 0;
  657. this->mAllocSize = 0;
  658. *this = val;
  659. }
  660. ArrayImpl(ArrayImpl&& val) : ArrayBase<T, TAlloc>(std::move(val))
  661. {
  662. }
  663. ~ArrayImpl()
  664. {
  665. if (this->mVals != NULL)
  666. {
  667. TAlloc::deallocate(this->mVals);
  668. }
  669. }
  670. ArrayImpl& operator=(const ArrayImpl& val)
  671. {
  672. if (&val == this)
  673. return *this;
  674. this->mSize = 0;
  675. if (val.mSize > this->mAllocSize)
  676. SetBufferSize(val.mSize);
  677. memcpy(this->mVals, val.mVals, val.mSize * sizeof(T));
  678. this->mSize = val.mSize;
  679. return *this;
  680. }
  681. void Resize(intptr size)
  682. {
  683. if (size < this->mSize)
  684. this->mSize = (int_cosize)size;
  685. else if (size > this->mSize)
  686. {
  687. Reserve(size);
  688. while (size > this->mSize)
  689. this->mVals[this->mSize++] = T();
  690. }
  691. }
  692. void ResizeRaw(intptr size)
  693. {
  694. if (size < this->mSize)
  695. this->mSize = (int_cosize)size;
  696. else if (size > this->mSize)
  697. {
  698. Reserve(size);
  699. this->mSize = (int_cosize)size;
  700. }
  701. }
  702. void Reserve(intptr size)
  703. {
  704. if (size > this->mAllocSize)
  705. SetBufferSize(size);
  706. }
  707. void SetSize(intptr size)
  708. {
  709. if (size > this->mAllocSize)
  710. SetBufferSize(size);
  711. this->mSize = (int_cosize)size;
  712. }
  713. void Clear()
  714. {
  715. this->mSize = 0;
  716. }
  717. void TrimExcess()
  718. {
  719. if (this->mSize > this->mAllocSize)
  720. SetBufferSize(this->mSize);
  721. }
  722. void Dispose()
  723. {
  724. Clear();
  725. delete this->mVals;
  726. this->mVals = NULL;
  727. this->mAllocSize = 0;
  728. }
  729. void RemoveAt(intptr idx)
  730. {
  731. BF_ASSERT((uintptr)idx < (uintptr)this->mSize);
  732. // If we're removing the last element then we don't have to move anything
  733. if (idx != this->mSize - 1)
  734. {
  735. intptr moveCount = this->mSize - idx - 1;
  736. memmove(this->mVals + idx, this->mVals + idx + 1, moveCount * sizeof(T));
  737. }
  738. this->mSize--;
  739. }
  740. // 'Fast' because it's allowed to change item order
  741. void RemoveAtFast(intptr idx)
  742. {
  743. BF_ASSERT((uintptr)idx < (uintptr)this->mSize);
  744. // If we're removing the last element then we don't have to move anything
  745. if (idx != this->mSize - 1)
  746. {
  747. this->mVals[idx] = this->mVals[this->mSize - 1];
  748. }
  749. this->mSize--;
  750. }
  751. void RemoveRange(intptr idx, intptr length)
  752. {
  753. BF_ASSERT(
  754. ((uintptr)idx < (uintptr)this->mSize) &&
  755. ((uintptr)length > 0) &&
  756. ((uintptr)(idx + length) <= (uintptr)this->mSize));
  757. // If we're removing the last element then we don't have to move anything
  758. if (idx != this->mSize - length)
  759. {
  760. intptr moveCount = this->mSize - idx - length;
  761. memmove(this->mVals + idx, this->mVals + idx + length, moveCount * sizeof(T));
  762. }
  763. this->mSize -= (int_cosize)length;
  764. }
  765. void Insert(intptr idx, const T& val)
  766. {
  767. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  768. if (this->mSize >= this->mAllocSize)
  769. {
  770. intptr newSize = this->mAllocSize + this->mAllocSize / 2 + 1;
  771. T* newVals = TAlloc::allocate(newSize);
  772. if (this->mVals != NULL)
  773. {
  774. if (idx > 0) // Copy left of idx
  775. memmove(newVals, this->mVals, idx * sizeof(T));
  776. if (idx < this->mSize) // Copy right of idx
  777. memmove(newVals + idx + 1, this->mVals + idx, (this->mSize - idx) * sizeof(T));
  778. TAlloc::deallocate(this->mVals);
  779. }
  780. this->mVals = newVals;
  781. this->mAllocSize = (int_cosize)newSize;
  782. }
  783. else if (idx != this->mSize)
  784. {
  785. intptr moveCount = this->mSize - idx;
  786. memmove(this->mVals + idx + 1, this->mVals + idx, moveCount * sizeof(T));
  787. }
  788. this->mVals[idx] = val;
  789. this->mSize++;
  790. }
  791. void Insert(intptr idx, T* vals, intptr size)
  792. {
  793. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  794. if (this->mSize + size > this->mAllocSize)
  795. {
  796. intptr newSize = BF_MAX(this->mSize + size, this->mAllocSize + this->mAllocSize / 2 + 1);
  797. T* newVals = TAlloc::allocate(newSize);
  798. if (this->mVals != NULL)
  799. {
  800. if (idx > 0) // Copy left of idx
  801. memmove(newVals, this->mVals, idx * sizeof(T));
  802. if (idx < this->mSize) // Copy right of idx
  803. memmove(newVals + idx + size, this->mVals + idx, (this->mSize - idx) * sizeof(T));
  804. TAlloc::deallocate(this->mVals);
  805. }
  806. this->mVals = newVals;
  807. this->mAllocSize = (int_cosize)newSize;
  808. }
  809. else if (idx != this->mSize)
  810. {
  811. intptr moveCount = this->mSize - idx;
  812. memmove(this->mVals + idx + size, this->mVals + idx, moveCount * sizeof(T));
  813. }
  814. for (intptr i = 0; i < size; i++)
  815. this->mVals[idx + i] = vals[i];
  816. this->mSize += (int_cosize)size;
  817. }
  818. void Insert(intptr idx, const T& val, intptr size)
  819. {
  820. BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
  821. if (this->mSize + size > this->mAllocSize)
  822. {
  823. intptr newSize = BF_MAX(this->mSize + size, this->mAllocSize + this->mAllocSize / 2 + 1);
  824. T* newVals = TAlloc::allocate(newSize);
  825. if (this->mVals != NULL)
  826. {
  827. if (idx > 0) // Copy left of idx
  828. memmove(newVals, this->mVals, idx * sizeof(T));
  829. if (idx < this->mSize) // Copy right of idx
  830. memmove(newVals + idx + size, this->mVals + idx, (this->mSize - idx) * sizeof(T));
  831. TAlloc::deallocate(this->mVals);
  832. }
  833. this->mVals = newVals;
  834. this->mAllocSize = (int_cosize)newSize;
  835. }
  836. else if (idx != this->mSize)
  837. {
  838. intptr moveCount = this->mSize - idx;
  839. memmove(this->mVals + idx + size, this->mVals + idx, moveCount * sizeof(T));
  840. }
  841. for (intptr i = 0; i < size; i++)
  842. this->mVals[idx + i] = val;
  843. this->mSize += (int_cosize)size;
  844. }
  845. bool Remove(const T& val)
  846. {
  847. for (intptr i = 0; i < this->mSize; i++)
  848. {
  849. if (this->mVals[i] == val)
  850. {
  851. RemoveAt(i);
  852. return true;
  853. }
  854. }
  855. return false;
  856. }
  857. bool RemoveAll(const T& val)
  858. {
  859. bool found = false;
  860. for (intptr i = 0; i < this->mSize; i++)
  861. {
  862. if (this->mVals[i] == val)
  863. {
  864. found = true;
  865. RemoveAt(i);
  866. i--;
  867. }
  868. }
  869. return found;
  870. }
  871. typename ArrayBase<T, TAlloc>::iterator erase(typename ArrayBase<T, TAlloc>::iterator itr)
  872. {
  873. RemoveAt(itr.mPtr - this->mVals);
  874. return itr;
  875. }
  876. void push_back(const T& val)
  877. {
  878. if (this->mSize >= this->mAllocSize)
  879. SetBufferSize(this->mAllocSize + this->mAllocSize / 2 + 1);
  880. this->mVals[this->mSize++] = val;
  881. }
  882. void pop_back()
  883. {
  884. BF_ASSERT(this->mSize > 0);
  885. --this->mSize;
  886. }
  887. void Add(const T& val)
  888. {
  889. if (this->mSize >= this->mAllocSize)
  890. SetBufferSize(this->mAllocSize + this->mAllocSize / 2 + 1);
  891. this->mVals[this->mSize++] = val;
  892. }
  893. };
  894. template <typename T, typename TAlloc = AllocatorCLib<T> >
  895. class Array : public ArrayImpl<T, TAlloc, std::is_pod<T>::value>
  896. {
  897. public:
  898. typedef ArrayImpl<T, TAlloc, std::is_pod<T>::value> _ArrayImpl;
  899. using ArrayImpl<T, TAlloc, std::is_pod<T>::value>::ArrayImpl;
  900. using _ArrayImpl::operator=;
  901. using _ArrayImpl::operator==;
  902. using _ArrayImpl::operator!=;
  903. Array() : _ArrayImpl()
  904. {
  905. }
  906. Array(const Array& val)
  907. {
  908. this->mVals = NULL;
  909. this->mSize = 0;
  910. this->mAllocSize = 0;
  911. *this = val;
  912. }
  913. Array(Array&& val) : _ArrayImpl(std::move(val))
  914. {
  915. }
  916. _ArrayImpl& operator=(const Array& val)
  917. {
  918. return _ArrayImpl::operator=(val);
  919. }
  920. _ArrayImpl& operator=(Array&& val)
  921. {
  922. return _ArrayImpl::operator=(val);
  923. }
  924. };
  925. NS_BF_END;
  926. namespace std
  927. {
  928. template<typename T>
  929. struct hash<Beefy::Array<T> >
  930. {
  931. size_t operator()(const Beefy::Array<T>& val) const
  932. {
  933. return HashBytes((const uint8*)val.mVals, sizeof(T) * val.mSize);
  934. }
  935. };
  936. }