On 6 January 2014 00:33, Brandon Bloom <[email protected]> wrote:
> Michał: This is awesome. Thanks for the continued awesome work on data
> structures!
>
>>
>> ;; if the key is not present in the collection, -1 is returned:
>> (avl/rank-of (avl/sorted-set 3 4 5) 0)
>> ;= -1
>
>
> Curious: Why not return nil instead of -1?
Thank you, happy to hear this might be useful!
As for rank-of's -1: the reason is as Mikera said. I wanted to be
consistent with primitive-returning Java methods like indexOf and to
be able to hint rank-of to ^long (although I haven't yet -- 0.0.11, I
guess!).
This is not the only sensible thing to do; I was thinking about maybe
adding variants of rank-of which would find the index of the "first <=
/ =>" element according to the comparator (which would still return -1
for empty collections and elements less / greater than the extreme
elements of the collection, but not for those in between a pair of
existing keys):
(rank-of-<= (avl/sorted-set 0 2 4) 3)
;= 1
(rank-of->= (avl/sorted-set 0 2 4) 3)
;= 2
(rank-of->= (avl/sorted-set 0 2 4) 2)
;= 1
Alternatively rank-of could take an optional argument to force this
behaviour; we could even use < / <= / >= / > like subseq does.
In fact, apparently there's a relevant ticket in the tracker already:
DAVL-1 [0].
It's doable right now with subseq / rsubseq:
(defn rank-of-<= [coll x]
(if (empty? coll)
-1
(let [r (avl/rank-of coll x)]
(if (== -1 r)
(let [s (rsubseq coll <= x)]
(avl/rank-of coll (first s)))
r))))
(mapv #(rank-of-<= (avl/sorted-set 0 2 4) %) [-1 0 1 2 3 4 5])
;= [-1 0 0 1 1 2 2]
But a direct approach would no doubt be faster.
Any ideas re: possible API, comments on the usefulness of such
additions etc. would be very welcome!
Cheers,
Michał
[0] http://dev.clojure.org/jira/browse/DAVL-1
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.