[libgee] Implement typed variants for Collection.to_array
- From: Didier Villevalois <dvillevalois src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgee] Implement typed variants for Collection.to_array
- Date: Mon, 2 Aug 2010 14:56:47 +0000 (UTC)
commit 3199e7a5d999cc5b3120593f073d4a39943b6ef1
Author: Didier 'Ptitjes <ptitjes free fr>
Date: Mon Aug 2 16:50:39 2010 +0200
Implement typed variants for Collection.to_array
Fixes bug 597737.
gee/abstractcollection.vala | 128 ++++++++++++++++++++++++++++++++++++++++++-
tests/testarraylist.vala | 39 +++++++++++++
2 files changed, 165 insertions(+), 2 deletions(-)
---
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index 55d8ba3..e7ef6be 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -68,9 +68,133 @@ public abstract class Gee.AbstractCollection<G> : Object, Iterable<G>, Collectio
* { inheritDoc}
*/
public virtual G[] to_array () {
- G[] array = new G[size];
+ var t = typeof (G);
+ if (t == typeof (bool)) {
+ return (G[]) to_bool_array((Collection<bool>) this);
+ } else if (t == typeof (char)) {
+ return (G[]) to_char_array((Collection<char>) this);
+ } else if (t == typeof (uchar)) {
+ return (G[]) to_uchar_array((Collection<uchar>) this);
+ } else if (t == typeof (int)) {
+ return (G[]) to_int_array((Collection<int>) this);
+ } else if (t == typeof (uint)) {
+ return (G[]) to_uint_array((Collection<uint>) this);
+ } else if (t == typeof (int64)) {
+ return (G[]) to_int64_array((Collection<int64>) this);
+ } else if (t == typeof (uint64)) {
+ return (G[]) to_uint64_array((Collection<uint64>) this);
+ } else if (t == typeof (long)) {
+ return (G[]) to_long_array((Collection<long>) this);
+ } else if (t == typeof (ulong)) {
+ return (G[]) to_ulong_array((Collection<ulong>) this);
+ } else if (t == typeof (float)) {
+ return (G[]) to_float_array((Collection<float>) this);
+ } else if (t == typeof (double)) {
+ return (G[]) to_double_array((Collection<double>) this);
+ } else {
+ G[] array = new G[size];
+ int index = 0;
+ foreach (G element in this) {
+ array[index++] = element;
+ }
+ return array;
+ }
+ }
+
+ private static bool[] to_bool_array(Collection<bool> coll) {
+ bool[] array = new bool[coll.size];
+ int index = 0;
+ foreach (bool element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static char[] to_char_array(Collection<char> coll) {
+ char[] array = new char[coll.size];
+ int index = 0;
+ foreach (char element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static uchar[] to_uchar_array(Collection<uchar> coll) {
+ uchar[] array = new uchar[coll.size];
+ int index = 0;
+ foreach (uchar element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static int[] to_int_array(Collection<int> coll) {
+ int[] array = new int[coll.size];
+ int index = 0;
+ foreach (int element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static uint[] to_uint_array(Collection<uint> coll) {
+ uint[] array = new uint[coll.size];
+ int index = 0;
+ foreach (uint element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static int64[] to_int64_array(Collection<int64?> coll) {
+ int64[] array = new int64[coll.size];
+ int index = 0;
+ foreach (int64 element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static uint64[] to_uint64_array(Collection<uint64?> coll) {
+ uint64[] array = new uint64[coll.size];
+ int index = 0;
+ foreach (uint64 element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static long[] to_long_array(Collection<long> coll) {
+ long[] array = new long[coll.size];
+ int index = 0;
+ foreach (long element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static ulong[] to_ulong_array(Collection<ulong> coll) {
+ ulong[] array = new ulong[coll.size];
+ int index = 0;
+ foreach (ulong element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static float[] to_float_array(Collection<float?> coll) {
+ float[] array = new float[coll.size];
+ int index = 0;
+ foreach (float element in coll) {
+ array[index++] = element;
+ }
+ return array;
+ }
+
+ private static double[] to_double_array(Collection<double?> coll) {
+ double[] array = new double[coll.size];
int index = 0;
- foreach (G element in this) {
+ foreach (double element in coll) {
array[index++] = element;
}
return array;
diff --git a/tests/testarraylist.vala b/tests/testarraylist.vala
index 6c649fd..e5340c5 100644
--- a/tests/testarraylist.vala
+++ b/tests/testarraylist.vala
@@ -33,6 +33,7 @@ public class ArrayListTests : ListTests {
add_test ("[ArrayList] GObject properties", test_gobject_properties);
add_test ("[ArrayList] small sort (insertion)", test_small_sort);
add_test ("[ArrayList] big sort (timsort)", test_big_sort);
+ add_test ("[ArrayList] typed to_array calls", test_typed_to_array);
}
private static const int BIG_SORT_SIZE = 1000000;
@@ -115,4 +116,42 @@ public class ArrayListTests : ListTests {
assert (big_test_list[i - 1] <= big_test_list[i]);
}
}
+
+ private void test_typed_to_array () {
+ // Test with a bool collection
+ Gee.List<bool> bool_list = new ArrayList<bool> ();
+ assert (bool_list.add (true));
+ assert (bool_list.add (true));
+ assert (bool_list.add (false));
+
+ bool[] bool_array = bool_list.to_array ();
+ int index = 0;
+ foreach (bool element in bool_list) {
+ assert (element == bool_array[index++]);
+ }
+
+ // Test with an int collection
+ Gee.List<int> int_list = new ArrayList<int> ();
+ assert (int_list.add (1));
+ assert (int_list.add (2));
+ assert (int_list.add (3));
+
+ int[] int_array = int_list.to_array ();
+ index = 0;
+ foreach (int element in int_list) {
+ assert (element == int_array[index++]);
+ }
+
+ // Test with a double collection
+ Gee.List<double?> double_list = new ArrayList<double?> ();
+ assert (double_list.add (1.0d));
+ assert (double_list.add (1.5d));
+ assert (double_list.add (2.0d));
+
+ double[] double_array = double_list.to_array ();
+ index = 0;
+ foreach (double element in double_list) {
+ assert (element == double_array[index++]);
+ }
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]