# HG changeset patch # User Eris Caffee # Date 1435072663 18000 # Node ID ec700842d1825440aab04aee89a7f692afb35ff4 # Parent 86281e8f24ba8e17985b8ba90468f85118da5e26# Parent a149b424b4e26bf33298657c83f8c61f95d0ce46 MErged changes from home and server. diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Bag.cpp --- a/algs4-c++/src/Bag.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Bag.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,182 +1,7 @@ -// g++ -D BAG_MAIN -std=c++0x Bag.cpp -// g++ -D BAG_MAIN -std=c++11 Bag.cpp - +// g++ -std=c++11 Bag.cpp #include "Bag.hpp" -#include -#include - -#include - -template -Bag::Bag( void ) : - N (0), - list (nullptr) - { - } - -template -Bag::~Bag( void ) { - // This will leak memory, won't it? - while ( list ) { - Node * next = list->next; - delete list; - list = next; - } - } - -template -void Bag::add( T &item ) { - Node * node = new Node(); - node->item = item; - node->next = list; - list = node; - N++; - } - -template -bool Bag::is_empty( void ) { - return N == 0; - } - -template -size_t Bag::size( void ) { - return N; - } - -template -typename Bag::iterator Bag::begin( void ) { - return Bag::iterator( list ); - } - -template -typename Bag::iterator Bag::end( void ) { - return Bag::iterator( nullptr ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -Bag::iterator::iterator( Node *c ) : - curr (c) - { - } - -template -typename Bag::iterator & Bag::iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Bag::iterator Bag::iterator::operator++( int ) { - auto t = Bag::iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -T Bag::iterator::operator*() { - return this->curr->item; - } - -template -bool Bag::iterator::operator!=( typename Bag::iterator other ) { - return this->curr != other.curr; - } - -//////////////////////////////////////////////////////////////////////////////// - - -#ifdef EXERCISES - -// 1.3.19 -// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. -template -T Bag::delete_bottom( void ) { - Node *prev, *curr; - T item; - - prev = nullptr; - curr = list; - if ( curr ) { - while ( curr->next ) { - prev = curr; - curr = curr->next; - } - - item = curr->item; - delete curr; - if ( prev ) - prev->next = nullptr; - else - list = nullptr; - - N--; - } - - return item; - } - - -// 1.3.20 -// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. -template -T Bag::remove( size_t k ) { - Node *prev, *curr; - size_t i=0; - T item; - - prev = nullptr; - curr = list; - while ( curr && (i < k) ) { - prev = curr; - curr = curr->next; - ++i; - } - - if ( curr &&( i == k) ) { - item = curr->item; - if ( prev ) - prev->next = curr->next; - else - list = curr->next;; - delete curr; - N--; - } - - return item; - } - -// 1.3.21 -// Returns true if the specified key is in the bag. -template -bool Bag::find( T key ) { - Node *curr = list; - - while ( curr ) { - if ( curr->item == key ) - return true; - curr = curr->next; - } - - return false; - } - -#endif - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef BAG_MAIN - #include int main ( int argc, char **argv ) { @@ -197,5 +22,3 @@ } } - -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Bag.hpp --- a/algs4-c++/src/Bag.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Bag.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,16 +1,16 @@ +// Linked list based bag after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 + #ifndef BAG_HPP #define BAG_HPP #include -// Linked list based bag after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 - template class Bag { private: - //////////////////////////////////// +//////////////////////////////////// class Node { public: T item; @@ -53,6 +53,12 @@ iterator begin( void ); iterator end( void ); +#ifdef EXERCISES + T delete_bottom( void ); + T remove( size_t k ); + bool find( T key ); +#endif + private: size_t N; @@ -61,6 +67,172 @@ }; +//////////////////////////////////////////////////////////////////////////////// + + +template +Bag::Bag( void ) : + N (0), + list (nullptr) + { + } + +template +Bag::~Bag( void ) { + // This will leak memory, won't it? + while ( list ) { + Node * next = list->next; + delete list; + list = next; + } + } + +template +void Bag::add( T &item ) { + Node * node = new Node(); + node->item = item; + node->next = list; + list = node; + N++; + } + +template +bool Bag::is_empty( void ) { + return N == 0; + } + +template +size_t Bag::size( void ) { + return N; + } + +template +typename Bag::iterator Bag::begin( void ) { + return Bag::iterator( list ); + } + +template +typename Bag::iterator Bag::end( void ) { + return Bag::iterator( nullptr ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +Bag::iterator::iterator( Node *c ) : + curr (c) + { + } + +template +typename Bag::iterator & Bag::iterator::operator++() { + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return *this; + } + +template +typename Bag::iterator Bag::iterator::operator++( int ) { + auto t = Bag::iterator( *this ); + + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return t; + } + +template +T Bag::iterator::operator*() { + return this->curr->item; + } + +template +bool Bag::iterator::operator!=( typename Bag::iterator other ) { + return this->curr != other.curr; + } + +//////////////////////////////////////////////////////////////////////////////// + + +#ifdef EXERCISES + +// 1.3.19 +// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. +template +T Bag::delete_bottom( void ) { + Node *prev, *curr; + T item; + + prev = nullptr; + curr = list; + if ( curr ) { + while ( curr->next ) { + prev = curr; + curr = curr->next; + } + + item = curr->item; + delete curr; + if ( prev ) + prev->next = nullptr; + else + list = nullptr; + + N--; + } + + return item; + } + + +// 1.3.20 +// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. +template +T Bag::remove( size_t k ) { + Node *prev, *curr; + size_t i=0; + T item; + + prev = nullptr; + curr = list; + while ( curr && (i < k) ) { + prev = curr; + curr = curr->next; + ++i; + } + + if ( curr &&( i == k) ) { + item = curr->item; + if ( prev ) + prev->next = curr->next; + else + list = curr->next;; + delete curr; + N--; + } + + return item; + } + +// 1.3.21 +// Returns true if the specified key is in the bag. +template +bool Bag::find( T key ) { + Node *curr = list; + + while ( curr ) { + if ( curr->item == key ) + return true; + curr = curr->next; + } + + return false; + } #endif + +#endif + diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Deque.cpp --- a/algs4-c++/src/Deque.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Deque.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,170 +1,8 @@ -// g++ -D DEQUE_MAIN -std=c++0x Deque.cpp -// g++ -D DEQUE_MAIN -std=c++11 Deque.cpp +// g++ -std=c++11 Deque.cpp #include "Deque.hpp" -#include -#include - -#include - -template -Deque::Deque( void ) : - N (0), - first (nullptr), - last (nullptr) - { - } - -template -Deque::~Deque( void ) { - assert( N == 0 ); // Catch the potential memory leak. - while ( first ) { - Node *next = first->next; - delete first; - first = next; - } - } - -template -void Deque::push_right( T &item ) { - Node * node = new Node(); - node->item = item; - node->next = nullptr; - node->prev = nullptr; - - if ( last ) { - last->next = node; - node->prev = last; - last = node; - } - else { - last = first = node; - } - - N++; - } - -template -void Deque::push_left( T &item ) { - Node * node = new Node(); - node->item = item; - node->next = nullptr; - node->prev = nullptr; - - if ( first ) { - node->next = first; - first->prev = node; - first = node; - } - else { - last = first = node; - } - - N++; - } - -template -T Deque::pop_left( void ) { - assert( N > 0 ); - - T item = first->item; - - Node *old = first; - if ( first->next ) - first->next->prev = nullptr; - first = first->next; - delete old; - N--; - - if ( ! first ) - last = nullptr; - - return item; - } - -template -T Deque::pop_right( void ) { - assert( N > 0 ); - - T item = last->item; - - Node *old = last; - if ( last->prev ) - last->prev->next = nullptr; - last = last->prev; - delete old; - N--; - - if ( ! last ) - first = nullptr; - - return item; - } - -template -bool Deque::is_empty( void ) { - return N == 0; - } - -template -size_t Deque::size( void ) { - return N; - } - -template -typename Deque::iterator Deque::begin( void ) { - return Deque::iterator( first ); - } - -template -typename Deque::iterator Deque::end( void ) { - return Deque::iterator( nullptr ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -Deque::iterator::iterator( Node *c ) : - curr (c) - { - } - -template -typename Deque::iterator & Deque::iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Deque::iterator Deque::iterator::operator++( int ) { - auto t = Deque::iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -T Deque::iterator::operator*() { - return this->curr->item; - } - -template -bool Deque::iterator::operator!=( typename Deque::iterator other ) { - return this->curr != other.curr; - } - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef DEQUE_MAIN - #include int main ( int argc, char **argv ) { @@ -211,5 +49,3 @@ std::cout << "Deque has " << deque.size() << " entries." << std::endl; } - -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Deque.hpp --- a/algs4-c++/src/Deque.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Deque.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,10 +1,11 @@ +// Linked list based deque after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.3 + #ifndef DEQUE_HPP #define DEQUE_HPP +#include #include -// Linked list based deque after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.3 - template class Deque { @@ -66,6 +67,158 @@ }; +//////////////////////////////////////////////////////////////////////////////// + +template +Deque::Deque( void ) : + N (0), + first (nullptr), + last (nullptr) + { + } + +template +Deque::~Deque( void ) { + assert( N == 0 ); // Catch the potential memory leak. + while ( first ) { + Node *next = first->next; + delete first; + first = next; + } + } + +template +void Deque::push_right( T &item ) { + Node * node = new Node(); + node->item = item; + node->next = nullptr; + node->prev = nullptr; + + if ( last ) { + last->next = node; + node->prev = last; + last = node; + } + else { + last = first = node; + } + + N++; + } + +template +void Deque::push_left( T &item ) { + Node * node = new Node(); + node->item = item; + node->next = nullptr; + node->prev = nullptr; + + if ( first ) { + node->next = first; + first->prev = node; + first = node; + } + else { + last = first = node; + } + + N++; + } + +template +T Deque::pop_left( void ) { + assert( N > 0 ); + + T item = first->item; + + Node *old = first; + if ( first->next ) + first->next->prev = nullptr; + first = first->next; + delete old; + N--; + + if ( ! first ) + last = nullptr; + + return item; + } + +template +T Deque::pop_right( void ) { + assert( N > 0 ); + + T item = last->item; + + Node *old = last; + if ( last->prev ) + last->prev->next = nullptr; + last = last->prev; + delete old; + N--; + + if ( ! last ) + first = nullptr; + + return item; + } + +template +bool Deque::is_empty( void ) { + return N == 0; + } + +template +size_t Deque::size( void ) { + return N; + } + +template +typename Deque::iterator Deque::begin( void ) { + return Deque::iterator( first ); + } + +template +typename Deque::iterator Deque::end( void ) { + return Deque::iterator( nullptr ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +Deque::iterator::iterator( Node *c ) : + curr (c) + { + } + +template +typename Deque::iterator & Deque::iterator::operator++() { + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return *this; + } + +template +typename Deque::iterator Deque::iterator::operator++( int ) { + auto t = Deque::iterator( *this ); + + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return t; + } + +template +T Deque::iterator::operator*() { + return this->curr->item; + } + +template +bool Deque::iterator::operator!=( typename Deque::iterator other ) { + return this->curr != other.curr; + } #endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Queue.hpp --- a/algs4-c++/src/Queue.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Queue.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -5,8 +5,6 @@ #include #include -#include - template class Queue { diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/RandomBag.cpp --- a/algs4-c++/src/RandomBag.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/RandomBag.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,122 +1,7 @@ -// g++ -D RANDOMBAG_MAIN -std=c++11 RandomBag.cpp - +// g++ -std=c++11 RandomBag.cpp #include "RandomBag.hpp" -#include -#include -#include -#include -#include -#include -#include -#include - - -template -RandomBag::RandomBag( void ) : - N (0), - max(1) { - data = new T[1]; - } - -template -RandomBag::~RandomBag( void ) { - // May leak memory. - if ( data ) - delete[] data; - } - -template -void RandomBag::add( T &item ) { - if ( max == N ) - resize( 2 * max ); - data[N++] = item; - } - -template -bool RandomBag::is_empty( void ) { - return N == 0; - } - -template -size_t RandomBag::size( void ) { - return N; - } - -template -void RandomBag::resize( size_t new_max ) { - T *new_data = new T[new_max]; - for ( size_t i = 0; i < N; ++i ) - new_data[i] = data[i]; - T *old_data = data; - data = new_data; - delete[] old_data; - max = new_max; - } - -template -typename RandomBag::iterator RandomBag::begin( void ) { - return RandomBag::iterator( data, data, data+N ); - } - -template -typename RandomBag::iterator RandomBag::end( void ) { - return RandomBag::iterator( data, data+N, data+N ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -RandomBag::iterator::iterator( T *b, T *f, T *e ) : - begin (b), - first (f), - end (e), - curr (0) { - size_t size = e - f; - for ( size_t i = 0; i < size; ++i ) { - order.push_back( (first - begin) + i ); - } - - unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); - shuffle (order.begin(), order.end(), std::default_random_engine(seed)); - - order.push_back( end - begin ); - } - -template -typename RandomBag::iterator & RandomBag::iterator::operator++() { - if ( this->curr < this->end - this->first ) { - this->curr += 1; - } - return *this; - } - -template -typename RandomBag::iterator RandomBag::iterator::operator++( int ) { - auto t = RandomBag::iterator( *this ); - if ( this->curr < this->end - this->first ) { - this->curr += 1; - } - return t; - } - -template -T RandomBag::iterator::operator*() { - return *(this->begin + order[curr]); - } - -template -bool RandomBag::iterator::operator!=( typename RandomBag::iterator other ) { - return (this->begin + this->order[this->curr]) != (other.begin + other.order[other.curr]); - } - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef RANDOMBAG_MAIN - #include int main ( int argc, char **argv ) { @@ -138,5 +23,3 @@ } - -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/RandomBag.hpp --- a/algs4-c++/src/RandomBag.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/RandomBag.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,7 +1,14 @@ +// Bag class with random iteration order. + #ifndef RANDOMBAG_HPP #define RANDOMBAG_HPP +#include #include +#include +#include +#include +#include #include template @@ -53,6 +60,108 @@ }; +//////////////////////////////////////////////////////////////////////////////// + +template +RandomBag::RandomBag( void ) : + N (0), + max(1) { + data = new T[1]; + } + +template +RandomBag::~RandomBag( void ) { + // May leak memory. + if ( data ) + delete[] data; + } + +template +void RandomBag::add( T &item ) { + if ( max == N ) + resize( 2 * max ); + data[N++] = item; + } + +template +bool RandomBag::is_empty( void ) { + return N == 0; + } + +template +size_t RandomBag::size( void ) { + return N; + } + +template +void RandomBag::resize( size_t new_max ) { + T *new_data = new T[new_max]; + for ( size_t i = 0; i < N; ++i ) + new_data[i] = data[i]; + T *old_data = data; + data = new_data; + delete[] old_data; + max = new_max; + } + +template +typename RandomBag::iterator RandomBag::begin( void ) { + return RandomBag::iterator( data, data, data+N ); + } + +template +typename RandomBag::iterator RandomBag::end( void ) { + return RandomBag::iterator( data, data+N, data+N ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +RandomBag::iterator::iterator( T *b, T *f, T *e ) : + begin (b), + first (f), + end (e), + curr (0) { + size_t size = e - f; + for ( size_t i = 0; i < size; ++i ) { + order.push_back( (first - begin) + i ); + } + + unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); + shuffle (order.begin(), order.end(), std::default_random_engine(seed)); + + order.push_back( end - begin ); + } + +template +typename RandomBag::iterator & RandomBag::iterator::operator++() { + if ( this->curr < this->end - this->first ) { + this->curr += 1; + } + return *this; + } + +template +typename RandomBag::iterator RandomBag::iterator::operator++( int ) { + auto t = RandomBag::iterator( *this ); + if ( this->curr < this->end - this->first ) { + this->curr += 1; + } + return t; + } + +template +T RandomBag::iterator::operator*() { + return *(this->begin + order[curr]); + } + +template +bool RandomBag::iterator::operator!=( typename RandomBag::iterator other ) { + return (this->begin + this->order[this->curr]) != (other.begin + other.order[other.curr]); + } + + #endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/ResizingArrayDeque.cpp --- a/algs4-c++/src/ResizingArrayDeque.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/ResizingArrayDeque.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,216 +1,8 @@ -// g++ -D RESIZINGARRAYDEQUE_MAIN -std=c++0x ResizingArrayDeque.cpp -// g++ -D RESIZINGARRAYDEQUE_MAIN -std=c++11 ResizingArrayDeque.cpp +// g++ -std=c++11 ResizingArrayDeque.cpp #include "ResizingArrayDeque.hpp" -#include -#include - -#include - -template -ResizingArrayDeque::ResizingArrayDeque( void ) : - N (0), - max(1), - first(nullptr), - last(nullptr) { - data = new T[2]; - } - -template -ResizingArrayDeque::~ResizingArrayDeque( void ) { - assert( N == 0 ); // Catch the potential memory leak. - if ( data ) - delete[] data; - } - -template -void ResizingArrayDeque::push_right( T &item ) { - if ( N == max ) - resize( 2 * max ); - - if ( last ) { - if ( last == data ) - last = data + max - 1; - else - ++last; - } - else { - first = last = data; - } - - *last = item; - ++N; - } - -template -void ResizingArrayDeque::push_left( T &item ) { - if ( N == max ) - resize( 2 * max ); - - if ( first ) { - if ( first == data ) - first = data + max - 1; - else - --first; - } - else { - first = last = data; - } - - *first = item; - ++N; - } - -template -T ResizingArrayDeque::pop_right( void ) { - assert( N > 0 ); - - T item = *last; - N--; - - if ( last == data ) { - last = data + max - 1; - } - else { - --last; - } - - if ( N == 0 ) - first = last = nullptr; - - // if ( N <= max/4 ) - // resize( max/2 ); - - return item; - } - -template -T ResizingArrayDeque::pop_left( void ) { - assert( N > 0 ); - - T item = *first; - N--; - - if ( first == data + max - 1 ) { - first = data; - } - else { - ++first; - } - - if ( N == 0 ) - first = last = nullptr; - - // if ( N <= max/4 ) - // resize( max/2 ); - - return item; - } - -template -bool ResizingArrayDeque::is_empty( void ) { - return N == 0; - } - -template -size_t ResizingArrayDeque::size( void ) { - return N; - } - -template -void ResizingArrayDeque::resize( size_t new_max ) { - T *new_data = new T[new_max]; - - if ( N ) { - size_t i = 0; - T *curr = first; - while ( i < N ) { - new_data[i] = *curr; - ++curr; - if ( curr == data + max ) - curr = data; - ++i; - } - } - first = new_data; - last = new_data + N - 1; - - T *old_data = data; - data = new_data; - delete[] old_data; - - max = new_max; - } - -template -typename ResizingArrayDeque::iterator ResizingArrayDeque::begin( void ) { - return ResizingArrayDeque::iterator( first, last, data, data + max - 1 ); - } - -template -typename ResizingArrayDeque::iterator ResizingArrayDeque::end( void ) { - return ResizingArrayDeque::iterator( nullptr, nullptr, nullptr, nullptr ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -ResizingArrayDeque::iterator::iterator( T *c, T *l, T *b, T *m ) : - curr (c), - last (l), - begin (b), - max (m) - { - } - -template -typename ResizingArrayDeque::iterator & ResizingArrayDeque::iterator::operator++() { - if ( this->curr == this->last ) { - this->curr = nullptr; - } - else { - this->curr += 1; - if ( this->curr > this->max ) - this->curr = this->begin; - } - - return *this; - } - -template -typename ResizingArrayDeque::iterator ResizingArrayDeque::iterator::operator++( int ) { - auto t = ResizingArrayDeque::iterator( *this ); - - if ( this->curr == this->last ) { - this->curr = nullptr; - } - else { - this->curr += 1; - if ( this->curr > this->max ) - this->curr = this->begin; - } - - return t; - } - -template -T ResizingArrayDeque::iterator::operator*() { - return *(this->curr); - } - -template -bool ResizingArrayDeque::iterator::operator!=( typename ResizingArrayDeque::iterator other ) { - return this->curr != other.curr; - } - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef RESIZINGARRAYDEQUE_MAIN - #include int main ( int argc, char **argv ) { @@ -258,4 +50,3 @@ } -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/ResizingArrayDeque.hpp --- a/algs4-c++/src/ResizingArrayDeque.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/ResizingArrayDeque.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,6 +1,9 @@ +// Double ended queue built on a resizing array. + #ifndef RESIZINGARRAYDEQUE_HPP #define RESIZINGARRAYDEQUE_HPP +#include #include template @@ -54,6 +57,206 @@ void resize( size_t new_max ); }; +//////////////////////////////////////////////////////////////////////////////// + +template +ResizingArrayDeque::ResizingArrayDeque( void ) : + N (0), + max(1), + first(nullptr), + last(nullptr) { + data = new T[2]; + } + +template +ResizingArrayDeque::~ResizingArrayDeque( void ) { + assert( N == 0 ); // Catch the potential memory leak. + if ( data ) + delete[] data; + } + +template +void ResizingArrayDeque::push_right( T &item ) { + if ( N == max ) + resize( 2 * max ); + + if ( last ) { + if ( last == data ) + last = data + max - 1; + else + ++last; + } + else { + first = last = data; + } + + *last = item; + ++N; + } + +template +void ResizingArrayDeque::push_left( T &item ) { + if ( N == max ) + resize( 2 * max ); + + if ( first ) { + if ( first == data ) + first = data + max - 1; + else + --first; + } + else { + first = last = data; + } + + *first = item; + ++N; + } + +template +T ResizingArrayDeque::pop_right( void ) { + assert( N > 0 ); + + T item = *last; + N--; + + if ( last == data ) { + last = data + max - 1; + } + else { + --last; + } + + if ( N == 0 ) + first = last = nullptr; + + // if ( N <= max/4 ) + // resize( max/2 ); + + return item; + } + +template +T ResizingArrayDeque::pop_left( void ) { + assert( N > 0 ); + + T item = *first; + N--; + + if ( first == data + max - 1 ) { + first = data; + } + else { + ++first; + } + + if ( N == 0 ) + first = last = nullptr; + + // if ( N <= max/4 ) + // resize( max/2 ); + + return item; + } + +template +bool ResizingArrayDeque::is_empty( void ) { + return N == 0; + } + +template +size_t ResizingArrayDeque::size( void ) { + return N; + } + +template +void ResizingArrayDeque::resize( size_t new_max ) { + T *new_data = new T[new_max]; + + if ( N ) { + size_t i = 0; + T *curr = first; + while ( i < N ) { + new_data[i] = *curr; + ++curr; + if ( curr == data + max ) + curr = data; + ++i; + } + } + first = new_data; + last = new_data + N - 1; + + T *old_data = data; + data = new_data; + delete[] old_data; + + max = new_max; + } + +template +typename ResizingArrayDeque::iterator ResizingArrayDeque::begin( void ) { + return ResizingArrayDeque::iterator( first, last, data, data + max - 1 ); + } + +template +typename ResizingArrayDeque::iterator ResizingArrayDeque::end( void ) { + return ResizingArrayDeque::iterator( nullptr, nullptr, nullptr, nullptr ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +ResizingArrayDeque::iterator::iterator( T *c, T *l, T *b, T *m ) : + curr (c), + last (l), + begin (b), + max (m) + { + } + +template +typename ResizingArrayDeque::iterator & ResizingArrayDeque::iterator::operator++() { + if ( this->curr == this->last ) { + this->curr = nullptr; + } + else { + this->curr += 1; + if ( this->curr > this->max ) + this->curr = this->begin; + } + + return *this; + } + +template +typename ResizingArrayDeque::iterator ResizingArrayDeque::iterator::operator++( int ) { + auto t = ResizingArrayDeque::iterator( *this ); + + if ( this->curr == this->last ) { + this->curr = nullptr; + } + else { + this->curr += 1; + if ( this->curr > this->max ) + this->curr = this->begin; + } + + return t; + } + +template +T ResizingArrayDeque::iterator::operator*() { + return *(this->curr); + } + +template +bool ResizingArrayDeque::iterator::operator!=( typename ResizingArrayDeque::iterator other ) { + return this->curr != other.curr; + } + + #endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/ResizingArrayStack.cpp --- a/algs4-c++/src/ResizingArrayStack.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/ResizingArrayStack.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,120 +1,8 @@ -// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++0x ResizingArrayStack.cpp -// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++11 ResizingArrayStack.cpp +// g++ -std=c++11 ResizingArrayStack.cpp #include "ResizingArrayStack.hpp" -#include -#include - -#include - -template -ResizingArrayStack::ResizingArrayStack( void ) : - N (0), - max(1) { - data = new T[1]; - } - -template -ResizingArrayStack::~ResizingArrayStack( void ) { - assert( N == 0 ); // Catch the potential memory leak. - if ( data ) - delete[] data; - } - -template -void ResizingArrayStack::push( T &item ) { - if ( max == N ) - resize( 2 * max ); - data[N++] = item; - } - -template -T ResizingArrayStack::pop( void ) { - assert( N > 0 ); - T item = data[N-1]; - N--; - - if ( N <= max/4 ) - resize( max/2 ); - - return item; - } - -template -bool ResizingArrayStack::is_empty( void ) { - return N == 0; - } - -template -size_t ResizingArrayStack::size( void ) { - return N; - } - -template -void ResizingArrayStack::resize( size_t new_max ) { - T *new_data = new T[new_max]; - for ( size_t i = 0; i < N; ++i ) - new_data[i] = data[i]; - T *old_data = data; - data = new_data; - delete[] old_data; - max = new_max; - } - -template -typename ResizingArrayStack::iterator ResizingArrayStack::begin( void ) { - return ResizingArrayStack::iterator( data+N-1, data-1 ); - } - -template -typename ResizingArrayStack::iterator ResizingArrayStack::end( void ) { - return ResizingArrayStack::iterator( data-1, data-1 ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -ResizingArrayStack::iterator::iterator( T *c, T *e ) : - curr (c), - end (e) - { - } - -template -typename ResizingArrayStack::iterator & ResizingArrayStack::iterator::operator++() { - if ( this->curr > this->end ) { - this->curr -= 1; - } - return *this; - } - -template -typename ResizingArrayStack::iterator ResizingArrayStack::iterator::operator++( int ) { - auto t = ResizingArrayStack::iterator( *this ); - if ( this->curr > this->end ) { - this->curr -= 1; - } - return t; - } - -template -T ResizingArrayStack::iterator::operator*() { - return *(this->curr); - } - -template -bool ResizingArrayStack::iterator::operator!=( typename ResizingArrayStack::iterator other ) { - return this->curr != other.curr; - } - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef RESIZINGARRAYSTACK_MAIN - #include int main ( int argc, char **argv ) { @@ -145,4 +33,3 @@ } -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/ResizingArrayStack.hpp --- a/algs4-c++/src/ResizingArrayStack.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/ResizingArrayStack.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,6 +1,9 @@ +// Stack built on a resizing array. + #ifndef RESIZINGARRAYSTACK_HPP #define RESIZINGARRAYSTACK_HPP +#include #include template @@ -49,6 +52,109 @@ void resize( size_t new_max ); }; +//////////////////////////////////////////////////////////////////////////////// + +template +ResizingArrayStack::ResizingArrayStack( void ) : + N (0), + max(1) { + data = new T[1]; + } + +template +ResizingArrayStack::~ResizingArrayStack( void ) { + assert( N == 0 ); // Catch the potential memory leak. + if ( data ) + delete[] data; + } + +template +void ResizingArrayStack::push( T &item ) { + if ( max == N ) + resize( 2 * max ); + data[N++] = item; + } + +template +T ResizingArrayStack::pop( void ) { + assert( N > 0 ); + T item = data[N-1]; + N--; + + if ( N <= max/4 ) + resize( max/2 ); + + return item; + } + +template +bool ResizingArrayStack::is_empty( void ) { + return N == 0; + } + +template +size_t ResizingArrayStack::size( void ) { + return N; + } + +template +void ResizingArrayStack::resize( size_t new_max ) { + T *new_data = new T[new_max]; + for ( size_t i = 0; i < N; ++i ) + new_data[i] = data[i]; + T *old_data = data; + data = new_data; + delete[] old_data; + max = new_max; + } + +template +typename ResizingArrayStack::iterator ResizingArrayStack::begin( void ) { + return ResizingArrayStack::iterator( data+N-1, data-1 ); + } + +template +typename ResizingArrayStack::iterator ResizingArrayStack::end( void ) { + return ResizingArrayStack::iterator( data-1, data-1 ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +ResizingArrayStack::iterator::iterator( T *c, T *e ) : + curr (c), + end (e) + { + } + +template +typename ResizingArrayStack::iterator & ResizingArrayStack::iterator::operator++() { + if ( this->curr > this->end ) { + this->curr -= 1; + } + return *this; + } + +template +typename ResizingArrayStack::iterator ResizingArrayStack::iterator::operator++( int ) { + auto t = ResizingArrayStack::iterator( *this ); + if ( this->curr > this->end ) { + this->curr -= 1; + } + return t; + } + +template +T ResizingArrayStack::iterator::operator*() { + return *(this->curr); + } + +template +bool ResizingArrayStack::iterator::operator!=( typename ResizingArrayStack::iterator other ) { + return this->curr != other.curr; + } + #endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Stack.cpp --- a/algs4-c++/src/Stack.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Stack.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,196 +1,8 @@ -// g++ -D STACK_MAIN -std=c++0x Stack.cpp -// g++ -D STACK_MAIN -std=c++11 Stack.cpp +// g++ -std=c++11 Stack.cpp #include "Stack.hpp" -#include -#include - -#include - -template -Stack::Stack( void ) : - N (0), - list (nullptr) - { - } - -template -Stack::~Stack( void ) { - assert( N == 0 ); // Catch the potential memory leak. - while ( list ) { - Node * next = list->next; - delete list; - list = next; - } - } - -template -void Stack::push( T &item ) { - Node * node = new Node(); - node->item = item; - node->next = list; - list = node; - N++; - } - -template -T Stack::pop( void ) { - assert( N > 0 ); - - T item = list->item; - - Node *old = list; - list = list->next; - delete old; - N--; - - return item; - } - -template -bool Stack::is_empty( void ) { - return N == 0; - } - -template -size_t Stack::size( void ) { - return N; - } - -template -typename Stack::iterator Stack::begin( void ) { - return Stack::iterator( list ); - } - -template -typename Stack::iterator Stack::end( void ) { - return Stack::iterator( nullptr ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -Stack::iterator::iterator( Node *c ) : - curr (c) - { - } - -template -typename Stack::iterator & Stack::iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Stack::iterator Stack::iterator::operator++( int ) { - auto t = Stack::iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -T Stack::iterator::operator*() { - return this->curr->item; - } - -template -bool Stack::iterator::operator!=( typename Stack::iterator other ) { - return this->curr != other.curr; - } - -//////////////////////////////////////////////////////////////////////////////// - - -#ifdef EXERCISES - -// 1.3.19 -// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. -template -T Stack::delete_bottom( void ) { - Node *prev, *curr; - T item; - - prev = nullptr; - curr = list; - if ( curr ) { - while ( curr->next ) { - prev = curr; - curr = curr->next; - } - - item = curr->item; - delete curr; - if ( prev ) - prev->next = nullptr; - else - list = nullptr; - - N--; - } - - return item; - } - - -// 1.3.20 -// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. -template -T Stack::remove( size_t k ) { - Node *prev, *curr; - size_t i=0; - T item; - - prev = nullptr; - curr = list; - while ( curr && (i < k) ) { - prev = curr; - curr = curr->next; - ++i; - } - - if ( curr &&( i == k) ) { - item = curr->item; - if ( prev ) - prev->next = curr->next; - else - list = curr->next;; - delete curr; - N--; - } - - return item; - } - -// 1.3.21 -// Returns true if the specified key is in the stack. -template -bool Stack::find( T key ) { - Node *curr = list; - - while ( curr ) { - if ( curr->item == key ) - return true; - curr = curr->next; - } - - return false; - } - -#endif - - -//////////////////////////////////////////////////////////////////////////////// - -#ifdef STACK_MAIN - #include int main ( int argc, char **argv ) { @@ -244,5 +56,3 @@ std::cout << "Stack has " << stack.size() << " entries." << std::endl; } - -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Stack.hpp --- a/algs4-c++/src/Stack.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Stack.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,9 +1,11 @@ +// Linked list based stack after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 + #ifndef STACK_HPP #define STACK_HPP +#include #include -// Linked list based stack after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 template class Stack { @@ -70,6 +72,186 @@ }; +//////////////////////////////////////////////////////////////////////////////// + + +template +Stack::Stack( void ) : + N (0), + list (nullptr) + { + } + +template +Stack::~Stack( void ) { + assert( N == 0 ); // Catch the potential memory leak. + while ( list ) { + Node * next = list->next; + delete list; + list = next; + } + } + +template +void Stack::push( T &item ) { + Node * node = new Node(); + node->item = item; + node->next = list; + list = node; + N++; + } + +template +T Stack::pop( void ) { + assert( N > 0 ); + + T item = list->item; + + Node *old = list; + list = list->next; + delete old; + N--; + + return item; + } + +template +bool Stack::is_empty( void ) { + return N == 0; + } + +template +size_t Stack::size( void ) { + return N; + } + +template +typename Stack::iterator Stack::begin( void ) { + return Stack::iterator( list ); + } + +template +typename Stack::iterator Stack::end( void ) { + return Stack::iterator( nullptr ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +Stack::iterator::iterator( Node *c ) : + curr (c) + { + } + +template +typename Stack::iterator & Stack::iterator::operator++() { + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return *this; + } + +template +typename Stack::iterator Stack::iterator::operator++( int ) { + auto t = Stack::iterator( *this ); + + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return t; + } + +template +T Stack::iterator::operator*() { + return this->curr->item; + } + +template +bool Stack::iterator::operator!=( typename Stack::iterator other ) { + return this->curr != other.curr; + } + +//////////////////////////////////////////////////////////////////////////////// + + +#ifdef EXERCISES + +// 1.3.19 +// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. +template +T Stack::delete_bottom( void ) { + Node *prev, *curr; + T item; + + prev = nullptr; + curr = list; + if ( curr ) { + while ( curr->next ) { + prev = curr; + curr = curr->next; + } + + item = curr->item; + delete curr; + if ( prev ) + prev->next = nullptr; + else + list = nullptr; + + N--; + } + + return item; + } + + +// 1.3.20 +// Returns the item if entry was found. If the entry did not exist, then the returned value is undefined. +template +T Stack::remove( size_t k ) { + Node *prev, *curr; + size_t i=0; + T item; + + prev = nullptr; + curr = list; + while ( curr && (i < k) ) { + prev = curr; + curr = curr->next; + ++i; + } + + if ( curr &&( i == k) ) { + item = curr->item; + if ( prev ) + prev->next = curr->next; + else + list = curr->next;; + delete curr; + N--; + } + + return item; + } + +// 1.3.21 +// Returns true if the specified key is in the stack. +template +bool Stack::find( T key ) { + Node *curr = list; + + while ( curr ) { + if ( curr->item == key ) + return true; + curr = curr->next; + } + + return false; + } #endif + +#endif + diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Steque.cpp --- a/algs4-c++/src/Steque.cpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Steque.cpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,141 +1,8 @@ -// g++ -D STEQUE_MAIN -std=c++0x Steque.cpp -// g++ -D STEQUE_MAIN -std=c++11 Steque.cpp +// g++ -std=c++11 Steque.cpp #include "Steque.hpp" -#include -#include - -#include - -template -Steque::Steque( void ) : - N (0), - list (nullptr) - { - } - -template -Steque::~Steque( void ) { - assert( N == 0 ); // Catch the potential memory leak. - while ( list ) { - Node * next = list->next; - delete list; - list = next; - } - } - -template -void Steque::push( T &item ) { - Node * node = new Node(); - node->item = item; - node->next = list; - list = node; - N++; - } - -template -T Steque::pop( void ) { - assert( N > 0 ); - - T item = list->item; - - Node *old = list; - list = list->next; - delete old; - N--; - - return item; - } - -template -void Steque::enqueue( T &item ) { - Node *curr = list; - - Node *node = new Node(); - node->item = item; - node->next = nullptr; - - if ( ! curr ) { - list = node; - N++; - return; - } - - while ( curr->next ) { - curr = curr->next; - } - - curr->next = node; - N++; - - return; - } - -template -bool Steque::is_empty( void ) { - return N == 0; - } - -template -size_t Steque::size( void ) { - return N; - } - -template -typename Steque::iterator Steque::begin( void ) { - return Steque::iterator( list ); - } - -template -typename Steque::iterator Steque::end( void ) { - return Steque::iterator( nullptr ); - } - - -//////////////////////////////////////////////////////////////////////////////// - -template -Steque::iterator::iterator( Node *c ) : - curr (c) - { - } - -template -typename Steque::iterator & Steque::iterator::operator++() { - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return *this; - } - -template -typename Steque::iterator Steque::iterator::operator++( int ) { - auto t = Steque::iterator( *this ); - - if ( this->curr != nullptr ) { - this->curr = this->curr->next; - } - return t; - } - -template -T Steque::iterator::operator*() { - return this->curr->item; - } - -template -bool Steque::iterator::operator!=( typename Steque::iterator other ) { - return this->curr != other.curr; - } - -//////////////////////////////////////////////////////////////////////////////// - - - -#ifdef STEQUE_MAIN - #include int main ( int argc, char **argv ) { @@ -174,5 +41,3 @@ std::cout << "Steque has " << steque.size() << " entries." << std::endl; } - -#endif diff -r 86281e8f24ba -r ec700842d182 algs4-c++/src/Steque.hpp --- a/algs4-c++/src/Steque.hpp Mon Jun 22 15:38:25 2015 -0500 +++ b/algs4-c++/src/Steque.hpp Tue Jun 23 10:17:43 2015 -0500 @@ -1,10 +1,11 @@ +// Linked list based steque after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 + #ifndef STEQUE_HPP #define STEQUE_HPP +#include #include -// Linked list based steque after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 - template class Steque { @@ -63,6 +64,129 @@ }; +//////////////////////////////////////////////////////////////////////////////// + +template +Steque::Steque( void ) : + N (0), + list (nullptr) + { + } + +template +Steque::~Steque( void ) { + assert( N == 0 ); // Catch the potential memory leak. + while ( list ) { + Node * next = list->next; + delete list; + list = next; + } + } + +template +void Steque::push( T &item ) { + Node * node = new Node(); + node->item = item; + node->next = list; + list = node; + N++; + } + +template +T Steque::pop( void ) { + assert( N > 0 ); + + T item = list->item; + + Node *old = list; + list = list->next; + delete old; + N--; + + return item; + } + +template +void Steque::enqueue( T &item ) { + Node *curr = list; + + Node *node = new Node(); + node->item = item; + node->next = nullptr; + + if ( ! curr ) { + list = node; + N++; + return; + } + + while ( curr->next ) { + curr = curr->next; + } + + curr->next = node; + N++; + + return; + } + +template +bool Steque::is_empty( void ) { + return N == 0; + } + +template +size_t Steque::size( void ) { + return N; + } + +template +typename Steque::iterator Steque::begin( void ) { + return Steque::iterator( list ); + } + +template +typename Steque::iterator Steque::end( void ) { + return Steque::iterator( nullptr ); + } + + +//////////////////////////////////////////////////////////////////////////////// + +template +Steque::iterator::iterator( Node *c ) : + curr (c) + { + } + +template +typename Steque::iterator & Steque::iterator::operator++() { + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return *this; + } + +template +typename Steque::iterator Steque::iterator::operator++( int ) { + auto t = Steque::iterator( *this ); + + if ( this->curr != nullptr ) { + this->curr = this->curr->next; + } + return t; + } + +template +T Steque::iterator::operator*() { + return this->curr->item; + } + +template +bool Steque::iterator::operator!=( typename Steque::iterator other ) { + return this->curr != other.curr; + } + #endif