[m-rev.] diff: add rbtree.foldl_values/4
Julien Fischer
jfischer at opturion.com
Wed Jul 27 14:25:30 AEST 2016
We don't seem to have a test case covering the rbtree module; I will
add one separately.
------------------
Add rbtree.foldl_values/4.
library/rbtree.m:
As above.
NEWS:
Announce the addition.
Fix spelling in a spot.
Julien.
diff --git a/NEWS b/NEWS
index 23a1a10..a94da46 100644
--- a/NEWS
+++ b/NEWS
@@ -104,7 +104,7 @@ Changes to the Mercury language:
`require_switch_arms_<determinism> [Var] Goal' tells the compiler to require
`Goal' to be a switch on `Var' in which all the switch arms have determinisms
- at least as tight as <determininism>, and to generate error messages
+ at least as tight as <determinism>, and to generate error messages
for any violations of this requirement.
* We have changed the meaning of require_complete_switch scopes slightly:
@@ -361,6 +361,10 @@ Changes to the Mercury standard library:
- mktime/1
- ctime/1
+* The following predicate has been added to the rbtree module:
+
+ - foldl_values/4
+
Changes to the Mercury compiler:
* We have added a new option --warn-dead-preds. While the existing option
diff --git a/library/rbtree.m b/library/rbtree.m
index 5ba6f80..bfc9bc5 100644
--- a/library/rbtree.m
+++ b/library/rbtree.m
@@ -222,6 +222,14 @@
:- mode foldl3(pred(in, in, di, uo, di, uo, di, uo) is det,
in, di, uo, di, uo, di, uo) is det.
+:- pred foldl_values(pred(V, A, A), rbtree(K, V), A, A).
+:- mode foldl_values(pred(in, in, out) is det, in, in, out) is det.
+:- mode foldl_values(pred(in, mdi, muo) is det, in, mdi, muo) is det.
+:- mode foldl_values(pred(in, di, uo) is det, in, di, uo) is det.
+:- mode foldl_values(pred(in, in, out) is semidet, in, in, out) is semidet.
+:- mode foldl_values(pred(in, mdi, muo) is semidet, in, mdi, muo) is semidet.
+:- mode foldl_values(pred(in, di, uo) is semidet, in, di, uo) is semidet.
+
:- func map_values(func(K, V) = W, rbtree(K, V)) = rbtree(K, W).
:- pred map_values(pred(K, V, W), rbtree(K, V), rbtree(K, W)).
:- mode map_values(pred(in, in, out) is det, in, out) is det.
@@ -1093,6 +1101,18 @@ foldl3(Pred, black(K, V, Left, Right), !Acc1, !Acc2, !Acc3) :-
%---------------------------------------------------------------------------%
+foldl_values(_Pred, empty, !Acc).
+foldl_values(Pred, red(_K, V, Left, Right), !Acc) :-
+ rbtree.foldl_values(Pred, Left, !Acc),
+ Pred(V, !Acc),
+ rbtree.foldl_values(Pred, Right, !Acc).
+foldl_values(Pred, black(_K, V, Left, Right), !Acc) :-
+ rbtree.foldl_values(Pred, Left, !Acc),
+ Pred(V, !Acc),
+ rbtree.foldl_values(Pred, Right, !Acc).
+
+%---------------------------------------------------------------------------%
+
map_values(F, T1) = T2 :-
P = ( pred(X::in, Y::in, Z::out) is det :- Z = F(X, Y) ),
rbtree.map_values(P, T1, T2).
More information about the reviews
mailing list