SizedArray.h 21 KB

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