diff options
author | Björn Gustavsson <[email protected]> | 2014-01-09 12:01:46 +0100 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2014-01-09 12:01:46 +0100 |
commit | 039ea3a6ed5dc147478f294910ba042850db2383 (patch) | |
tree | f357cf926092f1a111957c2a885598c148a8ffed /lib/stdlib/src/orddict.erl | |
parent | 31ecc95ef55de3e112fd0b5134366fccf742a92d (diff) | |
parent | 5a7b2115ca5b9c23aacf79b634133fea172a61fd (diff) | |
download | otp-039ea3a6ed5dc147478f294910ba042850db2383.tar.gz otp-039ea3a6ed5dc147478f294910ba042850db2383.tar.bz2 otp-039ea3a6ed5dc147478f294910ba042850db2383.zip |
Merge branch 'sv/faster-orddict-from_list/OTP-11552'
* sv/faster-orddict-from_list/OTP-11552:
improve performance for orddict:from_list/1
Diffstat (limited to 'lib/stdlib/src/orddict.erl')
-rw-r--r-- | lib/stdlib/src/orddict.erl | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/stdlib/src/orddict.erl b/lib/stdlib/src/orddict.erl index da60fc1bb6..c98d78b34d 100644 --- a/lib/stdlib/src/orddict.erl +++ b/lib/stdlib/src/orddict.erl @@ -56,8 +56,10 @@ to_list(Dict) -> Dict. List :: [{Key :: term(), Value :: term()}], Orddict :: orddict(). +from_list([]) -> []; +from_list([{_,_}]=Pair) -> Pair; from_list(Pairs) -> - lists:foldl(fun ({K,V}, D) -> store(K, V, D) end, [], Pairs). + lists:ukeysort(1, reverse_pairs(Pairs, [])). -spec size(Orddict) -> non_neg_integer() when Orddict :: orddict(). @@ -235,3 +237,7 @@ merge(F, [{K1,V1}|D1], [{_K2,V2}|D2]) -> %K1 == K2 [{K1,F(K1, V1, V2)}|merge(F, D1, D2)]; merge(F, [], D2) when is_function(F, 3) -> D2; merge(F, D1, []) when is_function(F, 3) -> D1. + +reverse_pairs([{_,_}=H|T], Acc) -> + reverse_pairs(T, [H|Acc]); +reverse_pairs([], Acc) -> Acc. |