[libgee] Provide sane defaults for equal, hash and compare functions



commit ed8b7b9b58d6d37a128d7d9ce211efc3e4d98f07
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Wed Aug 26 15:41:53 2009 +0200

    Provide sane defaults for equal, hash and compare functions

 gee/arraylist.vala  |    5 ++++-
 gee/functions.vala  |   28 ++++++++++++++++++++++++++++
 gee/hashmap.vala    |   11 ++++++++++-
 gee/hashset.vala    |    8 +++++++-
 gee/linkedlist.vala |    5 ++++-
 gee/treemap.vala    |    8 +++++++-
 gee/treeset.vala    |    5 ++++-
 7 files changed, 64 insertions(+), 6 deletions(-)
---
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index ea25575..0496c48 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -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;
 	}
 
diff --git a/gee/functions.vala b/gee/functions.vala
index 85777f0..8b0c99e 100644
--- a/gee/functions.vala
+++ b/gee/functions.vala
@@ -23,6 +23,34 @@
 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) {
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 32cef09..93bed36 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -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;
diff --git a/gee/hashset.vala b/gee/hashset.vala
index 65d67bd..d30d222 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -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;
 	}
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index 823d055..98c35db 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -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;
 	}
 
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 062312d..daf9027 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -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;
 	}
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 83120cf..8b6d1fd 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -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;
 	}
 



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