File: channel_double.h

package info (click to toggle)
quickplot 0.10.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 16,640; sh: 11,163; makefile: 395
file content (218 lines) | stat: -rw-r--r-- 5,724 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
  Quickplot - an interactive 2D plotter

  Copyright (C) 1998-2011  Lance Arsenault


  This file is part of Quickplot.

  Quickplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  Quickplot 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Quickplot.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <sys/types.h>
#include <math.h>


#ifndef _QP_DEBUG_H_
#error "You must include qp_debug.h before this file."
#endif


#ifndef NAN
#error "number NAN is not defined"
#endif
#ifndef INFINITY
#error "number INFINITY not defined"
#endif

#define END_DOUBLE  (NAN)


extern
void qp_channel_series_double_append(qp_channel_t channel, double val);

extern
double qp_channel_series_double_begin(qp_channel_t channel);

extern
double qp_channel_series_double_end(qp_channel_t channel);


/* Find the value that is less than and next to or as close as possible
 * For series that is increasing
 * *v in is the value to search on
 * *v out is the value found from the channel
 * sets the current read to that value in the channel
 * returns the index to the value in the channel
 * returns 0 if a value was not found and the value
 * is set to the first value
 * */
extern
size_t qp_channel_series_double_find_lt(qp_channel_t channel, double *v);


/* Find the value that is greater than and next to or as close as possible
 * For series that is increasing
 * *v in is the value to search on
 * *v out is the value found from the channel
 * sets the current read to that value in the channel
 * returns the index to the value in the channel
 * returns length - 1 if the value was not found
 * and the value is set to the last value
 * */
extern
size_t qp_channel_series_double_find_gt(qp_channel_t channel, double *v);



/* Check that i is in bounds before this call */
/* This is a little slow, so don't iterate with it
 * you can use it to start an iteration at a point
 * not necessarily at the first or last value.  It is
 * a lot faster than iterating to the i-th value
 * with qp_channel_series_double_next() and
 * qp_channel_series_double_prev()
 *
 *  Returns the i-th value where i = [ 0, length-1 ] and
 *  sets the current value to that value */
static inline
double qp_channel_series_double_index(qp_channel_t c, size_t i)
{
  double *array;
  ASSERT(c->form == QP_CHANNEL_FORM_SERIES);
  ASSERT(i >= 0);
  ASSERT(i < qp_channel_series_length(c));
  ASSERT(c->value_type == QP_TYPE_DOUBLE);
  ASSERT(*(c->series.ref_count) > 0);

  array = qp_dllist_begin(c->series.arrays);
  for(;i >= ARRAY_LENGTH; i -= ARRAY_LENGTH)
    array = qp_dllist_next(c->series.arrays);

  c->series.array_current_index = i;

  return array[i]; 
}

/* This is not so fast.  Do not use in a tight loop. */
/* This returns the current index or (size_t) -1 if their is none. */
static inline
size_t qp_channel_series_double_get_index(qp_channel_t c)
{
  size_t ret = 0;
  void *current_array, *array;
  ASSERT(c->form == QP_CHANNEL_FORM_SERIES);
  ASSERT(c->value_type == QP_TYPE_DOUBLE);
  ASSERT(qp_channel_series_is_reading(c));
  ASSERT(*(c->series.ref_count) > 0);

  if(!qp_channel_series_is_reading(c))
    return (size_t) -1;

  ret += c->series.array_current_index;
  current_array = qp_dllist_val(c->series.arrays);

  array = qp_dllist_begin(c->series.arrays);
  while(array != current_array)
  {
    ret += ARRAY_LENGTH;
    array = qp_dllist_next(c->series.arrays);
    ASSERT(array);
  }

  return ret;
}

static inline
double qp_channel_series_double_next(qp_channel_t c)
{
  double *array;
  struct qp_channel_series *cs;
  ASSERT(c);
  ASSERT(c->form == QP_CHANNEL_FORM_SERIES ||
      c->form == QP_CHANNEL_FORM_FUNC);
  ASSERT(c->value_type == QP_TYPE_DOUBLE);
  ASSERT(c->series.arrays);
  ASSERT(*(c->series.ref_count) > 0);

  cs = &c->series;

  ++(cs->array_current_index);

  array = (double *) qp_dllist_val(cs->arrays);

  if(!array) return END_DOUBLE;

  if(array != cs->last_array)
  {
    if(cs->array_current_index < ARRAY_LENGTH)
      return array[cs->array_current_index];
    array = (double *) qp_dllist_next(cs->arrays);
    cs->array_current_index = 0;
  }
  if(array != cs->last_array)
    return array[cs->array_current_index];
  if(array && cs->array_current_index <= cs->array_last_index)
    return array[cs->array_current_index];
  if(array)
  {
    /* push it past the end */
    qp_dllist_next(cs->arrays);
    ASSERT(!qp_dllist_val(cs->arrays));
    return END_DOUBLE;
  }
  /* there are no values left or there are no values */
  return END_DOUBLE;
}

static inline
double qp_channel_series_double_prev(qp_channel_t c)
{
  double *array;
  struct qp_channel_series *cs;
  ASSERT(c);
  ASSERT(c->form == QP_CHANNEL_FORM_SERIES ||
      c->form == QP_CHANNEL_FORM_FUNC);
  ASSERT(c->value_type == QP_TYPE_DOUBLE);
  ASSERT(*(c->series.ref_count) > 0);

  ASSERT(c->series.arrays);

  cs = &c->series;

  --(cs->array_current_index);

  array = (double *) qp_dllist_val(cs->arrays);

  if(array)
  {
    if(cs->array_current_index != (size_t) -1)
    {
      return array[cs->array_current_index];
    }
    else
    {
      array = (double *) qp_dllist_prev(cs->arrays);
      if(array)
      {
        cs->array_current_index = ARRAY_LENGTH - 1;
        return array[cs->array_current_index];
      }
    }
  }

  return END_DOUBLE;
}