[libgee] Add read_only_view to SortedMap



commit 9441b7bc4e55d4f6f821d7aaeb8b73a6ccffc13b
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Thu Oct 8 09:09:27 2009 +0100

    Add read_only_view to SortedMap

 gee/Makefile.am            |    2 +
 gee/abstractsortedmap.vala |    2 +
 gee/readonlymap.vala       |    2 +-
 gee/readonlysortedmap.vala |  110 ++++++++++++++++++++++++++++++++++++++++++++
 gee/sortedmap.vala         |   14 ++++++
 gee/treemap.vala           |   28 ++++++------
 6 files changed, 143 insertions(+), 15 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index bef9dc4..b1639a3 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -12,6 +12,7 @@ libgee_la_SOURCES = \
 	abstractmultiset.vala \
 	abstractqueue.vala \
 	abstractset.vala \
+	abstractsortedmap.vala \
 	abstractsortedset.vala \
 	arraylist.vala \
 	bidiriterator.vala \
@@ -40,6 +41,7 @@ libgee_la_SOURCES = \
 	readonlylist.vala \
 	readonlymap.vala \
 	readonlyset.vala \
+	readonlysortedmap.vala \
 	readonlysortedset.vala \
 	set.vala \
 	sortedmap.vala \
diff --git a/gee/abstractsortedmap.vala b/gee/abstractsortedmap.vala
index 20201f0..52916b1 100644
--- a/gee/abstractsortedmap.vala
+++ b/gee/abstractsortedmap.vala
@@ -40,10 +40,12 @@ public abstract class Gee.AbstractSortedMap<K, V> : AbstractMap<K,V>, SortedMap<
 	 * { inheritDoc}
 	 */
 	public abstract SortedSet<K> ascending_keys { owned get; }
+
 	/**
 	 * { inheritDoc}
 	 */
 	public abstract SortedSet<Map.Entry<K,V>> ascending_entries { owned get; }
+
 	/**
 	 * { inheritDoc}
 	 */
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index e01e2ff..17e76b5 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -81,7 +81,7 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Iterable<Map.Entry<K,V>>, Map<K,V>
 		}
 	}
 
-	private Map<K,V> _map;
+	protected Map<K,V> _map;
 
 	/**
 	 * Constructs a read-only map that mirrors the content of the specified map.
diff --git a/gee/readonlysortedmap.vala b/gee/readonlysortedmap.vala
new file mode 100644
index 0000000..bf25da9
--- /dev/null
+++ b/gee/readonlysortedmap.vala
@@ -0,0 +1,110 @@
+/* readonlysortedmap.vala
+ *
+ * Copyright (C) 2009-2011  Maciej Piechotka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ * 	Maciej Piechotka <uzytkownik2 gmail com>
+ */
+
+/**
+ * Read-only view for { link SortedMap} collections.
+ *
+ * This class decorates any class which implements the { link SortedMap} interface
+ * by making it read only. Any method which normally modify data will throw an
+ * error.
+ *
+ * @see SortedMap
+ */
+internal class Gee.ReadOnlySortedMap<K,V> : ReadOnlyMap<K,V>, SortedMap<K,V> {
+	/**
+	 * Constructs a read-only map that mirrors the content of the specified map.
+	 *
+	 * @param map the map to decorate.
+	 */
+	public ReadOnlySortedMap (Map<K,V> map) {
+		base (map);
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public SortedMap<K,V> head_map (K before) {
+		return (_map as SortedMap<K,V>).head_map (before).read_only_view;
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public SortedMap<K,V> tail_map (K after) {
+		return (_map as SortedMap<K,V>).tail_map (after).read_only_view;
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public SortedMap<K,V> sub_map (K from, K to) {
+		return (_map as SortedMap<K,V>).sub_map (from, to).read_only_view;
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public SortedSet<K> ascending_keys {
+		owned get {
+			return (_map as SortedMap<K,V>).ascending_keys.read_only_view;
+		}
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public SortedSet<Map.Entry<K,V>> ascending_entries {
+		owned get {
+			return (_map as SortedMap<K,V>).ascending_entries.read_only_view;
+		}
+	}
+
+	/**
+	 * { inheritDoc}
+	 */
+	public Gee.BidirMapIterator<K,V> bidir_map_iterator () {
+		return new BidirMapIterator<K,V> ((_map as SortedMap<K,V>).bidir_map_iterator ());
+	}
+
+	protected class BidirMapIterator<K,V> : Gee.ReadOnlyMap.MapIterator<K,V>, Gee.BidirMapIterator<K,V> {
+		public BidirMapIterator (Gee.BidirMapIterator<K,V> iterator) {
+			base (iterator);
+		}
+
+		public bool first () {
+			return (_iter as Gee.BidirMapIterator<K,V>).first ();
+		}
+
+		public bool previous () {
+			return (_iter as Gee.BidirMapIterator<K,V>).previous ();
+		}
+
+		public bool has_previous () {
+			return (_iter as Gee.BidirMapIterator<K,V>).has_previous ();
+		}
+
+		public bool last () {
+			return (_iter as Gee.BidirMapIterator<K,V>).last ();
+		}
+	}
+}
+
diff --git a/gee/sortedmap.vala b/gee/sortedmap.vala
index c95bde0..8693a91 100644
--- a/gee/sortedmap.vala
+++ b/gee/sortedmap.vala
@@ -50,5 +50,19 @@ public interface Gee.SortedMap<K,V> : Gee.Map<K,V> {
 	 * @return a bi-directional map iterator
 	 */
 	public abstract BidirMapIterator<K,V> bidir_map_iterator ();
+	
+	/**
+	 * The read-only view this map.
+	 */
+	public new abstract SortedMap<K,V> read_only_view { owned get; }
+
+	/**
+	 * Returns an immutable empty map.
+	 *
+	 * @return an immutable empty map
+	 */
+	public static Map<K,V> empty<K,V> () {
+		return new TreeMap<K,V> ().read_only_view;
+	}
 }
 
diff --git a/gee/treemap.vala b/gee/treemap.vala
index e0828d0..4081528 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -31,7 +31,7 @@ using GLib;
  *
  * @see HashMap
  */
-public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
+public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 	/**
 	 * { inheritDoc}
 	 */
@@ -395,28 +395,28 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 	/**
 	 * { inheritDoc}
 	 */
-	public SortedMap<K,V> head_map (K before) {
+	public override SortedMap<K,V> head_map (K before) {
 		return new SubMap<K,V> (this, new Range<K,V>.head (this, before));
 	}
 
 	/**
 	 * { inheritDoc}
 	 */
-	public SortedMap<K,V> tail_map (K after) {
+	public override SortedMap<K,V> tail_map (K after) {
 		return new SubMap<K,V> (this, new Range<K,V>.tail (this, after));
 	}
 
 	/**
 	 * { inheritDoc}
 	 */
-	public SortedMap<K,V> sub_map (K after, K before) {
+	public override SortedMap<K,V> sub_map (K after, K before) {
 		return new SubMap<K,V> (this, new Range<K,V> (this, after, before));
 	}
 
 	/**
 	 * { inheritDoc}
 	 */
-	public SortedSet<K> ascending_keys {
+	public override SortedSet<K> ascending_keys {
 		owned get {
 			var keys = _keys;
 			if (_keys == null) {
@@ -430,7 +430,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 	/**
 	 * @inheritDoc
 	 */
-	public SortedSet<Map.Entry<K,V>> ascending_entries {
+	public override SortedSet<Map.Entry<K,V>> ascending_entries {
 		owned get {
 			var entries = _entries;
 			if (_entries == null) {
@@ -452,7 +452,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 	/**
 	 * @inheritDoc
 	 */
-	public Gee.BidirMapIterator<K,V> bidir_map_iterator () {
+	public override Gee.BidirMapIterator<K,V> bidir_map_iterator () {
 		return new MapIterator<K,V> (this);
 	}
 
@@ -750,7 +750,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 		BOUNDED
 	}
 
-	private class SubMap<K,V> : AbstractMap<K,V>, SortedMap<K,V> {
+	private class SubMap<K,V> : AbstractSortedMap<K,V> {
 		public override int size { get { return keys.size; } }
 		public override bool is_empty { get { return keys.is_empty; } }
 
@@ -834,23 +834,23 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 			return new SubMapIterator<K,V> (map, range);
 		}
 		
-		public BidirMapIterator<K,V> bidir_map_iterator () {
+		public override BidirMapIterator<K,V> bidir_map_iterator () {
 			return new SubMapIterator<K,V> (map, range);
 		}
 
-		public SortedMap<K,V> head_map (K before) {
+		public override SortedMap<K,V> head_map (K before) {
 			return new SubMap<K,V> (map, range.cut_tail (before));
 		}
 
-		public SortedMap<K,V> tail_map (K after) {
+		public override SortedMap<K,V> tail_map (K after) {
 			return new SubMap<K,V> (map, range.cut_head (after));
 		}
 
-		public SortedMap<K,V> sub_map (K after, K before) {
+		public override SortedMap<K,V> sub_map (K after, K before) {
 			return new SubMap<K,V> (map, range.cut (after, before));
 		}
 
-		public SortedSet<K> ascending_keys {
+		public override SortedSet<K> ascending_keys {
 			owned get {
 				var keys = _keys;
 				if (_keys == null) {
@@ -862,7 +862,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V>, SortedMap<K,V> {
 			}
 		}
 
-		public SortedSet<K> ascending_entries {
+		public override SortedSet<K> ascending_entries {
 			owned get {
 				var entries = _entries;
 				if (_entries == null) {



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