commit 9c4e915295a30696aa211c2419d715a48082dbc7
parent dac9efe2cdb3832f33ee82bd4ae2c481ad2d23e1
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date: Sat, 30 May 2020 02:07:14 +0200
reduce build size with vector implementation smaller than STL's
Diffstat:
M | lmerge.cpp | | | 1 | - |
A | vector.hpp | | | 213 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 213 insertions(+), 1 deletion(-)
diff --git a/lmerge.cpp b/lmerge.cpp
@@ -11,7 +11,6 @@
#include <ctype.h>
#include <algorithm>
-#include <vector>
/**
* This program reads stdin and when consecutive lines have specific fields all
diff --git a/vector.hpp b/vector.hpp
@@ -0,0 +1,213 @@
+#ifndef VECTOR_HPP
+#define VECTOR_HPP
+
+#ifndef WITH_STL
+template <typename T>
+class vector
+{
+ T* m_data = nullptr;
+ size_t m_size = 0;
+ size_t m_alloc = 0;
+
+ bool allocate( size_t new_size );
+ bool release( size_t new_size );
+ bool construct( size_t new_size );
+public:
+ typedef T* iterator;
+
+ size_t size( void ) const;
+ T const& operator[]( size_t index ) const;
+ T& operator[]( size_t index );
+ bool shrink_to_fit( void );
+ bool reserve( size_t size );
+ bool resize( size_t size );
+ bool assign( iterator start, iterator end );
+ ~vector( void );
+ T const* data( void ) const;
+ T * data( void );
+ T const* begin( void ) const;
+ T * begin( void );
+ T const* end( void ) const;
+ T * end( void );
+ T const& back( void ) const;
+ T & back( void );
+
+};
+
+template <typename T>
+bool vector<T>::allocate( size_t new_size )
+{
+ T* tmp = static_cast<T*>( realloc( m_data, new_size * sizeof( T ) ) );
+ if( tmp )
+ {
+ m_data = tmp;
+ m_alloc = new_size;
+ }
+ return !tmp;
+}
+
+template <typename T>
+bool vector<T>::release( size_t new_size )
+{
+ if( new_size > m_size )
+ {
+ return true;
+ }
+ T* start = m_data + new_size;
+ T* end = m_data + m_size;
+ for( ; start != end; ++start )
+ {
+ start->T::~T();
+ }
+ m_size = new_size;
+ return false;
+}
+
+template <typename T>
+bool vector<T>::construct( size_t new_size )
+{
+ if( new_size < m_size )
+ {
+ return true;
+ }
+
+ T* start = m_data + m_size;
+ T* end = m_data + new_size;
+ for( ; start != end; ++start )
+ {
+ new ( start ) T();
+ }
+ m_size = new_size;
+ return false;
+}
+
+template <typename T>
+size_t vector<T>::size( void ) const
+{
+ return m_size;
+}
+
+template <typename T>
+T const& vector<T>::operator[]( size_t index ) const
+{
+ return m_data[index];
+}
+
+template <typename T>
+T& vector<T>::operator[]( size_t index )
+{
+ return m_data[index];
+}
+
+template <typename T>
+bool vector<T>::shrink_to_fit( void )
+{
+ return allocate( m_size );
+}
+
+template <typename T>
+bool vector<T>::reserve( size_t size )
+{
+ if( m_alloc < size )
+ {
+ return allocate( size );
+ }
+ return false;
+}
+
+template <typename T>
+bool vector<T>::resize( size_t size )
+{
+ if( size < m_size )
+ {
+ return release( size );
+ }
+
+ if( allocate( size ) )
+ {
+ return true;
+ }
+ return construct( size );
+}
+
+template <typename T>
+bool vector<T>::assign( iterator start, iterator end )
+{
+ size_t new_size = end - start;
+ release( new_size );
+ if( allocate( new_size ) )
+ {
+ return true;
+ }
+ m_size = new_size;
+
+ T* dst = m_data;
+ for( ; start != end; ++dst, ++start )
+ {
+ *dst = *start;
+ }
+
+ return false;
+}
+
+template <typename T>
+vector<T>::~vector( void )
+{
+ release( m_size );
+ free( m_data );
+}
+
+template <typename T>
+T const* vector<T>::data( void ) const
+{
+ return m_data;
+}
+
+template <typename T>
+T * vector<T>::data( void )
+{
+ return m_data;
+}
+
+template <typename T>
+T const* vector<T>::begin( void ) const
+{
+ return m_data;
+}
+
+template <typename T>
+T * vector<T>::begin( void )
+{
+ return m_data;
+}
+
+template <typename T>
+T const* vector<T>::end( void ) const
+{
+ return m_data + m_size;
+}
+
+template <typename T>
+T * vector<T>::end( void )
+{
+ return m_data + m_size;
+}
+
+template <typename T>
+T const& vector<T>::back( void ) const
+{
+ return m_data[m_size - 1];
+}
+
+template <typename T>
+T & vector<T>::back( void )
+{
+ return m_data[m_size - 1];
+}
+
+#else
+#include <vector>
+using std::vector;
+#endif
+
+#endif