[gnumeric] Collect: fix problems with errors. COUNT/COUNTA: simplify.



commit fc0d67d4534563f2b819ee481b1c06121263bfc0
Author: Morten Welinder <terra gnome org>
Date:   Thu Jan 3 15:32:50 2013 -0500

    Collect: fix problems with errors.  COUNT/COUNTA: simplify.

 ChangeLog                   |    7 ++++
 plugins/fn-stat/ChangeLog   |    9 +++++
 plugins/fn-stat/functions.c |   81 ++++++++++++++----------------------------
 src/collect.c               |   18 ++++++++-
 src/collect.h               |    1 +
 src/func.c                  |    2 +-
 6 files changed, 61 insertions(+), 57 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index af0d24d..b318df7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2013-01-03  Morten Welinder  <terra gnome org>
+
+	* src/collect.c (collect_floats): Fix handling of
+	COLLECT_IGNORE_ERRORS.
+	(collect_strings): Ditto.
+	(callback_function_collect): Handle new flag COLLECT_ZERO_ERRORS.
+
 2013-01-01  Morten Welinder  <terra gnome org>
 
 	* src/mstyle.c (gnm_style_set_from_pango_attribute): Don't use
diff --git a/plugins/fn-stat/ChangeLog b/plugins/fn-stat/ChangeLog
index f4bd085..b8224c3 100644
--- a/plugins/fn-stat/ChangeLog
+++ b/plugins/fn-stat/ChangeLog
@@ -1,3 +1,12 @@
+2013-01-03  Morten Welinder  <terra gnome org>
+
+	* functions.c (gnumeric_count, gnumeric_counta): Make this regular
+	float_range_function users.
+	(callback_function_count, callback_function_counta): Now unused.
+	Remove.
+	(gnumeric_subtotal): Regularize handling of 2 (COUNT) and 3
+	(COUNTA).
+
 2012-12-18  Morten Welinder <terra gnome org>
 
 	* Release 1.12.0
diff --git a/plugins/fn-stat/functions.c b/plugins/fn-stat/functions.c
index 967de7f..2a3c0bf 100644
--- a/plugins/fn-stat/functions.c
+++ b/plugins/fn-stat/functions.c
@@ -660,26 +660,15 @@ static GnmFuncHelp const help_count[] = {
 };
 
 static GnmValue *
-callback_function_count (GnmEvalPos const *ep, GnmValue const *value, void *closure)
-{
-	int *result = closure;
-
-	if (value && VALUE_IS_FLOAT (value))
-		(*result)++;
-	return NULL;
-}
-
-static GnmValue *
 gnumeric_count (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 {
-	int i = 0;
-
-	/* no need to check for error, this is not strict */
-	function_iterate_argument_values
-		(ei->pos, callback_function_count, &i,
-		 argc, argv, FALSE, CELL_ITER_IGNORE_BLANK);
-
-	return value_new_int (i);
+	return float_range_function (argc, argv, ei,
+				     gnm_range_count,
+				     COLLECT_IGNORE_ERRORS |
+				     COLLECT_IGNORE_STRINGS |
+				     COLLECT_IGNORE_BOOLS |
+				     COLLECT_IGNORE_BLANKS,
+				     GNM_ERROR_DIV0);
 }
 
 /***************************************************************************/
@@ -696,24 +685,15 @@ static GnmFuncHelp const help_counta[] = {
 };
 
 static GnmValue *
-callback_function_counta (GnmEvalPos const *ep, GnmValue const *value, void *closure)
-{
-        int *result = closure;
-	(*result)++;
-	return NULL;
-}
-
-static GnmValue *
 gnumeric_counta (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 {
-	int i = 0;
-
-	/* no need to check for error, this is not strict */
-        function_iterate_argument_values
-		(ei->pos, callback_function_counta, &i,
-		 argc, argv, FALSE, CELL_ITER_IGNORE_BLANK);
-
-        return value_new_int (i);
+	return float_range_function (argc, argv, ei,
+				     gnm_range_count,
+				     COLLECT_ZERO_ERRORS |
+				     COLLECT_ZERO_STRINGS |
+				     COLLECT_ZEROONE_BOOLS |
+				     COLLECT_IGNORE_BLANKS,
+				     GNM_ERROR_DIV0);
 }
 
 /***************************************************************************/
@@ -4142,9 +4122,13 @@ gnumeric_subtotal (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 {
         GnmExpr const *expr;
 	GnmValue *val;
-	int   fun_nbr, res;
+	int   fun_nbr;
 	float_range_function_t func;
 	GnmStdError err = GNM_ERROR_DIV0;
+	CollectFlags flags_errors = 0;
+	CollectFlags flags_strings = COLLECT_IGNORE_STRINGS;
+	CollectFlags flags_bools = COLLECT_IGNORE_BOOLS;
+	CollectFlags flags_other = COLLECT_IGNORE_BLANKS | COLLECT_IGNORE_SUBTOTAL;
 
 	if (argc == 0)
 		return value_new_error_NUM (ei->pos);
@@ -4164,23 +4148,13 @@ gnumeric_subtotal (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 	argv++;
 
 	switch (fun_nbr) {
-	case 2: res = 0;
-		/* no need to check for error, this is not strict */
-		function_iterate_argument_values
-			(ei->pos, callback_function_count, &res,
-			 argc, argv, FALSE,
-			 CELL_ITER_IGNORE_BLANK | CELL_ITER_IGNORE_SUBTOTAL);
-		return value_new_int (res);
-
-	case 3: res = 0;
-		/* no need to check for error, this is not strict */
-		function_iterate_argument_values
-			(ei->pos, callback_function_counta, &res,
-			 argc, argv, FALSE,
-			 CELL_ITER_IGNORE_BLANK | CELL_ITER_IGNORE_SUBTOTAL);
-		return value_new_int (res);
-
 	case  1: func = gnm_range_average;	break;
+	case  2: flags_errors = COLLECT_IGNORE_ERRORS;		
+		 func = gnm_range_count;        break;
+	case  3: flags_errors = COLLECT_ZERO_ERRORS;
+		 flags_strings = COLLECT_ZERO_STRINGS;
+		 flags_bools = COLLECT_ZEROONE_BOOLS;
+		 func = gnm_range_count;        break;
 	case  4: err = GNM_ERROR_VALUE;
 		 func = range_max0;		break;
 	case  5: err = GNM_ERROR_VALUE;
@@ -4199,9 +4173,8 @@ gnumeric_subtotal (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 	}
 
 	return float_range_function (argc, argv, ei, func,
-		COLLECT_IGNORE_STRINGS | COLLECT_IGNORE_BOOLS |
-		COLLECT_IGNORE_BLANKS | COLLECT_IGNORE_SUBTOTAL,
-		err);
+				     flags_errors | flags_strings | flags_bools | flags_other,
+				     err);
 }
 
 /***************************************************************************/
diff --git a/src/collect.c b/src/collect.c
index 058482d..9c8bd91 100644
--- a/src/collect.c
+++ b/src/collect.c
@@ -347,6 +347,8 @@ callback_function_collect (GnmEvalPos const *ep, GnmValue const *value,
 	case VALUE_ERROR:
 		if (cl->flags & COLLECT_IGNORE_ERRORS)
 			ignore = TRUE;
+		else if (cl->flags & COLLECT_ZERO_ERRORS)
+			x = 0;
 		else
 			return value_new_error_VALUE (ep);
 		break;
@@ -435,6 +437,7 @@ collect_floats (int argc, GnmExprConstPtr const *argv,
 	CellIterFlags iter_flags = CELL_ITER_ALL;
 	GnmValue *key = NULL;
 	CollectFlags keyflags = flags & ~COLLECT_ORDER_IRRELEVANT;
+	gboolean strict;
 
 	if (constp)
 		*constp = FALSE;
@@ -479,6 +482,8 @@ collect_floats (int argc, GnmExprConstPtr const *argv,
 	if (flags & COLLECT_IGNORE_SUBTOTAL)
 		iter_flags |= CELL_ITER_IGNORE_SUBTOTAL;
 
+	strict = (flags & (COLLECT_IGNORE_ERRORS | COLLECT_ZERO_ERRORS)) == 0;
+
 	cl.alloc_count = 0;
 	cl.data = NULL;
 	cl.count = 0;
@@ -489,7 +494,7 @@ collect_floats (int argc, GnmExprConstPtr const *argv,
 	*error = function_iterate_argument_values
 		(ep, &callback_function_collect, &cl,
 		 argc, argv,
-		 TRUE, iter_flags);
+		 strict, iter_flags);
 	if (*error) {
 		g_assert (VALUE_IS_ERROR (*error));
 		g_free (cl.data);
@@ -986,17 +991,26 @@ collect_strings (int argc, GnmExprConstPtr const *argv,
 {
 	collect_strings_t cl;
 	CellIterFlags iter_flags = CELL_ITER_ALL;
+	gboolean strict;
+
+	/* We don't handle these flags */
+	g_return_val_if_fail (!(flags & COLLECT_ZERO_ERRORS), NULL);
+	g_return_val_if_fail (!(flags & COLLECT_ZERO_STRINGS), NULL);
+	g_return_val_if_fail (!(flags & COLLECT_ZEROONE_BOOLS), NULL);
+	g_return_val_if_fail (!(flags & COLLECT_ZERO_BLANKS), NULL);
 
 	if (flags & COLLECT_IGNORE_BLANKS)
 		iter_flags = CELL_ITER_IGNORE_BLANK;
 
+	strict = (flags & (COLLECT_IGNORE_ERRORS | COLLECT_ZERO_ERRORS)) == 0;
+
 	cl.data = g_ptr_array_new ();
 	cl.flags = flags;
 
 	*error = function_iterate_argument_values
 		(ep, &callback_function_collect_strings, &cl,
 		 argc, argv,
-		 TRUE, iter_flags);
+		 strict, iter_flags);
 	if (*error) {
 		g_assert (VALUE_IS_ERROR (*error));
 		collect_strings_free (cl.data);
diff --git a/src/collect.h b/src/collect.h
index 42a3433..ba8288f 100644
--- a/src/collect.h
+++ b/src/collect.h
@@ -16,6 +16,7 @@ typedef enum {
 	COLLECT_ZEROONE_BOOLS	= 0x20,
 
 	COLLECT_IGNORE_ERRORS	= 0x100,
+	COLLECT_ZERO_ERRORS	= 0x200,
 
 	COLLECT_IGNORE_BLANKS	= 0x1000,
 	COLLECT_ZERO_BLANKS	= 0x2000,
diff --git a/src/func.c b/src/func.c
index 33e62cd..bd37299 100644
--- a/src/func.c
+++ b/src/func.c
@@ -1969,7 +1969,7 @@ function_iterate_do_value (GnmEvalPos const  *ep,
 		data.callback = callback;
 		data.closure  = closure;
 		data.strict   = strict;
-		data.ignore_subtotal = (iter_flags & CELL_ITER_IGNORE_SUBTOTAL);
+		data.ignore_subtotal = (iter_flags & CELL_ITER_IGNORE_SUBTOTAL) != 0;
 
 		res = workbook_foreach_cell_in_range (ep, value, iter_flags,
 						      cb_iterate_cellrange,



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