diff mbox

[i-g-t,07/16] plot: Add a map() to igt_vector_t

Message ID 1436186144-19665-8-git-send-email-damien.lespiau@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lespiau, Damien July 6, 2015, 12:35 p.m. UTC
Can use to reduce typing a bit, at the expense of a function call.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 lib/igt_plot.c       | 21 ++++++++++++++++++++-
 lib/igt_plot.h       |  9 +++++++++
 lib/tests/igt_plot.c | 11 ++++++-----
 3 files changed, 35 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/lib/igt_plot.c b/lib/igt_plot.c
index 3f3c2fe..9a1bea2 100644
--- a/lib/igt_plot.c
+++ b/lib/igt_plot.c
@@ -139,6 +139,25 @@  void igt_vector_unref(igt_vector_t *v)
 }
 
 /**
+ * igt_vector_map:
+ * @v: An #igt_vector_t
+ * @func: Map function
+ *
+ * igt_vector_map() creates a new array cycling through @v and applying @func
+ * to all @v's elements.
+ */
+igt_vector_t *igt_vector_map(igt_vector_t *v, igt_vector_map_func_t func)
+{
+	igt_vector_t *ret;
+	unsigned int i;
+
+	ret = igt_vector_new_internal(v->n);
+	for (i = 0; i < v->n; i++)
+		ret->values[i] = func(v->values[i]);
+	return ret;
+}
+
+/**
  * igt_vector_get_min_max:
  * @v: An #igt_vector_t
  * @min: (out): The minimum value in @v
@@ -547,7 +566,7 @@  static void igt_plot_layout_tick_labels(igt_plot_t *plot,
 		double v = axis->min +
 			(axis->max - axis->min) * i / (axis->n_ticks - 1);
 
-		asprintf(&label->text, "%.02lf", v);
+		asprintf(&label->text, "%.2lf", v);
 		cairo_text_extents(plot->cr, label->text, &label->extents);
 
 		if (axis->orientation == IGT_ORIENTATION_HORIZONTAL) {
diff --git a/lib/igt_plot.h b/lib/igt_plot.h
index 2e00c60..2af035e 100644
--- a/lib/igt_plot.h
+++ b/lib/igt_plot.h
@@ -48,6 +48,14 @@  typedef struct {
 	double values[];
 } igt_vector_t;
 
+/**
+ * igt_vector_map_func_t:
+ * @value: a double to act on
+ *
+ * Type of functions to use with igt_vector_map().
+ */
+typedef double (*igt_vector_map_func_t)(double value);
+
 igt_vector_t *igt_vector_new(unsigned int n);
 igt_vector_t *igt_vector_new_from_array(const double *array, unsigned int n);
 igt_vector_t *igt_vector_new_from_array_u64(const uint64_t *array,
@@ -55,6 +63,7 @@  igt_vector_t *igt_vector_new_from_array_u64(const uint64_t *array,
 igt_vector_t *igt_vector_linear(double min, double max, unsigned n);
 igt_vector_t *igt_vector_ref(igt_vector_t *v);
 void igt_vector_unref(igt_vector_t *v);
+igt_vector_t *igt_vector_map(igt_vector_t *v, igt_vector_map_func_t func);
 void igt_vector_get_min_max(const igt_vector_t *v, double *min, double *max);
 
 /**
diff --git a/lib/tests/igt_plot.c b/lib/tests/igt_plot.c
index cb7f022..2f5ad27 100644
--- a/lib/tests/igt_plot.c
+++ b/lib/tests/igt_plot.c
@@ -69,17 +69,18 @@  static void test_min_max(void)
 	igt_vector_unref(v2);
 }
 
+static double f(double x)
+{
+	return sin(2 * M_PI * x);
+}
+
 static void test_simple_plot(void)
 {
 	igt_vector_t *x, *y;
-	unsigned int i;
 	igt_plot_t plot;
 
 	x = igt_vector_linear(-1.0, 1.0, 200);
-
-	y = igt_vector_new(200);
-	for (i = 0; i < y->n; i++)
-		y->values[i] = sin(2 * M_PI * x->values[i]);
+	y = igt_vector_map(x, f);
 
 	igt_plot_init(&plot, 800, 600);
 	igt_plot_set_color(&plot, 0.0, 0.0, 1.0, 1.0);