Skip to content
This repository was archived by the owner on May 27, 2022. It is now read-only.

Commit 5ca3200

Browse files
committed
More efficient version of max
(as suggested by Peter)
1 parent 8aaad1f commit 5ca3200

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/vectorclock.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ from_list(List) ->
6262
-spec max([vectorclock()]) -> vectorclock().
6363
max([]) -> new();
6464
max([V]) -> V;
65-
max([V1, V2|T]) -> max([merge(fun erlang:max/2, V1, V2)|T]).
65+
max([V1, V2|T]) -> max([max2(V1, V2)|T]).
66+
67+
%% component-wise maximum of two clocks
68+
-spec max2(vectorclock(), vectorclock()) -> vectorclock().
69+
max2(V1, V2) ->
70+
FoldFun =
71+
fun(DC, A, Acc) ->
72+
B = get_clock_of_dc(DC, Acc),
73+
case A > B of
74+
true -> Acc#{DC => A};
75+
false -> Acc
76+
end
77+
end,
78+
maps:fold(FoldFun, V2, V1).
6679

6780
-spec min([vectorclock()]) -> vectorclock().
6881
min([]) -> new();

0 commit comments

Comments
 (0)