<commit_before>#ifndef RDESTL_VECTOR_H
#define RDESTL_VECTOR_H

#include "rdestl_common.h"
#include "algorithm.h"
#include "allocator.h"

namespace rde
{
//=============================================================================
struct base_vector
{
	typedef int				size_type;
	static const size_type	npos = size_type(-1);
};

//=============================================================================
// Standard vector storage.
// Dynamic allocation, can grow, can shrink.
template<typename T, class TAllocator>
struct standard_vector_storage 
{
	explicit standard_vector_storage(const TAllocator& allocator)
	:	m_begin(0),
		m_end(0),
		m_capacityEnd(0),
		m_allocator(allocator)
	{
		/**/
	}	
	explicit standard_vector_storage(e_noinitialize) 
	{
	}

	void reallocate(base_vector::size_type newCapacity, base_vector::size_type oldSize)
	{
		T* newBegin = static_cast<T*>(m_allocator.allocate(newCapacity * sizeof(T)));
		const base_vector::size_type newSize = oldSize < newCapacity ? oldSize : newCapacity;
		// Copy old data if needed.
		if (m_begin)
		{
			rde::copy_construct_n(m_begin, newSize, newBegin);
			destroy(m_begin, oldSize);
		}
		m_begin = newBegin;
		m_end = m_begin + newSize;
		m_capacityEnd = m_begin + newCapacity;
		RDE_ASSERT(invariant());
	}

	// Reallocates memory, doesnt copy contents of old buffer.
	void reallocate_discard_old(base_vector::size_type newCapacity)
	{
		RDE_ASSERT(newCapacity > base_vector::size_type(m_capacityEnd - m_begin));
		T* newBegin = static_cast<T*>(m_allocator.allocate(newCapacity * sizeof(T)));
		const base_vector::size_type currSize((base_vector::size_type)(m_end - m_begin));
		if (m_begin)
			destroy(m_begin, currSize);
		m_begin = newBegin;
		m_end = m_begin + currSize;
		m_capacityEnd = m_begin + newCapacity;
		RDE_ASSERT(invariant());
	}
	void destroy(T* ptr, base_vector::size_type n)
	{
		rde::destruct_n(ptr, n);
		m_allocator.deallocate(ptr, n * sizeof(T));
	}
	void reset()
	{
		if (m_begin)
			m_allocator.deallocate(m_begin, (m_end - m_begin) * sizeof(T));

		m_begin = m_end = 0;
		m_capacityEnd = 0;
	}
	bool invariant() const
	{
		return m_end >= m_begin;
	}
	RDE_FORCEINLINE void record_high_watermark()
	{
		// empty
	}

	T*			m_begin;
	T*			m_end;
	T*			m_capacityEnd;
	TAllocator	m_allocator;
};

//=============================================================================
// Simplified vector class.
// Mimics std::vector.
template<typename T, class TAllocator = rde::allocator,
	class TStorage = standard_vector_storage<T, TAllocator> >
class vector : public base_vector, private TStorage
{
private:
    using TStorage::m_begin;
    using TStorage::m_end;
    using TStorage::m_capacityEnd;
    using TStorage::m_allocator;
    using TStorage::invariant;
    using TStorage::reallocate;
    
public:
	typedef T				value_type;
	typedef T*				iterator;
	typedef const T*		const_iterator;
	typedef TAllocator		allocator_type;
	static const size_type	kInitialCapacity = 16;
   
	explicit vector(const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		/**/
	}
	explicit vector(size_type initialSize, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		resize(initialSize);
	}
	vector(const T* first, const T* last, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		assign(first, last);
	}
	// @note: allocator is not copied from rhs.
	// @note: will not perform default constructor for newly created objects.
	vector(const vector& rhs, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
        if(rhs.size() == 0) // nothing to do
            return;
		reallocate_discard_old(rhs.capacity());
		rde::copy_construct_n(rhs.m_begin, rhs.size(), m_begin);
		m_end = m_begin + rhs.size();
		TStorage::record_high_watermark();
		RDE_ASSERT(invariant());
	}
	explicit vector(e_noinitialize n)
	:	TStorage(n)
	{
	}
	~vector()
	{
		if (TStorage::m_begin != 0)
			TStorage::destroy(TStorage::m_begin, size());
	}

	// @note:	allocator is not copied!
	// @note:	will not perform default constructor for newly created objects,
	//			just initialize with copy ctor of elements of rhs.
	vector& operator=(const vector& rhs)
	{
        copy(rhs);		
		return *this;<commit_msg>Fix error with gcc 4.9.3<commit_after>#ifndef RDESTL_VECTOR_H
#define RDESTL_VECTOR_H

#include "rdestl_common.h"
#include "algorithm.h"
#include "allocator.h"

namespace rde
{
//=============================================================================
struct base_vector
{
	typedef int				size_type;
	static const size_type	npos = size_type(-1);
};

//=============================================================================
// Standard vector storage.
// Dynamic allocation, can grow, can shrink.
template<typename T, class TAllocator>
struct standard_vector_storage 
{
	explicit standard_vector_storage(const TAllocator& allocator)
	:	m_begin(0),
		m_end(0),
		m_capacityEnd(0),
		m_allocator(allocator)
	{
		/**/
	}	
	explicit standard_vector_storage(e_noinitialize) 
	{
	}

	void reallocate(base_vector::size_type newCapacity, base_vector::size_type oldSize)
	{
		T* newBegin = static_cast<T*>(m_allocator.allocate(newCapacity * sizeof(T)));
		const base_vector::size_type newSize = oldSize < newCapacity ? oldSize : newCapacity;
		// Copy old data if needed.
		if (m_begin)
		{
			rde::copy_construct_n(m_begin, newSize, newBegin);
			destroy(m_begin, oldSize);
		}
		m_begin = newBegin;
		m_end = m_begin + newSize;
		m_capacityEnd = m_begin + newCapacity;
		RDE_ASSERT(invariant());
	}

	// Reallocates memory, doesnt copy contents of old buffer.
	void reallocate_discard_old(base_vector::size_type newCapacity)
	{
		RDE_ASSERT(newCapacity > base_vector::size_type(m_capacityEnd - m_begin));
		T* newBegin = static_cast<T*>(m_allocator.allocate(newCapacity * sizeof(T)));
		const base_vector::size_type currSize((base_vector::size_type)(m_end - m_begin));
		if (m_begin)
			destroy(m_begin, currSize);
		m_begin = newBegin;
		m_end = m_begin + currSize;
		m_capacityEnd = m_begin + newCapacity;
		RDE_ASSERT(invariant());
	}
	void destroy(T* ptr, base_vector::size_type n)
	{
		rde::destruct_n(ptr, n);
		m_allocator.deallocate(ptr, n * sizeof(T));
	}
	void reset()
	{
		if (m_begin)
			m_allocator.deallocate(m_begin, (m_end - m_begin) * sizeof(T));

		m_begin = m_end = 0;
		m_capacityEnd = 0;
	}
	bool invariant() const
	{
		return m_end >= m_begin;
	}
	RDE_FORCEINLINE void record_high_watermark()
	{
		// empty
	}

	T*			m_begin;
	T*			m_end;
	T*			m_capacityEnd;
	TAllocator	m_allocator;
};

//=============================================================================
// Simplified vector class.
// Mimics std::vector.
template<typename T, class TAllocator = rde::allocator,
	class TStorage = standard_vector_storage<T, TAllocator> >
class vector : public base_vector, private TStorage
{
private:
    using TStorage::m_begin;
    using TStorage::m_end;
    using TStorage::m_capacityEnd;
    using TStorage::m_allocator;
    using TStorage::invariant;
    using TStorage::reallocate;
    
public:
	typedef T				value_type;
	typedef T*				iterator;
	typedef const T*		const_iterator;
	typedef TAllocator		allocator_type;
	static const size_type	kInitialCapacity = 16;
   
	explicit vector(const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		/**/
	}
	explicit vector(size_type initialSize, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		resize(initialSize);
	}
	vector(const T* first, const T* last, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
		assign(first, last);
	}
	// @note: allocator is not copied from rhs.
	// @note: will not perform default constructor for newly created objects.
	vector(const vector& rhs, const allocator_type& allocator = allocator_type())
	:	TStorage(allocator)
	{
        if(rhs.size() == 0) // nothing to do
            return;
		this->reallocate_discard_old(rhs.capacity());
		rde::copy_construct_n(rhs.m_begin, rhs.size(), m_begin);
		m_end = m_begin + rhs.size();
		TStorage::record_high_watermark();
		RDE_ASSERT(invariant());
	}
	explicit vector(e_noinitialize n)
	:	TStorage(n)
	{
	}
	~vector()
	{
		if (TStorage::m_begin != 0)
			TStorage::destroy(TStorage::m_begin, size());
	}

	// @note:	allocator is not copied!
	// @note:	will not perform default constructor for newly created objects,
	//			just initialize with copy ctor of elements of rhs.
	vector& operator=(const vector& rhs)
	{
        copy(rhs);		
		return *this;
{'fraction_non_alphanumeric': 0.1662963396094024, 'fraction_numerical': 0.002689743889603555, 'mean_word_length': 4.755047106325707, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '841389', 'n_tokens_mistral': 3173, 'n_tokens_neox': 2969, 'n_words': 645}