From: Toby S. Cubitt Date: Fri, 4 Aug 2017 19:34:28 +0000 (+0100) Subject: Implement iterator generator for avl-trees. X-Git-Tag: emacs-26.0.90~517^2~26 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4b7f822cd53a50e83008ab4f561563d8977a74ec;p=emacs.git Implement iterator generator for avl-trees. * lisp/emacs-lisp/avl-tree.el (avl-tree-iter): New iter-defun. --- diff --git a/lisp/emacs-lisp/avl-tree.el b/lisp/emacs-lisp/avl-tree.el index 17f1ffa9f61..32f7d2c6d8d 100644 --- a/lisp/emacs-lisp/avl-tree.el +++ b/lisp/emacs-lisp/avl-tree.el @@ -52,7 +52,7 @@ ;;; Code: (eval-when-compile (require 'cl-lib)) - +(require 'generator) ;; ================================================================ @@ -670,6 +670,21 @@ a null element stored in the AVL tree.)" (null (avl-tree--stack-store avl-tree-stack))) +(iter-defun avl-tree-iter (tree &optional reverse) + "Return an AVL tree iterator object. + +Calling `iter-next' on this object will retrieve the next element +from TREE. If REVERSE is non-nil, elements are returned in +reverse order. + +Note that any modification to TREE *immediately* invalidates all +iterators created from TREE before the modification (in +particular, calling `iter-next' will give unpredictable results)." + (let ((stack (avl-tree-stack tree reverse))) + (while (not (avl-tree-stack-empty-p stack)) + (iter-yield (avl-tree-stack-pop stack))))) + + (provide 'avl-tree) ;;; avl-tree.el ends here