Program Listing for File SplayTree.h
↰ Return to documentation for file (ADS/tree/SplayTree.h)
/*--------------------------------------------------------------------------*/
/*---------------------------- File SplayTree.h ----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/
#ifndef __ADS_SplayTree
#define __ADS_SplayTree
/* self-identification: #endif constant */
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include <concepts>
#include <cstddef>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/
namespace ADS {
/*--------------------------------------------------------------------------*/
/*--------------------------- CLASS SplayTree ------------------------------*/
/*--------------------------------------------------------------------------*/
template< std::totally_ordered K , class V >
class SplayTree {
/*--------------------------------------------------------------------------*/
public:
using Key = K;
using Value = V;
SplayTree( void ) = default;
SplayTree( const SplayTree & ) = delete;
SplayTree & operator=( const SplayTree & ) = delete;
SplayTree( SplayTree && other ) noexcept;
~SplayTree();
[[nodiscard]] std::optional< V > lookup( const K & k );
void insert( const K & k , const V & v );
void remove( const K & k );
[[nodiscard]] std::size_t size( void ) const;
[[nodiscard]] std::optional< K > root( void ) const;
template< class F >
void forEach( F && f ) const;
/*--------------------------------------------------------------------------*/
private:
struct Node {
K key;
V value;
Node * left = nullptr , * right = nullptr;
};
public:
/*------------------------- THE STANDARD INTERFACE -------------------------*/
// a constant view of the pairs ( key , value ) by increasing key,
// bidirectional, along the path from the root; a lookup splays the tree
// and invalidates it
struct Shape {
static auto root( const auto & o ) { return( o.f_root ); }
static auto left( const auto * n ) { return( n->left ); }
static auto right( const auto * n ) { return( n->right ); }
static auto read( const auto * n ) {
return( std::pair< const K & , const V & >( n->key , n->value ) );
}
};
using value_type = std::pair< const K & , const V & >;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type;
using const_reference = value_type;
using const_iterator = PathIterator< SplayTree , Shape >;
using iterator = const_iterator;
[[nodiscard]] const_iterator begin( void ) const {
return( const_iterator( this , true ) );
}
[[nodiscard]] const_iterator end( void ) const {
return( const_iterator( this , false ) );
}
[[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
[[nodiscard]] const_iterator cend( void ) const { return( end() ); }
private:
static Node * splay( Node * t , const K & k );
static void destroy( Node * t );
template< class F >
static void visit( const Node * t , F & f );
static bool ordered( const Node * t , const K * lo , const K * hi );
Node * f_root = nullptr;
std::size_t f_size = 0;
}; // end( class SplayTree )
/*--------------------------------------------------------------------------*/
} // end( namespace ADS )
/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADS/tree/impl/SplayTree.tpp"
/*--------------------------------------------------------------------------*/
#endif /* SplayTree.h included */
/*--------------------------------------------------------------------------*/
/*-------------------------- End File SplayTree.h --------------------------*/
/*--------------------------------------------------------------------------*/