Download presentation
Presentation is loading. Please wait.
1
EECE 310: Software Engineering
Iteration Abstraction
2
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Understand the design issues for iterators Design iterators that support concurrency
3
Problem Consider the IntSet ADT introduced earlier
Let’s say you want to compute the sum of elements in the set. How would you do it ? Add a member function computeSum Copy the rep to another array and return it Add an indexing operation to the ADT Add a new iterator abstraction Which method would you choose and why ?
4
Iteration Abstraction
General way to iterate over the elements of an ADT without tying the iteration to a specific operation (e.g., summation) Efficient in space and time (as far as possible) For each element e of the ADT: Perform operation o on e
5
Implementation Implemented using two entities:
1. Generator: Keeps track of the state of the iteration and exposes operations to actually perform the iteration 2. Iterator: Operation of an ADT that returns a generator object to begin the iteration process
6
Iteration Abstraction: Generators
Client Code 1 ADT state 1 Generator objects state 2 Client Code 2 Client Code 3
7
Generator: Example public interface Iterator<T> { public boolean hasNext() // EFFECTS: Returns true if there are more elements to // yield, else returns false public T next( ) throws NoSuchElementException // MODIFIES: this // EFFECTS: If there are more results to yield, // returns the next result and records the state of this // Otherwise, throws NoSuchElementException
8
Iterator: IntSet Example
public class IntSet { ... public Iterator<Integer> elements ( ) // EFFECTS: returns a generator that will produce // all the elements of this, each exactly once, // in arbitrary order. // REQUIRES: this must not be modified while // the generator is in use. }
9
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Design iterators that support concurrency
10
How do we use iterators ? Step 1: Client invokes the iterator operation on ADT and stores the returned generator object IntSet s = new IntSet(v); …. Iterator g = s.elements(); Step 2: Perform the iteration using the hasNext and next methods of the generator object while ( g.hasNext() ) { Integer o = g.next( ); int i = o.intValue(); } Optional Step: May pass the generator object to different procedures for doing the operation e.g., int m = computeMax(g); ADT (e.g., IntSet) Has iterator methods that return generator objects, e.g., elements Generator object: implements the Java iterator interface): hasNext next
11
Some points on using Generators
Using code interacts with a generator via the iterator interface (defined in java.util package) Using code must obey the constraint imposed on it by the iterator’s requires clause (given after the EFFECTS clause) Generators can be passed as arguments and returned as results by procedures Generators may be primed before using them in loops Generators may loop infinitely, e.g., list of prime numbers
12
In-class Exercise Write a procedure that iterates over the elements of the IntSet ADT and prints all Integers that exceed a certain threshold, max
13
Solution: Method 1 public static void printExceeds(IntSet s, int threshold) throws NPException { // EFFECTS: If s is null, throws NullPointerException, // Otherwise, Print all elements in s that exceed the given threshold Iterator<Integer> g = s.allElements(); try{ while ( true ) int x = ( g.next() ).intValue(); if (x > threshold) System.out.println(x); } catch( NoSuchElementException e) { } // Do nothing if we run out
14
Solution: Method 2 public static void printExceeds(IntSet s, int threshold) throws NPException { // EFFECTS: If s is null, throws NullPointerException, // Otherwise, Print all elements in s that exceed the threshold Iterator<Integer> g = s.allElements(); while ( g.hasNext() ) int x = ( g.next() ).intValue(); if (x > threshold) System.out.println(x); }
15
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Understand the design issues for iterators Design iterators that support concurrency
16
Implementing Iterator methods
public class IntSet { private vector<Integer> els; // The rep ... public Iterator<Integer> elements ( ) // EFFECTS: returns a generator object that will // produce all the elements of this, each exactly // once, in arbitrary oder. // REQUIRES (POST): this must not be modified while // the generator is in use. … }
17
Iterator Methods Must be declared as public method of the ADT
One iterator method for every iterator Can take in arguments, but not necessary Must return the generator corresponding to the iteration
18
Implementing Iterator methods
public class IntSet { private vector<Integer> els; // The rep ... public Iterator<Integer> elements ( ) // EFFECTS: returns a generator object that will // produce all the elements of this, each exactly // once, in arbitrary oder. // REQUIRES (POST): this must not be modified while // the generator is in use. return new IntSetGen(this); } Generator object
19
Implementing Generators
IntSetGen is declared as a nested class within the IntSet ADT and it’s scope is private Clients cannot create IntSetGen objects except through the iterator method of the IntSet ADT Clients cannot directly access any operation of IntSetGen except through the iterator interface IntSetGen has undeterred access to the private methods of the IntSet including its rep (i.e., els)
20
IntSetGen: Declaration
public class IntSet { private vector<Integer> els; // The rep public Iterator<Integer> elements( ) { // Iterator // EFFECTS: … return new IntSetGen( this ); } private static class IntSetGen implements Iterator<Integer> { // Generator object returned by elements …
21
IntSetGen: Rep and Constructor
private static class IntSetGen implements Iterator<Integer> { private int index; private IntSet s; // Constructor IntSetGen(IntSet is) { // REQUIRES: is!= null // EFFECTS: Initializes the generator for a // new iteration cycle s = is; index = 0; } Passed in by the elements() method of the IntSet ADT
22
IntSetGen: hasNext operation
public boolean hasNext( ){ // EFFECTS: If not reached the end // the elements vector, return true // otherwise, return false if ( index == s.els.size( ) ) return false; else return true; } IntSet ADT’s Rep – can access this because it is declared as a nested class
23
IntSetGen: next operation
public Integer next( ) throws NoSuchElementException { // EFFECTS: If there is a next object, return it // Otherwise, throw the NoSuchElementException if ( hasNext( ) ) return s.els.get(index++); else throw new NoSuchElementException(“IntSet.elements”); } Use the name of the iterator method that produced the generator object
24
Generators: Points to note
Constructor must “copy in” the ADT reference Also, initialize any other fields of the generator object hasNext() should never advance the iteration Corollary: It must be legal to call hasNext as many times as one can, and the result must be the same next() must return the current object AND must advance the state of the iteration (if possible) But should NOT require that hasNext() is called before
25
In-class exercise Implement an Iterator for the IntSet ADT, to return all integers of the IntSet that are greater than a certain threshold, threshold. You’d use the iterator as follows (for example): Iterator<Integer> isi = is.greaterThan(threshold); while ( isi.hasNext() ) { Integer i = isi.next().intValue(); System.out.println(i + “ “); }
26
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Understand the design issues for iterators Design iterators that support concurrency
27
Design Issues: Multiple Iterators
Types may sometimes have multiple iterators Examples: ForwardIterators, ReverseIterators for ordered collections such as lists Iterators that return elements satisfying a certain critereon (say, within a specified range) Iterators that perform some operation on their elements prior to returning them (e.g., sorting)
28
Design Issues: Changes during Iteration
Default behavior so far: Disallow changes during iteration (specified in post-REQUIRES clause) If changes are allowed, what are its semantics ? State of iteration is what it was when the generator was created (requires creating copy) Iteration sequence changes when changes occur to ADT (difficult to ensure consistency of state) How do we reset iterator to go back in the collection ? What if the current element is removed in a list ?
29
Design Issues: Can the generator itself change the ADT ?
The generator can change the rep of the ADT as long as it does not change the abstraction Example: An iterator that returns the elements of an un-ordered collection such as set in sorted order may sort the collection during the process of iteration Iterator changes the abstraction exposed by ADT This is usually best avoided unless there is a compelling need. For example, iterating over a task list may itself spawn new tasks to be added to the list.
30
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Understand the design issues for iterators Design iterators that support concurrency
31
Concurrent Iterators: Read only
Assume that we want to iterate over a list in two separate threads, but without modifying the list. Is this allowed ? Yes, as long as we keep the generator objects separate. i.e., we do not share generators Each generator keeps track of where it is in the iteration – no changes to the state of the ADT We need to make the iterator method synchronized (why ?)
32
Concurrent Iterators: Read-Write
Thread 1 Thread 2 Iterator<Integer> g = is.elements(); while ( g.hasNext() ) { Integer i = g.next(); System.out.println(i + “ “); } is.add(1); is.add(2); is.add(3); is.add(4); is.remove(2); No guarantees about what iterator returns
33
How to solve the concurrent iterator problem ?
Disallow concurrent writes by taking a lock Also disallows concurrent reads – performance ! Need to explicitly release lock when done Make a copy of the collection before iteration Expensive ! Throw a concurrent modification exception
34
Concurrent modification exception
Exception thrown when the ADT is modified concurrently (or not), during the iteration Thrown to the thread that performs the iteration Up to the thread what it wants to do with it Only one exception per concurrent modification will be thrown ConcurrentModification is checked exception – So need to check for it/propagate in client
35
Learning Objectives Define the basic elements used in iterators
Use iterators to iterate over a collection Implement iterators and the associated generator classes for an ADT Design iterators that support concurrency
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.