implemented insert_after
authorimmler@in.tum.de
Thu, 05 Mar 2009 10:13:25 +0100
changeset 34529 f0e55d9ffe45
parent 34528 3c3a23c1eb8c
child 34530 de629c23b389
implemented insert_after
src/Tools/jEdit/src/utils/LinearSet.scala
--- a/src/Tools/jEdit/src/utils/LinearSet.scala	Wed Mar 04 17:46:42 2009 +0100
+++ b/src/Tools/jEdit/src/utils/LinearSet.scala	Thu Mar 05 10:13:25 2009 +0100
@@ -40,10 +40,20 @@
   def contains(elem: A): Boolean =
     !isEmpty && (last_elem.get == elem || body.isDefinedAt(elem))
 
-  def +(elem: A): LinearSet[A] =
+  def insert_after(hook: Option[A], elem: A): LinearSet[A] =
     if (contains(elem)) throw new LinearSet.Duplicate(elem.toString)
-    else if (isEmpty) LinearSet.make(Some(elem), Some(elem), Map.empty)
-    else LinearSet.make(first_elem, Some(elem), body + (last_elem.get -> elem))
+    else hook match {
+      case Some(hook) =>
+        if (!contains(hook)) throw new LinearSet.Undefined(hook.toString)
+        else if (body.isDefinedAt(hook))
+          LinearSet.make(first_elem, last_elem, body - hook + (hook -> elem) + (elem -> body(hook)))
+        else LinearSet.make(first_elem, Some(elem), body + (hook -> elem))
+      case None =>
+        if (isEmpty) LinearSet.make(Some(elem), Some(elem), Map.empty)
+        else LinearSet.make(Some(elem), last_elem, body + (elem -> first_elem.get))
+    }
+    
+  def +(elem: A): LinearSet[A] = insert_after(last_elem, elem)
 
   def -(elem: A): LinearSet[A] =
     if (!contains(elem)) throw new LinearSet.Undefined(elem.toString)