Great question, I'd really like to see something like that!
> - Indexed: O(n), since nth might be O(n)
Actually I'd expect Indexed.nth to be more performant than O(n) (at
least O(log n)). For O(n), Sequential seems to be the correct
interface. The current behaviour of clojure.core/nth seems to support
this view (it only performs linear searches for things which are
Sequential but not Indexed).
Sincerely,
Michał
Am 07.09.2010 um 21:50 schrieb Michał Marczyk:
>> - Indexed: O(n), since nth might be O(n)
>
> Actually I'd expect Indexed.nth to be more performant than O(n) (at
> least O(log n)). For O(n), Sequential seems to be the correct
> interface. The current behaviour of clojure.core/nth seems to support
> this view (it only performs linear searches for things which are
> Sequential but not Indexed).
Well, the performance guarantee of of nth is O(n) because of the way it treats Sequential. So there's no reason why Indexed should be faster. I see it basically as a hook for data structures, which can provide some improved lookup. And in fact I know a datastructure, where the "usual" access time is O(1) (or O(log_s n) for a reasonable s, similar to vectors), but in the worst case it might be O(n). Just like quicksort is O(n^2) in the worst case, but is usually faster. I don't see a reason, why this data structure should not implement Indexed. It would not break the contract of nth.
But my background on these questions is quite limited. So by asking I'm on the save side. :)
Sincerely
Meikel