From patchwork Mon Jul 6 12:35:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 6723301 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C169AC05AC for ; Mon, 6 Jul 2015 12:35:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DD6EB2062C for ; Mon, 6 Jul 2015 12:35:55 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id E053620600 for ; Mon, 6 Jul 2015 12:35:54 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5623B6E872; Mon, 6 Jul 2015 05:35:54 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTP id 6DE0E6E875 for ; Mon, 6 Jul 2015 05:35:53 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP; 06 Jul 2015 05:35:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,414,1432623600"; d="scan'208";a="741421020" Received: from magarwal-mobl.amr.corp.intel.com (HELO strange.amr.corp.intel.com) ([10.254.77.111]) by fmsmga001.fm.intel.com with ESMTP; 06 Jul 2015 05:35:52 -0700 From: Damien Lespiau To: intel-gfx@lists.freedesktop.org Date: Mon, 6 Jul 2015 13:35:35 +0100 Message-Id: <1436186144-19665-8-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> References: <1436186144-19665-1-git-send-email-damien.lespiau@intel.com> Subject: [Intel-gfx] [PATCH i-g-t 07/16] plot: Add a map() to igt_vector_t X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Can use to reduce typing a bit, at the expense of a function call. Signed-off-by: Damien Lespiau --- lib/igt_plot.c | 21 ++++++++++++++++++++- lib/igt_plot.h | 9 +++++++++ lib/tests/igt_plot.c | 11 ++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) 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);