#define VT_TPL template #define VT vector namespace ft { /********************************************* * COPLIENS *********************************************/ // constructors ------------------------------ VT_TPL VT:: vector( const Allocator & alloc ) : _size(0) , _capacity(0) , _mem_ptr(NULL) , _allocator(alloc) { return; } VT_TPL VT:: vector( size_type n, const T & val, const Allocator & alloc ) : _size(0) , _capacity(0) , _mem_ptr(NULL) , _allocator(alloc) { assign(n, val); return; } VT_TPL template VT:: vector(InputIterator first, InputIterator last, const Allocator & alloc) : _size(0) , _capacity(0) , _mem_ptr(NULL) , _allocator(alloc) { assign(first, last); return; } // copy constructor -------------------------- VT_TPL VT:: vector( vector const & src ) { *this = src; return; } // destructors ------------------------------- VT_TPL VT:: ~vector() { return; } // operator= --------------------------------- VT_TPL VT & VT:: operator=( vector const & rhs ) { vector new_vector; //Base::operator=(rhs); if ( this != &rhs ) { new_vector.reserve(_capacity); new_vector.assign(rhs.begin(), rhs.end()); swap(new_vector); //_size = rhs.size(); } return *this; } /********************************************* * PUBLIC MEMBER FUNCTIONS *********************************************/ /************* * iterators : *************/ // begin ------------------------------------- VT_TPL typename VT::iterator VT:: begin() { return _mem_ptr; } VT_TPL typename VT::const_iterator VT:: begin() const { return _mem_ptr; } // end --------------------------------------- VT_TPL typename VT::iterator VT:: end() { return &_mem_ptr[_size]; } VT_TPL typename VT::const_iterator VT:: end() const { return &_mem_ptr[_size]; } // rbegin ------------------------------------ //VT_TPL typename VT::reverse_iterator VT:: // rbegin() { return reverse_iterator(end()); } //VT_TPL typename VT::const_reverse_iterator VT:: // rbegin() const { return const_reverse_iterator(end()); } // rend -------------------------------------- //VT_TPL typename VT::reverse_iterator VT:: // rend() { return reverse_iterator(begin()); } //VT_TPL typename VT::const_reverse_iterator VT:: // rend() const { return const_reverse_iterator(begin()); } /************ * capacity : ************/ // size -------------------------------------- VT_TPL typename VT::size_type VT:: size( ) const { return _size; } // max_size ---------------------------------- VT_TPL typename VT::size_type VT:: max_size() const { return (_allocator.max_size()); } // resize ------------------------------------ VT_TPL void VT:: resize(size_type n, value_type val) { if (n > _size) { if (n > _capacity) _increment_capacity(n); while (_size != n) { _allocator.construct(&_mem_ptr[_size], val); ++_size; } } else if (n < _size) { while (_size != n) _allocator.destroy(&_mem_ptr[--_size]); } } // capacity ---------------------------------- VT_TPL typename VT::size_type VT:: capacity() const { return _capacity; } // empty ------------------------------------- VT_TPL bool VT:: empty() const { return (_size == 0); } // reserve ----------------------------------- VT_TPL void VT:: reserve( size_type new_cap ) { value_type * tmp_ptr; if (new_cap > _allocator.max_size()) throw std::length_error("reserve: new_cap > max_size"); if (_capacity == _allocator.max_size()) throw std::length_error("reserve: capacity == max_size"); if (new_cap <= _capacity) return ; _capacity = new_cap; tmp_ptr = _allocator.allocate(new_cap); if (_mem_ptr) { for (size_type i = 0; i < _size; i++) tmp_ptr[i] = _mem_ptr[i]; _destroy(begin(), end()); _allocator.deallocate(_mem_ptr, _capacity); } _mem_ptr = tmp_ptr; } /****************** * element access : ******************/ // operator[] -------------------------------- VT_TPL typename VT::reference VT:: operator[](size_type n) { return _mem_ptr[n]; } VT_TPL typename VT::const_reference VT:: operator[](size_type n) const { return _mem_ptr[n]; } // at ---------------------------------------- VT_TPL typename VT::reference VT:: at(size_type n) { if (n >= _size) throw std::out_of_range("vector out of range"); return (_mem_ptr[n]); } VT_TPL typename VT::const_reference VT:: at(size_type n) const { if (n >= _size) throw std::out_of_range("vector out of range"); return (_mem_ptr[n]); } // front ------------------------------------- VT_TPL typename VT::reference VT:: front() { return (*_mem_ptr); } VT_TPL typename VT::const_reference VT:: front() const { return (*_mem_ptr); } // back -------------------------------------- VT_TPL typename VT::reference VT:: back() { return (_mem_ptr[_size - 1]); } VT_TPL typename VT::const_reference VT:: back() const { return (_mem_ptr[_size - 1]); } /************* * modifiers : *************/ // assign ------------------------------------ VT_TPL template typename enable_if< !is_integral::value,void >::type VT:: assign( InputIterator first, InputIterator last) { InputIterator tmp = first; unsigned int range = 0; clear(); while (tmp++ != last) range++; if (range >= _capacity) _increment_capacity(range); while (first != last) { _allocator.construct(&_mem_ptr[_size], *first); first++; _size++; } } VT_TPL void VT:: assign( size_type n, const T & val ) { if (n > _allocator.max_size()) throw std::length_error("assign: n > max_size"); value_type * tmp_ptr; _destroy(begin(), end()); if (n > _capacity) { _capacity = n; tmp_ptr = _allocator.allocate(n); if (_mem_ptr) _allocator.deallocate(_mem_ptr, _capacity); _mem_ptr = tmp_ptr; } _size = n; while (n) _allocator.construct(&_mem_ptr[--n], val); } // push_back --------------------------------- VT_TPL void VT:: push_back( const value_type & element ) { if (_size >= _capacity) _increment_capacity(1); _allocator.construct(&_mem_ptr[_size], element); _size++; } // pop_back ---------------------------------- VT_TPL void VT:: pop_back() { _allocator.destroy(end() - 1); _size--; } // insert ------------------------------------ VT_TPL typename VT::iterator VT:: insert(iterator position, const value_type& val) { difference_type distance; iterator it; if (_size + 1 > _capacity) { distance = position - begin(); _increment_capacity(1); position = begin() + distance; } it = end(); if (position != it) { _allocator.construct(it, *(it - 1)); while (--it != position) *(it + 1) = *it; } _allocator.destroy(position); _allocator.construct(position, val); _size++; return (position); } //VT_TPL void VT:: // insert(iterator position, size_type n, const value_type& val) { // //} //VT_TPL template void VT:: // insert(iterator position, InputIterator first, InputIterator last) { // //} // erase ------------------------------------- VT_TPL typename VT::iterator VT:: erase(iterator position) { iterator i = position; iterator it_end = end() - 1; while (i != it_end) { *i = *(i + 1); ++i; } _allocator.destroy(it_end); _size -= 1; return (position); } VT_TPL typename VT::iterator VT:: erase(iterator first, iterator last) { iterator it_end = end(); difference_type diff = std::distance(first, last); if (diff <= 0) return (first); it_end = end(); while (last != it_end) { *first = *last; first++; last++; } _destroy(it_end - diff, it_end); _size -= diff; return (first); } // swap -------------------------------------- VT_TPL void VT:: swap(vector& x) { T* tmp_mem_ptr; size_type tmp_size; size_type tmp_capacity; tmp_mem_ptr = x._mem_ptr; tmp_size = x._size; tmp_capacity = x._capacity; x._mem_ptr = _mem_ptr; x._size = _size; x._capacity = _capacity; _mem_ptr = tmp_mem_ptr; _size = tmp_size; _capacity = tmp_capacity; } // clear ------------------------------------- VT_TPL void VT:: clear() { _destroy(begin(), end()); _size = 0; } /************* * allocator : *************/ // get_allocator ----------------------------- VT_TPL typename VT::allocator_type VT:: get_allocator() const { return (_allocator); } /********************************************* * PRIVATE MEMBER FUNCTIONS *********************************************/ VT_TPL void VT:: _destroy(iterator first, iterator last) { while (first != last) { _allocator.destroy(first); first++; } } VT_TPL void VT:: _increment_capacity(size_type n) { size_type res; res = std::max(_size * 2, n); reserve(std::min(res, _allocator.max_size())); } /********************************************* * OPERATORS *********************************************/ //std::ostream & operator<<(std::ostream & o, vector const & rhs) //{ // o << rhs.getFoo(); // return (o); //} /********************************************* * NESTED CLASS *********************************************/ //void vector::Class::function() {} /********************************************* * STATICS *********************************************/ //std::string const vector::_bar = "bar"; } // namespace ft #undef VT #undef VT_TPL