Iteration Abstraction

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Double-Linked Lists and Circular Lists
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
EECE 310: Software Engineering Iteration Abstraction.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Comp 302: Software Engineering Data Abstractions.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Polymorphism SWE 619. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism Element.
Iteration Abstraction SWE Software Construction Fall 2009.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Type Hierarchies. Type Hieararchy Why?: Want to define family of related types. At the top of the hierarchy, a type whose spec defines behavior common.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Linked Data Structures
Iterators.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Concepts of Programming Languages
The List ADT.
EECE 310: Software Engineering
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Week 4 - Friday CS221.
Practicum 1: - Persistent vs. destructive lists - Java interfaces
Chapter 6: The Stack Abstract Data Type
ADT’s, Collections/Generics and Iterators
Type Abstraction SWE Spring 2009.
Introduction to Data Structures
Linked Lists, Stacks and Queues Textbook Sections
ArraySet Methods and ArrayIterator
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Need for Iterators O(n2) this is a common application
slides adapted from Marty Stepp
Type Abstraction Liskov, Chapter 7.
Iteration Abstraction
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Example: LinkedSet<T>
Interator and Iterable
JCF Collection classes and interfaces
Abstract Datatype + Iterator Module
ITI Introduction to Computing II Lab-12
Ordered Structures Wellesley College CS230 Lecture 10
Software Design Lecture : 39.
Iterators Dan Fleck.
CSE 143 Lecture 21 Advanced List Implementation
Type Abstraction SWE Spring 2013.
Presentation transcript:

Iteration Abstraction SWE/CS 332

Iterator Interface public Interface Iterator { public boolean hasNext(); public Object next() throws NoSuchElementException; public void remove() throws IllegalStateException; } Liskov has only first hasNext() and next() To satisfy the compiler, you need to override/implement remove() too See Iterator Documentation

Mutability Variation: Poly and IntSet Examples public Iterator terms() // Effects: Returns a generator that will produce exponents // of nonzero terms of this (as Integers) up to the degree, // in order of increasing exponent public Iterator elements() // Effects: Returns a generator that will produce all the elements // of this (as Integers) each exactly once, in arbitrary order // Requires: this must not be modified while the // generator is in use

Liskov Iterator State Current list of things to send back Essentially a Stack next corresponds to Stack pop() remove(), two-way iterators add complexity to the state More later

Example: Poly Poly p … // p = 2 + 3 x2+ 4 x5 Iterator itr = p.iterator(); // Usual Iterator itr = p.terms(); // Liskov’s itr = [0,2,5] itr.hasNext() // return true, itr = [0,2,5] itr.next() // return 0, itr = [2,5] itr.next() // return 2, itr = [5] itr.hasNext() // return true, itr = [5] itr.next() // return 5, itr = [] itr.hasNext() // return false, itr = [] itr.next() // return NSEE, itr = []

Implementation (Fig 6.8) public class Poly{ // Rep … public Iterator terms() {return new PolyGen(this);} // inner class private static class PolyGen implements Iterator { private Poly p; // the Poly being iterated private int n; // the next term to consider PolyGen (Poly it){ //Requires: it !=null p = it; if(p.trms[0] == 0) n=1; else n= 0; }

Implementation (contd.) public boolean hasNext() {return n<= p.deg;} public Object next () throws NSEE{ for(int e = n; e <= p.deg; e++) { if (p.trms[e] != 0) { n= e+1; return new Integer(e); } throw new NSEE(“Poly.terms”); } // end PolyGen

Inner Class private class visibility only inside the class where defined no outside code can see/instantiate it if it has public methods && an instance available, outside code can call it

Another example: PrimesGen private static class PrimesGen implements Iterator{ private Vector ps; // primes yielded private int p; // next candidate to try PrimesGen () { p =2 ; ps = new Vector();} //constructor public boolean hasNext() {return true;} // always true public Object next() throws NSEE { if (p==2) {p=3; return 2;} for (int n=p; true; n = n+2){ … //Prime number generation } }// end of PrimesGen

Abstract State for PrimesGen? Iterator itr = num.allPrimes(); AF(c) = [2,3,5,7,11,13,17,19, …] No end? Can we figure out the length of the tail? What does hasNext() have to do in this case? [2,3,5,7,9,…] Integer x = (Integer) itr.next(); [3,5,7,9,11,..] Integer y = (Integer) itr.next(); [5,7,9,11,13,17,…] .

Another Exercise public Interface TwoWayIterator { Object next (); Object previous (); boolean hasNext(); boolean hasPrevious(); Suppose we want to go back AND forward What does the abstraction function look like? Still a stack? What other state information is needed? How to implement this for Poly?

What about supporting remove()? The contract for remove(): Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method. This is complex! What is the new abstract state?

Iterable vs. Iterator Only one method required: Allows very nice code: public Iterator<T> iterator(); Allows very nice code: // Note: that Collection implements Iterable // Side note: String does NOT implement Iterable Set<String> mySet = new HashSet<String>(); // populate mySet with various Strings for (String s : mySet) { // auto invocation of iterator(), next() // do something with s }