vala r1109 - in trunk: . gobject tests



Author: juergbi
Date: Sat Mar  8 16:10:11 2008
New Revision: 1109
URL: http://svn.gnome.org/viewvc/vala?rev=1109&view=rev

Log:
2008-03-08  Juerg Billeter  <j bitron ch>

	* gobject/valaccodegenerator.vala: support foreach over
	  multi-dimensional arrays

	* tests/arrays.vala: test multi-dimensional arrays


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodegenerator.vala
   trunk/tests/arrays.vala

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Sat Mar  8 16:10:11 2008
@@ -1671,7 +1671,7 @@
 		if (stmt.collection.static_type is ArrayType) {
 			var array_type = (ArrayType) stmt.collection.static_type;
 			
-			var array_len = get_array_length_cexpression (stmt.collection, 1);
+			var array_len = get_array_length_cexpression (stmt.collection);
 			
 			/* the array has no length parameter i.e. is NULL-terminated array */
 			if (array_len is CCodeConstant) {
@@ -2264,9 +2264,23 @@
 		return "%s_length%d".printf (array_cname, dim);
 	}
 
-	public CCodeExpression! get_array_length_cexpression (Expression! array_expr, int dim) {
+	public CCodeExpression! get_array_length_cexpression (Expression! array_expr, int dim = -1) {
+		// dim == -1 => total size over all dimensions
+		if (dim == -1) {
+			var array_type = array_expr.static_type as ArrayType;
+			if (array_type != null && array_type.rank > 1) {
+				CCodeExpression cexpr = get_array_length_cexpression (array_expr, 1);
+				for (dim = 2; dim <= array_type.rank; dim++) {
+					cexpr = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, cexpr, get_array_length_cexpression (array_expr, dim));
+				}
+				return cexpr;
+			} else {
+				dim = 1;
+			}
+		}
+
 		bool is_out = false;
-	
+
 		if (array_expr is UnaryExpression) {
 			var unary_expr = (UnaryExpression) array_expr;
 			if (unary_expr.operator == UnaryOperator.OUT || unary_expr.operator == UnaryOperator.REF) {

Modified: trunk/tests/arrays.vala
==============================================================================
--- trunk/tests/arrays.vala	(original)
+++ trunk/tests/arrays.vala	Sat Mar  8 16:10:11 2008
@@ -240,6 +240,24 @@
 		accept_array (create_array ());
 	}
 
+	static void test_arrays_multi_dimensional () {
+		int[,] array = new int[3,2];
+
+		int i = 0;
+		for (int x = 0; x < 3; x++) {
+			for (int y = 0; y < 2; y++) {
+				array[x,y] = i++;
+			}
+		}
+
+		i = 0;
+		foreach (int v in array) {
+			assert (v == i);
+			i++;
+		}
+		assert (i == 3 * 2);
+	}
+
 	static void main (string[] args) {
 		test_integer_array ();
 		test_string_array ();
@@ -263,6 +281,8 @@
 		test_array_var_creation_with_structs ();
 
 		test_array_argument ();
+
+		test_arrays_multi_dimensional ();
 	}
 	
 	public static int inc () {



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