timedvaluecontrolsource: Use g_sequence_lookup where possible
authorJan Alexander Steffens (heftig) <jan.steffens@gmail.com>
Wed, 23 Sep 2015 21:03:29 +0000 (23:03 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Fri, 2 Oct 2015 14:30:38 +0000 (17:30 +0300)
When looking for exact matches in the sequence, this results
in much simpler code than when using g_sequence_search.

https://bugzilla.gnome.org/show_bug.cgi?id=755498

libs/gst/controller/gsttimedvaluecontrolsource.c

index 4c8dca9..04101c2 100644 (file)
@@ -171,36 +171,25 @@ static void
 gst_timed_value_control_source_set_internal (GstTimedValueControlSource *
     self, GstClockTime timestamp, const gdouble value)
 {
-  GSequenceIter *iter;
   GstControlPoint *cp;
 
   g_mutex_lock (&self->lock);
 
   /* check if a control point for the timestamp already exists */
-  /* iter contains the iter right *after* timestamp */
   if (G_LIKELY (self->values)) {
-    iter =
-        g_sequence_search (self->values, &timestamp,
+    GSequenceIter *iter = g_sequence_lookup (self->values, &timestamp,
         (GCompareDataFunc) gst_control_point_find, NULL);
-    if (iter) {
-      GSequenceIter *prev = g_sequence_iter_prev (iter);
 
-      if (!g_sequence_iter_is_end (prev)) {
-        GstControlPoint *cp = g_sequence_get (prev);
-
-        /* If the timestamp is the same just update the control point value */
-        if (cp->timestamp == timestamp) {
+    if (iter) {
+      GstControlPoint *cp = g_sequence_get (iter);
 
-          /* update control point */
-          cp->value = value;
-          g_mutex_unlock (&self->lock);
+      /* update control point */
+      cp->value = value;
+      g_mutex_unlock (&self->lock);
 
-          g_signal_emit (self,
-              gst_timed_value_control_source_signals[VALUE_CHANGED_SIGNAL], 0,
-              cp);
-          goto done;
-        }
-      }
+      g_signal_emit (self,
+          gst_timed_value_control_source_signals[VALUE_CHANGED_SIGNAL], 0, cp);
+      goto done;
     }
   } else {
     self->values = g_sequence_new ((GDestroyNotify) gst_control_point_free);
@@ -337,24 +326,17 @@ gst_timed_value_control_source_unset (GstTimedValueControlSource * self,
   g_mutex_lock (&self->lock);
   /* check if a control point for the timestamp exists */
   if (G_LIKELY (self->values) && (iter =
-          g_sequence_search (self->values, &timestamp,
+          g_sequence_lookup (self->values, &timestamp,
               (GCompareDataFunc) gst_control_point_find, NULL))) {
 
     /* Iter contains the iter right after timestamp, i.e.
      * we need to get the previous one and check the timestamp
      */
-    iter = g_sequence_iter_prev (iter);
-    cp = g_sequence_get (iter);
-    if (cp->timestamp == timestamp) {
-      cp = g_slice_dup (GstControlPoint, cp);
-      g_sequence_remove (iter);
-      self->nvalues--;
-      self->valid_cache = FALSE;
-      res = TRUE;
-    } else {
-      cp = NULL;
-    }
-
+    cp = g_slice_dup (GstControlPoint, g_sequence_get (iter));
+    g_sequence_remove (iter);
+    self->nvalues--;
+    self->valid_cache = FALSE;
+    res = TRUE;
   }
   g_mutex_unlock (&self->lock);