Dear all,

for a project, I need a data structure to represent finite distribution,
that is a set of elements, each of which having a mass in the set, and two
functions:

- total-mass : the sum of the masses of each element
- draw : return an element with a probability proportional to its mass

I currently use the simple idea : a Persistent Hash Map from elt to mass:
- the total-mass is the reduce of (+ . second)
- drawing is choosing a random-number between 0 and the total-mass and
looping through the seq, to look for the corresponding element.

Both operation are linear in the number of elements. This is a problem,
because these sets tend to be quite big in my project.

I think that if I used a HashTrie with total-mass cached in each node,
total-mass would be in constant time and draw in log time.

So here is my question : does anyone has, for example for the "clojure in
clojure" project, a sketch of an implementation of PersistentHashMap
in clojure itself? (I would be happy to contribute, if it needs more work).
Or has anyone a better idea that mine to have drawable sets?

Thanks for your help,

Best regards,

Nicolas.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to