Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MYTEMPLATE_VECTOR
- #define MYTEMPLATE_VECTOR
- #include <stdexcept>
- #include <algorithm>
- #include <cstdlib>
- #include "vector_iterator.h"
- namespace mytemplate
- {
- template <class T>
- class vector
- {
- private:
- T* array;
- size_t m_size;
- size_t m_capacity;
- // oblicza następną potęgę 2 nie mniejszą niż 'number'(może być równa)
- size_t nextGreatestPower( size_t number )
- {
- size_t next_power = number-1;
- next_power |= next_power >> 1;
- next_power |= next_power >> 2;
- next_power |= next_power >> 4;
- next_power |= next_power >> 8;
- next_power |= next_power >> 16;
- ++next_power;
- return next_power;
- }
- void copyArray( T* dst, const T* src, size_t n )
- {
- for( size_t i = 0; i < n; ++i )
- dst[i] = src[i];
- }
- public:
- vector() : array(nullptr), m_size(0), m_capacity(0) {}
- vector( const vector<T>& vec )
- {
- array = nullptr;
- *this = vec;
- }
- vector( size_t n, const T& val = T() )
- {
- if( n == 0 )
- {
- m_capacity = 0;
- m_size = 0;
- array = nullptr;
- return;
- }
- m_capacity = nextGreatestPower( n );
- m_size = n;
- array = static_cast<T*>( malloc( m_capacity * sizeof( T ) ) );
- for( size_t i = 0; i < m_capacity; ++i )
- new (&array[i]) T(val);
- }
- ~vector()
- {
- for( size_t i = 0; i < m_capacity; ++i )
- array[i].~T();
- free(array);
- }
- typedef vector_iterator<T, vector> iterator;
- typedef vector_const_iterator<T, vector> const_iterator;
- void push_back( const T& value )
- {
- if( m_capacity == 0 || m_size >= m_capacity )
- reserve( m_capacity + 2 );
- array[m_size] = value;
- ++m_size;
- }
- void pop_back()
- {
- --m_size;
- array[m_size].~T();
- new (&array[m_size]) T();
- }
- void insert( size_t position, const T& value )
- {
- if( (m_size == 0 && position == 0) || position == m_size )
- {
- push_back( value );
- return;
- }
- else if( position > m_size )
- throw std::out_of_range( "Vector Subscript Out of Range" );
- for( size_t i = m_size; ; --i )
- {
- if( i == position )
- {
- array[i] = value;
- break;
- }
- else
- array[i] = array[i-1];
- }
- ++m_size;
- }
- void insert( const_iterator position, size_t n, const T& val)
- {
- ptrdiff_t pos = position - begin();
- if( static_cast<size_t>(pos) > m_size )
- throw std::out_of_range( "Vector Subscript Out of Range" );
- if( pos + n > m_capacity )
- reserve( pos + n );
- for( ptrdiff_t i = m_size - 1; i >= pos; --i )
- array[i + n] = array[i];
- m_size += n;
- while( n-- )
- array[pos++] = val;
- }
- void erase( size_t position )
- {
- if( position >= m_size )
- throw std::out_of_range( "Vector Subscript Out of Range" );
- array[position].~T();
- new (&array[position]) T();
- for( size_t i = position; i < m_size - 1; ++i )
- array[i] = array[i+1];
- --m_size;
- }
- void clear()
- {
- for( size_t i = 0; i < m_size; ++i )
- {
- array[i].~T();
- new (&array[i]) T();
- }
- m_size = 0;
- }
- void reserve( size_t n )
- {
- if( n <= m_capacity )
- return;
- m_capacity = nextGreatestPower( n );
- T* tmp = array;
- array = static_cast<T*>( malloc( m_capacity * sizeof(T) ) );
- if( array == nullptr )
- throw std::bad_alloc();
- for( size_t i = 0; i < m_capacity; ++i )
- new (&array[i]) T();
- copyArray( array, tmp, m_size );
- free(tmp);
- }
- void swap( vector<T>& vec )
- {
- std::swap( array, vec.array );
- std::swap( m_size, vec.m_size );
- std::swap( m_capacity, vec.m_capacity );
- }
- size_t size() const
- {
- return m_size;
- }
- size_t capacity() const
- {
- return m_capacity;
- }
- bool empty() const
- {
- if( m_size == 0 )
- return true;
- else
- return false;
- }
- T& front()
- {
- return array[0];
- }
- const T& front() const
- {
- return array[0];
- }
- T& back()
- {
- return array[m_size-1];
- }
- const T& back() const
- {
- return array[m_size-1];
- }
- iterator begin()
- {
- return iterator( &array[0] );
- }
- const_iterator begin() const
- {
- return const_iterator( &array[0] );
- }
- iterator end()
- {
- return iterator( &array[m_size] );
- }
- const_iterator end() const
- {
- return const_iterator( &array[m_size] );
- }
- T* data()
- {
- return array;
- }
- const T* data() const
- {
- return array;
- }
- T& operator[]( size_t index )
- {
- if( index >= m_size )
- throw std::out_of_range( "Vector Subscript Out of Range" );
- return array[index];
- }
- const T& operator[]( size_t index ) const
- {
- if( index >= m_size )
- throw std::out_of_range( "Vector Subscript Out of Range" );
- return array[index];
- }
- vector<T>& operator=( const vector<T> &vec )
- {
- if( this == &vec )
- return *this;
- m_size = vec.size();
- m_capacity = vec.capacity();
- T* tmp = array;
- if( vec.capacity() != 0 )
- {
- array = static_cast<T*>( malloc( m_capacity * sizeof(T) ) );
- for( size_t i = 0; i < m_capacity; ++i )
- new (&array[i]) T();
- copyArray( array, vec.array, m_size );
- }
- else
- array = nullptr;
- free(tmp);
- return *this; // umożliwia łańcuchowe przypisywanie np: vec1 = vec2 = vec3 = vec4;
- }
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement