[libgee] Introduce Collection.to_array() method



commit f92b513709a91bf659607a2d12c995101846d17d
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Fri Jul 24 00:41:03 2009 +0200

    Introduce Collection.to_array() method
    
    A default naive implementation is provided in AbstractCollection. This
    implementation is overriden in ArrayList in order to take benefit of its array
    nature.

 gee/abstractcollection.vala |    9 +++++++++
 gee/arraylist.vala          |    6 ++++++
 gee/collection.vala         |    7 +++++++
 gee/readonlycollection.vala |    4 ++++
 gee/readonlylist.vala       |    4 ++++
 gee/readonlyset.vala        |    4 ++++
 tests/testarraylist.vala    |   15 +++++++++++++++
 7 files changed, 49 insertions(+), 0 deletions(-)
---
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index 1a4133c..42b2a17 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -40,6 +40,15 @@ public abstract class Gee.AbstractCollection<G> : Object, Iterable<G>, Collectio
 
 	public abstract void clear ();
 
+	public virtual G[] to_array() {
+		G[] array = new G[size];
+		int index = 0;
+		foreach (G element in this) {
+			array[index] = element;
+		}
+		return array;
+	}
+
 	//
 	// Inherited from Iterable<G>
 	//
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 1479520..3fc2134 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -138,6 +138,12 @@ public class Gee.ArrayList<G> : AbstractCollection<G>, Iterable<G>, Collection<G
 		return slice;
 	}
 
+	public override G[] to_array() {
+		G[] array = new G[_size];
+		Memory.copy(array, _items, sizeof(G) * _size);
+		return array;
+	}
+
 	private void shift (int start, int delta) {
 		assert (start >= 0);
 		assert (start <= _size);
diff --git a/gee/collection.vala b/gee/collection.vala
index 6e5cc7b..c6012d2 100644
--- a/gee/collection.vala
+++ b/gee/collection.vala
@@ -64,5 +64,12 @@ public interface Gee.Collection<G> : Iterable<G> {
 	 * read-only collections.
 	 */
 	public abstract void clear ();
+
+	/**
+	 * Returns an array containing all of items from this collection.
+	 *
+	 * @return an array containing all of items from this collection
+	 */
+	public abstract G[] to_array();
 }
 
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index 8bc247d..9c5c99b 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -72,6 +72,10 @@ public class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
 		assert_not_reached ();
 	}
 
+	public G[] to_array() {
+		return _collection.to_array ();
+	}
+
 	private class Iterator<G> : Object, Gee.Iterator<G> {
 		public bool next () {
 			return false;
diff --git a/gee/readonlylist.vala b/gee/readonlylist.vala
index 19b3089..9b1f147 100644
--- a/gee/readonlylist.vala
+++ b/gee/readonlylist.vala
@@ -104,6 +104,10 @@ public class Gee.ReadOnlyList<G> : Object, Iterable<G>, Collection<G>, List<G> {
 		assert_not_reached ();
 	}
 
+	public G[] to_array() {
+		return _list.to_array ();
+	}
+
 	class Iterator<G> : Object, Gee.Iterator<G> {
 		public bool next () {
 			return false;
diff --git a/gee/readonlyset.vala b/gee/readonlyset.vala
index e3ebe98..a02c356 100644
--- a/gee/readonlyset.vala
+++ b/gee/readonlyset.vala
@@ -72,6 +72,10 @@ public class Gee.ReadOnlySet<G> : Object, Iterable<G>, Collection<G>, Set<G> {
 		assert_not_reached ();
 	}
 
+	public G[] to_array() {
+		return _set.to_array ();
+	}
+
 	private class Iterator<G> : Object, Gee.Iterator<G> {
 		public bool next () {
 			return false;
diff --git a/tests/testarraylist.vala b/tests/testarraylist.vala
index 9d8bbd8..585f833 100644
--- a/tests/testarraylist.vala
+++ b/tests/testarraylist.vala
@@ -498,6 +498,20 @@ void test_arraylist_iterator () {
 	assert (!iterator.next());
 }
 
+void test_arraylist_to_array () {
+	var arraylistOfString = new ArrayList<string> ();
+
+	// Check iterate list
+	arraylistOfString.add ("42");
+	arraylistOfString.add ("43");
+	arraylistOfString.add ("44");
+
+	string[] array = (string[]) arraylistOfString.to_array ();
+	assert (array[0] == "42");
+	assert (array[1] == "43");
+	assert (array[2] == "44");
+}
+
 void main (string[] args) {
 	Test.init (ref args);
 	
@@ -514,6 +528,7 @@ void main (string[] args) {
 	Test.add_func ("/Arraylist/Collection/contains", test_arraylist_contains);
 	Test.add_func ("/Arraylist/Collection/remove", test_arraylist_remove);
 	Test.add_func ("/Arraylist/Collection/size", test_arraylist_size);
+	Test.add_func ("/Arraylist/Collection/to_array", test_arraylist_to_array);
 	
 	// Methods of Iterable interface
 	Test.add_func ("/Arraylist/Iterable/iterator", test_arraylist_iterator);



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