Commit 5fcd51c3 authored by Maciej (Matthew) Piechotka's avatar Maciej (Matthew) Piechotka
Browse files

Add read_only_view to Gee.MultiSet

parent 3e6be50f
......@@ -56,6 +56,7 @@ libgee_0_8_la_SOURCES = \
readonlycollection.vala \
readonlylist.vala \
readonlymap.vala \
readonlymultiset.vala \
readonlyset.vala \
readonlysortedmap.vala \
readonlysortedset.vala \
......
......@@ -92,6 +92,19 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
_nitems = 0;
}
private weak MultiSet<G> _read_only_view;
public virtual new MultiSet<G> read_only_view {
owned get {
MultiSet<G> instance = _read_only_view;
if (_read_only_view == null) {
instance = new ReadOnlyMultiSet<G> (this);
_read_only_view = instance;
instance.add_weak_pointer ((void**) (&_read_only_view));
}
return instance;
}
}
// Future-proofing
internal new virtual void reserved0() {}
internal new virtual void reserved1() {}
......@@ -102,7 +115,6 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
internal new virtual void reserved6() {}
internal new virtual void reserved7() {}
internal new virtual void reserved8() {}
internal new virtual void reserved9() {}
private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G> {
private AbstractMultiSet<G> _set;
......
......@@ -33,4 +33,23 @@ public interface Gee.MultiSet<G> : Collection<G> {
* @return the number of occurences of the item in this multiset.
*/
public abstract int count (G item);
/**
* The read-only view of this set.
*/
public virtual new MultiSet<G> read_only_view {
owned get {
return new ReadOnlyMultiSet<G> (this);
}
}
/**
* Returns an immutable empty set.
*
* @return an immutable empty set
*/
public static Set<G> empty<G> () {
return new HashSet<G> ().read_only_view;
}
}
/* readonlymultiset.vala
*
* Copyright (C) 2013 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>
*/
using GLib;
/**
* Read-only view for {@link MultiSet} collections.
*
* This class decorates any class which implements the {@link Collection}
* interface by making it read only. Any method which normally modify data will
* throw an error.
*
* @see MultiSet
*/
internal class Gee.ReadOnlyMultiSet<G> : Gee.ReadOnlyCollection<G>, MultiSet<G> {
/**
* Constructs a read-only multi-set that mirrors the content of the specified
* list.
*
* @param multiset the multi-set to decorate.
*/
public ReadOnlyMultiSet (MultiSet<G> multiset) {
base (multiset);
}
/**
* {@inheritDoc}
*/
public int count (G item) {
return ((Gee.MultiSet<G>) _collection).count (item);
}
/**
* {@inheritDoc}
*/
public virtual new MultiSet<G> read_only_view { owned get { return this; } }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment