diff options
author | Michael Thorpe <mt2309@ic.ac.uk> | 2013-01-31 21:06:02 +0000 |
---|---|---|
committer | Michael Thorpe <mt2309@ic.ac.uk> | 2013-01-31 21:06:02 +0000 |
commit | 8bd03e063c412cefcd52f88fef68283290893708 (patch) | |
tree | 200f01b54b6d4ce8ed8e746271c020ae41f0bb3e | |
parent | 1e69a0b67a184ca9d0b205a407e34cf85d11372c (diff) | |
download | scala-8bd03e063c412cefcd52f88fef68283290893708.tar.gz scala-8bd03e063c412cefcd52f88fef68283290893708.tar.bz2 scala-8bd03e063c412cefcd52f88fef68283290893708.zip |
SI-5151 - Add firstKey and lastKey to LongMap.
-rw-r--r-- | src/library/scala/collection/immutable/LongMap.scala | 16 | ||||
-rw-r--r-- | test/files/run/longmap.check | 0 | ||||
-rw-r--r-- | test/files/run/longmap.scala | 8 |
3 files changed, 24 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/LongMap.scala b/src/library/scala/collection/immutable/LongMap.scala index fab1b7f00b..60300c2a9e 100644 --- a/src/library/scala/collection/immutable/LongMap.scala +++ b/src/library/scala/collection/immutable/LongMap.scala @@ -12,6 +12,7 @@ package immutable import scala.collection.generic.{ CanBuildFrom, BitOperations } import scala.collection.mutable.{ Builder, MapBuilder } +import scala.annotation.tailrec /** Utility class for long maps. * @author David MacIver @@ -416,5 +417,20 @@ extends AbstractMap[Long, T] def ++[S >: T](that: LongMap[S]) = this.unionWith[S](that, (key, x, y) => y) + + @tailrec + final def firstKey: Long = this match { + case LongMap.Bin(_, _, l, r) => l.firstKey + case LongMap.Tip(k, v) => k + case LongMap.Nil => sys.error("Empty set") + } + + @tailrec + final def lastKey: Long = this match { + case LongMap.Bin(_, _, l, r) => r.lastKey + case LongMap.Tip(k , v) => k + case LongMap.Nil => sys.error("Empty set") + } + } diff --git a/test/files/run/longmap.check b/test/files/run/longmap.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/run/longmap.check diff --git a/test/files/run/longmap.scala b/test/files/run/longmap.scala new file mode 100644 index 0000000000..1f18eebd31 --- /dev/null +++ b/test/files/run/longmap.scala @@ -0,0 +1,8 @@ +object Test extends App{ + import scala.collection.immutable.LongMap; + + val it = LongMap(8L -> 2, 11L -> 3, 1L -> 2, 7L -> 13); + + assert(it.firstKey == 1L); + assert(it.lastKey == 11L); +} |