2008-04-22 23:51:32 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : treap.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Treaps implementation
|
|
|
|
%%% Created : 22 Apr 2008 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 ProcessOne
|
2008-04-22 23:51:32 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(treap).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-export([empty/0, insert/4, delete/2, delete_root/1,
|
|
|
|
get_root/1, lookup/2, is_empty/1, fold/3, from_list/1,
|
2010-03-17 15:40:48 +01:00
|
|
|
to_list/1]).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-type hashkey() :: {non_neg_integer(), any()}.
|
|
|
|
|
|
|
|
-type treap() :: {hashkey(), any(), any(), treap(), treap()} | nil.
|
|
|
|
|
|
|
|
-export_type([treap/0]).
|
|
|
|
|
|
|
|
empty() -> nil.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
|
|
|
insert(Key, Priority, Value, Tree) ->
|
|
|
|
HashKey = {erlang:phash2(Key), Key},
|
|
|
|
insert1(Tree, HashKey, Priority, Value).
|
|
|
|
|
|
|
|
insert1(nil, HashKey, Priority, Value) ->
|
|
|
|
{HashKey, Priority, Value, nil, nil};
|
2012-09-11 15:45:59 +02:00
|
|
|
insert1({HashKey1, Priority1, Value1, Left, Right} =
|
|
|
|
Tree,
|
2008-04-22 23:51:32 +02:00
|
|
|
HashKey, Priority, Value) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
if HashKey < HashKey1 ->
|
|
|
|
heapify({HashKey1, Priority1, Value1,
|
|
|
|
insert1(Left, HashKey, Priority, Value), Right});
|
|
|
|
HashKey > HashKey1 ->
|
|
|
|
heapify({HashKey1, Priority1, Value1, Left,
|
|
|
|
insert1(Right, HashKey, Priority, Value)});
|
|
|
|
Priority == Priority1 ->
|
|
|
|
{HashKey, Priority, Value, Left, Right};
|
|
|
|
true ->
|
|
|
|
insert1(delete_root(Tree), HashKey, Priority, Value)
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
heapify({_HashKey, _Priority, _Value, nil, nil} =
|
|
|
|
Tree) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
Tree;
|
2012-09-11 15:45:59 +02:00
|
|
|
heapify({HashKey, Priority, Value, nil = Left,
|
|
|
|
{HashKeyR, PriorityR, ValueR, LeftR, RightR}} =
|
|
|
|
Tree) ->
|
|
|
|
if PriorityR > Priority ->
|
|
|
|
{HashKeyR, PriorityR, ValueR,
|
|
|
|
{HashKey, Priority, Value, Left, LeftR}, RightR};
|
|
|
|
true -> Tree
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
|
|
|
heapify({HashKey, Priority, Value,
|
|
|
|
{HashKeyL, PriorityL, ValueL, LeftL, RightL},
|
2012-09-11 15:45:59 +02:00
|
|
|
nil = Right} =
|
|
|
|
Tree) ->
|
|
|
|
if PriorityL > Priority ->
|
|
|
|
{HashKeyL, PriorityL, ValueL, LeftL,
|
|
|
|
{HashKey, Priority, Value, RightL, Right}};
|
|
|
|
true -> Tree
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
|
|
|
heapify({HashKey, Priority, Value,
|
|
|
|
{HashKeyL, PriorityL, ValueL, LeftL, RightL} = Left,
|
2012-09-11 15:45:59 +02:00
|
|
|
{HashKeyR, PriorityR, ValueR, LeftR, RightR} = Right} =
|
|
|
|
Tree) ->
|
|
|
|
if PriorityR > Priority ->
|
|
|
|
{HashKeyR, PriorityR, ValueR,
|
|
|
|
{HashKey, Priority, Value, Left, LeftR}, RightR};
|
|
|
|
PriorityL > Priority ->
|
|
|
|
{HashKeyL, PriorityL, ValueL, LeftL,
|
|
|
|
{HashKey, Priority, Value, RightL, Right}};
|
|
|
|
true -> Tree
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
delete(Key, Tree) ->
|
|
|
|
HashKey = {erlang:phash2(Key), Key},
|
2008-04-23 15:14:08 +02:00
|
|
|
delete1(HashKey, Tree).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
delete1(_HashKey, nil) -> nil;
|
|
|
|
delete1(HashKey,
|
|
|
|
{HashKey1, Priority1, Value1, Left, Right} = Tree) ->
|
|
|
|
if HashKey < HashKey1 ->
|
|
|
|
{HashKey1, Priority1, Value1, delete1(HashKey, Left),
|
|
|
|
Right};
|
|
|
|
HashKey > HashKey1 ->
|
|
|
|
{HashKey1, Priority1, Value1, Left,
|
|
|
|
delete1(HashKey, Right)};
|
|
|
|
true -> delete_root(Tree)
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
delete_root({HashKey, Priority, Value, Left, Right}) ->
|
|
|
|
case {Left, Right} of
|
2012-09-11 15:45:59 +02:00
|
|
|
{nil, nil} -> nil;
|
|
|
|
{_, nil} -> Left;
|
|
|
|
{nil, _} -> Right;
|
|
|
|
{{HashKeyL, PriorityL, ValueL, LeftL, RightL},
|
|
|
|
{HashKeyR, PriorityR, ValueR, LeftR, RightR}} ->
|
|
|
|
if PriorityL > PriorityR ->
|
|
|
|
{HashKeyL, PriorityL, ValueL, LeftL,
|
|
|
|
delete_root({HashKey, Priority, Value, RightL, Right})};
|
|
|
|
true ->
|
|
|
|
{HashKeyR, PriorityR, ValueR,
|
|
|
|
delete_root({HashKey, Priority, Value, Left, LeftR}),
|
|
|
|
RightR}
|
|
|
|
end
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
is_empty(nil) -> true;
|
|
|
|
is_empty({_HashKey, _Priority, _Value, _Left,
|
|
|
|
_Right}) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
false.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
get_root({{_Hash, Key}, Priority, Value, _Left,
|
|
|
|
_Right}) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
{Key, Priority, Value}.
|
|
|
|
|
|
|
|
lookup(Key, Tree) ->
|
|
|
|
HashKey = {erlang:phash2(Key), Key},
|
|
|
|
lookup1(Tree, HashKey).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
lookup1(nil, _HashKey) -> error;
|
|
|
|
lookup1({HashKey1, Priority1, Value1, Left, Right},
|
|
|
|
HashKey) ->
|
|
|
|
if HashKey < HashKey1 -> lookup1(Left, HashKey);
|
|
|
|
HashKey > HashKey1 -> lookup1(Right, HashKey);
|
|
|
|
true -> {ok, Priority1, Value1}
|
2008-04-22 23:51:32 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
fold(_F, Acc, nil) -> Acc;
|
|
|
|
fold(F, Acc,
|
|
|
|
{{_Hash, Key}, Priority, Value, Left, Right}) ->
|
2009-07-22 08:46:07 +02:00
|
|
|
Acc1 = F({Key, Priority, Value}, Acc),
|
|
|
|
Acc2 = fold(F, Acc1, Left),
|
|
|
|
fold(F, Acc2, Right).
|
2010-03-17 15:40:48 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
to_list(Tree) -> to_list(Tree, []).
|
2010-03-17 15:40:48 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
to_list(nil, Acc) -> Acc;
|
2010-03-17 15:40:48 +01:00
|
|
|
to_list(Tree, Acc) ->
|
|
|
|
Root = get_root(Tree),
|
2012-09-11 15:45:59 +02:00
|
|
|
to_list(delete_root(Tree), [Root | Acc]).
|
2010-03-17 15:40:48 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
from_list(List) -> from_list(List, nil).
|
2010-03-17 15:40:48 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
from_list([{Key, Priority, Value} | Tail], Tree) ->
|
2010-03-17 15:40:48 +01:00
|
|
|
from_list(Tail, insert(Key, Priority, Value, Tree));
|
2012-09-11 15:45:59 +02:00
|
|
|
from_list([], Tree) -> Tree.
|