[gnome-nibbles] Use an array instead of hash for color lookup



commit 176d8f41b711edc97ede31274cd7715fb955cc87
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Jun 29 02:07:56 2015 +0300

    Use an array instead of hash for color lookup

 src/nibbles-view.vala |   52 ++++++++++++++++++++----------------------------
 src/worm.vala         |   46 +++++++++++++++++++++---------------------
 2 files changed, 45 insertions(+), 53 deletions(-)
---
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 5ab4a7b..dd4bc51 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -16,17 +16,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-public enum Color
-{
-    RED,
-    GREEN,
-    BLUE,
-    YELLOW,
-    CYAN,
-    PURPLE,
-    GRAY
-}
-
 public class NibblesView : GtkClutter.Embed
 {
     /* Game being played */
@@ -55,7 +44,16 @@ public class NibblesView : GtkClutter.Embed
 
     public const int NUM_COLORS = 7;
 
-    private static Gee.HashMap<string, Color> color_lookup;
+    public static string[] color_lookup =
+    {
+      "red",
+      "green",
+      "blue",
+      "yellow",
+      "cyan",
+      "purple",
+      "gray"
+    };
 
     public NibblesView (NibblesGame game)
     {
@@ -95,14 +93,6 @@ public class NibblesView : GtkClutter.Embed
         }
 
         worm_actors = new Gee.HashMap<Worm, WormActor> ();
-        color_lookup = new Gee.HashMap<string, Color> ();
-        color_lookup.set ("red", Color.RED);
-        color_lookup.set ("green", Color.GREEN);
-        color_lookup.set ("blue", Color.BLUE);
-        color_lookup.set ("yellow", Color.YELLOW);
-        color_lookup.set ("cyan", Color.CYAN);
-        color_lookup.set ("purple", Color.PURPLE);
-        color_lookup.set ("gray", Color.GRAY);
 
         load_pixmap ();
 
@@ -523,19 +513,21 @@ public class NibblesView : GtkClutter.Embed
         group.restore_easing_state ();
     }
 
-    public static Color colorval_from_name (string name)
+    public static int colorval_from_name (string name)
     {
-        return color_lookup.get (name);
-    }
+        for (int i = 0; i < NUM_COLORS; i++)
+        {
+            if (color_lookup[i] == name)
+                return i;
+        }
 
-    // public static string colorval_name (Color colorval)
-    // {
-    //     foreach (var entry in color_lookup.entries)
-    //         if (entry.value == colorval)
-    //             return entry.key;
+        return 0;
+    }
 
-    //     return "unknown";
-    // }
+    public static string colorval_name (int colorval)
+    {
+        return color_lookup[colorval];
+    }
 }
 
 public class WormActor : Clutter.Actor
diff --git a/src/worm.vala b/src/worm.vala
index f1c449d..5117229 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -16,29 +16,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-public struct Position
-{
-    int x;
-    int y;
-}
-
-public enum WormDirection
-{
-    UP,
-    DOWN,
-    LEFT,
-    RIGHT
-}
-
-public struct WormProperties
-{
-    int color;
-    uint left;
-    uint right;
-    uint up;
-    uint down;
-}
-
 public class Worm : Object
 {
     public const int STARTING_LENGTH = 5;
@@ -310,3 +287,26 @@ public class Worm : Object
         direction = key_queue.poll ();
     }
 }
+
+public struct Position
+{
+    int x;
+    int y;
+}
+
+public enum WormDirection
+{
+    UP,
+    DOWN,
+    LEFT,
+    RIGHT
+}
+
+public struct WormProperties
+{
+    int color;
+    uint left;
+    uint right;
+    uint up;
+    uint down;
+}


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