1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* Copyright (c) 2014 Benjamin Otte <ottte@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "graphdata.h"
enum {
PROP_0,
PROP_N_VALUES
};
struct _GtkGraphDataPrivate
{
guint n_values;
guint offset;
double *values;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkGraphData, gtk_graph_data, G_TYPE_OBJECT)
static void
gtk_graph_data_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GtkGraphData *graph = GTK_GRAPH_DATA (object);
GtkGraphDataPrivate *priv = graph->priv;
switch (param_id)
{
case PROP_N_VALUES:
g_value_set_boolean (value, priv->n_values);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
}
static void
gtk_graph_data_finalize (GObject *object)
{
GtkGraphData *graph = GTK_GRAPH_DATA (object);
GtkGraphDataPrivate *priv = graph->priv;
g_free (priv->values);
G_OBJECT_CLASS (gtk_graph_data_parent_class)->finalize (object);
}
static void
gtk_graph_data_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GtkGraphData *graph = GTK_GRAPH_DATA (object);
GtkGraphDataPrivate *priv = graph->priv;
switch (param_id)
{
case PROP_N_VALUES:
priv->n_values = g_value_get_uint (value);
priv->values = g_new0 (double, priv->n_values);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
}
static void
gtk_graph_data_class_init (GtkGraphDataClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gtk_graph_data_finalize;
object_class->get_property = gtk_graph_data_get_property;
object_class->set_property = gtk_graph_data_set_property;
g_object_class_install_property (object_class,
PROP_N_VALUES,
g_param_spec_uint ("n-values", NULL, NULL,
1, G_MAXUINT, 1,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
}
static void
gtk_graph_data_init (GtkGraphData *graph)
{
graph->priv = gtk_graph_data_get_instance_private (graph);
}
GtkGraphData *
gtk_graph_data_new (guint n_values)
{
return g_object_new (GTK_TYPE_GRAPH_DATA,
"n-values", n_values,
NULL);
}
guint
gtk_graph_data_get_n_values (GtkGraphData *data)
{
return data->priv->n_values;
}
double
gtk_graph_data_get_value (GtkGraphData *data,
guint i)
{
GtkGraphDataPrivate *priv = data->priv;
return priv->values[(priv->offset + i) % priv->n_values];
}
double
gtk_graph_data_get_minimum (GtkGraphData *data)
{
GtkGraphDataPrivate *priv = data->priv;
double minimum = G_MAXDOUBLE;
guint i;
for (i = 0; i < priv->n_values; i++)
{
minimum = MIN (minimum, priv->values[i]);
}
return minimum;
}
double
gtk_graph_data_get_maximum (GtkGraphData *data)
{
GtkGraphDataPrivate *priv = data->priv;
double maximum = -G_MAXDOUBLE;
guint i;
for (i = 0; i < priv->n_values; i++)
{
maximum = MAX (maximum, priv->values[i]);
}
return maximum;
}
void
gtk_graph_data_prepend_value (GtkGraphData *data,
double value)
{
GtkGraphDataPrivate *priv = data->priv;
priv->offset = (priv->offset + priv->n_values - 1) % priv->n_values;
priv->values[priv->offset] = value;
}
|