From 3199e7a5d999cc5b3120593f073d4a39943b6ef1 Mon Sep 17 00:00:00 2001 From: Didier 'Ptitjes Date: Mon, 2 Aug 2010 16:50:39 +0200 Subject: [PATCH] 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 : Object, Iterable, 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) this); + } else if (t == typeof (char)) { + return (G[]) to_char_array((Collection) this); + } else if (t == typeof (uchar)) { + return (G[]) to_uchar_array((Collection) this); + } else if (t == typeof (int)) { + return (G[]) to_int_array((Collection) this); + } else if (t == typeof (uint)) { + return (G[]) to_uint_array((Collection) this); + } else if (t == typeof (int64)) { + return (G[]) to_int64_array((Collection) this); + } else if (t == typeof (uint64)) { + return (G[]) to_uint64_array((Collection) this); + } else if (t == typeof (long)) { + return (G[]) to_long_array((Collection) this); + } else if (t == typeof (ulong)) { + return (G[]) to_ulong_array((Collection) this); + } else if (t == typeof (float)) { + return (G[]) to_float_array((Collection) this); + } else if (t == typeof (double)) { + return (G[]) to_double_array((Collection) 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 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 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 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 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 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 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 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 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 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 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 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_list = new ArrayList (); + 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_list = new ArrayList (); + 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_list = new ArrayList (); + 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++]); + } + } } -- 2.7.4