diff options
Diffstat (limited to 'lib/stdlib/src/orddict.erl')
-rw-r--r-- | lib/stdlib/src/orddict.erl | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/stdlib/src/orddict.erl b/lib/stdlib/src/orddict.erl index 45d3c84b3e..c98d78b34d 100644 --- a/lib/stdlib/src/orddict.erl +++ b/lib/stdlib/src/orddict.erl @@ -20,7 +20,7 @@ -module(orddict). %% Standard interface. --export([new/0,is_key/2,to_list/1,from_list/1,size/1]). +-export([new/0,is_key/2,to_list/1,from_list/1,size/1,is_empty/1]). -export([fetch/2,find/2,fetch_keys/1,erase/2]). -export([store/3,append/3,append_list/3,update/3,update/4,update_counter/3]). -export([fold/3,map/2,filter/2,merge/3]). @@ -56,14 +56,22 @@ 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(). size(D) -> length(D). +-spec is_empty(Orddict) -> boolean() when + Orddict :: orddict(). + +is_empty([]) -> true; +is_empty([_|_]) -> false. + -spec fetch(Key, Orddict) -> Value when Key :: term(), Value :: term(), @@ -229,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. |