技术控

    今日:19| 主题:57957
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] The stack of iterators pattern.

[复制链接]
此生赐情 投递于 2016-10-17 20:14:54
206 3
The stack of iterators pattern

   Gareth Rees , 2016-09-28
    Depth-first search  is a straightforward algorithm for visiting the nodes of a tree or tree-like data structure.
   

The stack of iterators pattern.

The stack of iterators pattern.-1-技术控-recursion,structure,function,children,problems

  Here’s how you might implement it in Python:
  1. def search(node):
  2.     for child in children(node):
  3.         search(child)
复制代码
This works well in many cases, but it has a few problems:
  
       
  •   It descends the tree using a function call search(child) , and function calls are quite slow in Python.
       
  •   It descends the tree by recursing, and so it uses as many levels of stack as the depth of the deepest node in the tree. But Python’s call stack is limited in size (see  sys.getrecursionlimit  ) and so deep enough trees will run out of call stack and fail with “RuntimeError: maximum recursion depth exceeded.”
       
  • If you’d like to be able to stop the search part way through and return a result (for example returning a target node as soon as you find it), then this requires a slightly awkward change to the code. You could add logic for exiting the recursion:
    1. def search(node):
    2.     if target(node):
    3.         return node
    4.     for child in children(node):
    5.         result = search(child)
    6.         if result is not None:
    7.             return result
    复制代码
    or you could return the result non-locally using an exception:
    1. class Found(Exception):
    2.     pass
    3. def search(node):
    4.     if target(node):
    5.         raise Found(node)
    6.     for child in children(node):
    7.         search(child)
    复制代码
    or you could rewrite the search to use generators:
    1. def search(node):
    2.     if target(node):
    3.         yield node
    4.     for child in children(node):
    5.         yield from search(child)
    复制代码
    and then the caller can call next(search(root)) .
      
   These problems can all be avoided using the stack of iterators design pattern.
  1. def search(root):
  2.     stack = [iter([root])]
  3.     while stack:
  4.         for node in stack[-1]:
  5.             stack.append(iter(children(node)))
  6.             break
  7.         else:
  8.             stack.pop()
复制代码
This avoids the three problems above:
  
       
  • Pushing and popping a list is faster than calling a function in Python.
       
  • Lists can grow without limit, unlike the function call stack.
       
  • Since there’s no recursion, you can just return when you have a result:
    1. def search(root):
    2.     stack = [iter([root])]
    3.     while stack:
    4.         for node in stack[-1]:
    5.             if target(node):
    6.                 return node
    7.             stack.append(iter(children(node)))
    8.             break
    9.         else:
    10.             stack.pop()
    复制代码

   The control flow here might seem a bit tricky if you’re not used to the way that Python’s for ... else construct interacts with break . The pattern works by maintaining a stack of iterators that remember the position reached in the iteration over the children of the node at each level of the search. After pushing a new iterator on the stack, the break exits the for loop, bypasses the else: clause, and goes round the while loop again, so that it picks up the new iterator from stack[-1] . When there are no more children in the current iteration, the for loop exits via the else: clause and pops the stack. Then the next iteration of the while loop picks up the iteration at the previous level from where it left off.
  ✴
  Two examples. First, finding a key in a possibly nested dictionary:
  1. def search(d, key, default=None):
  2.     """Return a value corresponding to the specified key in the (possibly
  3.     nested) dictionary d. If there is no item with that key, return
  4.     default.
  5.     """
  6.     stack = [iter(d.items())]
  7.     while stack:
  8.         for k, v in stack[-1]:
  9.             if isinstance(v, dict):
  10.                 stack.append(iter(v.items()))
  11.                 break
  12.             elif k == key:
  13.                 return v
  14.         else:
  15.             stack.pop()
  16.     return default
复制代码
  Second, finding a simple path visiting a set of positions on a grid:

The stack of iterators pattern.

The stack of iterators pattern.-2-技术控-recursion,structure,function,children,problems

  1. def hamilton_path(start, positions, directions=((0, 1), (1, 0), (0, -1), (-1, 0))):
  2.     """Find a simple path that visits all positions.
  3.     start: tuple(int, int)
  4.         Starting position for the path.
  5.     positions: iterable of tuple(int, int)
  6.         Iterable of positions to be visited by the path.
  7.     directions: iterable of tuple(int, int)
  8.         Iterable of directions to take at each step.
  9.     Return the path as a list of tuple(int, int) giving the order in
  10.     which the positions are visited. Raise ValueError if there are no
  11.     paths visiting all positions.
  12.     """
  13.     positions = set(positions)
  14.     directions = list(directions)
  15.     path = [start]
  16.     stack = [iter(directions)]
  17.     while path:
  18.         x, y = path[-1]
  19.         for dx, dy in stack[-1]:
  20.             pos = x + dx, y + dy
  21.             if pos in positions:
  22.                 path.append(pos)
  23.                 positions.remove(pos)
  24.                 stack.append(iter(directions))
  25.                 if not positions:
  26.                     return path
  27.                 break
  28.         else:
  29.             positions.add(path.pop())
  30.             stack.pop()
  31.     raise ValueError("no path")
复制代码
       
  •   This requires Python 3.3 or later in order to be able to use the  yield from  statement.
       
  •   In software, a  design pattern  is a technique for working around a defect or omission in a particular programming language. For example, the well-known singleton pattern is a work-around for the lack of global variables in the Java language.
       
  •   Example adapted from this answer on Code Review.
       
  •   Example adapted from this answer on Code Review.
      



上一篇:第 7 单元:字符串和运算符
下一篇:Open Source is not dead: On the recent demise of RethinkDB
刘星 投递于 2016-10-23 15:27:12
矿难在检讨中继续,楼价在控制中上升。
回复 支持 反对

使用道具 举报

雅霜 投递于 2016-11-1 23:21:02
爱情就像两个拉着橡皮筋的人,受伤的总是不愿意放手的那一个!
回复 支持 反对

使用道具 举报

小迷茫3 投递于 2016-11-12 19:31:56
楼主,笑一个,萌萌哒!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表