#define UNIT r6_tinyvec #include #include #include class insufficient_space; void throw_insufficient_space(); template< typename value_t, size_t max_size > class tiny_vector { std::array< uint8_t, max_size * sizeof( value_t ) > _mem; int _count = 0; public: value_t *slot( int i ) { assert( i >= 0 ); assert( i < _count ); return reinterpret_cast< value_t * >( _mem.begin() ) + i; } const value_t *slot( int i ) const { return reinterpret_cast< const value_t * >( _mem.begin() ) + i; } ~tiny_vector() { while ( _count ) erase( _count - 1 ); } void erase( int idx ) { std::destroy_at( slot( idx ) ); for ( int i = idx; i < _count - 1; ++i ) { std::uninitialized_move_n( slot( i + 1 ), 1, slot( i ) ); std::destroy_at( slot( i + 1 ) ); } -- _count; } void insert( int idx, value_t &&v ) { if ( _count == max_size ) throw_insufficient_space(); ++ _count; for ( int i = _count - 1; i > idx; --i ) { std::uninitialized_move_n( slot( i - 1 ), 1, slot( i ) ); std::destroy_at( slot( i - 1 ) ); } if ( idx < _count - 1 ) std::destroy_at( slot( idx ) ); std::uninitialized_move_n( &v, 1, slot( idx ) ); } const value_t &front() const { return *slot( 0 ); } const value_t &back() const { return *slot( _count - 1 ); } value_t &front() { return *slot( 0 ); } value_t &back() { return *slot( _count - 1 ); } }; #include "test_main.cpp" void throw_insufficient_space() { throw insufficient_space(); }