Reputation: 20670
Here is the code for assoc
:
(def assoc
(fn assoc
([map key val] (. clojure.lang.RT (assoc map key val)))))
What is the meaning of clojure.lang.RT
?
What is the complexity of calling assoc
on a vector/map?
What is the complexity of accessing a structure that has been created by assoc
?
Upvotes: 3
Views: 440
Reputation: 91554
clojure.lang.RT
is the main Clojure RunTime class. It has most of the methods that make up the core of the language.
assoc
is O(1) for all the associative data structure including vectors and maps. array-map
starts out linear for the first few item, then promoted itself to a hash-map
so it is O(1) as well. You could of course implement the associative interface with something that is not O(1) if you need to.
Technically the access time for an item in a map or vector that was created by calling assoc
on another map is O(log32 N). Because these data structures have an upper bound on the size of ~2^32 items this leaves a maximum tree depth of six which effectively constant time
Clojure's associative data structures are all O(nLog n) in space (because they are trees).
Upvotes: 4