Title: COTS Challenges for Embedded Systems
1E61 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_at_cse.wustl.edu
Thanks to Jim Wang, Peng Wang
2What 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) - Well consider interface methods above with
- istream, ostream, array, linked list, bi-linked
list
3Syntax and Semantics
- Syntax governs how interface is written
- First () Next() IsDone() CurrentItem()
- p A0 p p A gt 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 ?
4A 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?
5Review 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
6Iterator Concepts Expressions, Models
- Input Iterator (read from a stream/container)
- i (destructive), i t, i-gtm, i, i , i
- istream_iterator, most other STL iterators
- Output Iterator (write to a stream/container)
- X y(x), X yx, 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?
7Iterator 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 - slistltintgtiterator, slistltdoublegtconst_iterato
r, hash_setltstringgtiterator - Bidirectional Iterator (moves both ways)
- Forward Iterator expressions,--i, i--
- listltintgtiterator, setltstringgtiterator
- 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 in, in,
ni, i-n, i-n, n-i, i-j, iltj, in, int - Question we can express the distance between two
Forward Iterators, so why dont those iterators
have i-j, iltj ? - vectorltintgtiterator, dequeltintgtiterator, 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