#include "DSL.h" /** * Construct the tree. * inf is the largest Comparable * and is used to signal failed finds. */ template DSL::DSL( const Comparable & inf ) : INFINITY( inf ) { bottom = new SkipNode( ); bottom->right = bottom->down = bottom; tail = new SkipNode( INFINITY ); tail->right = tail; header = new SkipNode( INFINITY, tail, bottom ); } /** * Copy constructor. * Left as an exercise. */ template DSL::DSL( const DSL & rhs ) : INFINITY( rhs.INFINITY) { cout << "Copy constructor is unimplemented" << endl; } /** * Destructor. */ template DSL::~DSL( ) { makeEmpty( ); delete header; delete tail; delete bottom; } /** * Insert item x into the DSL. */ template void DSL::insert( const Comparable & x ) { SkipNode *current = header; bottom->element = x; while( current != bottom ) { while( current->element < x ) current = current->right; // If gap size is 3 or at bottom level and // must insert, then promote middle element if( current->down->right->right->element < current->element ) { current->right = new SkipNode( current->element, current->right, current->down->right->right ); current->element = current->down->right->element; } else current = current->down; } // Raise height of DSL if necessary if( header->right != tail ) header = new SkipNode( INFINITY, tail, header ); } /** * Remove item x from the DSL. Unimplemented. */ template void DSL::remove( const Comparable & x ) { cout << "Sorry, remove unimplemented; " << x << " still present" << endl; } /** * Find the smallest item in the tree. * Return smallest item or INFINITY if empty. */ template const Comparable & DSL::findMin( ) const { if( isEmpty( ) ) return INFINITY; SkipNode *current = header; while( current->down != bottom ) current = current->down; return elementAt( current ); } /** * Find the largest item in the tree. * Return the largest item or INFINITY if empty. */ template const Comparable & DSL::findMax( ) const { if( isEmpty( ) ) return INFINITY; SkipNode *current = header; for( ; ; ) if( current->right->right != tail ) current = current->right; else if( current->down != bottom ) current = current->down; else return elementAt( current ); } /** * Find item x in the tree. * Return the matching item or INFINITY if not found. */ template const Comparable & DSL::find( const Comparable & x ) const { SkipNode *current = header; bottom->element = x; for( ; ; ) if( x < current->element ) current = current->down; else if( current->element < x ) current = current->right; else return elementAt( current ); } /** * Make the tree logically empty. */ template void DSL::makeEmpty( ) { reclaimMemory( header ); header->right = tail; header->down = bottom; } /** * Test if the tree is logically empty. * Return true if empty, false otherwise. */ template bool DSL::isEmpty( ) const { return header->right == tail && header->down == bottom; } /** * Internal method to get element field from node t. * Return the element field or INFINITY if t is at the bottom. */ template const Comparable & DSL:: elementAt( SkipNode *t ) const { if( t == bottom ) return INFINITY; else return t->element; } /** * Print the DSL. */ template void DSL::printList( ) const { SkipNode *current = header; while( current->down != bottom ) current = current->down; while( current->right != tail ) { cout << current->element << endl; current = current->right; } } /** * Deep copy. Left as an exercise */ template const DSL & DSL::operator=( const DSL & rhs ) { if( this != &rhs ) cout << "Sorry, operator= is unimplemented" << endl; return *this; } /** * reclaimMemory is left as an exercise. * Hint: delete from top level to bottom level. */ template void DSL::reclaimMemory( SkipNode *t ) const { if( t != bottom ) cout << "reclaimMemory is unimplemented -- leaking!" << endl; }