|
| 1 | +/* __ *\ |
| 2 | +** ________ ___ / / ___ Scala API ** |
| 3 | +** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** |
| 4 | +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** |
| 5 | +** /____/\___/_/ |_/____/_/ | | ** |
| 6 | +** |/ ** |
| 7 | +\* */ |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +package scala.collection |
| 12 | +package immutable |
| 13 | + |
| 14 | +import generic._ |
| 15 | +import mutable.Builder |
| 16 | +import annotation.unchecked.uncheckedVariance |
| 17 | +import annotation.bridge |
| 18 | + |
| 19 | +/** A map whose keys are sorted. |
| 20 | + * |
| 21 | + * @tparam A the type of the keys contained in this sorted map. |
| 22 | + * @tparam B the type of the values associated with the keys. |
| 23 | + * |
| 24 | + * @author Sean McDirmid |
| 25 | + * @author Martin Odersky |
| 26 | + * @version 2.8 |
| 27 | + * @since 2.4 |
| 28 | + * @define Coll immutable.SortedMap |
| 29 | + * @define coll immutable sorted map |
| 30 | + */ |
| 31 | +trait SortedMap[A, +B] extends Map[A, B] |
| 32 | + with scala.collection.SortedMap[A, B] |
| 33 | + with MapLike[A, B, SortedMap[A, B]] |
| 34 | + with SortedMapLike[A, B, SortedMap[A, B]] { self => |
| 35 | + |
| 36 | + override protected[this] def newBuilder : Builder[(A, B), SortedMap[A, B]] = |
| 37 | + SortedMap.newBuilder[A, B] |
| 38 | + |
| 39 | + override def empty: SortedMap[A, B] = SortedMap.empty |
| 40 | + override def updated [B1 >: B](key: A, value: B1): SortedMap[A, B1] = this + ((key, value)) |
| 41 | + override def keySet: immutable.SortedSet[A] = new DefaultKeySortedSet |
| 42 | + |
| 43 | + protected class DefaultKeySortedSet extends super.DefaultKeySortedSet with immutable.SortedSet[A] { |
| 44 | + override def + (elem: A): SortedSet[A] = |
| 45 | + if (this(elem)) this |
| 46 | + else SortedSet[A]() ++ this + elem |
| 47 | + override def - (elem: A): SortedSet[A] = |
| 48 | + if (this(elem)) SortedSet[A]() ++ this - elem |
| 49 | + else this |
| 50 | + override def rangeImpl(from : Option[A], until : Option[A]) : SortedSet[A] = { |
| 51 | + val map = self.rangeImpl(from, until) |
| 52 | + new map.DefaultKeySortedSet |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** Add a key/value pair to this map. |
| 57 | + * @param kv the key/value pair |
| 58 | + * @return A new map with the new binding added to this map |
| 59 | + * @note needs to be overridden in subclasses |
| 60 | + */ |
| 61 | + def + [B1 >: B](kv: (A, B1)): SortedMap[A, B1] = throw new AbstractMethodError("SortedMap.+") |
| 62 | + |
| 63 | + /** Adds two or more elements to this collection and returns |
| 64 | + * a new collection. |
| 65 | + * |
| 66 | + * @param elem1 the first element to add. |
| 67 | + * @param elem2 the second element to add. |
| 68 | + * @param elems the remaining elements to add. |
| 69 | + */ |
| 70 | + override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = |
| 71 | + this + elem1 + elem2 ++ elems |
| 72 | + |
| 73 | + /** Adds a number of elements provided by a traversable object |
| 74 | + * and returns a new collection with the added elements. |
| 75 | + * |
| 76 | + * @param xs the traversable object. |
| 77 | + */ |
| 78 | + override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): SortedMap[A, B1] = |
| 79 | + ((repr: SortedMap[A, B1]) /: xs.seq) (_ + _) |
| 80 | + |
| 81 | + @bridge def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): SortedMap[A, B1] = ++(xs: GenTraversableOnce[(A, B1)]) |
| 82 | +} |
| 83 | + |
| 84 | +/** $factoryInfo |
| 85 | + * @define Coll immutable.SortedMap |
| 86 | + * @define coll immutable sorted map |
| 87 | + */ |
| 88 | +object SortedMap extends ImmutableSortedMapFactory[SortedMap] { |
| 89 | + /** $sortedMapCanBuildFromInfo */ |
| 90 | + implicit def canBuildFrom[A, B](implicit ord: Ordering[A]): CanBuildFrom[Coll, (A, B), SortedMap[A, B]] = new SortedMapCanBuildFrom[A, B] |
| 91 | + def empty[A, B](implicit ord: Ordering[A]): SortedMap[A, B] = sys.error("TreeMap is not supported due to bugs. We're working on a fix.") |
| 92 | +} |
0 commit comments