Generic Associative Containers and Sorted Lists Andy Wang

  • Slides: 16
Download presentation
Generic Associative Containers and Sorted Lists Andy Wang Data Structures, Algorithms, and Generic Programming

Generic Associative Containers and Sorted Lists Andy Wang Data Structures, Algorithms, and Generic Programming

Generic Associative Containers n A generic container C n n C is a template

Generic Associative Containers n A generic container C n n C is a template class for any proper type T C<T> is a proper type C<T> is capable of storing arbitrary numbers of T objects C<T> supports an associated iterator class C<T>: : Iterator

Generic Associative Containers n Organized and accessible by value n n Client may insert

Generic Associative Containers n Organized and accessible by value n n Client may insert or remove any T object in C<T> Client may not determine position in C<T> Bidirectional Iterators a. Container for short

Multimodal vs. Unimodal Associative Containers n Multimodal associative container n n n Duplicate elements

Multimodal vs. Unimodal Associative Containers n Multimodal associative container n n n Duplicate elements are allowed Insert operations always increase size by 1 Unimodal associative container n n Duplicate elements not allowed Insert operations have dual personality If t is not in C, then Insert(t) n If t is in C, then overwrites the existing t n

Generic Sorted Associative Containers n A generic associative container Traverse elements in sorted order

Generic Sorted Associative Containers n A generic associative container Traverse elements in sorted order n for (C: : Iterator I = c. Begin(); I != c. End(); ++I) { cout << *I; } n n sa. Container for short May be either unimodal or multimodal

Sorted Associative Container Public Interface // element operations Iterator Insert(const T& t); int Insert(Iterator&

Sorted Associative Container Public Interface // element operations Iterator Insert(const T& t); int Insert(Iterator& I, const T& t); size_t Remove(const T& t); // remove all instances of t int Remove(Iterator& I); void Clear(); // locating elements Iterator Lower. Bound(const T& t) const; // returns an iterator that points to the 1 st instance of t Iterator Upper. Bound(const T& t) const; // returns an iterator that points to the element after the last instance of t Iterator Includes(const T&) const; // returns the lower bound if found // size operations int Empty() const; int Size() const; // // // logical operations iterator support for bidirectional iterators proper type

Sorted Associative Container Template template <typename T, class P> class sa. Container { P

Sorted Associative Container Template template <typename T, class P> class sa. Container { P predicate_object; public: // terminology support typedef T value_type; typedef sa. Container. Iterator<T, P> Iterator; // constructors sa. Container(); ~sa. Container(); sa. Container(const sa. Container<T, P> & c); // assignment sa. Container<T, P>& operator=(const sa. Container<T, P>& c);

Sorted Associative Container Template (2) // element operations Iterator Insert(const T& t); int Insert(Iterator&

Sorted Associative Container Template (2) // element operations Iterator Insert(const T& t); int Insert(Iterator& I, const T& t); size_t Remove(const T& t); int Remove(Iterator& I); void Clear(); // locating elements Iterator Lower. Bound(const T& t) const; Iterator Upper. Bound(const T& t) const; Iterator Includes(const T& t 0 const; // iterator support—bidirectional iterators Iterator Begin() const; Iterator End() const; Iterator r. Begin() const; Iterator r. End() const;

Sorted Associative Container Template (3) // size operations int Empty() const; int Size() const;

Sorted Associative Container Template (3) // size operations int Empty() const; int Size() const; }; template <typename T, class P> int operator==(const sa. Container<T, P>& L 1, const sa. Container<T, P>& L 2); template <typename T, class P> int operator!=(const sa. Container<T, P>& L 1, const sa. Container<T, P>& L 2);

Multimodal Sorted List Template Class template <typename T, class P = TLess. Than<T> >

Multimodal Sorted List Template Class template <typename T, class P = TLess. Than<T> > class TMSList : public TList<T> { public: typedef T value_type; typedef TList. Iterator<T> Iterator; // constructors TMSList(); TMSList(const TMSList&); // use the default destructor // assignment TMSList<T, P>& operator=(const TMSList<T, P>& L); // element insertions Iterator Insert(const T& t); int Insert(Iterator& I, const T& t); // use inherited Remove() and Clear() from TList<T>

Multimodal Sorted List Template Class (2) // locating elements—associative container operations Iterator Lower. Bound(const

Multimodal Sorted List Template Class (2) // locating elements—associative container operations Iterator Lower. Bound(const T& t) const; Iterator Upper. Bound(const T& t) const; Iterator Includes(const T& t) const; // use the inherited Begin(), End(), r. Begin(), r. End(), Empty(), // and Size() from TList<T> protected: P Less. Than; private: int Push. Front(const T&); int Push. Back(const T&); }; template <typename T, class P> int operator==(const TMSList<T, P>& L 1, const TMSList<T, P>& L 2);

Multimodal Sorted List Template Class (3) template <typename T, class P> int operator!=(const TMSList<T,

Multimodal Sorted List Template Class (3) template <typename T, class P> int operator!=(const TMSList<T, P>& L 1, const TMSList<T, P>& L 2); template <typename T, class P> std: : ostream& operator<<(std: : ostream& os, const TMSList<T, P>& L);

Multimodal Sorted List Implementation template <typename T, class P> int TMSList<T, P>: : Insert(TMSList<T,

Multimodal Sorted List Implementation template <typename T, class P> int TMSList<T, P>: : Insert(TMSList<T, P>: : Iterator& I, const T& t) { if (Empty()) { return TList<T>: : Insert(I, t); } // list is not empty TMSList<T, P>: : Iterator J = I; if (I. Valid()) { --J; // insert iff *J <= t <= *I if ((!J. Valid() || (J. Valid() && (!Less. Than(t, *J)) && (!Less. Than(*I, t)) { return TList<t>: : Insert(I, t); } } else {

Multimodal Sorted List Implementation (2) J = r. Begin(); if (!Less. Than(t, *J)) {

Multimodal Sorted List Implementation (2) J = r. Begin(); if (!Less. Than(t, *J)) { // t >= *J if (TList<T>: : Push. Back(t)) { I = r. Begin(); return 1; } } } return 0; }

Multimodal Sorted List Implementation (3) template <typename T, class P> TMSList<T, P>: : Iterator

Multimodal Sorted List Implementation (3) template <typename T, class P> TMSList<T, P>: : Iterator TMSList<T, P>: : Lower. Bound(const T& t) const { for (TMSList<T, P>: : Iterator I(*this); I. Valid() && Less. Than(*I, t); ++I) { } return I; } template <typename T, class P> TMSList<T, P>: : Iterator TMSList<T, P>: : Insert(const T& t) { TMSList<T, P>: : Iterator I = Lower. Bound(t); if (TList<T>: : Insert(I, t)) { return I; } else { return End(); } }

Announcement n Exam 2 (11/3) n n n n Generic list class and linked

Announcement n Exam 2 (11/3) n n n n Generic list class and linked lists Generic positional containers and double ended queues Stacks and queues Function classes and objects Generic algorithms Iterators Generic set algorithms Generic associative containers and sorted lists