diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-05-12 06:21:58 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-05-12 06:21:58 -0700 |
commit | 60f8d42f1928b91feaa9b027a3494c17f8ea958a (patch) | |
tree | a09d91f42a23473111faea7773735bc172c9fb98 | |
parent | 34bd40c5fddfd8d4219141e9695e686d2bddf526 (diff) | |
download | txr-60f8d42f1928b91feaa9b027a3494c17f8ea958a.tar.gz txr-60f8d42f1928b91feaa9b027a3494c17f8ea958a.tar.bz2 txr-60f8d42f1928b91feaa9b027a3494c17f8ea958a.zip |
tree: let tree-iter be iterable via generic iteration.
* lib.c (seq_iter_init_with_info): Recognize tree_iter object,
and treat using tree iterator function.
* tests/010/tree.tl: test case for tree subrange iteration
with collect-each.
* txr.1: Updated.
-rw-r--r-- | lib.c | 10 | ||||
-rw-r--r-- | tests/010/tree.tl | 5 | ||||
-rw-r--r-- | txr.1 | 21 |
3 files changed, 36 insertions, 0 deletions
@@ -857,6 +857,16 @@ void seq_iter_init_with_info(val self, seq_iter_t *it, } } } + if (it->inf.obj->co.cls == tree_iter_s) + { + it->ui.iter = if3(support_rewind, + copy_tree_iter(it->inf.obj), + it->inf.obj); + it->ul.len = 0; + it->get = seq_iter_get_tree; + it->peek = seq_iter_peek_tree; + break; + } /* fallthrough */ default: switch (it->inf.kind) { diff --git a/tests/010/tree.tl b/tests/010/tree.tl index b41e7973..06d02ce6 100644 --- a/tests/010/tree.tl +++ b/tests/010/tree.tl @@ -166,6 +166,11 @@ (copy-search-tree tr) #T((identity < =) 1 2 3) (make-similar-tree tr) #T((identity < =)))) +(test + (collect-each ((el (tree-begin #T(() 1 2 3 4 5) 2 5))) + (* 10 el)) + (20 30 40)) + (mtest (uni #T(() "a" "b") #T(() "b" "c")) ("a" "b" "c") (diff #T(() "a" "b") #T(() "b" "c")) ("a") @@ -34636,6 +34636,14 @@ and are equivalent to .codn "(iter-begin X)" . +Search trees are iterable. Iteration entails an in-order visits of the elements +of a tree. A tree iterator created by +.code tree-begin +is also iterable. It is unspecified whether iteration over a +.code tree-iter +object modifies that object to perform the traversal, or whether it uses a copy +of the iterator. + If .code seq is not an iterable object, an error exception is thrown. @@ -51866,6 +51874,19 @@ function to the returned .code tree-iter object. +A +.code tree-iter +object is iterable. + +.TP* Example: + +.verb + (collect-each ((el (tree-begin #T(() 1 2 3 4 5) + 2 5))) + (* 10 el)) + --> (20 30 40) +.brev + .coNP Function @ tree-reset .synb .mets (tree-reset < iter < tree >> [ low-key <> [ high-key ]]) |