diff options
| author | Toby S. Cubitt | 2017-08-04 20:34:28 +0100 |
|---|---|---|
| committer | Toby S. Cubitt | 2017-08-04 20:39:05 +0100 |
| commit | 4b7f822cd53a50e83008ab4f561563d8977a74ec (patch) | |
| tree | a2980b5bcf9a2e329805f8956f81bc31bab6e499 | |
| parent | 929c60603ca19574159c78f12f5f953c31188bc6 (diff) | |
| download | emacs-4b7f822cd53a50e83008ab4f561563d8977a74ec.tar.gz emacs-4b7f822cd53a50e83008ab4f561563d8977a74ec.zip | |
Implement iterator generator for avl-trees.
* lisp/emacs-lisp/avl-tree.el (avl-tree-iter): New iter-defun.
| -rw-r--r-- | lisp/emacs-lisp/avl-tree.el | 17 |
1 files changed, 16 insertions, 1 deletions
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 @@ | |||
| 52 | ;;; Code: | 52 | ;;; Code: |
| 53 | 53 | ||
| 54 | (eval-when-compile (require 'cl-lib)) | 54 | (eval-when-compile (require 'cl-lib)) |
| 55 | 55 | (require 'generator) | |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | ;; ================================================================ | 58 | ;; ================================================================ |
| @@ -670,6 +670,21 @@ a null element stored in the AVL tree.)" | |||
| 670 | (null (avl-tree--stack-store avl-tree-stack))) | 670 | (null (avl-tree--stack-store avl-tree-stack))) |
| 671 | 671 | ||
| 672 | 672 | ||
| 673 | (iter-defun avl-tree-iter (tree &optional reverse) | ||
| 674 | "Return an AVL tree iterator object. | ||
| 675 | |||
| 676 | Calling `iter-next' on this object will retrieve the next element | ||
| 677 | from TREE. If REVERSE is non-nil, elements are returned in | ||
| 678 | reverse order. | ||
| 679 | |||
| 680 | Note that any modification to TREE *immediately* invalidates all | ||
| 681 | iterators created from TREE before the modification (in | ||
| 682 | particular, calling `iter-next' will give unpredictable results)." | ||
| 683 | (let ((stack (avl-tree-stack tree reverse))) | ||
| 684 | (while (not (avl-tree-stack-empty-p stack)) | ||
| 685 | (iter-yield (avl-tree-stack-pop stack))))) | ||
| 686 | |||
| 687 | |||
| 673 | (provide 'avl-tree) | 688 | (provide 'avl-tree) |
| 674 | 689 | ||
| 675 | ;;; avl-tree.el ends here | 690 | ;;; avl-tree.el ends here |