[libgee/mapadaptor: 1/2] Add Map.put method



commit b463d57c92d57caf2702f91bf0211b78ad8fabfb
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Thu Sep 29 12:53:27 2011 +0200

    Add Map.put method

 gee/abstractmap.vala |   11 +++++++++++
 gee/hashmap.vala     |   17 +++++++++++++++++
 gee/map.vala         |   10 ++++++++++
 gee/readonlymap.vala |    7 +++++++
 gee/treemap.vala     |   24 +++++++++++++++++++-----
 5 files changed, 64 insertions(+), 5 deletions(-)
---
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index 33a1ab6..66ec2fc 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -90,6 +90,17 @@ public abstract class Gee.AbstractMap<K,V> : Object, Traversable<Map.Entry<K,V>>
         */
        public abstract new void set (K key, V value);
 
+       
+       /**
+        * { inheritDoc}
+        */
+       public virtual bool put (K key, V value) {
+               if (contains (key))
+                       return false;
+               this[key] = value;
+               return true;
+       }
+
        /**
         * { inheritDoc}
         */
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 57f1412..962ebe1 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -206,6 +206,23 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
        /**
         * { inheritDoc}
         */
+       public override bool put (K key, V value) {
+               Node<K,V>** node = lookup_node (key);
+               if (*node == null) {
+                       return false;
+               } else {
+                       uint hash_value = key_hash_func (key);
+                       *node = new Node<K,V> (key, value, hash_value);
+                       _nnodes++;
+                       resize ();
+                       _stamp++;
+                       return true;
+               }
+       }
+
+       /**
+        * { inheritDoc}
+        */
        public override bool unset (K key, out V? value = null) {
                Node<K,V>** node = lookup_node (key);
                if (*node != null) {
diff --git a/gee/map.vala b/gee/map.vala
index 00e81b2..60f9318 100644
--- a/gee/map.vala
+++ b/gee/map.vala
@@ -120,6 +120,16 @@ public interface Gee.Map<K,V> : Object, Iterable<Map.Entry<K,V>> {
        public abstract void set (K key, V value);
 
        /**
+        * Inserts a new key and value into this map if the
+        * key have not been set.
+        *
+        * @param key   the key to insert
+        * @param value the value to associate with key
+        * @returns     ``true`` if the map was modified
+        */
+       public abstract bool put (K key, V value);
+
+       /**
         * Removes the specified key from this map.
         *
         * @param key   the key to remove from the map
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 8921244..24e8156 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -130,6 +130,13 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Traversable<Map.Entry<K,V>>, Itera
        /**
         * Unimplemented method (read only map).
         */
+       public new bool put (K key, V value) {
+               assert_not_reached ();
+       }
+
+       /**
+        * Unimplemented method (read only map).
+        */
        public bool unset (K key, out V? value = null) {
                assert_not_reached ();
        }
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 02587c6..f63dd64 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -195,7 +195,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
                return null;
        }
 
-       private bool set_to_node (ref Node<K, V>? node, K key, V value, out V? old_value, Node<K, V>? prev, 
Node<K, V>? next) {
+       private bool set_to_node (ref Node<K, V>? node, K key, V value, out V? old_value, Node<K, V>? prev, 
Node<K, V>? next, bool preserve) {
                if (node == null) {
                        old_value = null;
                        node = new Node<K,V> (key, value, prev, next);
@@ -212,7 +212,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
                int cmp = key_compare_func (key, node.key);
                bool changed;
                if (cmp == 0) {
-                       if (value_equal_func (value, node.value)) {
+                       if (preserve) {
+                               old_value = null;
+                               changed = false;
+                       } else if (value_equal_func (value, node.value)) {
                                old_value = null;
                                changed = false;
                        } else {
@@ -221,9 +224,9 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
                                changed = true;
                        }
                } else if (cmp < 0) {
-                       changed = set_to_node (ref node.left, key, value, out old_value, node.prev, node);
+                       changed = set_to_node (ref node.left, key, value, out old_value, node.prev, node, 
preserve);
                } else {
-                       changed = set_to_node (ref node.right, key, value, out old_value, node, node.next);
+                       changed = set_to_node (ref node.right, key, value, out old_value, node, node.next, 
preserve);
                }
 
                fix_up (ref node);
@@ -235,9 +238,20 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
         */
        public override void set (K key, V value) {
                V old_value;
-               set_to_node (ref root, key, value, out old_value, null, null);
+               set_to_node (ref root, key, value, out old_value, null, null, false);
+               root.color = Node.Color.BLACK;
+               stamp++;
+       }
+
+       /**
+        * { inheritDoc}
+        */
+       public override bool put (K key, V value) {
+               V old_value;
+               bool success = set_to_node (ref root, key, value, out old_value, null, null, true);
                root.color = Node.Color.BLACK;
                stamp++;
+               return success;
        }
 
        private void move_red_left (ref Node<K, V> root) {


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