Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. #pragma once
  2. #ifndef SPARKLYVECTOR_H
  3. #define SPARKLYVECTOR_H
  4. #include <memory>
  5. #include <stdexcept>
  6.  
  7. #pragma pack(push,_CRT_PACKING)
  8. #pragma warning(push,3)
  9. #pragma push_macro("new")
  10. #undef new
  11.  
  12. #pragma warning(disable: 4127)
  13. #pragma warning(disable: 4244)
  14.  
  15. namespace sp
  16. {
  17. template<class T>
  18. class _citr : public _Iterator012<random_access_iterator_tag, typename T::value_type, typename T::difference_type, typename T::const_pointer, typename T::const_reference, _Iterator_base>
  19. {
  20. public:
  21. typedef _citr<T> _Myiter;
  22. typedef random_access_iterator_tag iterator_category;
  23. typedef typename T::value_type value_type;
  24. typedef typename T::difference_type difference_type;
  25. typedef typename T::const_pointer pointer;
  26. typedef typename T::const_reference reference;
  27. typedef typename T::pointer _Tptr;
  28.  
  29. _citr() : data()
  30. {
  31. }
  32.  
  33. _citr(_Tptr _Parg, const _Container_base *_Pvector) : data(_Parg)
  34. {
  35. this->_Adopt(_Pvector);
  36. }
  37.  
  38. typedef pointer _Unchecked_type;
  39.  
  40. T& _Rechecked(_Unchecked_type _Right)
  41. {
  42. this->data = (_Tptr)_Right;
  43. return (*this);
  44. }
  45.  
  46. _Unchecked_type _Unchecked() const
  47. {
  48. return (_Unchecked_type(this->data));
  49. }
  50.  
  51. reference operator*() const
  52. {
  53. return (*this->data);
  54. }
  55.  
  56. pointer operator->() const
  57. {
  58. return (_STD pointer_traits<pointer>::pointer_to(**this));
  59. }
  60.  
  61. _Myiter& operator++()
  62. {
  63. ++this->data;
  64. return (*this);
  65. }
  66.  
  67. _Myiter operator++(int)
  68. {
  69. _Myiter _Tmp = *this;
  70. ++*this;
  71. return (_Tmp);
  72. }
  73.  
  74. _Myiter& operator--()
  75. {
  76. --this->data;
  77. return (*this);
  78. }
  79.  
  80. _Myiter operator--(int)
  81. {
  82. _Myiter _Tmp = *this;
  83. --*this;
  84. return (_Tmp);
  85. }
  86.  
  87. _Myiter& operator+=(difference_type _Off)
  88. {
  89. data += _Off;
  90. return (*this);
  91. }
  92.  
  93. _Myiter operator+(difference_type _Off) const
  94. {
  95. _Myiter _Tmp = *this;
  96. return (_Tmp += _Off);
  97. }
  98.  
  99. _Myiter& operator-=(difference_type _Off)
  100. {
  101. return (*this += -_Off);
  102. }
  103.  
  104. _Myiter operator-(difference_type _Off) const
  105. {
  106. _Myiter _Tmp = *this;
  107. return (_Tmp -= _Off);
  108. }
  109.  
  110. difference_type operator-(const _Myiter& _Right) const
  111. {
  112. return (this->data - _Right.data);
  113. }
  114.  
  115. reference operator[](difference_type _Off) const
  116. {
  117. return (*(*this + _Off));
  118. }
  119.  
  120. bool operator==(const _Myiter& _Right) const
  121. {
  122. return (this->data == _Right.data);
  123. }
  124.  
  125. bool operator!=(const _Myiter& _Right) const
  126. {
  127. return (!(*this == _Right));
  128. }
  129.  
  130. bool operator<(const _Myiter& _Right) const
  131. {
  132. return (this->data < _Right.data);
  133. }
  134.  
  135. bool operator>(const _Myiter& _Right) const
  136. {
  137. return (_Right < *this);
  138. }
  139.  
  140. bool operator<=(const _Myiter& _Right) const
  141. {
  142. return (!(_Right < *this));
  143. }
  144.  
  145. bool operator>=(const _Myiter& _Right) const
  146. {
  147. return (!(*this < _Right));
  148. }
  149.  
  150. void _Compat(const _Myiter&) const
  151. {
  152. }
  153.  
  154. _Tptr data;
  155. };
  156.  
  157.  
  158. /*
  159. template<class T>
  160. inline typename _citr<T>::_Unchecked_type _Unchecked(_citr<T> _Iter)
  161. {
  162. return (_Iter._Unchecked());
  163. }
  164.  
  165. template<class T>
  166. inline _citr<T>& _Rechecked(_citr<T>& _Iter, typename _citr<T>::_Unchecked_type _Right)
  167. {
  168. return (_Iter._Rechecked(_Right));
  169. }
  170.  
  171. template<class T>
  172. inline _citr<T> operator+(typename _citr<T>::difference_type _Off, _citr<T> _Next)
  173. {
  174. return (_Next += _Off);
  175. }
  176. */
  177.  
  178. template<class T>
  179. class _itr : public _citr < T >
  180. {
  181. public:
  182.  
  183. typedef _itr<T> _Myiter;
  184. typedef _citr<T> _Mybase;
  185. typedef random_access_iterator_tag iterator_category;
  186.  
  187. typedef typename T::value_type value_type;
  188. typedef typename T::difference_type difference_type;
  189. typedef typename T::pointer pointer;
  190. typedef typename T::reference reference;
  191.  
  192. _itr()
  193. {
  194. }
  195.  
  196. _itr(pointer _Parg, const _Container_base *_Pvector) : _Mybase(_Parg, _Pvector)
  197. {
  198. }
  199.  
  200. typedef pointer _Unchecked_type;
  201.  
  202. _Myiter& _Rechecked(_Unchecked_type _Right)
  203. {
  204. this->data = _Right;
  205. return (*this);
  206. }
  207.  
  208. _Unchecked_type _Unchecked() const
  209. {
  210. return (_Unchecked_type(this->data));
  211. }
  212.  
  213. reference operator*() const
  214. {
  215. return ((reference)**(_Mybase *)this);
  216. }
  217.  
  218. pointer operator->() const
  219. {
  220. return (_STD pointer_traits<pointer>::pointer_to(**this));
  221. }
  222.  
  223. _Myiter& operator++()
  224. {
  225. ++*(_Mybase *)this;
  226. return (*this);
  227. }
  228.  
  229. _Myiter operator++(int)
  230. {
  231. _Myiter _Tmp = *this;
  232. ++*this;
  233. return (_Tmp);
  234. }
  235.  
  236. _Myiter& operator--()
  237. {
  238. --*(_Mybase *)this;
  239. return (*this);
  240. }
  241.  
  242. _Myiter operator--(int)
  243. {
  244. _Myiter _Tmp = *this;
  245. --*this;
  246. return (_Tmp);
  247. }
  248.  
  249. _Myiter& operator+=(difference_type _Off)
  250. {
  251. *(_Mybase *)this += _Off;
  252. return (*this);
  253. }
  254.  
  255. _Myiter operator+(difference_type _Off) const
  256. {
  257. _Myiter _Tmp = *this;
  258. return (_Tmp += _Off);
  259. }
  260.  
  261. _Myiter& operator-=(difference_type _Off)
  262. {
  263. return (*this += -_Off);
  264. }
  265.  
  266. _Myiter operator-(difference_type _Off) const
  267. {
  268. _Myiter _Tmp = *this;
  269. return (_Tmp -= _Off);
  270. }
  271.  
  272. difference_type operator-(const _Mybase& _Right) const {
  273. return (*(_Mybase *)this - _Right);
  274. }
  275.  
  276. reference operator[](difference_type _Off) const {
  277. return (*(*this + _Off));
  278. }
  279. };
  280.  
  281. /*
  282. template<class T>
  283. inline typename _itr<T>::_Unchecked_type _Unchecked(_itr<T> _Iter) {
  284. return (_Iter._Unchecked());
  285. }
  286.  
  287. template<class T>
  288. inline _itr<T>& _Rechecked(_itr<T>& _Iter, typename _itr<T> ::_Unchecked_type _Right) {
  289. return (_Iter._Rechecked(_Right));
  290. }
  291.  
  292. template<class T>
  293. inline _itr<T> operator+(typename _itr<T>::difference_type _Off, _itr<T> _Next) {
  294. return (_Next += _Off);
  295. }
  296. */
  297.  
  298. template<class _Value_type, class _Size_type, class _Difference_type, class _Pointer, class _Const_pointer, class _Reference, class _Const_reference>
  299.  
  300. struct _itr_types
  301. {
  302. typedef _Value_type value_type;
  303. typedef _Size_type size_type;
  304. typedef _Difference_type difference_type;
  305. typedef _Pointer pointer;
  306. typedef _Const_pointer const_pointer;
  307. typedef _Reference reference;
  308. typedef _Const_reference const_reference;
  309. };
  310.  
  311. template<class _Ty, class _Alloc0>
  312. struct _vbasetypes
  313. {
  314. typedef _Alloc0 _Alloc;
  315. typedef _vbasetypes<_Ty, _Alloc> _Myt;
  316. typedef _Wrap_alloc<_Alloc> _Alty0;
  317. typedef typename _Alty0::template rebind<_Ty>::other _Alty;
  318.  
  319. typedef typename _Alty::pointer _Tptr;
  320. typedef typename _Alty::template rebind<_Tptr>::other _Alpty;
  321.  
  322. typedef typename _If<_Is_simple_alloc<_Alty>::value, _Simple_types<typename _Alty::value_type>,
  323. _itr_types<typename _Alty::value_type, typename _Alty::size_type, typename _Alty::difference_type, typename _Alty::pointer, typename _Alty::const_pointer, typename _Alty::reference, typename _Alty::const_reference> >::type _Val_types;
  324. };
  325.  
  326. template<class T>
  327. class _vecvalue : public _Container_base
  328. {
  329. public:
  330. typedef _vecvalue<T> _Myt;
  331.  
  332. typedef typename T::value_type value_type;
  333. typedef typename T::size_type size_type;
  334. typedef typename T::difference_type difference_type;
  335. typedef typename T::pointer pointer;
  336. typedef typename T::const_pointer const_pointer;
  337. typedef typename T::reference reference;
  338. typedef typename T::const_reference const_reference;
  339.  
  340. typedef _itr<_Myt> iterator;
  341. typedef _citr<_Myt> const_iterator;
  342.  
  343. _vecvalue()
  344. {
  345. }
  346. };
  347.  
  348. template<bool _Al_has_storage, class _Alloc_types>
  349. class _calloc : public _vecvalue <typename _Alloc_types::_Val_types>
  350. {
  351. public:
  352. typename _Alloc_types::_Alty _Alval;
  353. typedef _calloc<_Al_has_storage, _Alloc_types> _Myt;
  354. typedef typename _Alloc_types::_Alloc _Alloc;
  355. typedef typename _Alloc_types::_Alty _Alty;
  356.  
  357. _calloc(const _Alloc& _Al = _Alloc()) : _Alval(_Al)
  358. {
  359. }
  360.  
  361. void _Change_alloc(const _Alty& _Al)
  362. {
  363. this->_Alval = _Al;
  364. }
  365.  
  366. void _Swap_alloc(_Myt& _Right)
  367. {
  368. _Swap_adl(this->_Alval, _Right._Alval);
  369. }
  370.  
  371. _Alty& _Getal()
  372. {
  373. return (this->_Alval);
  374. }
  375.  
  376. const _Alty& _Getal() const
  377. {
  378. return (this->_Alval);
  379. }
  380. };
  381.  
  382. template<class _Alloc_types>
  383. class _calloc<false, _Alloc_types> : public _vecvalue<typename _Alloc_types::_Val_types>
  384. {
  385. public:
  386. typedef _calloc<false, _Alloc_types> _Myt;
  387. typedef typename _Alloc_types::_Alloc _Alloc;
  388. typedef typename _Alloc_types::_Alty _Alty;
  389.  
  390. _calloc(const _Alloc& = _Alloc())
  391. {
  392. }
  393.  
  394. void _Change_alloc(const _Alty&)
  395. {
  396. }
  397.  
  398. void _Swap_alloc(_Myt&)
  399. {
  400. }
  401.  
  402. _Alty _Getal() const
  403. {
  404. return (_Alty());
  405. }
  406. };
  407.  
  408. template<class _Ty, class _Alloc = allocator<_Ty> >
  409. class cvec : public _calloc < !is_empty<_Alloc>::value, _vbasetypes<_Ty, _Alloc> >
  410. {
  411. public:
  412. typedef cvec<_Ty, _Alloc> _Myt;
  413. typedef _calloc < !is_empty<_Alloc>::value, _vbasetypes<_Ty, _Alloc> > _Mybase;
  414. typedef _Alloc allocator_type;
  415.  
  416. typedef typename _Mybase::_Alty _Alty;
  417.  
  418. typedef typename _Mybase::value_type value_type;
  419. typedef typename _Mybase::size_type size_type;
  420. typedef typename _Mybase::difference_type difference_type;
  421. typedef typename _Mybase::pointer pointer;
  422. typedef typename _Mybase::const_pointer const_pointer;
  423. typedef typename _Mybase::reference reference;
  424. typedef typename _Mybase::const_reference const_reference;
  425.  
  426. #define _VICONT(it) it._Getcont()
  427. #define _VIPTR(it) (it)._Ptr
  428.  
  429. typedef typename _Mybase::iterator iterator;
  430. typedef typename _Mybase::const_iterator const_iterator;
  431.  
  432. typedef _STD reverse_iterator<iterator> reverse_iterator;
  433. typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
  434.  
  435. pointer data;
  436.  
  437. cvec() : _Mybase()
  438. {
  439. data = pointer();
  440. _Mylast = pointer();
  441. _Myend = pointer();
  442. }
  443.  
  444.  
  445. explicit cvec (const _Alloc& _Al) : _Mybase(_Al)
  446. {
  447. data = pointer();
  448. _Mylast = pointer();
  449. _Myend = pointer();
  450. }
  451.  
  452. size_type size()
  453. {
  454. return this->_Mylast - this->data;
  455. }
  456.  
  457. size_type reserved()
  458. {
  459. return this->_Myend - this->_Mylast;
  460. }
  461.  
  462. size_type total()
  463. {
  464. return this->_Myend - this->data;
  465. }
  466.  
  467.  
  468. void push(value_type&& _Val)
  469. {
  470. if (this->_Mylast == this->_Myend) grow(growing_size(1));
  471. this->_Getal().construct(this->_Mylast, _STD forward<value_type>(_Val));
  472. this->_Mylast++;
  473. }
  474.  
  475. void push(const value_type& _Val)
  476. {
  477. if (this->_Mylast == this->_Myend) grow(growing_size(1));
  478. this->_Getal().construct(this->_Mylast, _Val);
  479. this->_Mylast++;
  480. }
  481.  
  482. template<class... _Valty>
  483. void push(size_type pos, _Valty&&... _Val)
  484. {
  485. if (this->_Mylast == this->_Myend) grow(growing_size(1));
  486. this->_Getal().construct(this->_Mylast, _STD forward<_Valty>(_Val)...);
  487. ++this->_Mylast;
  488. // emplace_back(_STD forward<_Valty>(_Val)...);
  489. _STD rotate(this->data + pos, this->_Mylast - 1, this->_Mylast);
  490. }
  491.  
  492. template<class... _Valty>
  493. void emplace_back(_Valty&&... _Val)
  494. {
  495. if (this->_Mylast == this->_Myend) grow(growing_size(1));
  496. this->_Getal().construct(this->_Mylast, _STD forward<_Valty>(_Val)...);
  497. ++this->_Mylast;
  498. }
  499.  
  500. size_type growing_size(size_type in)
  501. {
  502. return (total() / 2) + size() + in;
  503. }
  504.  
  505. size_type growing_size_n(size_type in)
  506. {
  507. return size() + reserved() + in;
  508. }
  509.  
  510. void grow(size_type newsize)
  511. {
  512. // pointer _Ptr = this->_Getal().allocate(newsize);
  513. pointer _Ptr = new _Ty[newsize];
  514.  
  515. _TRY_BEGIN
  516. _Umove(this->data, this->_Mylast, _Ptr);
  517. _CATCH_ALL
  518. // this->_Getal().deallocate(_Ptr, newsize);
  519. delete[] _Ptr;
  520. _RERAISE;
  521. _CATCH_END
  522.  
  523. if (this->data != pointer())
  524. {
  525. _Destroy(this->data, this->_Mylast);
  526. // this->_Getal().deallocate(this->data, this->_Myend - this->data);
  527. delete[] this->data;
  528. }
  529.  
  530. this->_Orphan_all();
  531. this->_Myend = _Ptr + newsize;
  532. this->_Mylast = _Ptr + size();
  533. this->data = _Ptr;
  534. }
  535.  
  536. const_reference operator[](size_type _Pos) const
  537. {
  538. return (*(this->data + _Pos));
  539. }
  540.  
  541. reference operator[](size_type _Pos)
  542. {
  543. return (*(this->data + _Pos));
  544. }
  545.  
  546. protected:
  547. pointer _Mylast;
  548. pointer _Myend;
  549.  
  550.  
  551. void _Destroy(pointer _First, pointer _Last)
  552. {
  553. _Alty _Alval(this->_Getal());
  554. _Destroy_range(_First, _Last, _Alval);
  555. }
  556.  
  557. template<class _Iter>
  558. pointer _Umove(_Iter _First, _Iter _Last, pointer _Ptr)
  559. {
  560. _Alty _Alval(this->_Getal());
  561. return (_Uninitialized_move(_First, _Last, _Ptr, _Alval));
  562. }
  563.  
  564. size_type _Unused_capacity() const _NOEXCEPT
  565. {
  566. return (this->_Myend - this->_Mylast);
  567. }
  568. };
  569. }
  570.  
  571. #pragma pop_macro("new")
  572. #pragma warning(pop)
  573. #pragma pack(pop)
  574. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement