Mercurial > Algorithms__Sedgewick
diff algs4-c++/src/ResizingArrayStack.cpp @ 18:a149b424b4e2
Updated all template classes to have the implementaiton in the header file.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Sat, 20 Jun 2015 19:36:11 -0500 |
parents | 3db1a894bbdf |
children |
line diff
1.1 --- a/algs4-c++/src/ResizingArrayStack.cpp Fri Jun 19 18:18:47 2015 -0500 1.2 +++ b/algs4-c++/src/ResizingArrayStack.cpp Sat Jun 20 19:36:11 2015 -0500 1.3 @@ -1,120 +1,8 @@ 1.4 -// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++0x ResizingArrayStack.cpp 1.5 -// g++ -D RESIZINGARRAYSTACK_MAIN -std=c++11 ResizingArrayStack.cpp 1.6 +// g++ -std=c++11 ResizingArrayStack.cpp 1.7 1.8 1.9 #include "ResizingArrayStack.hpp" 1.10 1.11 -#include <cassert> 1.12 -#include <cstddef> 1.13 - 1.14 -#include <iostream> 1.15 - 1.16 -template <typename T> 1.17 -ResizingArrayStack<T>::ResizingArrayStack( void ) : 1.18 - N (0), 1.19 - max(1) { 1.20 - data = new T[1]; 1.21 - } 1.22 - 1.23 -template <typename T> 1.24 -ResizingArrayStack<T>::~ResizingArrayStack( void ) { 1.25 - assert( N == 0 ); // Catch the potential memory leak. 1.26 - if ( data ) 1.27 - delete[] data; 1.28 - } 1.29 - 1.30 -template <typename T> 1.31 -void ResizingArrayStack<T>::push( T &item ) { 1.32 - if ( max == N ) 1.33 - resize( 2 * max ); 1.34 - data[N++] = item; 1.35 - } 1.36 - 1.37 -template <typename T> 1.38 -T ResizingArrayStack<T>::pop( void ) { 1.39 - assert( N > 0 ); 1.40 - T item = data[N-1]; 1.41 - N--; 1.42 - 1.43 - if ( N <= max/4 ) 1.44 - resize( max/2 ); 1.45 - 1.46 - return item; 1.47 - } 1.48 - 1.49 -template <typename T> 1.50 -bool ResizingArrayStack<T>::is_empty( void ) { 1.51 - return N == 0; 1.52 - } 1.53 - 1.54 -template <typename T> 1.55 -size_t ResizingArrayStack<T>::size( void ) { 1.56 - return N; 1.57 - } 1.58 - 1.59 -template <typename T> 1.60 -void ResizingArrayStack<T>::resize( size_t new_max ) { 1.61 - T *new_data = new T[new_max]; 1.62 - for ( size_t i = 0; i < N; ++i ) 1.63 - new_data[i] = data[i]; 1.64 - T *old_data = data; 1.65 - data = new_data; 1.66 - delete[] old_data; 1.67 - max = new_max; 1.68 - } 1.69 - 1.70 -template <typename T> 1.71 -typename ResizingArrayStack<T>::iterator ResizingArrayStack<T>::begin( void ) { 1.72 - return ResizingArrayStack<T>::iterator( data+N-1, data-1 ); 1.73 - } 1.74 - 1.75 -template <typename T> 1.76 -typename ResizingArrayStack<T>::iterator ResizingArrayStack<T>::end( void ) { 1.77 - return ResizingArrayStack<T>::iterator( data-1, data-1 ); 1.78 - } 1.79 - 1.80 - 1.81 -//////////////////////////////////////////////////////////////////////////////// 1.82 - 1.83 -template <typename T> 1.84 -ResizingArrayStack<T>::iterator::iterator( T *c, T *e ) : 1.85 - curr (c), 1.86 - end (e) 1.87 - { 1.88 - } 1.89 - 1.90 -template <typename T> 1.91 -typename ResizingArrayStack<T>::iterator & ResizingArrayStack<T>::iterator::operator++() { 1.92 - if ( this->curr > this->end ) { 1.93 - this->curr -= 1; 1.94 - } 1.95 - return *this; 1.96 - } 1.97 - 1.98 -template <typename T> 1.99 -typename ResizingArrayStack<T>::iterator ResizingArrayStack<T>::iterator::operator++( int ) { 1.100 - auto t = ResizingArrayStack<T>::iterator( *this ); 1.101 - if ( this->curr > this->end ) { 1.102 - this->curr -= 1; 1.103 - } 1.104 - return t; 1.105 - } 1.106 - 1.107 -template <typename T> 1.108 -T ResizingArrayStack<T>::iterator::operator*() { 1.109 - return *(this->curr); 1.110 - } 1.111 - 1.112 -template <typename T> 1.113 -bool ResizingArrayStack<T>::iterator::operator!=( typename ResizingArrayStack<T>::iterator other ) { 1.114 - return this->curr != other.curr; 1.115 - } 1.116 - 1.117 - 1.118 -//////////////////////////////////////////////////////////////////////////////// 1.119 - 1.120 -#ifdef RESIZINGARRAYSTACK_MAIN 1.121 - 1.122 #include <iostream> 1.123 1.124 int main ( int argc, char **argv ) { 1.125 @@ -145,4 +33,3 @@ 1.126 1.127 } 1.128 1.129 -#endif