Program Listing for File SmallVector.h

↰ Return to documentation for file (ADS/sequence/SmallVector.h)

/*--------------------------------------------------------------------------*/
/*--------------------------- File SmallVector.h ---------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_SmallVector
 #define __ADS_SmallVector
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"

#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*-------------------------- CLASS SmallVector -----------------------------*/
/*--------------------------------------------------------------------------*/

template< class T , std::size_t N = 8 >
class SmallVector {

 static_assert( N >= 1 , "room for one item at least" );

/*--------------------------------------------------------------------------*/
 public:

 using value_type = T;
 using size_type = std::size_t;
 using difference_type = std::ptrdiff_t;
 using reference = T &;
 using const_reference = const T &;
 using pointer = T *;
 using const_pointer = const T *;
 using iterator = T *;
 using const_iterator = const T *;
 using reverse_iterator = std::reverse_iterator< iterator >;
 using const_reverse_iterator = std::reverse_iterator< const_iterator >;

 SmallVector( void ) noexcept : f_A( inner() ) {}

 explicit SmallVector( std::size_t n , const T & x = T() );

 SmallVector( std::initializer_list< T > items );

 template< std::input_iterator I , std::sentinel_for< I > S >
 SmallVector( I first , S last );

 SmallVector( const SmallVector & other );

 SmallVector( SmallVector && other )
  noexcept( std::is_nothrow_move_constructible_v< T > );

 SmallVector & operator=( const SmallVector & other );

 SmallVector & operator=( SmallVector && other )
  noexcept( std::is_nothrow_move_constructible_v< T > );

 ~SmallVector();

 void swap( SmallVector & other );

 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
 [[nodiscard]] std::size_t capacity( void ) const { return( f_cap ); }

 [[nodiscard]] bool small( void ) const { return( f_A == inner() ); }

 [[nodiscard]] T * data( void ) { return( f_A ); }
 [[nodiscard]] const T * data( void ) const { return( f_A ); }

 [[nodiscard]] iterator begin( void ) { return( f_A ); }
 [[nodiscard]] iterator end( void ) { return( f_A + f_n ); }
 [[nodiscard]] const_iterator begin( void ) const { return( f_A ); }
 [[nodiscard]] const_iterator end( void ) const { return( f_A + f_n ); }
 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
 [[nodiscard]] reverse_iterator rbegin( void ) {
  return( reverse_iterator( end() ) );
  }
 [[nodiscard]] reverse_iterator rend( void ) {
  return( reverse_iterator( begin() ) );
  }
 [[nodiscard]] const_reverse_iterator rbegin( void ) const {
  return( const_reverse_iterator( end() ) );
  }
 [[nodiscard]] const_reverse_iterator rend( void ) const {
  return( const_reverse_iterator( begin() ) );
  }

 [[nodiscard]] T & operator[]( std::size_t i ) {
  ADS_PRE( i < f_n );
  return( f_A[ i ] );
  }
 [[nodiscard]] const T & operator[]( std::size_t i ) const {
  ADS_PRE( i < f_n );
  return( f_A[ i ] );
  }

 [[nodiscard]] T & front( void ) { ADS_PRE( f_n > 0 ); return( f_A[ 0 ] ); }
 [[nodiscard]] const T & front( void ) const {
  ADS_PRE( f_n > 0 );
  return( f_A[ 0 ] );
  }
 [[nodiscard]] T & back( void ) {
  ADS_PRE( f_n > 0 );
  return( f_A[ f_n - 1 ] );
  }
 [[nodiscard]] const T & back( void ) const {
  ADS_PRE( f_n > 0 );
  return( f_A[ f_n - 1 ] );
  }

 void push_back( const T & x ) { emplace_back( x ); }
 void push_back( T && x ) { emplace_back( std::move( x ) ); }

 template< class... Args >
 T & emplace_back( Args &&... args );

 void pop_back( void );

 void reserve( std::size_t n );

 void resize( std::size_t n , const T & x = T() );

 void clear( void );

 [[nodiscard]] bool operator==( const SmallVector & o ) const {
  return( std::equal( begin() , end() , o.begin() , o.end() ) );
  }

/*--------------------------------------------------------------------------*/
 private:

 T * inner( void ) { return( reinterpret_cast< T * >( f_room ) ); }
 const T * inner( void ) const {
  return( reinterpret_cast< const T * >( f_room ) );
  }

 void grow( std::size_t c );

 void take( SmallVector & o );

 T * f_A;
 std::size_t f_n = 0;
 std::size_t f_cap = N;
 alignas( T ) unsigned char f_room[ N * sizeof( T ) ];

 };  // end( class SmallVector )

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/sequence/impl/SmallVector.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* SmallVector.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File SmallVector.h -------------------------*/
/*--------------------------------------------------------------------------*/