diff options
Diffstat (limited to 'src/library/scala/collection/mutable/TreeMap.scala')
-rw-r--r-- | src/library/scala/collection/mutable/TreeMap.scala | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/library/scala/collection/mutable/TreeMap.scala b/src/library/scala/collection/mutable/TreeMap.scala index 244cc18735..dc7d5d750e 100644 --- a/src/library/scala/collection/mutable/TreeMap.scala +++ b/src/library/scala/collection/mutable/TreeMap.scala @@ -52,6 +52,20 @@ sealed class TreeMap[A, B] private (tree: RB.Tree[A, B])(implicit val ordering: override def empty = TreeMap.empty override protected[this] def newBuilder = TreeMap.newBuilder[A, B] + /** + * Creates a ranged projection of this map. Any mutations in the ranged projection will update the original map and + * vice versa. + * + * Only entries with keys between this projection's key range will ever appear as elements of this map, independently + * of whether the entries are added through the original map or through this view. That means that if one inserts a + * key-value in a view whose key is outside the view's bounds, calls to `get` or `contains` will _not_ consider the + * newly added entry. Mutations are always reflected in the original map, though. + * + * @param from the lower bound (inclusive) of this projection wrapped in a `Some`, or `None` if there is no lower + * bound. + * @param until the upper bound (exclusive) of this projection wrapped in a `Some`, or `None` if there is no upper + * bound. + */ def rangeImpl(from: Option[A], until: Option[A]): TreeMap[A, B] = new TreeMapView(from, until) def -=(key: A): this.type = { RB.delete(tree, key); this } @@ -64,7 +78,7 @@ sealed class TreeMap[A, B] private (tree: RB.Tree[A, B])(implicit val ordering: def keysIteratorFrom(start: A) = RB.keysIterator(tree, Some(start)) def valuesIteratorFrom(start: A) = RB.valuesIterator(tree, Some(start)) - override def size = tree.size + override def size = RB.size(tree) override def isEmpty = RB.isEmpty(tree) override def contains(key: A) = RB.contains(tree, key) @@ -78,7 +92,7 @@ sealed class TreeMap[A, B] private (tree: RB.Tree[A, B])(implicit val ordering: override def foreach[U](f: ((A, B)) => U): Unit = RB.foreach(tree, f) override def transform(f: (A, B) => B) = { RB.transform(tree, f); this } - override def clear(): Unit = tree.root = null + override def clear(): Unit = RB.clear(tree) override def stringPrefix = "TreeMap" @@ -157,10 +171,15 @@ sealed class TreeMap[A, B] private (tree: RB.Tree[A, B])(implicit val ordering: } } + // Using the iterator should be efficient enough; if performance is deemed a problem later, specialized + // `foreach(f, from, until)` and `transform(f, from, until)` methods can be created in `RedBlackTree`. See + // https://github.com/scala/scala/pull/4608#discussion_r34307985 for a discussion about this. override def foreach[U](f: ((A, B)) => U): Unit = iterator.foreach(f) override def transform(f: (A, B) => B) = { iterator.foreach { case (key, value) => update(key, f(key, value)) } this } + + override def clone() = super.clone().rangeImpl(from, until) } } |