1 / 9

E61 CS 342S: Object-Oriented Software Development

E61 CS 342S: Object-Oriented Software Development. The STL and the Iterator Pattern. Christopher Gill Department of Computer Science and Engineering Washington University, St. Louis cdgill@cse.wustl.edu. Thanks to Jim Wang, Peng Wang. What is an Iterator?. GoF book suggests an interface

yetty
Download Presentation

E61 CS 342S: Object-Oriented Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. E61 CS 342S: Object-Oriented Software Development The STL and the Iterator Pattern Christopher Gill Department of Computer Science and Engineering Washington University, St. Louis cdgill@cse.wustl.edu Thanks to Jim Wang, Peng Wang

  2. What is an Iterator? • GoF book suggests an interface • First (), Next(), IsDone(), CurrentItem() • However, intent they give is more essential • “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation” • Having different representations • means aggregate objects may have different semantics for the interface above (some trivial) • We’ll consider interface methods above with • istream, ostream, array, linked list, bi-linked list

  3. Syntax and Semantics • Syntax governs how interface is written First (); Next(); IsDone(); CurrentItem(); p = &A[0]; ++p; p – A > 3; *p; • Semantics governs what implementation does • Move to start, move forward one position, test completion, return item at current position • Compilers can only check syntax (C++, Java) • The programmer must address semantics • Including memory allocation, safe aliasing, etc. • Is it “bad” to implement Next () as --p ?

  4. A Few Key Ideas to Consider • Difference Type • Type for “distance” between two iterators i1 and i2 • E.g., ptrdiff_t • Reference, Value Types • For T *p, value type is T, *p normally returns T & • For const T *p, value type is const T, *p gives const T & • Iterator Category • What concept(s) it models how far? units? how far? units?

  5. Review: Iterator Concept Hierarchy destructive read at head of stream write to stream read/write once Input Iterator Output Iterator Linked-list style access Forward Iterator value persists after read/write, values have locations, can express distance between two iterators Bi-linked-list style access Bidirectional Iterator Array/buffer style access Random Access Iterator

  6. Iterator Concepts: Expressions, Models • Input Iterator (read from a stream/container) • *i (destructive), *i = t, i->m, ++i, i++ , *i++ • istream_iterator, most other STL iterators • Output Iterator (write to a stream/container) • X y(x), X y=x, y = x, *x = t, x++, ++x , *x++ = t • front_insert_iterator, back_insert_iterator, insert_iterator, ostream_iterator • How are First(), Next(), IsDone(), CurrentItem() implemented for each of these?

  7. Iterator Concepts: Expressions, Models • Forward Iterator (moves in one direction) • Can pass same forward iterator in multiple args • Dereference does not move the iterator • X x, X(), ++i, i++, *i (non-destructive) in addition to the expressions for Input Iterator and Output Iterator • slist<int>::iterator, slist<double>::const_iterator, hash_set<string>::iterator • Bidirectional Iterator (moves both ways) • Forward Iterator expressions,--i,i-- • list<int>::iterator, set<string>::iterator • How are First(), Next(), IsDone(), CurrentItem() implemented for each of these? • Can we do more than with Input/Output Iterator?

  8. Iterator Concepts: Expressions, Models • Random Access Iterator (constant time index) • Concept fully expresses C++ pointers • Bidirectional Iterator expressions and i+=n, i+n, n+i, i-=n, i-n, n-i, i-j, i<j, i[n], i[n]=t • Question: we can express the distance between two Forward Iterators, so why don’t those iterators have i-j, i<j ? • vector<int>::iterator, deque<int>::iterator,char * • How are First(), Next(), IsDone(), CurrentItem() implemented for each of these? • Can we do more than with Forward/Bidirectional ?

  9. Conclusions • What really matters with a pattern is • Its intent (the problem it solves) • Its context (when and how it applies) • The consequences of different solutions • Comparing Iterator pattern and STL iterators • Gives a deeper understanding of both • Offers clues about implementation using STL • A design and implementation approach • Design your solution according to given problem • Keep track of the patterns used in your design • Map design patterns into libraries/frameworks • Write code only when you have to

More Related