EECE 310: Software Engineering

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Chapter 3 Introduction to Collections – Stacks Modified
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
EECE 310: Software Engineering Iteration Abstraction.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Data Abstractions EECE 310: Software Engineering.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Iteration Abstraction SWE Software Construction Fall 2009.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Concurrent Programming Acknowledgements: Some slides adapted from David Evans, U. Virginia.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Recursion Powerful Tool
Iterators.
EECE 310: Software Engineering
EECE 309: Software Engineering
Abstract Data Types and Encapsulation Concepts
ADT’s, Collections/Generics and Iterators
Exceptions, Interfaces & Generics
Why exception handling in C++?
Lecture 15: More Iterators
Stacks.
Chapter 3: Using Methods, Classes, and Objects
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Algorithm Analysis CSE 2011 Winter September 2018.
Functional Programming with Java
Iteration Abstraction
CS313D: Advanced Programming Language
Abstract Data Types and Encapsulation Concepts
Java Programming Language
Stacks.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
EE 422C Sets.
CS18000: Problem Solving and Object-Oriented Programming
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Object Oriented Programming in java
Coding Concepts (Basics)
Iteration Abstraction
Lecture 4: Data Abstraction CS201j: Engineering Software
EECE 310: Software Engineering
Statement-Level Control Structures
int [] scores = new int [10];
Stacks Abstract Data Types (ADTs) Stacks
Basics of Recursion Programming with Recursion
Chapter 4 Unordered List.
List Implementation via Arrays
Data Structures & Algorithms
slides created by Marty Stepp
Recursive Objects Singly Linked Lists.
Chapter 18 Recursion.
CS2013 Lecture 7 John Hurley Cal State LA.
Creating and Modifying Text part 3
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CMSC 202 Exceptions 2nd Lecture.
Review for Midterm 3.
slides created by Marty Stepp
CHAPTER 3: Collections—Stacks
Presentation transcript:

EECE 310: Software Engineering Iteration Abstraction

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

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 ?

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

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

Iteration Abstraction: Generators Client Code 1 ADT state 1 Generator objects state 2 Client Code 2 Client Code 3

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

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. }

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

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

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

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

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

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); }

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

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. … }

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

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

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)

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 …

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

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

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

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

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 + “ “); }

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

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)

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 ?

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.

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

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 ?)

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

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

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

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