Is there a version of NavigableMap Scala?

In Java 1.6, NavigableMap (and NavigableSet ) and TreeMap have been updated to implement the new interface. Among other things, NavigableMap is useful for questions like "Which item in the collection is closest to X?" (See this excellent Francois Sarradin blog post for an example and discussion.)

I was hoping to find something similar in the Scala 2.8 TreeMap implementation, but alas, it seems like this is not the case (at least it is not obvious). Is there another Scala class or class that is similar to Java NavigableMap? If not, are there simple Scala idioms that can be used to accomplish something like this?

I understand that I can use Java TreeMap, but I would like to stay within the Scala framework (if only for simplicity).

+7
source share
2 answers

In immutable collections with backlinks, it is not possible to make the collection permanent. Thus, people instead use lightning to navigate such structures. Anti-xml has a good example of using lightning when working with XML.

+2
source

Here is the topic in this section.

It seems that SortedMap can help you get involved, but what I have tested so far, I'm not sure if this is O (log (n)), what the search should be:

 def searchMap(m: SortedMap[Int,_], k: Int) = { val left = m to(k) lastKey val right = m from(k) take(2) lastKey if (k - left < right - k) left else right } 

Based on the definitions of from and to in terms of rangeImpl , it looks like it could be O (log (n)), but, based on the actual time of it, it seems to grow linearly for all plausible values โ€‹โ€‹of n.

So I'm not sure.

+1
source

All Articles