Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #ifndef SPARKLYVECTOR_H
- #define SPARKLYVECTOR_H
- #include <memory>
- #include <stdexcept>
- #pragma pack(push,_CRT_PACKING)
- #pragma warning(push,3)
- #pragma push_macro("new")
- #undef new
- #pragma warning(disable: 4127)
- #pragma warning(disable: 4244)
- namespace sp
- {
- template<class T>
- 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>
- {
- public:
- typedef _citr<T> _Myiter;
- typedef random_access_iterator_tag iterator_category;
- typedef typename T::value_type value_type;
- typedef typename T::difference_type difference_type;
- typedef typename T::const_pointer pointer;
- typedef typename T::const_reference reference;
- typedef typename T::pointer _Tptr;
- _citr() : data()
- {
- }
- _citr(_Tptr _Parg, const _Container_base *_Pvector) : data(_Parg)
- {
- this->_Adopt(_Pvector);
- }
- typedef pointer _Unchecked_type;
- T& _Rechecked(_Unchecked_type _Right)
- {
- this->data = (_Tptr)_Right;
- return (*this);
- }
- _Unchecked_type _Unchecked() const
- {
- return (_Unchecked_type(this->data));
- }
- reference operator*() const
- {
- return (*this->data);
- }
- pointer operator->() const
- {
- return (_STD pointer_traits<pointer>::pointer_to(**this));
- }
- _Myiter& operator++()
- {
- ++this->data;
- return (*this);
- }
- _Myiter operator++(int)
- {
- _Myiter _Tmp = *this;
- ++*this;
- return (_Tmp);
- }
- _Myiter& operator--()
- {
- --this->data;
- return (*this);
- }
- _Myiter operator--(int)
- {
- _Myiter _Tmp = *this;
- --*this;
- return (_Tmp);
- }
- _Myiter& operator+=(difference_type _Off)
- {
- data += _Off;
- return (*this);
- }
- _Myiter operator+(difference_type _Off) const
- {
- _Myiter _Tmp = *this;
- return (_Tmp += _Off);
- }
- _Myiter& operator-=(difference_type _Off)
- {
- return (*this += -_Off);
- }
- _Myiter operator-(difference_type _Off) const
- {
- _Myiter _Tmp = *this;
- return (_Tmp -= _Off);
- }
- difference_type operator-(const _Myiter& _Right) const
- {
- return (this->data - _Right.data);
- }
- reference operator[](difference_type _Off) const
- {
- return (*(*this + _Off));
- }
- bool operator==(const _Myiter& _Right) const
- {
- return (this->data == _Right.data);
- }
- bool operator!=(const _Myiter& _Right) const
- {
- return (!(*this == _Right));
- }
- bool operator<(const _Myiter& _Right) const
- {
- return (this->data < _Right.data);
- }
- bool operator>(const _Myiter& _Right) const
- {
- return (_Right < *this);
- }
- bool operator<=(const _Myiter& _Right) const
- {
- return (!(_Right < *this));
- }
- bool operator>=(const _Myiter& _Right) const
- {
- return (!(*this < _Right));
- }
- void _Compat(const _Myiter&) const
- {
- }
- _Tptr data;
- };
- /*
- template<class T>
- inline typename _citr<T>::_Unchecked_type _Unchecked(_citr<T> _Iter)
- {
- return (_Iter._Unchecked());
- }
- template<class T>
- inline _citr<T>& _Rechecked(_citr<T>& _Iter, typename _citr<T>::_Unchecked_type _Right)
- {
- return (_Iter._Rechecked(_Right));
- }
- template<class T>
- inline _citr<T> operator+(typename _citr<T>::difference_type _Off, _citr<T> _Next)
- {
- return (_Next += _Off);
- }
- */
- template<class T>
- class _itr : public _citr < T >
- {
- public:
- typedef _itr<T> _Myiter;
- typedef _citr<T> _Mybase;
- typedef random_access_iterator_tag iterator_category;
- typedef typename T::value_type value_type;
- typedef typename T::difference_type difference_type;
- typedef typename T::pointer pointer;
- typedef typename T::reference reference;
- _itr()
- {
- }
- _itr(pointer _Parg, const _Container_base *_Pvector) : _Mybase(_Parg, _Pvector)
- {
- }
- typedef pointer _Unchecked_type;
- _Myiter& _Rechecked(_Unchecked_type _Right)
- {
- this->data = _Right;
- return (*this);
- }
- _Unchecked_type _Unchecked() const
- {
- return (_Unchecked_type(this->data));
- }
- reference operator*() const
- {
- return ((reference)**(_Mybase *)this);
- }
- pointer operator->() const
- {
- return (_STD pointer_traits<pointer>::pointer_to(**this));
- }
- _Myiter& operator++()
- {
- ++*(_Mybase *)this;
- return (*this);
- }
- _Myiter operator++(int)
- {
- _Myiter _Tmp = *this;
- ++*this;
- return (_Tmp);
- }
- _Myiter& operator--()
- {
- --*(_Mybase *)this;
- return (*this);
- }
- _Myiter operator--(int)
- {
- _Myiter _Tmp = *this;
- --*this;
- return (_Tmp);
- }
- _Myiter& operator+=(difference_type _Off)
- {
- *(_Mybase *)this += _Off;
- return (*this);
- }
- _Myiter operator+(difference_type _Off) const
- {
- _Myiter _Tmp = *this;
- return (_Tmp += _Off);
- }
- _Myiter& operator-=(difference_type _Off)
- {
- return (*this += -_Off);
- }
- _Myiter operator-(difference_type _Off) const
- {
- _Myiter _Tmp = *this;
- return (_Tmp -= _Off);
- }
- difference_type operator-(const _Mybase& _Right) const {
- return (*(_Mybase *)this - _Right);
- }
- reference operator[](difference_type _Off) const {
- return (*(*this + _Off));
- }
- };
- /*
- template<class T>
- inline typename _itr<T>::_Unchecked_type _Unchecked(_itr<T> _Iter) {
- return (_Iter._Unchecked());
- }
- template<class T>
- inline _itr<T>& _Rechecked(_itr<T>& _Iter, typename _itr<T> ::_Unchecked_type _Right) {
- return (_Iter._Rechecked(_Right));
- }
- template<class T>
- inline _itr<T> operator+(typename _itr<T>::difference_type _Off, _itr<T> _Next) {
- return (_Next += _Off);
- }
- */
- template<class _Value_type, class _Size_type, class _Difference_type, class _Pointer, class _Const_pointer, class _Reference, class _Const_reference>
- struct _itr_types
- {
- typedef _Value_type value_type;
- typedef _Size_type size_type;
- typedef _Difference_type difference_type;
- typedef _Pointer pointer;
- typedef _Const_pointer const_pointer;
- typedef _Reference reference;
- typedef _Const_reference const_reference;
- };
- template<class _Ty, class _Alloc0>
- struct _vbasetypes
- {
- typedef _Alloc0 _Alloc;
- typedef _vbasetypes<_Ty, _Alloc> _Myt;
- typedef _Wrap_alloc<_Alloc> _Alty0;
- typedef typename _Alty0::template rebind<_Ty>::other _Alty;
- typedef typename _Alty::pointer _Tptr;
- typedef typename _Alty::template rebind<_Tptr>::other _Alpty;
- typedef typename _If<_Is_simple_alloc<_Alty>::value, _Simple_types<typename _Alty::value_type>,
- _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;
- };
- template<class T>
- class _vecvalue : public _Container_base
- {
- public:
- typedef _vecvalue<T> _Myt;
- typedef typename T::value_type value_type;
- typedef typename T::size_type size_type;
- typedef typename T::difference_type difference_type;
- typedef typename T::pointer pointer;
- typedef typename T::const_pointer const_pointer;
- typedef typename T::reference reference;
- typedef typename T::const_reference const_reference;
- typedef _itr<_Myt> iterator;
- typedef _citr<_Myt> const_iterator;
- _vecvalue()
- {
- }
- };
- template<bool _Al_has_storage, class _Alloc_types>
- class _calloc : public _vecvalue <typename _Alloc_types::_Val_types>
- {
- public:
- typename _Alloc_types::_Alty _Alval;
- typedef _calloc<_Al_has_storage, _Alloc_types> _Myt;
- typedef typename _Alloc_types::_Alloc _Alloc;
- typedef typename _Alloc_types::_Alty _Alty;
- _calloc(const _Alloc& _Al = _Alloc()) : _Alval(_Al)
- {
- }
- void _Change_alloc(const _Alty& _Al)
- {
- this->_Alval = _Al;
- }
- void _Swap_alloc(_Myt& _Right)
- {
- _Swap_adl(this->_Alval, _Right._Alval);
- }
- _Alty& _Getal()
- {
- return (this->_Alval);
- }
- const _Alty& _Getal() const
- {
- return (this->_Alval);
- }
- };
- template<class _Alloc_types>
- class _calloc<false, _Alloc_types> : public _vecvalue<typename _Alloc_types::_Val_types>
- {
- public:
- typedef _calloc<false, _Alloc_types> _Myt;
- typedef typename _Alloc_types::_Alloc _Alloc;
- typedef typename _Alloc_types::_Alty _Alty;
- _calloc(const _Alloc& = _Alloc())
- {
- }
- void _Change_alloc(const _Alty&)
- {
- }
- void _Swap_alloc(_Myt&)
- {
- }
- _Alty _Getal() const
- {
- return (_Alty());
- }
- };
- template<class _Ty, class _Alloc = allocator<_Ty> >
- class cvec : public _calloc < !is_empty<_Alloc>::value, _vbasetypes<_Ty, _Alloc> >
- {
- public:
- typedef cvec<_Ty, _Alloc> _Myt;
- typedef _calloc < !is_empty<_Alloc>::value, _vbasetypes<_Ty, _Alloc> > _Mybase;
- typedef _Alloc allocator_type;
- typedef typename _Mybase::_Alty _Alty;
- typedef typename _Mybase::value_type value_type;
- typedef typename _Mybase::size_type size_type;
- typedef typename _Mybase::difference_type difference_type;
- typedef typename _Mybase::pointer pointer;
- typedef typename _Mybase::const_pointer const_pointer;
- typedef typename _Mybase::reference reference;
- typedef typename _Mybase::const_reference const_reference;
- #define _VICONT(it) it._Getcont()
- #define _VIPTR(it) (it)._Ptr
- typedef typename _Mybase::iterator iterator;
- typedef typename _Mybase::const_iterator const_iterator;
- typedef _STD reverse_iterator<iterator> reverse_iterator;
- typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
- pointer data;
- cvec() : _Mybase()
- {
- data = pointer();
- _Mylast = pointer();
- _Myend = pointer();
- }
- explicit cvec (const _Alloc& _Al) : _Mybase(_Al)
- {
- data = pointer();
- _Mylast = pointer();
- _Myend = pointer();
- }
- size_type size()
- {
- return this->_Mylast - this->data;
- }
- size_type reserved()
- {
- return this->_Myend - this->_Mylast;
- }
- size_type total()
- {
- return this->_Myend - this->data;
- }
- void push(value_type&& _Val)
- {
- if (this->_Mylast == this->_Myend) grow(growing_size(1));
- this->_Getal().construct(this->_Mylast, _STD forward<value_type>(_Val));
- this->_Mylast++;
- }
- void push(const value_type& _Val)
- {
- if (this->_Mylast == this->_Myend) grow(growing_size(1));
- this->_Getal().construct(this->_Mylast, _Val);
- this->_Mylast++;
- }
- template<class... _Valty>
- void push(size_type pos, _Valty&&... _Val)
- {
- if (this->_Mylast == this->_Myend) grow(growing_size(1));
- this->_Getal().construct(this->_Mylast, _STD forward<_Valty>(_Val)...);
- ++this->_Mylast;
- // emplace_back(_STD forward<_Valty>(_Val)...);
- _STD rotate(this->data + pos, this->_Mylast - 1, this->_Mylast);
- }
- template<class... _Valty>
- void emplace_back(_Valty&&... _Val)
- {
- if (this->_Mylast == this->_Myend) grow(growing_size(1));
- this->_Getal().construct(this->_Mylast, _STD forward<_Valty>(_Val)...);
- ++this->_Mylast;
- }
- size_type growing_size(size_type in)
- {
- return (total() / 2) + size() + in;
- }
- size_type growing_size_n(size_type in)
- {
- return size() + reserved() + in;
- }
- void grow(size_type newsize)
- {
- // pointer _Ptr = this->_Getal().allocate(newsize);
- pointer _Ptr = new _Ty[newsize];
- _TRY_BEGIN
- _Umove(this->data, this->_Mylast, _Ptr);
- _CATCH_ALL
- // this->_Getal().deallocate(_Ptr, newsize);
- delete[] _Ptr;
- _RERAISE;
- _CATCH_END
- if (this->data != pointer())
- {
- _Destroy(this->data, this->_Mylast);
- // this->_Getal().deallocate(this->data, this->_Myend - this->data);
- delete[] this->data;
- }
- this->_Orphan_all();
- this->_Myend = _Ptr + newsize;
- this->_Mylast = _Ptr + size();
- this->data = _Ptr;
- }
- const_reference operator[](size_type _Pos) const
- {
- return (*(this->data + _Pos));
- }
- reference operator[](size_type _Pos)
- {
- return (*(this->data + _Pos));
- }
- protected:
- pointer _Mylast;
- pointer _Myend;
- void _Destroy(pointer _First, pointer _Last)
- {
- _Alty _Alval(this->_Getal());
- _Destroy_range(_First, _Last, _Alval);
- }
- template<class _Iter>
- pointer _Umove(_Iter _First, _Iter _Last, pointer _Ptr)
- {
- _Alty _Alval(this->_Getal());
- return (_Uninitialized_move(_First, _Last, _Ptr, _Alval));
- }
- size_type _Unused_capacity() const _NOEXCEPT
- {
- return (this->_Myend - this->_Mylast);
- }
- };
- }
- #pragma pop_macro("new")
- #pragma warning(pop)
- #pragma pack(pop)
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement