Program Listing for File ArrayStack.h

↰ Return to documentation for file (ADS/stack/ArrayStack.h)

/*--------------------------------------------------------------------------*/
/*--------------------------- File ArrayStack.h ----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

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

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

#include "ADS/Contract.h"

#include <cstddef>
#include <memory>
#include <utility>

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

namespace ADS {

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS ArrayStack ----------------------------*/
/*--------------------------------------------------------------------------*/

template< class T >
class ArrayStack {

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

 using Item = T;

 explicit ArrayStack( std::size_t dim );

 ArrayStack( const ArrayStack & other );

 ArrayStack( ArrayStack && other ) noexcept;

 ArrayStack & operator=( ArrayStack other ) noexcept;

 void swap( ArrayStack & other ) noexcept;

 [[nodiscard]] bool empty( void ) const;

 [[nodiscard]] const T & top( void ) const;

 T pop( void );

 void push( const T & v );

 [[nodiscard]] std::size_t size( void ) const;

 [[nodiscard]] std::size_t capacity( void ) const;

 [[nodiscard]] bool operator==( const ArrayStack & o ) const;

/*------------------------- THE STANDARD INTERFACE -------------------------*/

 // a constant view of the items, from the bottom to the top, contiguous;
 // the operations of the stack stay the only way to change it

 using value_type = T;
 using size_type = std::size_t;
 using difference_type = std::ptrdiff_t;
 using reference = const T &;
 using const_reference = const T &;
 using iterator = const T *;
 using const_iterator = const T *;

 [[nodiscard]] const_iterator begin( void ) const { return( f_A.get() ); }
 [[nodiscard]] const_iterator end( void ) const {
  return( f_A.get() + f_size );
  }
 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }

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

 std::unique_ptr< T[] > f_A;
 std::size_t f_size;
 std::size_t f_capacity;

 };  // end( class ArrayStack )

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

}  // end( namespace ADS )

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

#include "ADS/stack/impl/ArrayStack.tpp"

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

#endif  /* ArrayStack.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File ArrayStack.h --------------------------*/
/*--------------------------------------------------------------------------*/