Introduce Collection.to_array() method
authorDidier 'Ptitjes <ptitjes@free.fr>
Thu, 23 Jul 2009 22:41:03 +0000 (00:41 +0200)
committerDidier 'Ptitjes <ptitjes@free.fr>
Thu, 23 Jul 2009 22:41:22 +0000 (00:41 +0200)
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
gee/arraylist.vala
gee/collection.vala
gee/readonlycollection.vala
gee/readonlylist.vala
gee/readonlyset.vala
tests/testarraylist.vala

index 1a4133c..42b2a17 100644 (file)
@@ -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>
        //
index 1479520..3fc2134 100644 (file)
@@ -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);
index 6e5cc7b..c6012d2 100644 (file)
@@ -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();
 }
 
index 8bc247d..9c5c99b 100644 (file)
@@ -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;
index 19b3089..9b1f147 100644 (file)
@@ -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;
index e3ebe98..a02c356 100644 (file)
@@ -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;
index 9d8bbd8..585f833 100644 (file)
@@ -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);