1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2008-2017. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21-module(dict_test_lib). 22 23-export([new/2]). 24 25new(Mod, Eq) -> 26 fun (enter, {K,V,D}) -> enter(Mod, K, V, D); 27 (empty, []) -> empty(Mod); 28 (equal, {D1,D2}) -> Eq(D1, D2); 29 (from_list, L) -> from_list(Mod, L); 30 (module, []) -> Mod; 31 (size, D) -> Mod:size(D); 32 (is_empty, D) -> Mod:is_empty(D); 33 (iterator, S) -> Mod:iterator(S); 34 (iterator_from, {Start, S}) -> Mod:iterator_from(Start, S); 35 (next, I) -> Mod:next(I); 36 (to_list, D) -> to_list(Mod, D); 37 (erase, {K,D}) -> erase(Mod, K, D); 38 (take, {K,D}) -> take(Mod, K, D) 39 end. 40 41empty(Mod) -> 42 case erlang:function_exported(Mod, new, 0) of 43 false -> Mod:empty(); 44 true -> Mod:new() 45 end. 46 47to_list(Mod, D) -> 48 Mod:to_list(D). 49 50from_list(Mod, L) -> 51 case erlang:function_exported(Mod, from_orddict, 1) of 52 false -> 53 Mod:from_list(L); 54 true -> 55 %% The gb_trees module has no from_list/1 function. 56 %% 57 %% The keys in S are not unique. To make sure 58 %% that we pick the same key/value pairs as 59 %% dict/orddict, first convert the list to an orddict. 60 Orddict = orddict:from_list(L), 61 Mod:from_orddict(Orddict) 62 end. 63 64%% Store new value into dictionary or update previous value in dictionary. 65enter(Mod, Key, Val, Dict) -> 66 case erlang:function_exported(Mod, store, 3) of 67 false -> 68 Mod:enter(Key, Val, Dict); 69 true -> 70 Mod:store(Key, Val, Dict) 71 end. 72 73erase(Mod, Key, Val) when Mod =:= dict; Mod =:= orddict -> 74 Mod:erase(Key, Val); 75erase(gb_trees, Key, Val) -> 76 gb_trees:delete_any(Key, Val). 77 78take(gb_trees, Key, Val) -> 79 Res = try 80 gb_trees:take(Key, Val) 81 catch 82 error:_ -> 83 error 84 end, 85 Res = gb_trees:take_any(Key, Val); 86take(Mod, Key, Val) -> 87 Mod:take(Key, Val). 88