From: Didier 'Ptitjes Date: Sun, 20 Sep 2009 14:00:39 +0000 (+0200) Subject: Modify the Map interface API X-Git-Tag: LIBGEE_0_5_0~37 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=05f44df63b0b63777002990df758637dbd964b13;p=platform%2Fupstream%2Flibgee.git Modify the Map interface API We do apply the following renaming: - remove to unset - contains to has_key - remove_all to unset_all - contains_all to has_all Old methods are documented as deprecated. Also get_keys() and get_values() are transformed into properties. --- diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala index 25f702c..a70fcd7 100644 --- a/gee/abstractmap.vala +++ b/gee/abstractmap.vala @@ -47,17 +47,24 @@ public abstract class Gee.AbstractMap : Object, Map { /** * @inheritDoc */ - public abstract Set get_keys (); + public abstract Set keys { owned get; } /** * @inheritDoc */ - public abstract Collection get_values (); + public abstract Collection values { owned get; } /** * @inheritDoc */ - public abstract bool contains (K key); + public abstract bool has_key (K key); + + /** + * @inheritDoc + */ + public bool contains (K key) { + return has_key (key); + } /** * @inheritDoc @@ -72,7 +79,19 @@ public abstract class Gee.AbstractMap : Object, Map { /** * @inheritDoc */ - public abstract bool remove (K key, out V? value = null); + public abstract bool unset (K key, out V? value = null); + + /** + * @inheritDoc + */ + public bool remove (K key, out V? value = null) { + V removed_value; + bool result = unset (key, out removed_value); + if (&value != null) { + value = removed_value; + } + return result; + } /** * @inheritDoc @@ -83,7 +102,7 @@ public abstract class Gee.AbstractMap : Object, Map { * @inheritDoc */ public virtual void set_all (Map map) { - foreach (K key in map.get_keys ()) { + foreach (K key in map.keys) { set (key, map.get (key)); } } @@ -91,9 +110,9 @@ public abstract class Gee.AbstractMap : Object, Map { /** * @inheritDoc */ - public virtual bool remove_all (Map map) { + public virtual bool unset_all (Map map) { bool changed = false; - foreach (K key in map.get_keys ()) { + foreach (K key in map.keys) { changed = changed | remove (key); } return changed; @@ -102,15 +121,29 @@ public abstract class Gee.AbstractMap : Object, Map { /** * @inheritDoc */ - public virtual bool contains_all (Map map) { - foreach (K key in map.get_keys ()) { - if (!contains (key)) { + public bool remove_all (Map map) { + return unset_all (map); + } + + /** + * @inheritDoc + */ + public virtual bool has_all (Map map) { + foreach (K key in map.keys) { + if (!has_key (key)) { return false; } } return true; } + /** + * @inheritDoc + */ + public bool contains_all (Map map) { + return has_all (map); + } + private weak Map _read_only_view; /** diff --git a/gee/hashmap.vala b/gee/hashmap.vala index f9211f8..799a4aa 100644 --- a/gee/hashmap.vala +++ b/gee/hashmap.vala @@ -42,6 +42,24 @@ public class Gee.HashMap : Gee.AbstractMap { } /** + * @inheritDoc + */ + public override Set keys { + owned get { + return new KeySet (this); + } + } + + /** + * @inheritDoc + */ + public override Collection values { + owned get { + return new ValueCollection (this); + } + } + + /** * The keys' hash function. */ public HashFunc key_hash_func { private set; get; } @@ -91,20 +109,6 @@ public class Gee.HashMap : Gee.AbstractMap { _nodes = new Node[_array_size]; } - /** - * @inheritDoc - */ - public override Set get_keys () { - return new KeySet (this); - } - - /** - * @inheritDoc - */ - public override Collection get_values () { - return new ValueCollection (this); - } - internal Gee.UpdatableKeyIterator updatable_key_iterator () { return new UpdatableKeyIterator (this); } @@ -121,7 +125,7 @@ public class Gee.HashMap : Gee.AbstractMap { /** * @inheritDoc */ - public override bool contains (K key) { + public override bool has_key (K key) { Node** node = lookup_node (key); return (*node != null); } @@ -157,7 +161,7 @@ public class Gee.HashMap : Gee.AbstractMap { /** * @inheritDoc */ - public override bool remove (K key, out V? value = null) { + public override bool unset (K key, out V? value = null) { Node** node = lookup_node (key); if (*node != null) { Node next = (owned) (*node)->next; diff --git a/gee/hashmultimap.vala b/gee/hashmultimap.vala index 14dbb97..0c46a10 100644 --- a/gee/hashmultimap.vala +++ b/gee/hashmultimap.vala @@ -62,12 +62,12 @@ public class Gee.HashMultiMap : GLib.Object, MultiMap { } public Set get_keys () { - return _items.get_keys (); + return _items.keys; } public MultiSet get_all_keys () { MultiSet result = new HashMultiSet (_key_hash_func, _key_equal_func); - foreach (var key in _items.get_keys ()) { + foreach (var key in _items.keys) { for (int i = 0; i < _items.get (key).size; i++) { result.add (key); } @@ -77,7 +77,7 @@ public class Gee.HashMultiMap : GLib.Object, MultiMap { public Collection get_values () { var result = new ArrayList (_value_equal_func); - foreach (var key in _items.get_keys ()) { + foreach (var key in _items.keys) { foreach (var value in _items.get (key)) { result.add (value); } diff --git a/gee/map.vala b/gee/map.vala index 40d25f3..e28cba1 100644 --- a/gee/map.vala +++ b/gee/map.vala @@ -35,18 +35,23 @@ public interface Gee.Map : GLib.Object { public abstract bool is_empty { get; } /** - * Returns the keys of this map as a read-only set. - * - * @return the keys of the map + * The read-only view of the keys of this map. + */ + public abstract Set keys { owned get; } + + /** + * The read-only view of the values of this map. */ - public abstract Set get_keys (); + public abstract Collection values { owned get; } /** - * Returns the values of this map as a read-only collection. + * Determines whether this map has the specified key. * - * @return the values of the map + * @param key the key to locate in the map + * + * @return true if key is found, false otherwise */ - public abstract Collection get_values (); + public abstract bool has_key (K key); /** * Determines whether this map contains the specified key. @@ -54,6 +59,7 @@ public interface Gee.Map : GLib.Object { * @param key the key to locate in the map * * @return true if key is found, false otherwise + * @deprecated Use {@link has_key} method instead. */ public abstract bool contains (K key); @@ -83,6 +89,17 @@ public interface Gee.Map : GLib.Object { * * @return true if the map has been changed, false otherwise */ + public abstract bool unset (K key, out V? value = null); + + /** + * Removes the specified key from this map. + * + * @param key the key to remove from the map + * @param value the receiver variable for the removed value + * + * @return true if the map has been changed, false otherwise + * @deprecated Use {@link unset} method instead. + */ public abstract bool remove (K key, out V? value = null); /** @@ -94,15 +111,24 @@ public interface Gee.Map : GLib.Object { /** * Inserts all items that are contained in the input map to this map. * - * @param map the map which items are inserted to this map + * @param map the map which items are inserted to this map */ public abstract void set_all (Map map); /** - * Removes all items from this map that are common to the input map + * Removes all items from this map that are common to the input map + * and this map. + * + * @param map the map which common items are deleted from this map + */ + public abstract bool unset_all (Map map); + + /** + * Removes all items from this map that are common to the input map * and this map. * - * @param map the map which common items are deleted from this map + * @param map the map which common items are deleted from this map + * @deprecated Use {@link unset_all} method instead. */ public abstract bool remove_all (Map map); @@ -111,6 +137,14 @@ public interface Gee.Map : GLib.Object { * * @param map the map which items will be compared with this map. */ + public abstract bool has_all (Map map); + + /** + * Returns true it this map contains all items as the input map. + * + * @param map the map which items will be compared with this map. + * @deprecated Use {@link has_all} method instead. + */ public abstract bool contains_all (Map map); /** diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala index 180b941..8bdc4c6 100644 --- a/gee/readonlymap.vala +++ b/gee/readonlymap.vala @@ -47,6 +47,24 @@ internal class Gee.ReadOnlyMap : Object, Map { get { return _map.is_empty; } } + /** + * @inheritDoc + */ + public Set keys { + owned get { + return _map.keys; + } + } + + /** + * @inheritDoc + */ + public Collection values { + owned get { + return _map.values; + } + } + private Map _map; /** @@ -61,22 +79,15 @@ internal class Gee.ReadOnlyMap : Object, Map { /** * @inheritDoc */ - public Set get_keys () { - return _map.get_keys (); - } - - /** - * @inheritDoc - */ - public Collection get_values () { - return _map.get_values (); + public bool has_key (K key) { + return _map.has_key (key); } /** * @inheritDoc */ public bool contains (K key) { - return _map.contains (key); + return _map.has_key (key); } /** @@ -96,6 +107,13 @@ internal class Gee.ReadOnlyMap : Object, Map { /** * Unimplemented method (read only map). */ + public bool unset (K key, out V? value = null) { + assert_not_reached (); + } + + /** + * Unimplemented method (read only map). + */ public bool remove (K key, out V? value = null) { assert_not_reached (); } @@ -117,6 +135,13 @@ internal class Gee.ReadOnlyMap : Object, Map { /** * Unimplemented method (read only map). */ + public bool unset_all (Map map) { + assert_not_reached (); + } + + /** + * Unimplemented method (read only map). + */ public bool remove_all (Map map) { assert_not_reached (); } @@ -124,8 +149,15 @@ internal class Gee.ReadOnlyMap : Object, Map { /** * @inheritDoc */ + public bool has_all (Map map) { + return _map.has_all (map); + } + + /** + * @inheritDoc + */ public bool contains_all (Map map) { - return _map.contains_all (map); + return _map.has_all (map); } public virtual Map read_only_view { diff --git a/gee/treemap.vala b/gee/treemap.vala index 660e81c..0b402a8 100644 --- a/gee/treemap.vala +++ b/gee/treemap.vala @@ -40,6 +40,24 @@ public class Gee.TreeMap : Gee.AbstractMap { } /** + * @inheritDoc + */ + public override Set keys { + owned get { + return new KeySet (this); + } + } + + /** + * @inheritDoc + */ + public override Collection values { + owned get { + return new ValueCollection (this); + } + } + + /** * The keys' comparator function. */ public CompareFunc key_compare_func { private set; get; } @@ -69,20 +87,6 @@ public class Gee.TreeMap : Gee.AbstractMap { this.value_equal_func = value_equal_func; } - /** - * @inheritDoc - */ - public override Set get_keys () { - return new KeySet (this); - } - - /** - * @inheritDoc - */ - public override Collection get_values () { - return new ValueCollection (this); - } - private void rotate_right (ref Node root) { Node pivot = (owned) root.left; pivot.color = root.color; @@ -112,7 +116,7 @@ public class Gee.TreeMap : Gee.AbstractMap { /** * @inheritDoc */ - public override bool contains (K key) { + public override bool has_key (K key) { weak Node? cur = root; while (cur != null) { int res = key_compare_func (key, cur.key); @@ -285,7 +289,7 @@ public class Gee.TreeMap : Gee.AbstractMap { /** * @inheritDoc */ - public override bool remove (K key, out V? value = null) { + public override bool unset (K key, out V? value = null) { V node_value; bool b = remove_from_node (ref root, key, out node_value); diff --git a/tests/testmap.vala b/tests/testmap.vala index 739f1a9..b5656df 100644 --- a/tests/testmap.vala +++ b/tests/testmap.vala @@ -29,137 +29,137 @@ public abstract class MapTests : Gee.TestCase { public MapTests (string name) { base (name); - add_test ("[Map] contains, size and is_empty", - test_contains_size_is_empty); - add_test ("[Map] get keys", test_get_keys); - add_test ("[Map] get values", test_get_values); + add_test ("[Map] has_key, size and is_empty", + test_has_key_size_is_empty); + add_test ("[Map] keys", test_keys); + add_test ("[Map] values", test_values); add_test ("[Map] set all", test_set_all); - add_test ("[Map] remove all", test_remove_all); - add_test ("[Map] contains all", test_contains_all); + add_test ("[Map] unset all", test_unset_all); + add_test ("[Map] has all", test_has_all); add_test ("[Map] GObject properties", test_gobject_properties); } protected Map test_map; - public void test_contains_size_is_empty () { + public void test_has_key_size_is_empty () { // Check the collection exists assert (test_map != null); string value; // Check the collection is initially empty - assert (! test_map.contains ("one")); - assert (! test_map.contains ("two")); - assert (! test_map.contains ("three")); + assert (! test_map.has_key ("one")); + assert (! test_map.has_key ("two")); + assert (! test_map.has_key ("three")); assert (test_map.size == 0); assert (test_map.is_empty); // Add a binding test_map.set ("one", "value_of_one"); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); assert (test_map.get ("one") == "value_of_one"); - assert (! test_map.contains ("two")); + assert (! test_map.has_key ("two")); assert (test_map.get ("two") == null); - assert (! test_map.contains ("three")); + assert (! test_map.has_key ("three")); assert (test_map.get ("three") == null); assert (test_map.size == 1); assert (! test_map.is_empty); // Remove the last added binding - assert (test_map.remove ("one")); - assert (! test_map.contains ("one")); + assert (test_map.unset ("one")); + assert (! test_map.has_key ("one")); assert (test_map.get ("one") == null); - assert (! test_map.contains ("two")); + assert (! test_map.has_key ("two")); assert (test_map.get ("two") == null); - assert (! test_map.contains ("three")); + assert (! test_map.has_key ("three")); assert (test_map.get ("three") == null); assert (test_map.size == 0); assert (test_map.is_empty); // Add more bindings test_map.set ("one", "value_of_one"); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); assert (test_map.get ("one") == "value_of_one"); - assert (! test_map.contains ("two")); + assert (! test_map.has_key ("two")); assert (test_map.get ("two") == null); - assert (! test_map.contains ("three")); + assert (! test_map.has_key ("three")); assert (test_map.get ("three") == null); assert (test_map.size == 1); assert (! test_map.is_empty); test_map.set ("two", "value_of_two"); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); assert (test_map.get ("one") == "value_of_one"); - assert (test_map.contains ("two")); + assert (test_map.has_key ("two")); assert (test_map.get ("two") == "value_of_two"); - assert (! test_map.contains ("three")); + assert (! test_map.has_key ("three")); assert (test_map.get ("three") == null); assert (test_map.size == 2); assert (! test_map.is_empty); test_map.set ("three", "value_of_three"); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); assert (test_map.get ("one") == "value_of_one"); - assert (test_map.contains ("two")); + assert (test_map.has_key ("two")); assert (test_map.get ("two") == "value_of_two"); - assert (test_map.contains ("three")); + assert (test_map.has_key ("three")); assert (test_map.get ("three") == "value_of_three"); assert (test_map.size == 3); assert (! test_map.is_empty); // Update an existent binding test_map.set ("two", "value_of_two_new"); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); assert (test_map.get ("one") == "value_of_one"); - assert (test_map.contains ("two")); + assert (test_map.has_key ("two")); assert (test_map.get ("two") == "value_of_two_new"); - assert (test_map.contains ("three")); + assert (test_map.has_key ("three")); assert (test_map.get ("three") == "value_of_three"); assert (test_map.size == 3); assert (! test_map.is_empty); // Remove one element - assert (test_map.remove ("two", out value)); + assert (test_map.unset ("two", out value)); assert (value == "value_of_two_new"); - assert (test_map.contains("one")); + assert (test_map.has_key("one")); assert (test_map.get ("one") == "value_of_one"); - assert (! test_map.contains("two")); + assert (! test_map.has_key("two")); assert (test_map.get ("two") == null); - assert (test_map.contains("three")); + assert (test_map.has_key("three")); assert (test_map.get ("three") == "value_of_three"); assert (test_map.size == 2); assert (! test_map.is_empty); // Remove the same element again - assert (! test_map.remove ("two", out value)); + assert (! test_map.unset ("two", out value)); assert (value == null); - assert (test_map.contains("one")); - assert (! test_map.contains("two")); - assert (test_map.contains("three")); + assert (test_map.has_key("one")); + assert (! test_map.has_key("two")); + assert (test_map.has_key("three")); assert (test_map.size == 2); assert (! test_map.is_empty); // Remove all elements test_map.clear (); - assert (! test_map.contains("one")); + assert (! test_map.has_key("one")); assert (test_map.get ("one") == null); - assert (! test_map.contains("two")); + assert (! test_map.has_key("two")); assert (test_map.get ("two") == null); - assert (! test_map.contains("three")); + assert (! test_map.has_key("three")); assert (test_map.get ("three") == null); assert (test_map.size == 0); assert (test_map.is_empty); } - public void test_get_keys () { + public void test_keys () { // Check keys on empty map - var keySet = test_map.get_keys (); + var keySet = test_map.keys; assert (keySet.size == 0); // Check keys on map with one item test_map.set ("one", "value_of_one"); assert (keySet.size == 1); assert (keySet.contains ("one")); - keySet = test_map.get_keys (); + keySet = test_map.keys; assert (keySet.size == 1); assert (keySet.contains ("one")); @@ -177,7 +177,7 @@ public abstract class MapTests : Gee.TestCase { assert (keySet.size == 2); assert (keySet.contains ("one")); assert (keySet.contains ("two")); - keySet = test_map.get_keys (); + keySet = test_map.keys; assert (keySet.size == 2); assert (keySet.contains ("one")); assert (keySet.contains ("two")); @@ -185,20 +185,20 @@ public abstract class MapTests : Gee.TestCase { // Check keys on map clear test_map.clear (); assert (keySet.size == 0); - keySet = test_map.get_keys (); + keySet = test_map.keys; assert (keySet.size == 0); } - public void test_get_values () { + public void test_values () { // Check keys on empty map - var valueCollection = test_map.get_values (); + var valueCollection = test_map.values; assert (valueCollection.size == 0); // Check keys on map with one item test_map.set ("one", "value_of_one"); assert (valueCollection.size == 1); assert (valueCollection.contains ("value_of_one")); - valueCollection = test_map.get_values (); + valueCollection = test_map.values; assert (valueCollection.size == 1); assert (valueCollection.contains ("value_of_one")); @@ -216,7 +216,7 @@ public abstract class MapTests : Gee.TestCase { assert (valueCollection.size == 2); assert (valueCollection.contains ("value_of_one")); assert (valueCollection.contains ("value_of_two")); - valueCollection = test_map.get_values (); + valueCollection = test_map.values; assert (valueCollection.size == 2); assert (valueCollection.contains ("value_of_one")); assert (valueCollection.contains ("value_of_two")); @@ -224,7 +224,7 @@ public abstract class MapTests : Gee.TestCase { // Check keys on map clear test_map.clear (); assert (valueCollection.size == 0); - valueCollection = test_map.get_values (); + valueCollection = test_map.values; assert (valueCollection.size == 0); } @@ -243,12 +243,12 @@ public abstract class MapTests : Gee.TestCase { test_map.set_all (another_map); assert (test_map.size == 6); - assert (test_map.contains ("one")); - assert (test_map.contains ("two")); - assert (test_map.contains ("three")); - assert (test_map.contains ("four")); - assert (test_map.contains ("five")); - assert (test_map.contains ("six")); + assert (test_map.has_key ("one")); + assert (test_map.has_key ("two")); + assert (test_map.has_key ("three")); + assert (test_map.has_key ("four")); + assert (test_map.has_key ("five")); + assert (test_map.has_key ("six")); assert (test_map.get ("one") == "value_of_one"); assert (test_map.get ("two") == "value_of_two"); @@ -258,16 +258,16 @@ public abstract class MapTests : Gee.TestCase { assert (test_map.get ("six") == "value_of_six"); } - public void test_remove_all () { + public void test_unset_all () { var another_map = new HashMap (str_hash, str_equal, str_equal); - // Check remove all on empty maps. + // Check unset all on empty maps. assert (test_map.is_empty); assert (another_map.is_empty); - assert (! test_map.remove_all (another_map)); + assert (! test_map.unset_all (another_map)); assert (test_map.is_empty); assert (another_map.is_empty); @@ -282,7 +282,7 @@ public abstract class MapTests : Gee.TestCase { assert (test_map.is_empty); assert (another_map.size == 2); - assert (! test_map.remove_all (another_map)); + assert (! test_map.unset_all (another_map)); assert (test_map.is_empty); assert (another_map.size == 2); @@ -297,7 +297,7 @@ public abstract class MapTests : Gee.TestCase { assert (test_map.size == 2); assert (another_map.is_empty); - assert (! test_map.remove_all (another_map)); + assert (! test_map.unset_all (another_map)); assert (test_map.size == 2); assert (another_map.is_empty); @@ -315,7 +315,7 @@ public abstract class MapTests : Gee.TestCase { assert (test_map.size == 2); assert (another_map.size == 2); - assert (test_map.remove_all (another_map)); + assert (test_map.unset_all (another_map)); assert (test_map.is_empty); assert (another_map.size == 2); @@ -337,26 +337,26 @@ public abstract class MapTests : Gee.TestCase { assert (test_map.size == 3); assert (another_map.size == 4); - assert (test_map.remove_all (another_map)); + assert (test_map.unset_all (another_map)); assert (test_map.size == 1); assert (another_map.size == 4); - assert (test_map.contains ("one")); + assert (test_map.has_key ("one")); } - public void test_contains_all () { + public void test_has_all () { var another_map = new HashMap (str_hash, str_equal, str_equal); // Check empty. - assert (test_map.contains_all (another_map)); + assert (test_map.has_all (another_map)); // Test_Map has items, another_map is empty. test_map.set ("one", "value_of_one"); - assert (test_map.contains_all (another_map)); + assert (test_map.has_all (another_map)); test_map.clear (); another_map.clear (); @@ -364,7 +364,7 @@ public abstract class MapTests : Gee.TestCase { // Test_Map is empty, another_map has items. another_map.set ("one", "value_of_one"); - assert (! test_map.contains_all (another_map)); + assert (! test_map.has_all (another_map)); test_map.clear (); another_map.clear (); @@ -376,7 +376,7 @@ public abstract class MapTests : Gee.TestCase { another_map.set ("one", "another_value_of_one"); another_map.set ("two", "another_value_of_two"); - assert (test_map.contains_all (another_map)); + assert (test_map.has_all (another_map)); test_map.clear (); another_map.clear (); @@ -385,7 +385,7 @@ public abstract class MapTests : Gee.TestCase { test_map.set ("one", "value_of_one"); another_map.set ("two", "value_of_two"); - assert (! test_map.contains_all (another_map)); + assert (! test_map.has_all (another_map)); test_map.clear (); another_map.clear (); @@ -401,7 +401,7 @@ public abstract class MapTests : Gee.TestCase { another_map.set ("three", "value_of_three"); another_map.set ("four", "value_of_four"); - assert (test_map.contains_all (another_map)); + assert (test_map.has_all (another_map)); test_map.clear (); another_map.clear (); @@ -418,7 +418,7 @@ public abstract class MapTests : Gee.TestCase { another_map.set ("four", "value_of_four"); another_map.set ("height", "value_of_height"); - assert (! test_map.contains_all (another_map)); + assert (! test_map.has_all (another_map)); } public void test_gobject_properties() { diff --git a/tests/testtreemap.vala b/tests/testtreemap.vala index 2de2da0..d7b5ca4 100644 --- a/tests/testtreemap.vala +++ b/tests/testtreemap.vala @@ -88,7 +88,7 @@ public class TreeMapTests : MapTests { test_tree_map.set ("eleven", "eleven"); test_tree_map.set ("twelve", "twelve"); - Iterator iterator = test_tree_map.get_keys ().iterator (); + Iterator iterator = test_tree_map.keys.iterator (); assert (iterator.next () == true); assert (iterator.get () == "eight"); assert (iterator.next () == true);