码迷,mamicode.com
首页 > 其他好文 > 详细

Erlang gb_tree操作

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

标签:java   ar   size   on   class   date   as   c   

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操作

标签:java   ar   size   on   class   date   as   c   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!