[libgee] Introduce AbstractCollection base class



commit d67d706ec1d2f660e0ee54fed564e700c1691d8a
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Thu Jul 23 14:05:31 2009 +0200

    Introduce AbstractCollection base class
    
    All collection implementations now have a common AbstractCollection base class
    and now share the implementation of the get_element_type() method.

 gee/Makefile.am             |    1 +
 gee/abstractcollection.vala |   52 +++++++++++++++++++++++++++++++++++++++++++
 gee/arraylist.vala          |   18 +++++---------
 gee/hashmap.vala            |   36 +++++++++++------------------
 gee/hashset.vala            |   18 +++++---------
 gee/linkedlist.vala         |   18 +++++---------
 gee/treemap.vala            |   36 +++++++++++------------------
 gee/treeset.vala            |   18 +++++---------
 8 files changed, 109 insertions(+), 88 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index 37a1567..d46b5ed 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -13,6 +13,7 @@ lib_LTLIBRARIES = \
 	$(NULL)
 
 libgee_la_VALASOURCES = \
+	abstractcollection.vala \
 	arraylist.vala \
 	collection.vala \
 	functions.vala \
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
new file mode 100644
index 0000000..1a4133c
--- /dev/null
+++ b/gee/abstractcollection.vala
@@ -0,0 +1,52 @@
+/* abstractcollection.vala
+ *
+ * Copyright (C) 2007  Jürg Billeter
+ * Copyright (C) 2009  Didier Villevalois
+ *
+ * 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:
+ * 	Didier 'Ptitjes' Villevalois <ptitjes free fr>
+ */
+
+/**
+ * Serves as the base class for implementing collection classes.
+ */
+public abstract class Gee.AbstractCollection<G> : Object, Iterable<G>, Collection<G> {
+
+	//
+	// Inherited from Collection<G>
+	//
+
+	public abstract int size { get; }
+
+	public abstract bool contains (G item);
+
+	public abstract bool add (G item);
+
+	public abstract bool remove (G item);
+
+	public abstract void clear ();
+
+	//
+	// Inherited from Iterable<G>
+	//
+
+	public Type get_element_type () {
+		return typeof (G);
+	}
+
+	public abstract Iterator<G> iterator ();
+}
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 7793376..1479520 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -27,8 +27,8 @@ using GLib;
 /**
  * Arrays of arbitrary elements which grow automatically as elements are added.
  */
-public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
-	public int size {
+public class Gee.ArrayList<G> : AbstractCollection<G>, Iterable<G>, Collection<G>, List<G> {
+	public override int size {
 		get { return _size; }
 	}
 
@@ -44,15 +44,11 @@ public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		this.equal_func = equal_func;
 	}
 
-	public Type get_element_type () {
-		return typeof (G);
-	}
-
-	public Gee.Iterator<G> iterator () {
+	public override Gee.Iterator<G> iterator () {
 		return new Iterator<G> (this);
 	}
 
-	public bool contains (G item) {
+	public override bool contains (G item) {
 		return (index_of (item) != -1);
 	}
 
@@ -79,7 +75,7 @@ public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		_items[index] = item;
 	}
 
-	public bool add (G item) {
+	public override bool add (G item) {
 		if (_size == _items.length) {
 			grow_if_needed (1);
 		}
@@ -100,7 +96,7 @@ public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		_stamp++;
 	}
 
-	public bool remove (G item) {
+	public override bool remove (G item) {
 		for (int index = 0; index < _size; index++) {
 			if (equal_func (_items[index], item)) {
 				remove_at (index);
@@ -121,7 +117,7 @@ public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		_stamp++;
 	}
 
-	public void clear () {
+	public override void clear () {
 		for (int index = 0; index < _size; index++) {
 			_items[index] = null;
 		}
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index ce24fd1..d06677d 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -177,38 +177,34 @@ public class Gee.HashMap<K,V> : Object, Map<K,V> {
 		}
 	}
 
-	private class KeySet<K,V> : Object, Iterable<K>, Collection<K>, Set<K> {
+	private class KeySet<K,V> : AbstractCollection<K>, Iterable<K>, Collection<K>, Set<K> {
 		public HashMap<K,V> map { construct; get; }
 
 		public KeySet (HashMap map) {
 			this.map = map;
 		}
 
-		public Type get_element_type () {
-			return typeof (K);
-		}
-
-		public Iterator<K> iterator () {
+		public override Iterator<K> iterator () {
 			return new KeyIterator<K,V> (map);
 		}
 
-		public int size {
+		public override int size {
 			get { return map.size; }
 		}
 
-		public bool add (K key) {
+		public override bool add (K key) {
 			assert_not_reached ();
 		}
 
-		public void clear () {
+		public override void clear () {
 			assert_not_reached ();
 		}
 
-		public bool remove (K key) {
+		public override bool remove (K key) {
 			assert_not_reached ();
 		}
 
-		public bool contains (K key) {
+		public override bool contains (K key) {
 			return _map.contains (key);
 		}
 	}
@@ -250,38 +246,34 @@ public class Gee.HashMap<K,V> : Object, Map<K,V> {
 		}
 	}
 
-	private class ValueCollection<K,V> : Object, Iterable<V>, Collection<V> {
+	private class ValueCollection<K,V> : AbstractCollection<K>, Iterable<V>, Collection<V> {
 		public HashMap<K,V> map { construct; get; }
 
 		public ValueCollection (HashMap map) {
 			this.map = map;
 		}
 
-		public Type get_element_type () {
-			return typeof (V);
-		}
-
-		public Iterator<V> iterator () {
+		public override Iterator<V> iterator () {
 			return new ValueIterator<K,V> (map);
 		}
 
-		public int size {
+		public override int size {
 			get { return map.size; }
 		}
 
-		public bool add (V value) {
+		public override bool add (V value) {
 			assert_not_reached ();
 		}
 
-		public void clear () {
+		public override void clear () {
 			assert_not_reached ();
 		}
 
-		public bool remove (V value) {
+		public override bool remove (V value) {
 			assert_not_reached ();
 		}
 
-		public bool contains (V value) {
+		public override bool contains (V value) {
 			Iterator<V> it = iterator ();
 			while (it.next ()) {
 				if (map.value_equal_func (it.get (), value)) {
diff --git a/gee/hashset.vala b/gee/hashset.vala
index c15a6da..f5e1597 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -27,8 +27,8 @@ using GLib;
 /**
  * Hashtable implementation of the Set interface.
  */
-public class Gee.HashSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
-	public int size {
+public class Gee.HashSet<G> : AbstractCollection<G>, Iterable<G>, Collection<G>, Set<G> {
+	public override int size {
 		get { return _nnodes; }
 	}
 
@@ -65,20 +65,16 @@ public class Gee.HashSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		return node;
 	}
 
-	public bool contains (G key) {
+	public override bool contains (G key) {
 		Node<G>** node = lookup_node (key);
 		return (*node != null);
 	}
 
-	public Type get_element_type () {
-		return typeof (G);
-	}
-
-	public Gee.Iterator<G> iterator () {
+	public override Gee.Iterator<G> iterator () {
 		return new Iterator<G> (this);
 	}
 
-	public bool add (G key) {
+	public override bool add (G key) {
 		Node<G>** node = lookup_node (key);
 		if (*node != null) {
 			return false;
@@ -92,7 +88,7 @@ public class Gee.HashSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		}
 	}
 
-	public bool remove (G key) {
+	public override bool remove (G key) {
 		Node<G>** node = lookup_node (key);
 		if (*node != null) {
 			Node<G> next = (owned) (*node)->next;
@@ -110,7 +106,7 @@ public class Gee.HashSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		return false;
 	}
 
-	public void clear () {
+	public override void clear () {
 		for (int i = 0; i < _array_size; i++) {
 			Node<G> node = (owned) _nodes[i];
 			while (node != null) {
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index 0fab4f6..7961329 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -27,7 +27,7 @@
 /**
  * A Gee.List implementation, using a doubly-linked list.
  */
-public class Gee.LinkedList<G> : Object, Iterable<G>, Collection<G>, List<G> {
+public class Gee.LinkedList<G> : AbstractCollection<G>, Iterable<G>, Collection<G>, List<G> {
 	private int _size = 0;
 	private int _stamp = 0;
 	private Node? _head = null;
@@ -40,24 +40,20 @@ public class Gee.LinkedList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 	}
 
 	// Iterable<G>
-	public Type get_element_type () {
-		return typeof (G);
-	}
-
-	public Gee.Iterator<G> iterator () {
+	public override Gee.Iterator<G> iterator () {
 		return new Iterator<G> (this);
 	}
 
 	// Collection<G>
-	public int size {
+	public override int size {
 		get { return this._size; }
 	}
 
-	public bool contains (G item) {
+	public override bool contains (G item) {
 		return this.index_of (item) != -1;
 	}
 
-	public bool add (G item) {
+	public override bool add (G item) {
 		Node<G> n = new Node<G> (item);
 		if (this._head == null && this._tail == null) {
 			this._head = this._tail = n;
@@ -74,7 +70,7 @@ public class Gee.LinkedList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		return true;
 	}
 
-	public bool remove (G item) { // Should remove only the first occurence (a test should be added)
+	public override bool remove (G item) { // Should remove only the first occurence (a test should be added)
 		for (Node<G> n = this._head; n != null; n = n.next) {
 			if (this.equal_func (item, n.data)) {
 				this._remove_node (n);
@@ -84,7 +80,7 @@ public class Gee.LinkedList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		return false;
 	}
 
-	public void clear () {
+	public override void clear () {
 		++this._stamp;
 		this._head = this._tail = null;
 		this._size = 0;
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 8a64cb5..cfe6e9d 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -295,74 +295,66 @@ public class Gee.TreeMap<K,V> : Object, Map<K,V> {
 	private weak Node<K, V>? first;
 	private int stamp = 0;
 
-	private class KeySet<K,V> : Object, Iterable<K>, Collection<K>, Set<K> {
+	private class KeySet<K,V> : AbstractCollection<K>, Iterable<K>, Collection<K>, Set<K> {
 		public TreeMap<K,V> map { construct; get; }
 
 		public KeySet (TreeMap<K,V> map) {
 			this.map = map;
 		}
 
-		public Type get_element_type () {
-			return typeof (K);
-		}
-
-		public Iterator<K> iterator () {
+		public override Iterator<K> iterator () {
 			return new KeyIterator<K,V> (map);
 		}
 
-		public int size {
+		public override int size {
 			get { return map.size; }
 		}
 
-		public bool add (K key) {
+		public override bool add (K key) {
 			assert_not_reached ();
 		}
 
-		public void clear () {
+		public override void clear () {
 			assert_not_reached ();
 		}
 
-		public bool remove (K key) {
+		public override bool remove (K key) {
 			assert_not_reached ();
 		}
 
-		public bool contains (K key) {
+		public override bool contains (K key) {
 			return map.contains (key);
 		}
 	}
 
-	private class ValueCollection<K,V> : Object, Iterable<V>, Collection<V> {
+	private class ValueCollection<K,V> : AbstractCollection<V>, Iterable<V>, Collection<V> {
 		public TreeMap<K,V> map { construct; get; }
 
 		public ValueCollection (TreeMap map) {
 			this.map = map;
 		}
 
-		public Type get_element_type () {
-			return typeof (V);
-		}
-
-		public Iterator<V> iterator () {
+		public override Iterator<V> iterator () {
 			return new ValueIterator<K,V> (map);
 		}
 
-		public int size {
+		public override int size {
 			get { return map.size; }
 		}
 
-		public bool add (V key) {
+		public override bool add (V key) {
 			assert_not_reached ();
 		}
 
-		public void clear () {
+		public override void clear () {
 			assert_not_reached ();
 		}
 
-		public bool remove (V key) {
+		public override bool remove (V key) {
 			assert_not_reached ();
 		}
 
-		public bool contains (V key) {
+		public override bool contains (V key) {
 			Iterator<V> it = iterator ();
 			while (it.next ()) {
 				if (map.value_equal_func (key, it.get ())) {
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 0104bec..cf982c0 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -25,8 +25,8 @@ using GLib;
 /**
  * Left-leaning red-black tree implementation of the Set interface.
  */
-public class Gee.TreeSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
-	public int size {
+public class Gee.TreeSet<G> : AbstractCollection<G>, Iterable<G>, Collection<G>, Set<G> {
+	public override int size {
 		get {return _size;}
 	}
 
@@ -38,7 +38,7 @@ public class Gee.TreeSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		this.compare_func = compare_func;
 	}
 
-	public bool contains (G item) {
+	public override bool contains (G item) {
 		weak Node<G>? cur = root;
 		while (cur != null) {
 			int res = compare_func (item, cur.key);
@@ -117,7 +117,7 @@ public class Gee.TreeSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		}
 	}
 
-	public bool add (G item) {
+	public override bool add (G item) {
 		bool r = add_to_node (ref root, item, null, null);
 		root.color = Node.Color.BLACK;
 		stamp++;
@@ -199,7 +199,7 @@ public class Gee.TreeSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		}
 	}
 
-	public bool remove (G item) {
+	public override bool remove (G item) {
 		bool b = remove_from_node (ref root, item);
 		if (root != null) {
 			root.color = Node.Color.BLACK;
@@ -208,17 +208,13 @@ public class Gee.TreeSet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		return b;
 	}
 
-	public void clear () {
+	public override void clear () {
 		root = null;
 		_size = 0;
 		stamp++;
 	}
 
-	public Type get_element_type () {
-		return typeof (G);
-	}
-
-	public Gee.Iterator<G> iterator () {
+	public override Gee.Iterator<G> iterator () {
 		return  new Iterator<G> (this);
 	}
 



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