Add read_only_view to SortedMap
authorMaciej Piechotka <uzytkownik2@gmail.com>
Thu, 8 Oct 2009 08:09:27 +0000 (09:09 +0100)
committerMaciej Piechotka <uzytkownik2@gmail.com>
Mon, 2 May 2011 10:45:25 +0000 (11:45 +0100)
gee/Makefile.am
gee/abstractsortedmap.vala
gee/readonlymap.vala
gee/readonlysortedmap.vala [new file with mode: 0644]
gee/sortedmap.vala
gee/treemap.vala

index bef9dc4..b1639a3 100644 (file)
@@ -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 \
index 20201f0..52916b1 100644 (file)
@@ -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}
         */
index e01e2ff..17e76b5 100644 (file)
@@ -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 (file)
index 0000000..bf25da9
--- /dev/null
@@ -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 ();
+               }
+       }
+}
+
index c95bde0..8693a91 100644 (file)
@@ -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;
+       }
 }
 
index e0828d0..4081528 100644 (file)
@@ -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) {