Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences and Iterators

Similar presentations


Presentation on theme: "Sequences and Iterators"— Presentation transcript:

1 Sequences and Iterators
6/25/2018 9:52 PM Sequences and Iterators Sequences and Iterators

2 Sequences and Iterators
Introduction Consider the problem of printing out the elements in a collection, represented as an array V. So, its contents are easily printed with code like the following: In this loop, i is an iterator object, because it is used to control the iteration. However, using the integer i as an iterator constrains the design: We can only store the collection in an array-like structure. A more flexible alternative is to design an iterator class (object) that encapsulates a position inside a collection. An iterator object controls iteration of a collection of elements (e.g, array, array-list, list, or sequence). for (int i = 0; i < V.length; i++) system.out.println (V[i]); Sequences and Iterators

3 Sequences and Iterators
6.3.1 The Iterator ADT An iterator is a software design pattern that abstracts the process of scanning through a collection of elements one element at a time. An iterator consists of: A sequence S; A current element in S; and A way of stepping to the next element in S, making it the current element. Thus, an iterator extends the concept of the position ADT, introduced before. In fact, a position can be thought of as an iterator that doesn’t go anyway. An iterator encapsulates the concepts of “place” and “next” in a collection of objects. Sequences and Iterators

4 The Iterator ADT (Cont.)
An iterator abstracts the process of scanning through a collection of elements Methods of the object iterator ADT: object element() to get data or object stored at that position Boolean hasNext() Test whether there are elements left in the iterator object next() Return the next element in the iterator iterator extends the concept of position by adding a traversal capability Implementation can be with an array, singly- or doubly- linked list Sequences and Iterators

5 The Iterator ADT (Cont.)
Note that the iterator ADT has the cursor notion of the “current” element in a traversal of a sequence. The first element in an iterator is returned by the first call to the method next() , assuming that the iterator contains at least one element. Advantages of iterator ADT An iterator provides a unified scheme to access all the elements of a collection of objects, independently of the specific organization of the collection. An iterator for an array list, node list, or sequence should return the elements according to their linear ordering. Sequences and Iterators

6 The Iterable Abstract Data Type
In order to provide a unified generic mechanism for scanning through a data structure, ADTs storing collections of objects should support the following method: iterator(): Return an iterator of the elements in the collection. This method is supported by the java.util.ArrayList class. This method can make it simple for us to specify computations that need to loop through the elements of a list. To guarantee that a node list supports this method, for example, we could add this method to the PositionList interface, as shown below: Sequences and Iterators

7 Sequences and Iterators
Public interface PositionList<E> extends Iterable<E> { //… all the other methods of the list ADT… /* Returns an iterator of all the elements in the list. */ Public Iterator<E> iterator(); } We assume that our array lists and node lists support the iterator() method. Given such a PositionList definition, we could use an iterator to create a string representation of a node list, as shown below: /** Returns a textual representation of a given node list */ Public static <E> StringtoString (PositionList <E> ) l) { Iterator<E> it = l.iterator(); String s = “[“; while (it.hasNext()) { s += it.Next(); // implicit cast of the next element to String if (it.hasNext()) { s += “ , “; } s += “]”; return s; Sequences and Iterators

8 Sequences and Iterators
6.3.3 Implementing Iterators An iterator is typically associated with another data structure We can augment the Stack, Queue, Vector, Node List and Sequence ADTs with method: Object Iterator elements() Two ways of implementation of iterator: Snapshot: freezes the contents of the data structure at a given time Dynamic: follows changes to the data structure Sequences and Iterators

9 Sequences and Iterators
a. Snapshot Method One way to implement an iterator for a collection of elements is to make a ‘snapshot’ of it and iterate over it. This approach would involve storing the collection in a separate data structure that supports sequential access to its elements. For example, we could insert (freeze) all the elements of the collection into a queue, in which case: Method hasNext() corresponds to !isEmpty() Method next() corresponds to dequeue() With this approach, the method iterator() takes O(n) time for a collection of size n. Since this copying process is costly, we prefer to have iterators operate on the collection itself, not a copy of it. Sequences and Iterators

10 Sequences and Iterators
b. Direct Approach In this method of implementing an iterator , we need only to keep track of where in the collection the iterator’s cursor points. Thus, creating a new iterator in this case simply involves creating an iterator object that represents a cursor placed just before the first element of the collection. Performing the next() method would return the next element, if it exists, and moving the cursor just after this element’s position. Thus, in this implementation way, creating an iterator takes O(1) time, as do each of the iterator’s methods. Sequences and Iterators

11 Sequences and Iterators
A sequence is an ADT that supports all of the methods of the deque ADT, the Array-List ADT (or, vector), and the Node-List ADT. That is, a sequence provides explicit access to the elements in the list either by their indices or by their positions. Methods of a sequence ADT include: Generic methods: size(), isEmpty() Vector-based methods: get(i), set(i, e), add(i, e), remove (i) Sequences and Iterators

12 Sequences and Iterators
Sequences (Cont.) Node List-based methods: first(), last(), prev(p), next(p), set(p, e), addBefore(p, e), addAfter(p, e), addFirst(e), addLast(e), remove(p) Since sequence provides this dual access capability, we also include, in the sequence ADT, the following two “bridging” methods that provide connections between indices and positions: Bridge methods: atIndex(i) : Returns the position of the element with index i; an error condition occurs if i < 0 or i > size() -1. indexOf(p) : Return the index of the element at position p. Sequences and Iterators

13 Multiple Inheritance in the Sequence ADT
The definition of the sequence ADT as including all the methods from three different ADTs is an example of multiple inheritance. That’s, the sequence ADT inherits methods from the three “super” abstract data types. In other words, its methods include the union of the methods of these three “super” ADTs. Sequences and Iterators

14 Applications of Sequences
The Sequence ADT is a basic, general-purpose, data structure for storing an ordered collection of elements Direct applications: Generic replacement for stack, queue, vector, or list small database (e.g., address book) Indirect applications: Building block of more complex data structures Sequences and Iterators

15 Linked List Implementation of a Sequence
A doubly linked list provides a reasonable implementation of the sequence ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes Position-based methods run in constant time Index-based methods require searching from header or trailer while keeping track of indices; hence, run in linear time trailer header nodes/positions elements Sequences and Iterators

16 Array-based Implementation of a Sequence
elements We use a circular array S storing positions A position object stores: Element Index Indices f and l keep track of first and last positions 1 2 3 positions S f l Sequences and Iterators

17 Sequence Implementations
Operation Array List size, isEmpty 1 atIndex, indexOf, get n first, last, prev, next Set(p,e) Set(i,e) Add(i,e), remove(i) addFirst, addLast addAfter, addBefore Remove(p) Sequences and Iterators


Download ppt "Sequences and Iterators"

Similar presentations


Ads by Google