Skip to content

Commit 7aaae8b

Browse files
committed
Add SeqSet and SetFromMap implementations
1 parent e597bcf commit 7aaae8b

File tree

12 files changed

+1110
-0
lines changed

12 files changed

+1110
-0
lines changed
Lines changed: 37 additions & 0 deletions
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+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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])
69+
extends SetFromMapFactory[SeqMap, SeqSetFromMap](mf) {
70+
protected[this] def fromMap[A](map: SeqMap[A, Unit]): SeqSetFromMap[A] =
71+
new SeqSetFromMap(map)
72+
}
73+
74+
}
75+
76+
@SerialVersionUID(3L)
77+
private class SortedSetFromMap[A](protected[collection] val underlying: SortedMap[A, Unit])(implicit
78+
val ordering: Ordering[A]
79+
) extends AbstractSet[A]
80+
with SetFromMapOps.Unknown[A, Map, SortedMap[A, Unit], Set, SortedSetFromMap[A]]
81+
with SetFromMapOps.Sorted[A, SortedMap, Set, SortedSetFromMap]
82+
with SortedSet[A]
83+
with SortedSetOps[A, SortedSetFromMap, SortedSetFromMap[A]]
84+
with IterableFactoryDefaults[A, Set]
85+
with SortedSetFactoryDefaults[A, SortedSetFromMap, Set]
86+
with DefaultSerializable {
87+
protected[this] def fromMap[B](m: Map[B, Unit]): SetFromMap[B] = new SetFromMap(m)
88+
89+
protected[this] def fromSortedMap[B: Ordering](m: SortedMap[B, Unit]): SortedSetFromMap[B] =
90+
new SortedSetFromMap(m)
91+
92+
override protected[this] def className: String = "SortedSetFromMap"
93+
94+
override def iterableFactory: IterableFactory[Set] = SetFromMap(underlying.mapFactory)
95+
96+
override def sortedIterableFactory: SortedIterableFactory[SortedSetFromMap] =
97+
new SortedSetFromMap.WrapperFactory(underlying.sortedMapFactory)
98+
}
99+
100+
private object SortedSetFromMap extends SortedSetFromMapMetaFactory[SortedMap, SortedSet] {
101+
def apply(factory: SortedMapFactory[SortedMap]): SortedIterableFactory[SortedSet] =
102+
new WrapperFactory(factory)
103+
104+
def apply[A](map: SortedMap[A, Unit]): SortedSet[A] = new SortedSetFromMap(map)(map.ordering)
105+
106+
@SerialVersionUID(3L)
107+
private final class WrapperFactory(mf: SortedMapFactory[SortedMap])
108+
extends SortedSetFromMapFactory[SortedMap, SortedSetFromMap](mf) {
109+
protected[this] def fromMap[A: Ordering](map: SortedMap[A, Unit]): SortedSetFromMap[A] =
110+
new SortedSetFromMap(map)
111+
}
112+
113+
}

0 commit comments

Comments
 (0)