2
0

Deque.h 21 KB

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