Program Listing for File DirectTable.h

↰ Return to documentation for file (ADS/dictionary/DirectTable.h)

/*--------------------------------------------------------------------------*/
/*--------------------------- File DirectTable.h ---------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

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

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

#include "ADS/Contract.h"
#include "ADS/Iterators.h"

#include <cstddef>
#include <limits>
#include <optional>
#include <utility>
#include <vector>

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

namespace ADS {

/*--------------------------------------------------------------------------*/
/*--------------------------- CLASS DirectTable ----------------------------*/
/*--------------------------------------------------------------------------*/

template< class V >
class DirectTable {

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

 using Key = long;
 using Value = V;

 DirectTable( long a , long b );

 [[nodiscard]] std::optional< V > lookup( long k ) const;

 void insert( long k , const V & v );

 void remove( long k );

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

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

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

 // a constant view of the pairs ( key , value ) by increasing key,
 // bidirectional, the key computed from the place

 struct Places {
  static std::size_t size( const auto & o ) { return( o.f_A.size() ); }
  static bool occupied( const auto & o , std::size_t i ) {
   return( o.f_A[ i ].has_value() );
   }
  static auto read( const auto & o , std::size_t i ) {
   return( std::pair< long , const V & >( o.f_a + long( i ) , *o.f_A[ i ] ) );
   }
  };

 using value_type = std::pair< long , const V & >;
 using size_type = std::size_t;
 using difference_type = std::ptrdiff_t;
 using reference = std::pair< long , const V & >;
 using const_reference = std::pair< long , const V & >;
 using const_iterator = SlotIterator< DirectTable , Places >;
 using iterator = const_iterator;

 [[nodiscard]] const_iterator begin( void ) const {
  return( const_iterator( this , 0 ) );
  }
 [[nodiscard]] const_iterator end( void ) const {
  return( const_iterator( this , Places::size( *this ) ) );
  }
 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }

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

 [[nodiscard]] std::size_t H( long k ) const;

 [[nodiscard]] bool inRange( long k ) const;

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

 long f_a;
 std::vector< std::optional< V > > f_A;
 std::size_t f_size = 0;

 };  // end( class DirectTable )

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

}  // end( namespace ADS )

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

#include "ADS/dictionary/impl/DirectTable.tpp"

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

#endif  /* DirectTable.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File DirectTable.h -------------------------*/
/*--------------------------------------------------------------------------*/