Commits

Yit Phang Khoo committed 70ff09d

Fix TotalOrder so that all valid elements can be properly invalidated (the initial element is considered invalid).

Comments (0)

Files changed (1)

Source/Adapton/TotalOrder.ml

 
     (**/**) (* sentinel values *)
     let rec null_parent = {
-        parent_label=(-1);
+        parent_label=min_int;
         parent_next=null_parent;
         parent_prev=null_parent;
         front=null;
         back=null;
     } and null = {
-        label=(-1);
+        label=min_int;
         parent=null_parent;
         prev=null;
         next=null;
         } in
         ts
 
+    (** Return if a total-order element is the initial element (i.e., that was returned by [create]). *)
+    let is_initial ts = ts.label == 0 && ts.parent.parent_label == 0
+
     (** Return if a total-order element is valid (i.e., has not been removed). *)
-    let is_valid ts = ts.label >= 0 && ts.parent.parent_label >= 0
+    let is_valid ts = ts.label > 0 && ts.parent.parent_label >= 0
 
     (**/**) (* helper functions *)
     let neg = (lor) min_int
 
     (** Compare two total-order elements. *)
     let compare ts ts' =
+        if ts == null || ts' == null then failwith "compare";
         let p = Pervasives.compare (pos ts.parent.parent_label) (pos ts'.parent.parent_label) in
         if p != 0 then p else Pervasives.compare (pos ts.label) (pos ts'.label)
 
     (** Add a new total-order element after the given element. *)
     let add_next ts =
-        if not (is_valid ts) then failwith "add_next";
+        if not (is_valid ts or is_initial ts) then failwith "add_next";
 
         let parent = ts.parent in
         let ts' = if ts.next != null then begin
                         in
                         rebalance lower label
                     end;
-                    rebalance 0 parent' next next
+                    rebalance (if parent'.parent_label == 0 then 0 else 1) parent' next next
                 end
             in
-            rebalance 0 parent parent.front parent.front
+            rebalance (if parent.parent_label == 0 then 0 else 1) parent parent.front parent.front
         end;
         ts'
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.