]> git.cworth.org Git - acre/commitdiff
Add new add_data_point_2d_named
authorCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 17:39:16 +0000 (09:39 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 17:39:16 +0000 (09:39 -0800)
Nothing is actually done with the names yet, but this will be useful
when we add new timeline-like rendering modes for data sets.

acre.c
acre.h

diff --git a/acre.c b/acre.c
index 5ac51bfae1c7f34c148d64013065d5b7c874cae8..8945292b3578ad530c0d653ee407da73d429a718 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -37,14 +37,28 @@ typedef struct _acre_data_point_2d {
 } acre_data_point_2d_t;
 
 struct _acre_data {
+    /* The name of this data set. */
     char *name;
 
+    /* Minimum and mximum extents of data. */
     acre_data_point_2d_t min;
     acre_data_point_2d_t max;
 
+    /* The data itself. */
     acre_data_point_2d_t *points;
     unsigned int points_size;
     unsigned int num_points;
+
+    /* The names of data points (if any).
+     *
+     * This array is indexed with the same index as the 'points' array
+     * to provide names for points. It is legal for this array to be
+     * NULL or smaller than 'points', (in which case, the
+     * corresponding points simply have no names).
+     */
+    char **names;
+    unsigned int names_size;
+    unsigned int num_names;
 };
 
 typedef struct _acre_axis {
@@ -1054,6 +1068,14 @@ acre_data_create (void)
 void
 acre_data_destroy (acre_data_t *data)
 {
+    unsigned i;
+
+    for (i = 0; i < data->num_names; i++) {
+       if (data->names[i])
+           free (data->names[i]);
+    }
+    free (data->names);
+
     free (data->name);
 
     free (data->points);
@@ -1106,3 +1128,27 @@ acre_data_add_point_2d (acre_data_t *data, double x, double y)
 
     data->num_points++;
 }
+
+/* Add a datapoint with a name to the given dataset. */
+void
+acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
+{
+    unsigned i;
+
+    acre_data_add_point_2d (data, x, y);
+
+    if (data->names_size < data->points_size) {
+       data->names_size = data->points_size;
+       data->names = xrealloc_ab (data->names,
+                                  data->names_size,
+                                  sizeof (char *));
+    }
+
+    /* Initialize any newly-created holes in the array to NULL. */
+    for (i = data->num_names; i < data->num_points - 1; i++)
+       data->names[i] = NULL;
+
+    data->num_names = data->num_points;
+
+    data->names[data->num_names - 1] = xstrdup (name);
+}
diff --git a/acre.h b/acre.h
index df23199cbc0ea6a4a7230cadc1b48fbf818a0d85..663835c333dd8daca54b1f0da639f357280ea460 100644 (file)
--- a/acre.h
+++ b/acre.h
@@ -119,4 +119,9 @@ acre_data_set_name (acre_data_t *data, const char *name);
 void
 acre_data_add_point_2d (acre_data_t *data, double x, double y);
 
+/* Add a datapoint with a name to the given dataset. */
+void
+acre_data_add_point_2d_named (acre_data_t *data, double x, double y,
+                             const char *name);
+
 #endif /* ACRE_H */