90 likes | 182 Views
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
E N D
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 • 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
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 ?
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?
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
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?
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?
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 ?
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