首页 > 其他 > 详细

Erlang gb_tree操作

时间:2014-08-13 01:25:05      阅读:402      评论:0      收藏:0      [点我收藏+]

1、gb_tree的查询操作:
lookup(Key, {_, T}) ->

lookup_1(Key, T).

lookup_1(Key, {Key1, , Smaller, }) when Key < Key1 ->

lookup_1(Key, Smaller);

lookup_1(Key, {Key1, , , Bigger}) when Key > Key1 ->

lookup_1(Key, Bigger);

lookup_1(, {, Value, , }) ->

{value, Value};

lookup_1(_, nil) ->

none.

2、gb_tree的更新操作:
update_1(Key, Value, {Key1, V, Smaller, Bigger}) when Key < Key1 ->

{Key1, V, update_1(Key, Value, Smaller), Bigger};

update_1(Key, Value, {Key1, V, Smaller, Bigger}) when Key > Key1 ->

{Key1, V, Smaller, update_1(Key, Value, Bigger)};

update_1(Key, Value, {, , Smaller, Bigger}) ->

{Key, Value, Smaller, Bigger}.

3、gb_tree查找所有键值操作:
keys({_, T}) ->

keys(T, []).

keys({Key, _Value, Small, Big}, L) ->

keys(Small, [Key | keys(Big, L)]);

keys(nil, L) -> L.
4、gb_tree查找所有value操作:
values({_, T}) ->

values(T, []).

values({_Key, Value, Small, Big}, L) ->

values(Small, [Value | values(Big, L)]);

values(nil, L) -> L.
5、take_largest操作:
take_largest({Size, Tree}) when is_integer(Size), Size >= 0 ->

{Key, Value, Smaller} = take_largest1(Tree),
{Key, Value, {Size - 1, Smaller}}.

take_largest1({Key, Value, Smaller, nil}) ->

{Key, Value, Smaller};

take_largest1({Key, Value, Smaller, Larger}) ->

{Key1, Value1, Larger1} = take_largest1(Larger),
{Key1, Value1, {Key, Value, Smaller, Larger1}}.

Erlang gb_tree操作,布布扣,bubuko.com

Erlang gb_tree操作

原文:http://my.oschina.net/u/932809/blog/300540

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!