[libgee] Introduce the Map.has (K key, V value) method



commit a5453fe497a5f5b961f5879eb593f4ea0708bc0a
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Sun Sep 20 16:23:51 2009 +0200

    Introduce the Map.has (K key, V value) method
    
    Also we use Map.has to fix the implementation of has_all.

 gee/abstractmap.vala |    7 ++++++-
 gee/hashmap.vala     |    8 ++++++++
 gee/map.vala         |   10 ++++++++++
 gee/readonlymap.vala |    7 +++++++
 gee/treemap.vala     |    8 ++++++++
 tests/testmap.vala   |   18 +++++++++++++++++-
 6 files changed, 56 insertions(+), 2 deletions(-)
---
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index a70fcd7..e753795 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -69,6 +69,11 @@ public abstract class Gee.AbstractMap<K,V> : Object, Map<K,V> {
 	/**
 	 * @inheritDoc
 	 */
+	public abstract bool has (K key, V value);
+
+	/**
+	 * @inheritDoc
+	 */
 	public abstract new V? get (K key);
 
 	/**
@@ -130,7 +135,7 @@ public abstract class Gee.AbstractMap<K,V> : Object, Map<K,V> {
 	 */
 	public virtual bool has_all (Map<K,V> map) {
 		foreach (K key in map.keys) {
-			if (!has_key (key)) {
+			if (!has (key, map.get (key))) {
 				return false;
 			}
 		}
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 799a4aa..31e359e 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -133,6 +133,14 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 	/**
 	 * @inheritDoc
 	 */
+	public override bool has (K key, V value) {
+		Node<K,V>** node = lookup_node (key);
+		return (*node != null && value_equal_func ((*node)->value, value));
+	}
+
+	/**
+	 * @inheritDoc
+	 */
 	public override V? get (K key) {
 		Node<K,V>* node = (*lookup_node (key));
 		if (node != null) {
diff --git a/gee/map.vala b/gee/map.vala
index e28cba1..e98e995 100644
--- a/gee/map.vala
+++ b/gee/map.vala
@@ -64,6 +64,16 @@ public interface Gee.Map<K,V> : GLib.Object {
 	public abstract bool contains (K key);
 
 	/**
+	 * Determines whether this map has the specified key/value entry.
+	 *
+	 * @param key the key to locate in the map
+	 * @param value the corresponding value
+	 *
+	 * @return    true if key is found, false otherwise
+	 */
+	public abstract bool has (K key, V value);
+
+	/**
 	 * Returns the value of the specified key in this map.
 	 *
 	 * @param key the key whose value is to be retrieved
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 8bdc4c6..ddcc47e 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -93,6 +93,13 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Map<K,V> {
 	/**
 	 * @inheritDoc
 	 */
+	public bool has (K key, V value) {
+		return _map.has (key, value);
+	}
+
+	/**
+	 * @inheritDoc
+	 */
 	public new V? get (K key) {
 		return _map.get (key);
 	}
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 0b402a8..d4f807b 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -134,6 +134,14 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 	/**
 	 * @inheritDoc
 	 */
+	public override bool has (K key, V value) {
+		V? own_value = get (key);
+		return (own_value != null && value_equal_func (own_value, value));
+	}
+
+	/**
+	 * @inheritDoc
+	 */
 	public override V? get (K key) {
 		weak Node<K, V>? cur = root;
 		while (cur != null) {
diff --git a/tests/testmap.vala b/tests/testmap.vala
index b5656df..3254482 100644
--- a/tests/testmap.vala
+++ b/tests/testmap.vala
@@ -56,6 +56,8 @@ public abstract class MapTests : Gee.TestCase {
 		// Add a binding
 		test_map.set ("one", "value_of_one");
 		assert (test_map.has_key ("one"));
+		assert (test_map.has ("one", "value_of_one"));
+		assert (! test_map.has ("one", "another_value_for_one"));
 		assert (test_map.get ("one") == "value_of_one");
 		assert (! test_map.has_key ("two"));
 		assert (test_map.get ("two") == null);
@@ -67,6 +69,8 @@ public abstract class MapTests : Gee.TestCase {
 		// Remove the last added binding
 		assert (test_map.unset ("one"));
 		assert (! test_map.has_key ("one"));
+		assert (! test_map.has ("one", "value_of_one"));
+		assert (! test_map.has ("one", "another_value_for_one"));
 		assert (test_map.get ("one") == null);
 		assert (! test_map.has_key ("two"));
 		assert (test_map.get ("two") ==  null);
@@ -373,10 +377,22 @@ public abstract class MapTests : Gee.TestCase {
 		test_map.set ("one", "value_of_one");
 		test_map.set ("two", "value_of_two");
 
+		another_map.set ("one", "value_of_one");
+		another_map.set ("two", "value_of_two");
+
+		assert (test_map.has_all (another_map));
+
+		test_map.clear ();
+		another_map.clear ();
+
+		// Test_Map and another_map are not the same.
+		test_map.set ("one", "value_of_one");
+		test_map.set ("two", "value_of_two");
+
 		another_map.set ("one", "another_value_of_one");
 		another_map.set ("two", "another_value_of_two");
 
-		assert (test_map.has_all (another_map));
+		assert (! test_map.has_all (another_map));
 
 		test_map.clear ();
 		another_map.clear ();



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