Provide sane defaults for equal, hash and compare functions
authorDidier 'Ptitjes <ptitjes@free.fr>
Wed, 26 Aug 2009 13:41:53 +0000 (15:41 +0200)
committerDidier 'Ptitjes <ptitjes@free.fr>
Wed, 2 Sep 2009 17:25:10 +0000 (19:25 +0200)
gee/arraylist.vala
gee/functions.vala
gee/hashmap.vala
gee/hashset.vala
gee/linkedlist.vala
gee/treemap.vala
gee/treeset.vala

index ea25575..0496c48 100644 (file)
@@ -40,7 +40,10 @@ public class Gee.ArrayList<G> : AbstractList<G> {
        // concurrent modification protection
        private int _stamp = 0;
 
-       public ArrayList (EqualFunc equal_func = GLib.direct_equal) {
+       public ArrayList (EqualFunc? equal_func = null) {
+               if (equal_func == null) {
+                       equal_func = Functions.get_equal_func_for (typeof (G));
+               }
                this.equal_func = equal_func;
        }
 
index 85777f0..8b0c99e 100644 (file)
 using GLib;
 
 namespace Gee {
+
+       public class Functions {
+
+               public static EqualFunc get_equal_func_for (Type t) {
+                       if (t == typeof (string)) {
+                               return str_equal;
+                       } else {
+                               return direct_equal;
+                       }
+               }
+
+               public static HashFunc get_hash_func_for (Type t) {
+                       if (t == typeof (string)) {
+                               return str_hash;
+                       } else {
+                               return direct_hash;
+                       }
+               }
+
+               public static CompareFunc get_compare_func_for (Type t) {
+                       if (t == typeof (string)) {
+                               return (CompareFunc) strcmp;
+                       } else {
+                               return direct_compare;
+                       }
+               }
+       }
+
        public static int direct_compare (void* _val1, void* _val2) {
                long val1 = (long)_val1, val2 = (long)_val2;
                if (val1 > val2) {
index 32cef09..93bed36 100644 (file)
@@ -48,7 +48,16 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
        private const int MIN_SIZE = 11;
        private const int MAX_SIZE = 13845163;
 
-       public HashMap (HashFunc key_hash_func = GLib.direct_hash, EqualFunc key_equal_func = GLib.direct_equal, EqualFunc value_equal_func = GLib.direct_equal) {
+       public HashMap (HashFunc? key_hash_func = null, EqualFunc? key_equal_func = null, EqualFunc? value_equal_func = null) {
+               if (key_hash_func == null) {
+                       key_hash_func = Functions.get_hash_func_for (typeof (K));
+               }
+               if (key_equal_func == null) {
+                       key_equal_func = Functions.get_equal_func_for (typeof (K));
+               }
+               if (value_equal_func == null) {
+                       value_equal_func = Functions.get_equal_func_for (typeof (V));
+               }
                this.key_hash_func = key_hash_func;
                this.key_equal_func = key_equal_func;
                this.value_equal_func = value_equal_func;
index 65d67bd..d30d222 100644 (file)
@@ -46,7 +46,13 @@ public class Gee.HashSet<G> : AbstractCollection<G>, Set<G> {
        private const int MIN_SIZE = 11;
        private const int MAX_SIZE = 13845163;
 
-       public HashSet (HashFunc hash_func = GLib.direct_hash, EqualFunc equal_func = GLib.direct_equal) {
+       public HashSet (HashFunc? hash_func = null, EqualFunc? equal_func = null) {
+               if (hash_func == null) {
+                       hash_func = Functions.get_hash_func_for (typeof (G));
+               }
+               if (equal_func == null) {
+                       equal_func = Functions.get_equal_func_for (typeof (G));
+               }
                this.hash_func = hash_func;
                this.equal_func = equal_func;
        }
index 823d055..98c35db 100644 (file)
@@ -35,7 +35,10 @@ public class Gee.LinkedList<G> : AbstractList<G> {
 
        public EqualFunc equal_func { private set; get; }
 
-       public LinkedList (EqualFunc equal_func = direct_equal) {
+       public LinkedList (EqualFunc? equal_func = null) {
+               if (equal_func == null) {
+                       equal_func = Functions.get_equal_func_for (typeof (G));
+               }
                this.equal_func = equal_func;
        }
 
index 062312d..daf9027 100644 (file)
@@ -35,7 +35,13 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 
        private int _size = 0;
 
-       public TreeMap (CompareFunc key_compare_func = Gee.direct_compare, EqualFunc value_equal_func = GLib.direct_equal) {
+       public TreeMap (CompareFunc? key_compare_func = null, EqualFunc? value_equal_func = null) {
+               if (key_compare_func == null) {
+                       key_compare_func = Functions.get_compare_func_for (typeof (K));
+               }
+               if (value_equal_func == null) {
+                       value_equal_func = Functions.get_equal_func_for (typeof (V));
+               }
                this.key_compare_func = key_compare_func;
                this.value_equal_func = value_equal_func;
        }
index 83120cf..8b6d1fd 100644 (file)
@@ -34,7 +34,10 @@ public class Gee.TreeSet<G> : AbstractCollection<G>, Set<G> {
 
        private int _size = 0;
 
-       public TreeSet (CompareFunc compare_func = Gee.direct_compare) {
+       public TreeSet (CompareFunc? compare_func = null) {
+               if (compare_func == null) {
+                       compare_func = Functions.get_compare_func_for (typeof (G));
+               }
                this.compare_func = compare_func;
        }