Iterators and Generators

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
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.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Design Patterns Part 3 (Slides by Mike Ernst and David Notkin) 1.
OOP Languages: Java vs C++
Computer Science 112 Fundamentals of Programming II Expression Trees.
High-Level Programming Languages: C++
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
The Java Programming Language
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Python iterators and generators. Iterators and generators  Python makes good use of iterators  And has a special kind of generator function that is.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Iterators and Generators Thomas Wouters XS4ALL
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
Iteration Abstraction SWE Software Construction Fall 2009.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Generic Programming in C++ Giuseppe Attardi Università di Pisa.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
Chapter 2 Objects and Classes
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Advanced .NET Programming I 2nd Lecture
EECE 310: Software Engineering
Priority Queues (Heaps)
CS1022 Computer Programming & Principles
Lecture 7-2 : STL Iterators
Python: Control Structures
CS-104 Final Exam Review Victor Norman.
ADT’s, Collections/Generics and Iterators
Homework 4 questions???.
Student Book An Introduction
Creating and Modifying Text part 2
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
What remains Topics Assignments Final exam
Iteration Abstraction
Lecture 7-2 : STL Iterators
Starting JavaProgramming
CMSC 341 Binary Search Trees 11/19/2018.
null, true, and false are also reserved.
CMSC 341 Binary Search Trees.
Iteration Abstraction
CMSC 341 Binary Search Trees 1/3/2019.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Statement-Level Control Structures
CMSC 341 Binary Search Trees 2/23/2019.
Lists - I The List ADT.
Lecture 8-2 : STL Iterators and Algorithms
CMSC 341 Binary Search Trees.
CSC 143 Binary Search Trees.
CMSC 341 Binary Search Trees 5/8/2019.
CMSC 341 Binary Search Trees 5/28/2019.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
Exception Handling.
CMSC 341 Binary Search Trees 2/21/2006.
Presentation transcript:

Iterators and Generators Giuseppe Attardi Dipartimento di Informatica Università di Pisa

What is an iterator? An Iterator is an object that can be used to control the iteration behavior of a loop A generator yields values one at a time instead of returning all values The benefits of using generators: Generates values on demand Requires less memory Allows the caller to start processing immediately Improves the performance of an application

Example: Tree Traversal Tree is a very common data structure A lot of applications: File system Video categorization ... Find and process data contained in a certain of tree nodes Find blue nodes and do something with the information stored in them slide by Hayouan Li

Find blue nodes and do something on each All-In-One Solution Code reuse problem ReadBlue(Node n) { if (n.color == Blue) Read(n); for (Node child: n.children()) ReadBlue(child); } WriteBlue(Node n) { if (n.color == Blue) Write(n, what_ever); for (Node child: n.children()) WriteBlue(child); } PlayBlue(Node n) {...} EmptyBlue(Node n) {...} Find blue nodes and do something on each slide by Hayouan Li

Find blue nodes and do something on each With Function Objects MapBlue(Node n, func(Node n)) { if (n.color == Blue) func(n); for (Node child: n.children()) MapBlue(child, func); } Read(Node n) {...} Write(Node n) { what_ever; ...}; MapBlue(root, Read); MapBlue(root, Write); Find blue nodes and do something on each slide by Hayouan Li

Visitor Pattern Pass object that visits the nodes Better integrated with OO paradigm Still fairly complex

Tree interface Tree { } class Node implements Tree { List<Tree> children; } class Leaf implements Tree { int value;

Visitor on a Tree interface Visitable { void accept(Visitor v); } interface Visitor { void visit(Visitable v); class VisitableNode extends Node, implements Visitable { void accept(Visitor v) { v.visit(this); for (VisitableNode c: children) c.accept(visitor); class VisitableLeaf extends Leaf, implements Visitable { void accept(Visitor v) { v.visit(this); }

Visitor Usage class Printer implements Visitor { void visit(VisitableNode n) { } void visit(VisitableLeaf l) { print(l.value); } }

CMM Garbage Collector Generational Mostly Copying Collector Tricolor marking white object are copied to NextGeneration and turned into gray by function scavenge(x) gray objects are turned to black by invoking x.traverse() x.traverse() in turn call scavenge(p) for all pointers p in x

CMM Phases Before Collection After Page Promotion After Compaction Root Set Heap Root Set Heap Root Set Heap Before Collection After Page Promotion After Compaction

CMM Visitor Pattern class CmmObject { void* operator new(size_t, CmmHeap*); virtual void traverse() = 0; // accept visiting GC void mark(); }; class Node : public CmmObject { void traverse() { scavenge(left); // GC visit scavenge(right); // GC visit

Iterators

C++ Template Enumeration template<class T> class EnumerableVector : std::vector<T> { public: Enumeration getEnumeration() { return (Enumeration(this)); } class Enumeration { … } };

Enumeration (2) class Enumeration { private: vector<T> const* vp; unsigned idx; public: Enumeration(vector<T> const* vector) : vp(vector), idx(0) { } T const& next() { // uses 'T‘ if (idx == vp->size()) throw NoSuchElementException(index); return (*vp)[idx++]; } bool hasNext() { return idx < vp->size(); } };

Enumeration (3) EnumerableVector<int> ev; … EnumerableVector<int>::Enumeration en = ev.getEnumeration(); while (en.hasNext()) cout << en.next() << endl;

C# Iterators interface IEnumerable<T> interface IEnumerator<T> : IDisposable { bool MoveNext(); T Current { get; } void Dispose(); }

Java Enumeration Interface interface Enumeration<T> { boolean hasMoreElements(); T nextElement(); }

Java Iterator Interface interface Iterator<T> { boolean hasNext(); T next(); void remove(); }

Java for loop ArrayList<String> items; for (String item : items) { System.out.println(item); } Works for any object that implements the Iterable interface

Java Iterable Interface interface Iterable<T> { Iterator<T> iterator(); void forEach(Consumer<? super T> action); default Spliterator<T> spliterator(); }

Java 8: forEach + lambda Map<String, Integer> items = new HashMap<>(); items.put("A", 10); … items.forEach((k,v)-> System.out.println("Item: " + k + " Count: " + v)); // method reference items.forEach(System.out::println);

Python Iterators Obtain an iterator. Method in iterable class: def __iter__(self): … Iterator interface. Single method def __next__(self): … Termination by raising StopIterationException Builtin function iter() takes an iterable object and returns an iterator

Generators

What is a generator? A generator is an iterator (not viceversa) A method or a function can be turned into a generator by a specific language construct like: yield

Problem: collecting all results An accumulator is needed Nodes FindBlue(Node n) { Nodes buf = new Nodes(); if (n.color == Blue) buf.append(n); for (Node child: n.children()) buf.append(FindBlue(child)); return buf; } Nodes B = FindBlue(root); for (Node b: B) { Read(b); Write(b); Play(b); Empty(b); } Find blue nodes and do something on each

Find blue nodes and do something on each With a Generator Enumerator<Node> FindBlue(Node n) { if (n.color == Blue) yield return n; for (Node child: n.children()) FindBlue(child); } for (Node n: FindBlue(root)) { Read(n); Write(n); Play(n); Empty(n); Delete(n); } Find blue nodes and do something on each

Generator vs Stateful Function Language-level construct that keeps runtime state of a function across invocations Uses simple instructions with clear semantics yield break yield return Stateful Function, i.e. closure Must be implemented by user Requires complex control structures Visitor Pattern

Yield Operator Available in: Special case of closure (or continuation) JavaScript Python Ruby Special case of closure (or continuation)

Infinite Sequence def fib(): first = 0 second = 1 yield first yield second while True: next = first + second yield next first = second second = next for n in fib(): print n

Compiler turn into a closure-like def fib(): first = [0] second = [1] def next(): res = first[0] + second[0] first[0] = second[0] second[0] = res return res return next

Tree Visit class Node(): def __init__(self, label): self.label = label self.left = None self.right = None

Hand coded iterator class Node(): … def __iter__(self): return TreeIterator(self) class TreeIterator(): def __init__(self, node): self.stack = [node] # state can be either: 'goLeft', 'visit', 'goRight' self.state = 'goLeft'

Iteration method def next(self): while self.stack: node = self.stack[-1] # stack top if self.state == 'goLeft': if node.left: self.stack.append(node.left) else: self.state = 'visit' elif self.state == 'visit': self.state = ‘goRight’ return node.label elif self.state == 'goRight': self.stack.pop() # its visit is complete if node.right: self.state = 'goLeft' self.stack.append(node.right) self.state = 'visit‘ # no fully visited nodes are on the stack self.stack.pop() raise StopIteration

Testing the iterator n0 = Node(0); n1 = Node(1); n2 = Node(2) n3 = Node(3); n4 = Node(4); n5 = Node(5) n0.left = n1 n0.right = n2 n1.left = n3 n1.right = n4 n2.left = n5 for n in n0: print n 1 3 4 2 5

Expansion of the for loop it = n0.__iter__() try: while True: v = it.next() print v catch StopIteration: continue

Inorder Visit def inorder(t): if t: for x in inorder(t.left): yield x yield t.label for x in inorder(t.right):

Tree insertion class Node(): def __init__(self, label): self.label = label self.left = None self.right = None def insert(self, val): if self.label < val: if self.right: self.right.insert(val) else: self.right = Node(val) elif self.left: self.left.insert(val) self.left = Node(val)

Test r = Node(0) for i in [2, 1, -2, -1, 3]: r.insert(i) for v in inorder(r): print v -2 -1 0 1 2 3

Example def map(func, iterable): result = [] for item in iterable: result.append(func(item)) return result def imap(func, iterable): yield func(item)

'yield' and 'try'/'finally' Python does not allow 'yield' inside a 'try' block with a 'finally' clause: try: yield x finally: print x 'yield' inside 'finally' or in 'try'/'except' is allowed