[california/wip/732671-double] Potential fix



commit c283d736c738658dce2772a2818a2b2d44b9b28c
Author: Jim Nelson <jim yorba org>
Date:   Tue Sep 9 16:38:41 2014 -0700

    Potential fix
    
    TreeSet is searching for the Event in a red-black tree, but the
    object has mutated, and so it's possible the comparator will lead
    the search down the wrong path.

 src/collection/collection.vala          |   14 ++++++++++++++
 src/component/component-event.vala      |   11 -----------
 src/view/common/common-events-cell.vala |   15 ++++++++++-----
 3 files changed, 24 insertions(+), 16 deletions(-)
---
diff --git a/src/collection/collection.vala b/src/collection/collection.vala
index f352f6b..9090ff2 100644
--- a/src/collection/collection.vala
+++ b/src/collection/collection.vala
@@ -48,5 +48,19 @@ public inline int size(Gee.Collection? c) {
     return !is_empty(c) ? c.size : 0;
 }
 
+public int remove_matching<G>(Gee.Iterable<G> iterable, Gee.Predicate<G> pred) {
+    int count = 0;
+    
+    Gee.Iterator<G> iter = iterable.iterator();
+    while (iter.next()) {
+        if (pred(iter.get())) {
+            iter.remove();
+            count++;
+        }
+    }
+    
+    return count;
+}
+
 }
 
diff --git a/src/component/component-event.vala b/src/component/component-event.vala
index 1302c3c..ed3e2ed 100644
--- a/src/component/component-event.vala
+++ b/src/component/component-event.vala
@@ -400,17 +400,6 @@ public class Event : Instance, Gee.Comparable<Event> {
         if (this == other)
             return 0;
         
-        // look for equality first, then worry about sort order
-        if (uid.equal_to(other.uid)) {
-            if (rid != null && other.rid != null && rid.equal_to(other.rid))
-                return 0;
-            else if (rid == null && other.rid == null)
-                return 0;
-            
-            debug("Comparing two w/ same UID: %s %Xh vs. %Xh\nTHIS\n%s\n\nOTHER\n%s\n", uid.to_string(),
-                (uint) this, (uint) other, source, other.source);
-        }
-        
         // sort all-day events before timed events
         if (is_all_day && !other.is_all_day)
             return -1;
diff --git a/src/view/common/common-events-cell.vala b/src/view/common/common-events-cell.vala
index 3703529..92cab64 100644
--- a/src/view/common/common-events-cell.vala
+++ b/src/view/common/common-events-cell.vala
@@ -241,9 +241,10 @@ internal abstract class EventsCell : Gtk.EventBox, InstanceContainer {
     }
     
     private bool internal_remove_event(Component.Event event) {
-        if (!sorted_events.remove(event)) {
-            debug("Unable to remove event %Xh from cell for %s: not present in sorted_events",
-                (uint) event, date.to_string());
+        int count = Collection.remove_matching<Component.Event>(sorted_events, (item) => item == event);
+        if (count != 1) {
+            debug("Unable to remove event %Xh from cell for %s: not present in sorted_events (%d removed)",
+                (uint) event, date.to_string(), count);
             
             return false;
         }
@@ -397,11 +398,15 @@ internal abstract class EventsCell : Gtk.EventBox, InstanceContainer {
                 date.to_string());
             
             remove_event(event);
-        } else if (sorted_events.remove(event)) {
+        } else if (Collection.remove_matching<Component.Event>(sorted_events, (item) => item == event) > 0) {
             debug("Re-sorting event %Xh in cell for %s: date/time changed", (uint) event,
                 date.to_string());
             
-            sorted_events.add(event);
+            if (!sorted_events.add(event)) {
+                debug("Unable to re-sort event %Xh in cell for %s: already present", (uint) event,
+                    date.to_string());
+            }
+            
             assign_line_numbers();
         } else {
             debug("Unknown event %Xh associated with cell %s", (uint) event, date.to_string());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]