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
原文:http://my.oschina.net/u/932809/blog/300540