|
COT 5405 Advanced Algorithms Chris Lacher Notes 5: Sequential ADTs and Data Structures |
Abstract Container Types Container P|A Iterator Distinguishing Operations Vector P RandomAccess PushBack, PopBack, SetSize, Indexed BracketOperator List P BiDirectional PushFront, PopFront, PushBack, PopBack, Insert, Remove Deque P RandomAccess PushFront, PopFront, PushBack, PopBack, Indexed BracketOperator Stack P Push, Pop, Top Queue P Push, Pop, Front PriorityQueue A Push, Pop, Front OUSet A BiDirectional Ordered UniSet: Insert, Remove, Includes OMSet A BiDirectional Ordered MultiSet: Insert, Remove, Includes UUSet A BiDirectional Unordered UniSet: Insert, Remove, Includes UMSet A BiDirectional Unordered MultiSet: Insert, Remove, Includes AssociativeArray A BiDirectional Unordered Associative Bracket Operator For each operation: behavior specification, runtime constraint, runspace constraint
For each container: reason(s) to select it over the other choices
template < typename T > class SContainer { public: // Proper type: default constructor, copy constructor, destructor, assignment operator // Terminology support: typedef T ValueType; typedef SContainerIterator<T> Iterator; typedef ConstSContainerIterator<T> ConstIterator; // tightly couple with Iterator // information accessors bool Empty () const; size_t Size () const; T& Front (); T& Back (); const T& Front () const; const T& Back () const; // mutators bool PushBack (const T&); bool PopBack (); void Clear (); // Iterator support // Note: Iterator and ConstIterator are at least a bidirectional Iterators Iterator Begin (); Iterator End (); Iterator rBegin (); Iterator rEnd (); ConstIterator Begin () const; ConstIterator End () const; ConstIterator rBegin () const; ConstIterator rEnd () const; } ;
template < typename T > class Vector { public: // all default SContainer operations, plus: bool SetSize (size_t); bool SetSize (size_t, const T&); bool SetCapacity (size_t); size_t Capacity () const; T& operator[] (size_t index); const T& operator[] (size_t index) const; // Iterator is random access iterator private: // variables size_t size_, // elements in vector capacity_; // size of underlying array data_ T* data_; // pointer to the primative array elements } ;
template < typename T > class List { public: // all default SContainer operations, plus: bool PushFront (const T&); bool PopFront (); bool Insert (Iterator& , const T&); bool Remove (Iterator&); // Iterator is bidirectional iterator private: // type class Link { friend class List; Link(const T& t) : value_(t), prevLink_(0), nextLink_(0){} T value_; Link * prevLink; Link * nextLink; }; // variables Link * firstLink_; Link * lastLink_; } ;
template < typename T > class Deque { public: // all default SContainer operations, plus: bool PushFront (const T&); bool PopFront (); T& operator[] (size_t index); const T& operator[] (size_t index) const; // Iterator is random access iterator private: // variables size_t beg_, // first element end_, // 1 past last element size_; // size allocated to data_ T * data_; // storage array } ;
template <typename T> class BidirectionalIterator { public: // terminology support typedef T ValueType; typedef BidirectionalIterator Iterator; // tightly couple with container // Proper type: default constructor, copy constructor, destructor, assignment // Bidirectional Iterator protocol bool operator == (const Iterator& i2) const; bool operator != (const Iterator& i2) const; T& operator * (); // Return reference to current Tval const T& operator * () const; // const version Iterator& operator = (const Iterator & i); Iterator& operator ++ (); // prefix Iterator operator ++ (int); // postfix Iterator& operator -- (); // prefix Iterator operator -- (int); // postfix } ;
template <typename T> class RandomAccessIterator { public: // Bidirectional Iterator, plus bracket operator and "pointer" arithmetic: T& operator [] (size_t index); // return reference to Tval at index const T& operator [] (size_t index) const; // const version long operator - (const Iterator & i2) const; template <typename N> Iterator operator + (N n) const; template <typename N> Iterator& operator += (N n); template <typename N> Iterator& operator -= (N n); } ;
template <typename T> class VectorIterator { friend class Vector<T> public: // Random Access Iterator private: T* Tptr_; } ;
template <typename T> class ListIterator { friend class List<T> public: // Bidirectional Iterator private: typename List::Link * Tptr_; } ;
template <typename T> class DequeIterator { friend class Deque<T> public: // Random Access Iterator private: typename Deque<T*> * myDeque_; size_t myIndex_; } ;
Vector List Deque PushFront(t) O(1) AO(1) PopFront() O(1) O(1) PushBack(t) AO(1) O(1) AO(1) PopBack() O(1) O(1) O(1) Insert(i,t) O(1) Remove(i) O(1) Resize(n) O(size) Iterator Support RA BD RA Search Algorithm Binary(*) Sequential Binary(*) --------- (*) if range is sorted, otherwise use sequential search Stack < T , Vector < T > > Stack < T , List < T > > Stack < T , Deque < T > > // default Queue < T , List < T > > Queue < T , Deque < T > > // default