aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorMagnus Henoch <[email protected]>2013-09-04 14:08:34 +0100
committerMagnus Henoch <[email protected]>2013-09-13 10:44:23 +0100
commit0e4b0ae7ce57117a561f70fa51026d85b4702aaa (patch)
tree4a0aba55aaeef71d6f2abed3b8aec50cd0018c6a /lib/stdlib/src
parent5b56a49d15d10ad4a35dbc8b2a4b01486f1d5294 (diff)
downloadotp-0e4b0ae7ce57117a561f70fa51026d85b4702aaa.tar.gz
otp-0e4b0ae7ce57117a561f70fa51026d85b4702aaa.tar.bz2
otp-0e4b0ae7ce57117a561f70fa51026d85b4702aaa.zip
Add dict:is_empty/1 and orddict:is_empty/1
dict:size/1 runs in constant time, but orddict:size/1 does not. With this change, the two modules stay API compatible and gain a constant-time function for checking whether a dictionary is empty.
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r--lib/stdlib/src/dict.erl7
-rw-r--r--lib/stdlib/src/orddict.erl8
2 files changed, 13 insertions, 2 deletions
diff --git a/lib/stdlib/src/dict.erl b/lib/stdlib/src/dict.erl
index e3bfb6c2e2..e67433ffb7 100644
--- a/lib/stdlib/src/dict.erl
+++ b/lib/stdlib/src/dict.erl
@@ -36,7 +36,7 @@
-module(dict).
%% 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]).
@@ -112,6 +112,11 @@ from_list(L) ->
size(#dict{size=N}) when is_integer(N), N >= 0 -> N.
+-spec is_empty(Dict) -> boolean() when
+ Dict :: dict().
+
+is_empty(#dict{size=N}) -> N =:= 0.
+
-spec fetch(Key, Dict) -> Value when
Key :: term(),
Dict :: dict(),
diff --git a/lib/stdlib/src/orddict.erl b/lib/stdlib/src/orddict.erl
index 45d3c84b3e..da60fc1bb6 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]).
@@ -64,6 +64,12 @@ from_list(Pairs) ->
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(),