8000 SI-4147 Add an implementation of `mutable.TreeMap` by ruippeixotog · Pull Request #4504 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

SI-4147 Add an implementation of mutable.TreeMap #4504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
ruippeixotog committed May 30, 2015
commit 0e8b73b29d96e3a37346ae196039ebded23303a7
24 changes: 24 additions & 0 deletions src/library/scala/collection/generic/MutableSortedMapFactory.scala
7018
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scala
package collection
package generic

import scala.language.higherKinds

/**
* A template for companion objects of `SortedMap` and subclasses thereof.
*
* @tparam CC the type of the collection.
*
* @author Rui Gonçalves
* @since 2.12
* @version 2.12
*
* @define Coll `mutable.SortedMap`
* @define coll mutable sorted map
* @define factoryInfo
* This object provides a set of operations needed to create sorted maps of type `$Coll`.
* @define sortedMapCanBuildFromInfo
* The standard `CanBuildFrom` instance for sorted maps
*/
abstract class MutableSortedMapFactory[CC[A, B] <: mutable.SortedMap[A, B] with SortedMapLike[A, B, CC[A, B]]]
extends SortedMapFactory[CC]
Loading
0