From a7a0bb29fd162cdfc6cdf6eb18cb5e2cf22a430e Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 21 Jan 2015 18:22:58 +0100 Subject: erts: Add hashmap:find/2 --- erts/emulator/beam/bif.tab | 1 + erts/emulator/beam/erl_hashmap.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'erts/emulator/beam') diff --git a/erts/emulator/beam/bif.tab b/erts/emulator/beam/bif.tab index 6408883e96..c0fb1f8827 100644 --- a/erts/emulator/beam/bif.tab +++ b/erts/emulator/beam/bif.tab @@ -617,6 +617,7 @@ bif erlang:get_keys/0 # Hash Array Mappped Trie bif hashmap:put/3 bif hashmap:get/2 +bif hashmap:find/2 bif hashmap:remove/2 bif hashmap:info/1 bif hashmap:to_list/1 diff --git a/erts/emulator/beam/erl_hashmap.c b/erts/emulator/beam/erl_hashmap.c index 8e651dd776..a5d6daadd7 100644 --- a/erts/emulator/beam/erl_hashmap.c +++ b/erts/emulator/beam/erl_hashmap.c @@ -133,6 +133,28 @@ BIF_RETTYPE hashmap_get_2(BIF_ALIST_2) { BIF_ERROR(BIF_P, BADARG); } +/* hashmap:find/2 */ + +BIF_RETTYPE hashmap_find_2(BIF_ALIST_2) { + if (is_hashmap(BIF_ARG_2)) { + Eterm *hp, res; + const Eterm *value; + Uint32 hx = make_hash2(BIF_ARG_1); + + if ((value = hashmap_get(hx, BIF_ARG_1, BIF_ARG_2)) != NULL) { + hp = HAlloc(BIF_P, 3); + res = make_tuple(hp); + *hp++ = make_arityval(2); + *hp++ = am_ok; + *hp++ = *value; + BIF_RET(res); + } + BIF_RET(am_error); + } + BIF_ERROR(BIF_P, BADARG); +} + + /* hashmap:remove/2 */ BIF_RETTYPE hashmap_remove_2(BIF_ALIST_2) { -- cgit v1.2.3