[libgee] Reuse the keys, values and entries view instances where possible



commit 3e12cfb532bb0600144a6156599e78fb4335832b
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Sun Sep 20 17:19:17 2009 +0200

    Reuse the keys, values and entries view instances where possible
    
    We use the same mechanism as for the read-only views.

 gee/hashmap.vala |   28 +++++++++++++++++++++++++---
 gee/treemap.vala |   28 +++++++++++++++++++++++++---
 2 files changed, 50 insertions(+), 6 deletions(-)
---
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 300dead..0976fa4 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -46,7 +46,13 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Set<K> keys {
 		owned get {
-			return new KeySet<K,V> (this);
+			Set<K> keys = _keys;
+			if (_keys == null) {
+				keys = new KeySet<K,V> (this);
+				_keys = keys;
+				keys.add_weak_pointer (&_keys);
+			}
+			return keys;
 		}
 	}
 
@@ -55,7 +61,13 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Collection<V> values {
 		owned get {
-			return new ValueCollection<K,V> (this);
+			Collection<K> values = _values;
+			if (_values == null) {
+				values = new ValueCollection<K,V> (this);
+				_values = values;
+				values.add_weak_pointer (&_values);
+			}
+			return values;
 		}
 	}
 
@@ -64,7 +76,13 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Set<Map.Entry<K,V>> entries {
 		owned get {
-			return new EntrySet<K,V> (this);
+			Set<Map.Entry<K,V>> entries = _entries;
+			if (_entries == null) {
+				entries = new EntrySet<K,V> (this);
+				_entries = entries;
+				entries.add_weak_pointer (&_entries);
+			}
+			return entries;
 		}
 	}
 
@@ -87,6 +105,10 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 	private int _nnodes;
 	private Node<K,V>[] _nodes;
 
+	private weak Set<K> _keys;
+	private weak Collection<V> _values;
+	private weak Set<Map.Entry<K,V>> _entries;
+
 	// concurrent modification protection
 	private int _stamp = 0;
 
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 32c6a70..7ce274a 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -44,7 +44,13 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Set<K> keys {
 		owned get {
-			return new KeySet<K,V> (this);
+			Set<K> keys = _keys;
+			if (_keys == null) {
+				keys = new KeySet<K,V> (this);
+				_keys = keys;
+				keys.add_weak_pointer (&_keys);
+			}
+			return keys;
 		}
 	}
 
@@ -53,7 +59,13 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Collection<V> values {
 		owned get {
-			return new ValueCollection<K,V> (this);
+			Collection<K> values = _values;
+			if (_values == null) {
+				values = new ValueCollection<K,V> (this);
+				_values = values;
+				values.add_weak_pointer (&_values);
+			}
+			return values;
 		}
 	}
 
@@ -62,7 +74,13 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 	 */
 	public override Set<Map.Entry<K,V>> entries {
 		owned get {
-			return new EntrySet<K,V> (this);
+			Set<Map.Entry<K,V>> entries = _entries;
+			if (_entries == null) {
+				entries = new EntrySet<K,V> (this);
+				_entries = entries;
+				entries.add_weak_pointer (&_entries);
+			}
+			return entries;
 		}
 	}
 
@@ -78,6 +96,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 
 	private int _size = 0;
 
+	private weak Set<K> _keys;
+	private weak Collection<V> _values;
+	private weak Set<Map.Entry<K,V>> _entries;
+
 	/**
 	 * Constructs a new, empty tree map sorted according to the specified
 	 * comparator function.



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