BorjaEst

BorjaEst

Trees (specially gb_trees) are a very efficient/fast tool to store Key-Value. However, depending on the program it’s usage cat get a bit more complicated.

Here goes my challenge.
What would be the most efficient way to get a random element and it’s path from a gb_tree?

Showing Posts 1 to 3

al2o3cr

al2o3cr

:gb_trees.iterator/1 plus a reservoir sampling algorithm?

BorjaEst

BorjaEst OP

The idea is good if we don’t want to cover the whole tree (just the k first), however to cover the whole tree, wouldn’t we have to execute rand:random() in each iteration (rand operations are quite expensive)?

BorjaEst

BorjaEst OP

So after thinking about it, my solution would be something like this:

iex(1)> tree = :gb_tree.example() #Any f that returns a tree
{3, {:a, 1, nil, {:b, 2, nil, {:c, 3, nil, nil}}}}
iex(2)> n = :rand.uniform(:gb_trees.size(tree))
2
iex(11)> iter = :gb_trees.iterator(tree) 
[{:a, 1, nil, {:b, 2, nil, {:c, 3, nil, nil}}}]
iex(14)> {k,v,_}=Enum.reduce(1..n, {:nil,:nil,:gb_trees.iterator(tree)}, fn _, {_,_,i} -> :gb_trees.next(i) end)
{:b, 2, [{:c, 3, nil, nil}]}

However I am thinking, now gb_trees are not used any longer as we do have maps.

— All posts loaded —

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews