Skip to content

Commit 1109efa

Browse files
committed
Add SeqSet and SetFromMap implementations
1 parent 593476e commit 1109efa

File tree

12 files changed

+1065
-0
lines changed

12 files changed

+1065
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection
14+
15+
/**
16+
* A generic trait for ordered sets. Concrete classes have to provide
17+
* functionality for the abstract methods in `SeqSet`.
18+
*
19+
* Note that when checking for equality [[SeqSet]] does not take into account
20+
* ordering.
21+
*
22+
* @tparam A the type of the values contained in this linked set.
23+
* @define coll seq set
24+
* @define Coll `collection.SeqSet`
25+
*/
26+
trait SeqSet[A]
27+
extends Set[A]
28+
with SetOps[A, SeqSet, SeqSet[A]]
29+
with IterableFactoryDefaults[A, SeqSet] {
30+
override def iterableFactory: IterableFactory[SeqSet] = SeqSet
31+
}
32+
33+
object SeqSet extends IterableFactory.Delegate[SeqSet](immutable.SeqSet) {
34+
def fromMap(factory: MapFactory[SeqMap]): IterableFactory[SeqSet] = SeqSetFromMap(factory)
35+
36+
def fromMap[A](map: SeqMap[A, Unit]): SeqSet[A] = SeqSetFromMap(map)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala
14+
package collection
15+
16+
import scala.collection.generic.DefaultSerializable
17+
18+
@SerialVersionUID(3L)
19+
private class SetFromMap[A](protected[collection] val underlying: Map[A, Unit])
20+
extends AbstractSet[A]
21+
with SetFromMapOps.Unknown[A, Map, Map[A, Unit], SetFromMap, SetFromMap[A]]
22+
with SetFromMapOps.Unsorted[A, Map, SetFromMap]
23+
with IterableFactoryDefaults[A, SetFromMap]
24+
with DefaultSerializable {
25+
protected[this] def fromMap[B](m: Map[B, Unit]): SetFromMap[B] = new SetFromMap(m)
26+
27+
override protected[this] def className: String = "SetFromMap"
28+
29+
override def iterableFactory: IterableFactory[SetFromMap] =
30+
new SetFromMap.WrapperFactory(underlying.mapFactory)
31+
}
32+
33+
private object SetFromMap extends SetFromMapMetaFactory[Map, Set] {
34+
def apply(factory: MapFactory[Map]): IterableFactory[Set] = new WrapperFactory(factory)
35+
36+
def apply[A](map: Map[A, Unit]): Set[A] = new SetFromMap(map)
37+
38+
@SerialVersionUID(3L)
39+
private class WrapperFactory(mf: MapFactory[Map]) extends SetFromMapFactory[Map, SetFromMap](mf) {
40+
protected[this] def fromMap[A](map: Map[A, Unit]): SetFromMap[A] =
41+
new SetFromMap(map)
42+
}
43+
44+
}
45+
46+
@SerialVersionUID(3L)
47+
private class SeqSetFromMap[A](protected[collection] val underlying: SeqMap[A, Unit])
48+
extends AbstractSet[A]
49+
with SeqSet[A]
50+
with SetFromMapOps.Unknown[A, SeqMap, SeqMap[A, Unit], SeqSetFromMap, SeqSetFromMap[A]]
51+
with SetFromMapOps.Unsorted[A, SeqMap, SeqSetFromMap]
52+
with IterableFactoryDefaults[A, SeqSetFromMap]
53+
with DefaultSerializable {
54+
protected[this] def fromMap[B](m: SeqMap[B, Unit]): SeqSetFromMap[B] = new SeqSetFromMap(m)
55+
56+
override protected[this] def className: String = "SeqSetFromMap"
57+
58+
override def iterableFactory: IterableFactory[SeqSetFromMap] =
59+
new SeqSetFromMap.WrapperFactory(underlying.mapFactory)
60+
}
61+
62+
private object SeqSetFromMap extends SetFromMapMetaFactory[SeqMap, SeqSet] {
63+
def apply(factory: MapFactory[SeqMap]): IterableFactory[SeqSet] = new WrapperFactory(factory)
64+
65+
def apply[A](map: SeqMap[A, Unit]): SeqSet[A] = new SeqSetFromMap(map)
66+
67+
@SerialVersionUID(3L)
68+
private class WrapperFactory(mf: MapFactory[SeqMap]) extends SetFromMapFactory[SeqMap, SeqSetFromMap](mf) {
69+
protected[this] def fromMap[A](map: SeqMap[A, Unit]): SeqSetFromMap[A] =
70+
new SeqSetFromMap(map)
71+
}
72+
73+
}
74+
75+
@SerialVersionUID(3L)
76+
private class SortedSetFromMap[A](protected[collection] val underlying: SortedMap[A, Unit])(implicit val ordering: Ordering[A])
77+
extends AbstractSet[A]
78+
with SetFromMapOps.Unknown[A, Map, SortedMap[A, Unit], Set, SortedSetFromMap[A]]
79+
with SetFromMapOps.Sorted[A, SortedMap, Set, SortedSetFromMap]
80+
with SortedSet[A]
81+
with SortedSetOps[A, SortedSetFromMap, SortedSetFromMap[A]]
82+
with IterableFactoryDefaults[A, Set]
83+
with SortedSetFactoryDefaults[A, SortedSetFromMap, Set]
84+
with DefaultSerializable {
85+
protected[this] def fromMap[B](m: Map[B, Unit]): SetFromMap[B] = new SetFromMap(m)
86+
87+
protected[this] def fromSortedMap[B: Ordering](m: SortedMap[B, Unit]): SortedSetFromMap[B] =
88+
new SortedSetFromMap(m)
89+
90+
override protected[this] def className: String = "SortedSetFromMap"
91+
92+
override def iterableFactory: IterableFactory[Set] = SetFromMap(underlying.mapFactory)
93+
94+
override def sortedIterableFactory: SortedIterableFactory[SortedSetFromMap] =
95+
new SortedSetFromMap.WrapperFactory(underlying.sortedMapFactory)
96+
}
97+
98+
private object SortedSetFromMap extends SortedSetFromMapMetaFactory[SortedMap, SortedSet] {
99+
def apply(factory: SortedMapFactory[SortedMap]): SortedIterableFactory[SortedSet] =
100+
new WrapperFactory(factory)
101+
102+
def apply[A](map: SortedMap[A, Unit]): SortedSet[A] = new SortedSetFromMap(map)(map.ordering)
103+
104+
@SerialVersionUID(3L)
105+
private final class WrapperFactory(mf: SortedMapFactory[SortedMap])
106+
extends SortedSetFromMapFactory[SortedMap, SortedSetFromMap](mf) {
107+
protected[this] def fromMap[A: Ordering](map: SortedMap[A, Unit]): SortedSetFromMap[A] =
108+
new SortedSetFromMap(map)
109+
}
110+
111+
}

0 commit comments

Comments
 (0)