[libgames-support] Rename get_n_scores and return a Gee list



commit 2aa69d67bfd3b669d62c5a338f9c0c9c7106e5a1
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sat Sep 26 10:53:41 2015 -0500

    Rename get_n_scores and return a Gee list
    
    Thinking about this more, the API should be optimized for Vala, not for
    C.
    
    Also, just return an empty list instead of null if there are no scores
    for the category.

 games/scores/context.vala |   31 +++++++++++++------------------
 games/scores/dialog.vala  |   10 +++++-----
 2 files changed, 18 insertions(+), 23 deletions(-)
---
diff --git a/games/scores/context.vala b/games/scores/context.vala
index d948984..9f445ec 100644
--- a/games/scores/context.vala
+++ b/games/scores/context.vala
@@ -131,25 +131,20 @@ public class Context : Object
     }
 
     /* Get a maximum of best n scores from the given category */
-    public List<Score>? get_best_n_scores (Category category, int n)
+    public Gee.List<Score> get_high_scores (Category category, int n = 10)
     {
+        var result = new Gee.ArrayList<Score> ();
         if (!scores_per_category.has_key (category))
-        {
-            return null;
-        }
-
-        var n_scores = new List<Score> ();
-        var scores_of_this_category = scores_per_category[category];
+            return result;
 
-        for (int i = 0; i < n; i++)
-        {
-            if (scores_of_this_category.size == 0)
-                break;
-            n_scores.append (scores_of_this_category.poll ());
-        }
+        /* Remove the first n elements and then add them back. Seems insane, but
+         * it's necessary because the PriorityQueue iterators are unordered. */
+        for (int i = 0; i < n && scores_per_category[category].size > 0; i++)
+            result.add (scores_per_category[category].poll ());
+        foreach (var score in result)
+            scores_per_category[category].add (score);
 
-        n_scores.foreach ((x) => scores_of_this_category.add (x));
-        return n_scores;
+        return result;
     }
 
     /* Return true if a dialog was launched on attaining high score */
@@ -258,16 +253,16 @@ public class Context : Object
 
     private bool is_high_score (long score_value, Category category)
     {
-        var best_scores = get_best_n_scores (category, 10);
+        var best_scores = get_high_scores (category);
 
         /* The given category doesn't yet exist and thus this score would be the first score and hence a 
high score. */
         if (best_scores == null)
             return true;
 
-        if (best_scores.length () < 10)
+        if (best_scores.size < 10)
             return true;
 
-        var lowest = best_scores.nth_data (9).score;
+        var lowest = best_scores  get (9).score;
 
         if (style == Style.PLAIN_ASCENDING || style == Style.TIME_ASCENDING)
             return score_value < lowest;
diff --git a/games/scores/dialog.vala b/games/scores/dialog.vala
index 4039db0..4f0c7ba 100644
--- a/games/scores/dialog.vala
+++ b/games/scores/dialog.vala
@@ -244,15 +244,15 @@ private class Dialog : Gtk.Dialog
         else
             active_category = new Category (combo.get_active_id (), combo.get_active_text ());
 
-        var best_n_scores = context.get_best_n_scores (active_category, rows_to_display);
-        uint no_scores = best_n_scores.length ();
+        var best_n_scores = context.get_high_scores (active_category, rows_to_display);
 
         int row_count = 1;
 
-        best_n_scores.foreach ((x) => {
-            display_single_score (x, row_count, no_scores);
+        foreach (var score in best_n_scores)
+        {
+            display_single_score (score, row_count, best_n_scores.size);
             row_count++;
-        });
+        }
 
         if (row_count < rows_to_display + 1)
             make_remaining_labels_empty (row_count);


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