-
Notifications
You must be signed in to change notification settings - Fork 2
/
skew_kv.erl
50 lines (38 loc) · 1.2 KB
/
skew_kv.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
%%% Adpated from : http://www.erlang.org/pipermail/erlang-questions/2007-August/028769.html
%%% File : skew_kv.erl
%%% Author : <[email protected]>
%%% Description : Skew heaps using Key->Value structure
%%% Created : 30 May 2003 by Pierpaolo BERNARDI <[email protected]>
%%% Modified : 17 Dec 2009 by James Aimonetti <[email protected]>
-module(skew_kv).
-export([empty/0,
is_empty/1,
min/1,
delete_min/1,
insert/3,
merge/2
]).
%% Aggressive inlining - will increase code size.
%%-compile(inline).
%%-compile({inline_size,100}).
%%-define(TESTING,true).
-ifdef(TESTING).
-compile(export_all).
-endif.
-define(THE_EMPTY_HEAP, the_empty_heap).
empty() ->
?THE_EMPTY_HEAP.
is_empty(?THE_EMPTY_HEAP) -> true;
is_empty(_) -> false.
min({K, V, _, _}) -> {K, V}.
delete_min({_K, _V, A, B}) -> merge(A, B).
insert(Key, Value, A) ->
merge({Key, Value, ?THE_EMPTY_HEAP, ?THE_EMPTY_HEAP}, A).
merge(A, ?THE_EMPTY_HEAP) ->
A;
merge(?THE_EMPTY_HEAP, B) ->
B;
merge({KeyA, ValueA, LA, RA}, B={KeyB, _ValueB, _, _}) when KeyA =< KeyB ->
{KeyA, ValueA, RA, merge(LA, B)};
merge(A, {KeyB, ValueB, L, R}) ->
{KeyB, ValueB, R, merge(L, A)}.