summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-05-11 06:45:18 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-05-11 06:45:18 -0700
commit25b5bdc5dfe62037faef8f19c070de434b660aa6 (patch)
treee19019ee59b345baf153a170f620f5912d5bdb27
parentd29381d5d2d9af69e954cfd9d3fd8d533003a7e2 (diff)
downloadtxr-25b5bdc5dfe62037faef8f19c070de434b660aa6.tar.gz
txr-25b5bdc5dfe62037faef8f19c070de434b660aa6.tar.bz2
txr-25b5bdc5dfe62037faef8f19c070de434b660aa6.zip
tree: replace-tree-iter function.
* tree.c (replace_tree_iter): New function. (tree_init): Register replace-tree-iter intrinsic. * tree.h (tree_init): Declared. * share/txr/stdlib/doc-syms.tl: Updated. * txr.1: Documented. * tests/010/tree.tl: New test case.
-rw-r--r--share/txr/stdlib/doc-syms.tl3
-rw-r--r--tests/010/tree.tl11
-rw-r--r--tree.c21
-rw-r--r--tree.h1
-rw-r--r--txr.131
5 files changed, 66 insertions, 1 deletions
diff --git a/share/txr/stdlib/doc-syms.tl b/share/txr/stdlib/doc-syms.tl
index 64fbcf6c..c13b8ea9 100644
--- a/share/txr/stdlib/doc-syms.tl
+++ b/share/txr/stdlib/doc-syms.tl
@@ -771,6 +771,7 @@
("promisep" "N-00C7553F")
("throwf" "N-015466AD")
("find-symbol-fb" "N-01EA8B50")
+ ("replace-tree-iter" "N-01225FF3")
("val" "N-00DB04DD")
("fnm-period" "N-0330E15A")
("mboundp" "N-01FBF828")
@@ -1467,8 +1468,8 @@
("right" "N-020D5C1D")
("carray-ref" "N-001F5BCA")
("sig-cont" "N-0176430F")
- ("n-perm-k" "N-02ACFDE6")
("mapf" "N-0026CEF1")
+ ("n-perm-k" "N-02ACFDE6")
("log-crit" "N-035D75EC")
("!&gt;" "N-02B10DF9")
("seq-begin" "N-0068A845")
diff --git a/tests/010/tree.tl b/tests/010/tree.tl
index e3b2ce0c..ed86c076 100644
--- a/tests/010/tree.tl
+++ b/tests/010/tree.tl
@@ -103,6 +103,17 @@
(key (tree-next t1))))
(1 2 3))
+(test (let* ((t0 (tree-begin tr))
+ (t1 (progn (tree-next t0) (copy-tree-iter t0)))
+ (t2 (replace-tree-iter (tree-begin tr) t0)))
+ (tree-next t0)
+ (tree-next t0)
+ (list (key (tree-next t1))
+ (key (tree-next t1))
+ (key (tree-next t2))
+ (key (tree-next t2))))
+ (1 2 1 2))
+
(test (tree-next (tree-begin-at tr 20)) nil)
(test (tree-next (tree-begin-at #T(()) 0)) nil)
diff --git a/tree.c b/tree.c
index e8b42523..191647bd 100644
--- a/tree.c
+++ b/tree.c
@@ -832,6 +832,26 @@ val copy_tree_iter(val iter)
return iter_copy;
}
+val replace_tree_iter(val diter, val siter)
+{
+ val self = lit("replace-tree-iter");
+ struct tree_diter *tdid = coerce(struct tree_diter *,
+ cobj_handle(self, diter, tree_iter_s));
+ struct tree_diter *tdis = coerce(struct tree_diter *,
+ cobj_handle(self, siter, tree_iter_s));
+ int depth = tdis->ti.depth;
+
+ tdid->ti.depth = depth;
+ tdid->ti.state = tdis->ti.state;
+ tdid->lastnode = tdis->lastnode;
+
+ memcpy(tdid->ti.path, tdis->ti.path, sizeof tdid->ti.path[0] * depth);
+
+ mut(diter);
+
+ return diter;
+}
+
val tree_reset(val iter, val tree)
{
val self = lit("tree-reset");
@@ -930,6 +950,7 @@ void tree_init(void)
reg_fun(intern(lit("tree-begin"), user_package), func_n1(tree_begin));
reg_fun(intern(lit("tree-begin-at"), user_package), func_n2(tree_begin_at));
reg_fun(intern(lit("copy-tree-iter"), user_package), func_n1(copy_tree_iter));
+ reg_fun(intern(lit("replace-tree-iter"), user_package), func_n2(replace_tree_iter));
reg_fun(intern(lit("tree-reset"), user_package), func_n2(tree_reset));
reg_fun(intern(lit("tree-reset-at"), user_package), func_n3(tree_reset_at));
reg_fun(intern(lit("tree-next"), user_package), func_n1(tree_next));
diff --git a/tree.h b/tree.h
index 72ee43db..7b045f1a 100644
--- a/tree.h
+++ b/tree.h
@@ -51,6 +51,7 @@ val tree_delete(val tree, val key);
val tree_begin(val tree);
val tree_begin_at(val tree, val lowkey);
val copy_tree_iter(val iter);
+val replace_tree_iter(val diter, val siter);
val tree_reset(val iter, val tree);
val tree_reset_at(val iter, val tree, val lowkey);
val tree_next(val iter);
diff --git a/txr.1 b/txr.1
index 42c73080..814147e2 100644
--- a/txr.1
+++ b/txr.1
@@ -51946,6 +51946,37 @@ Calls to
on the original have no effect on the duplicate and
.IR vice-versa .
+.coNP Function @ replace-tree-iter
+.synb
+.mets (replace-tree-iter < dest-iter << src-iter )
+.syne
+.desc
+The
+.code replace-tree-iter
+function causes the tree iterator
+.meta dest-iter
+to be in the same state as
+.metn src-iter .
+
+Both
+.meta dest-iter
+and
+.meta src-iter
+must be tree iterator objects returned by
+.code tree-begin
+or
+.codn tree-begin-at .
+
+The contents of
+.meta dest-iter
+are updated such that it now references the same tree as
+.metn src-iter ,
+at the same position.
+
+The
+.meta dest-iter
+argument is returned.
+
.coNP Special variable @ *tree-fun-whitelist*
.desc
The