assoc! order problem

84 views
Skip to first unread message

Jeff Heon

unread,
Aug 4, 2012, 3:53:04 PM8/4/12
to clo...@googlegroups.com
You are using the map literal, which corresponds to the hash map.

Use this if you want a sorted map: http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/sorted-map

Bronsa

unread,
Aug 4, 2012, 3:59:30 PM8/4/12
to clo...@googlegroups.com
Note that you can't use a sorted-map in a transient

2012/8/4 Jeff Heon <jfh...@gmail.com>
You are using the map literal, which corresponds to the hash map.

Use this if you want a sorted map: http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/sorted-map

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Warren Lynn

unread,
Aug 4, 2012, 5:34:51 PM8/4/12
to clo...@googlegroups.com
I have not used transient yet so I don't understand your code. But for ordered maps, I use ArrayMap.


llj098

unread,
Aug 5, 2012, 4:10:05 AM8/5/12
to clo...@googlegroups.com
thanks for reply,

I know the sorted-map, but my question is : why the map is not 'ordered' when size is larger than 32 ? 

Bronsa

unread,
Aug 5, 2012, 4:37:17 AM8/5/12
to clo...@googlegroups.com

because of performance reasons, hash-maps are not ordered.
the fact that they are ordered for the first 32 elements is just an implementation detail you shouldn't rely on

--

Evan Mezeske

unread,
Aug 5, 2012, 4:40:18 AM8/5/12
to clo...@googlegroups.com
I know the sorted-map, but my question is : why the map is not 'ordered' when size is larger than 32 ?

Because a hash-map is never guaranteed to be sorted.  In your case, it sounds like Clojure is being smart and using a different implementation for small maps (say, less than 32 elements) than it is using for larger maps.  This is probably because there's some clever optimization that can be done for very small maps that becomes a pessimization as the map grows larger.  The data structure that's being used behind the scenes for small maps might *happen* to have the property of being sorted, but if so, it is just an implementation quirk and you can't rely on that fact in your code.  Sorted order is simply not part of the hash-map contract.  The 32 element behind-the-scenes limit might change tomorrow, after someone does an analysis and determines that really, three different implementations should be used, one for up to 16 elements, one for up to 128, and one for more.  Who knows? 

Christian Sperandio

unread,
Aug 5, 2012, 5:31:44 AM8/5/12
to clo...@googlegroups.com
Hi,

By default, maps are array-map for short map and hash map for larger one.
That explains why the map seems sorted at the beginning. 

Christian

Le 5 août 2012 à 10:40, Evan Mezeske <emez...@gmail.com> a écrit :

I know the sorted-map, but my question is : why the map is not 'ordered' when size is larger than 32 ?

Because a hash-map is never guaranteed to be sorted.  In your case, it sounds like Clojure is being smart and using a different implementation for small maps (say, less than 32 elements) than it is using for larger maps.  This is probably because there's some clever optimization that can be done for very small maps that becomes a pessimization as the map grows larger.  The data structure that's being used behind the scenes for small maps might *happen* to have the property of being sorted, but if so, it is just an implementation quirk and you can't rely on that fact in your code.  Sorted order is simply not part of the hash-map contract.  The 32 element behind-the-scenes limit might change tomorrow, after someone does an analysis and determines that really, three different implementations should be used, one for up to 16 elements, one for up to 128, and one for more.  Who knows? 

--
Reply all
Reply to author
Forward
0 new messages