interruptible iterators
play

Interruptible Iterators Jed Liu Aaron Kimball Andrew C. Myers - PowerPoint PPT Presentation

Interruptible Iterators Jed Liu Aaron Kimball Andrew C. Myers Department of Computer Science Cornell University 33 rd ACM Symposium on Principles of Programming Languages 13 January, 2006 Jed Liu et al. Interruptible Iterators 1 Iteration


  1. Interruptible Iterators Jed Liu Aaron Kimball Andrew C. Myers Department of Computer Science Cornell University 33 rd ACM Symposium on Principles of Programming Languages 13 January, 2006 Jed Liu et al. Interruptible Iterators 1

  2. Iteration Abstractions Important to support iteration abstractions well ◮ Clients get on-demand access to elements of a lazily-evaluated sequence ◮ Many mainstream languages support IA s ◮ e.g., C ++ , Python, Ruby ◮ Evolving to support better: C # 2.0 , Java 1.5 ◮ Libraries too: Java Collections, Microsoft .NET ◮ Iterators are hard to implement ◮ Especially if they support imperative update Interruptible iterators make IA s easier to implement ◮ Implemented as part of JMatch Jed Liu et al. Interruptible Iterators 1

  3. Iterators: Easy to Use, Hard to Write Easy to use: Java iterator interface interface Iterator { boolean hasNext(); // Is there a next element? Object next(); // Return the next element void remove(); // Remove last element returned } Can be hard to implement ◮ Iteration must continue where it last left off ◮ Iterator can become awkward state machine Jed Liu et al. Interruptible Iterators 2

  4. Binary Tree Iterator Example in Java class TreeIterator implements Iterator { private Object preload() { Iterator subIterator; loop: while (true) { boolean hasNext; switch (state) { Object current; case 1: case 3: // 1 = Iterating through left child hasNext = true; // 2 = Just yielded current node value // 3 = Iterating through right child if (subIterator.hasNext()) { int state; return subIterator.next(); } TreeIterator() { subIterator = Tree.this.left.iterator(); if (state == 1) { state = 1; state = 2; current = preload(); return Tree.this.value; } } public boolean hasNext() { hasNext = false; return hasNext; return null; } case 2: public Object next() { subIterator = if (!hasNext) Tree.this.right.iterator(); throw new NoSuchElementException(); state = 3; continue loop; Object result = current; } current = preload(); } return result; } } } Jed Liu et al. Interruptible Iterators 3

  5. Binary Tree Iterator Example in Java class TreeIterator implements Iterator { private Object preload() { Iterator subIterator; loop: while (true) { boolean hasNext; switch (state) { Object current; case 1: case 3: // 1 = Iterating through left child hasNext = true; // 2 = Just yielded current node value // 3 = Iterating through right child if (subIterator.hasNext()) { int state; return subIterator.next(); } TreeIterator() { subIterator = Tree.this.left.iterator(); if (state == 1) { Even worse when you Even worse when you state = 1; state = 2; current = preload(); return Tree.this.value; } add support for updates add support for updates } public boolean hasNext() { hasNext = false; return hasNext; return null; } case 2: public Object next() { subIterator = if (!hasNext) Tree.this.right.iterator(); throw new NoSuchElementException(); state = 3; continue loop; Object result = current; } current = preload(); } return result; } } } Jed Liu et al. Interruptible Iterators 3

  6. Coroutine Iterators ◮ Increasingly popular: C # 2.0 , Python, Ruby ◮ Iterator as a coroutine: ◮ Separate stack ◮ Iterator suspends execution by yielding values ◮ Client obtains more values by resuming iterator Example: JMatch binary tree iterator class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  7. Coroutine Iterators ◮ Increasingly popular: C # 2.0 , Python, Ruby ◮ Iterator as a coroutine: ◮ Separate stack ◮ Iterator suspends execution by yielding values ◮ Client obtains more values by resuming iterator Example: JMatch binary tree iterator elements is an iterator elements is an iterator class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  8. Coroutine Iterators ◮ Increasingly popular: C # 2.0 , Python, Ruby ◮ Iterator as a coroutine: ◮ Separate stack ◮ Iterator suspends execution by yielding values ◮ Client obtains more values by resuming iterator Example: JMatch binary tree iterator class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  9. Coroutine Iterators ◮ Increasingly popular: C # 2.0 , Python, Ruby ◮ Iterator as a coroutine: ◮ Separate stack ◮ Iterator suspends execution by yielding values ◮ Client obtains more values by resuming iterator Example: JMatch binary tree iterator class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  10. Coroutine Iterators ◮ Increasingly popular: C # 2.0 , Python, Ruby ◮ Iterator as a coroutine: ◮ Separate stack ◮ Iterator suspends execution by yielding values ◮ Client obtains more values by resuming iterator Example: JMatch binary tree iterator class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  11. Coroutine Iterators Client yield elements yield elements yield elements class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  12. Coroutine Iterators Client yield elements yield elements yield elements Only a partial solution: no imperative updates Only a partial solution: no imperative updates class Node { int val; Node left, right; int elements() iterates(result) { foreach (int elt = left.elements()) yield elt; yield val; foreach (int elt = right.elements()) yield elt; } } Jed Liu et al. Interruptible Iterators 4

  13. Imperative Updates Example: Tree iterator ◮ Unsafe to change underlying data structure directly during iteration ◮ All updates must go through the iterator iterator iterator ◮ Java: remove() ◮ Previous coroutine iterators don’t have update Jed Liu et al. Interruptible Iterators 5

  14. Imperative Updates Example: Tree iterator ◮ Unsafe to change underlying data structure directly during iteration ◮ All updates must go through the iterator iterator iterator ◮ Java: remove() ◮ Previous coroutine iterators don’t have Iterator no longer points update to part of the tree Jed Liu et al. Interruptible Iterators 5

  15. Interruptible Iterators JMatch extends coroutine iterators to handle updates via interrupts: 1. Client raises interrupt 2. Iterator handles interrupt 3. Control returns to client after raise Example Collection c = ...; foreach (Object o = c.elements()) { if (o == null) raise new Remove(); System.out.println(o); } Jed Liu et al. Interruptible Iterators 6

  16. Interruptible Iterators JMatch extends coroutine iterators to handle updates via interrupts: 1. Client raises interrupt 2. Iterator handles interrupt 3. Control returns to client after raise Generates a Remove interrupt Generates a Remove interrupt Example Collection c = ...; foreach (Object o = c.elements()) { if (o == null) raise new Remove(); System.out.println(o); } Jed Liu et al. Interruptible Iterators 6

  17. Interruptible Iterators JMatch extends coroutine iterators to handle updates via interrupts: 1. Client raises interrupt 2. Iterator handles interrupt 3. Control returns to client after raise Receives and handles the interrupt Receives and handles the interrupt Example Collection c = ...; foreach (Object o = c.elements()) { if (o == null) raise new Remove(); System.out.println(o); } Jed Liu et al. Interruptible Iterators 6

  18. Interruptible Iterators JMatch extends coroutine iterators to handle updates via interrupts: 1. Client raises interrupt 2. Iterator handles interrupt 3. Control returns to client after raise Execution continues immediately Execution continues immediately Example after raise statement after raise statement Collection c = ...; foreach (Object o = c.elements()) { if (o == null) raise new Remove(); System.out.println(o); } Jed Liu et al. Interruptible Iterators 6

  19. Interruptible Iterators JMatch extends coroutine iterators to handle updates via interrupts: 1. Client raises interrupt 2. Iterator handles interrupt 3. Control returns to client after raise Example Collection c = ...; foreach (Object o = c.elements()) { if (o == null) raise new Remove(); System.out.println(o); } Jed Liu et al. Interruptible Iterators 6

  20. Declaring Interrupt Handlers ◮ JMatch iterators declare handled interrupts ◮ Compiler checks all interrupts are handled Example interface Collection { ... Object elements() traps Remove iterates(result); } Jed Liu et al. Interruptible Iterators 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend