Phil
Phil

Reputation: 5615

Efficient Immutable Map Implementation?

I'm wondering if there is an implementation of a map which is:

In short, is there a functional data structure that can compare with Hash Maps in performance?

Upvotes: 10

Views: 4089

Answers (4)

Brian
Brian

Reputation: 118865

I haven't read it, but I think some people consider Purely Functional Data Structures as the bible for this kind of thing.

Upvotes: 1

Phil
Phil

Reputation: 5615

Anyway just to share with people, these are two interesting blog entries about Persistent Vectors implementation in Scala using Tries. They also mention Clojure's implementation, as well as the new IntMap in recent Scala releases.

http://www.codecommit.com/blog/scala/implementing-persistent-vectors-in-scala http://www.codecommit.com/blog/scala/more-persistent-vectors-performance-analysis

For these data structures, I have tested with key as integers, but not yet strings. Because my real application is gonna use strings as keys, I'm not sure if the implementation will be more efficient than a Hash Map. What if I use the HashCode of the String as a key, then use a Persistent Vector to support the map? I will use a 32-way trie to implement the Persistent Vector. I guess collision would be very rare, and memory would only be spent accordingly. But I'm not sure about the actual amount needed copying on updates.

I'm gonna post my results soon.

Upvotes: 1

Chris Conway
Chris Conway

Reputation: 55989

Scala also has immutable maps, but they are slower than hash tables. I suspect the answer to your question is no, you won't find an immutable map implementation with O(1) expected time insert/query operations.

Upvotes: 3

Vijay Mathew
Vijay Mathew

Reputation: 27174

Clojure has immutable maps. (link). Not sure what underlying data structure it is using. Clojure source code will give you more information!

Upvotes: 4

Related Questions