6 */ |
6 */ |
7 |
7 |
8 package isabelle |
8 package isabelle |
9 |
9 |
10 |
10 |
11 import scala.collection.SetLike |
11 import scala.collection.mutable |
12 import scala.collection.generic.{SetFactory, CanBuildFrom, GenericSetTemplate, GenericCompanion} |
12 import scala.collection.immutable.SetOps |
13 import scala.collection.mutable.{Builder, SetBuilder} |
13 import scala.collection.{IterableFactory, IterableFactoryDefaults} |
14 import scala.language.higherKinds |
|
15 |
14 |
16 |
15 |
17 object Linear_Set extends SetFactory[Linear_Set] |
16 object Linear_Set extends IterableFactory[Linear_Set] |
18 { |
17 { |
19 private val empty_val: Linear_Set[Nothing] = new Linear_Set[Nothing](None, None, Map(), Map()) |
18 private val empty_val: Linear_Set[Nothing] = new Linear_Set[Nothing](None, None, Map(), Map()) |
20 override def empty[A]: Linear_Set[A] = empty_val.asInstanceOf[Linear_Set[A]] |
19 override def empty[A]: Linear_Set[A] = empty_val.asInstanceOf[Linear_Set[A]] |
21 |
20 |
22 implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, Linear_Set[A]] = setCanBuildFrom[A] |
21 def from[A](entries: IterableOnce[A]): Linear_Set[A] = (empty[A] /: entries)(_.incl(_)) |
23 def newBuilder[A]: Builder[A, Linear_Set[A]] = new SetBuilder[A, Linear_Set[A]](empty[A]) |
22 |
|
23 override def newBuilder[A]: mutable.Builder[A, Linear_Set[A]] = new Builder[A] |
|
24 private class Builder[A] extends mutable.Builder[A, Linear_Set[A]] |
|
25 { |
|
26 private var res = empty[A] |
|
27 override def clear() { res = empty[A] } |
|
28 override def addOne(elem: A): this.type = { res = res.incl(elem); this } |
|
29 override def result(): Linear_Set[A] = res |
|
30 } |
24 |
31 |
25 class Duplicate[A](x: A) extends Exception |
32 class Duplicate[A](x: A) extends Exception |
26 class Undefined[A](x: A) extends Exception |
33 class Undefined[A](x: A) extends Exception |
27 class Next_Undefined[A](x: Option[A]) extends Exception |
34 class Next_Undefined[A](x: Option[A]) extends Exception |
28 } |
35 } |
29 |
36 |
30 |
37 |
31 final class Linear_Set[A] private( |
38 final class Linear_Set[A] private( |
32 start: Option[A], end: Option[A], val nexts: Map[A, A], prevs: Map[A, A]) |
39 start: Option[A], end: Option[A], val nexts: Map[A, A], prevs: Map[A, A]) |
33 extends scala.collection.immutable.Set[A] |
40 extends Iterable[A] |
34 with GenericSetTemplate[A, Linear_Set] |
41 with SetOps[A, Linear_Set, Linear_Set[A]] |
35 with SetLike[A, Linear_Set[A]] |
42 with IterableFactoryDefaults[A, Linear_Set] |
36 { |
43 { |
37 override def companion: GenericCompanion[Linear_Set] = Linear_Set |
|
38 |
|
39 |
|
40 /* relative addressing */ |
44 /* relative addressing */ |
41 |
45 |
42 def next(elem: A): Option[A] = |
46 def next(elem: A): Option[A] = |
43 if (contains(elem)) nexts.get(elem) |
47 if (contains(elem)) nexts.get(elem) |
44 else throw new Linear_Set.Undefined(elem) |
48 else throw new Linear_Set.Undefined(elem) |
76 nexts + (elem1 -> elem) + (elem -> elem2), |
80 nexts + (elem1 -> elem) + (elem -> elem2), |
77 prevs + (elem2 -> elem) + (elem -> elem1)) |
81 prevs + (elem2 -> elem) + (elem -> elem1)) |
78 } |
82 } |
79 } |
83 } |
80 |
84 |
81 def append_after(hook: Option[A], elems: Traversable[A]): Linear_Set[A] = // FIXME reverse fold |
85 def append_after(hook: Option[A], elems: Iterable[A]): Linear_Set[A] = // FIXME reverse fold |
82 ((hook, this) /: elems) { |
86 ((hook, this) /: elems) { |
83 case ((last, set), elem) => (Some(elem), set.insert_after(last, elem)) |
87 case ((last, set), elem) => (Some(elem), set.insert_after(last, elem)) |
84 }._2 |
88 }._2 |
85 |
89 |
86 def delete_after(hook: Option[A]): Linear_Set[A] = |
90 def delete_after(hook: Option[A]): Linear_Set[A] = |
151 |
153 |
152 def reverse: Linear_Set[A] = new Linear_Set(end, start, prevs, nexts) |
154 def reverse: Linear_Set[A] = new Linear_Set(end, start, prevs, nexts) |
153 |
155 |
154 override def last: A = reverse.head |
156 override def last: A = reverse.head |
155 |
157 |
156 def + (elem: A): Linear_Set[A] = insert_after(end, elem) |
158 def incl(elem: A): Linear_Set[A] = insert_after(end, elem) |
|
159 def excl(elem: A): Linear_Set[A] = delete_after(prev(elem)) |
157 |
160 |
158 def - (elem: A): Linear_Set[A] = delete_after(prev(elem)) |
161 override def iterableFactory: IterableFactory[Linear_Set] = Linear_Set |
|
162 |
|
163 override def toString: String = mkString("Linear_Set(", ", ", ")") |
159 } |
164 } |