Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 0e8b73b

Browse files
committed
SI-4147 Add an implementation of mutable.TreeMap
This commit contains an implementation of a mutable red-black tree with focus on performance. It also contains a new `mutable.TreeMap` Scala collection that is backed by the aforementioned tree. The common generic factories and traits related to mutable sorted maps didn't exist yet, so this commit also adds them. Regarding performance, `TreeMap` overrides (from `MapLike` and `SortedMapLike`) all of the most common methods for maps and also those whose default implementations are asymptotically worse than direct red-black tree algorithms (e.g. `last`, `clear`). The `rangeImpl` method of `TreeMap` returns an instance of `TreeMapView`, an inner class of `TreeMap`. This view is backed by the same `RedBlackTree.Tree` instance, and therefore changes to the original map are reflected in the view and vice-versa. The semantics of mutating a view by adding and removing keys outside the view's range are the same of the current `mutable.TreeSet`. A bit less focus was given on the performance of views - in particular, getting the `size` of a `TreeMapView` is O(n) on the number of elements inside the view bounds. That can be improved in the future. In a future commit, `mutable.TreeSet` can be changed to be backed by this red-black tree implementation.
1 parent 73f4056 commit 0e8b73b

File tree

7 files changed

+1129
-3
lines changed

7 files changed

+1129
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package scala
2+
package collection
3+
package generic
4+
5+
import scala.language.higherKinds
6+
7+
/**
8+
* A template for companion objects of `SortedMap` and subclasses thereof.
9+
*
10+
* @tparam CC the type of the collection.
11+
*
12+
* @author Rui Gonçalves
13+
* @since 2.12
14+
* @version 2.12
15+
*
16+
* @define Coll `mutable.SortedMap`
17+
* @define coll mutable sorted map
18+
* @define factoryInfo
19+
* This object provides a set of operations needed to create sorted maps of type `$Coll`.
20+
* @define sortedMapCanBuildFromInfo
21+
* The standard `CanBuildFrom` instance for sorted maps
22+
*/
23+
abstract class MutableSortedMapFactory[CC[A, B] <: mutable.SortedMap[A, B] with SortedMapLike[A, B, CC[A, B]]]
24+
extends SortedMapFactory[CC]

0 commit comments

Comments
 (0)