File: ArrayField.h

package info (click to toggle)
quickplot 0.8.6-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,548 kB
  • ctags: 1,019
  • sloc: cpp: 10,052; sh: 7,597; makefile: 176
file content (162 lines) | stat: -rw-r--r-- 3,896 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
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */

#include <stdlib.h>
#include <errno.h>
#include <string.h>

/* The data type the quickplot uses to plot is a kind of float set in
 * value_t.h.  The type that the Field stores the values as can be any
 * type, which may save memory in most cases.
 */
template<class T> class ArrayField : public Field
{
 public:

  ArrayField(Source *source, count_t numberOfValues);
  virtual ~ArrayField(void);

  // read the list and advance the pointer to the next value in the
  // series.
  inline value_t read(void)
    {
      return static_cast<value_t>(value[index++]);
    }
  
  // find the maximum values in the range of indexes
  // indexing starts at 0.
  inline value_t max(count_t i_max=((count_t) -1), count_t i_min=0)
    {
      value_t Max = -MAXVALUE;
      if(i_max == ((count_t) -1))
        i_max = _numberOfValues - 1;
      count_t i;
      for(i=i_min;i<=i_max;i++)
        if(value[i] > Max)
          Max = value[i];
      return Max;
    }
  
  // find the minimum values in the range of indexes
  // indexing starts at 0.
  inline value_t min(count_t i_max=((count_t) -1), count_t i_min=0)
    {
      value_t Min = MAXVALUE;
      if(i_max == ((count_t) -1))
        i_max = _numberOfValues - 1;
      count_t i;
      for(i=i_min;i<=i_max;i++)
        if(value[i] < Min)
          Min = value[i];
      return Min;
    }

  // move the pointer to the first value.
  inline void rewind(void) { index = 0; }

  void write(T t);

  struct Dequeuer
  {
    count_t index, numberOfValuesMinus1;
  };

  
  void *makeDequeuer(count_t number_of_values);
  void destroyDequeuer(void *dequeuer);

  // The FieldReader object can use this to read.
  inline value_t read(void *dequeuer)
    {
      struct Dequeuer *d = (struct Dequeuer *) dequeuer;
      value_t val = static_cast<value_t>(value[d->index]);
      if(d->index < d->numberOfValuesMinus1)
        (d->index)++;
      return val;
    }
  
  // read backwards through the list.
  virtual value_t readBack(void *dequeuer)
    {
     struct Dequeuer *d = (struct Dequeuer *) dequeuer;
     value_t val = static_cast<value_t>(value[d->index]);
     if(d->index > 0)
       (d->index)--;
      return val;
    }

  // The FieldReader object can use this to rewind.
  inline void rewind(void *dequeuer)
    {
      ((struct Dequeuer *) dequeuer)->index = 0;
    }
  
private:
  
  // Index to the element to be read or written.
  count_t index;
  T *value;
};

template<class T>
ArrayField<T>::ArrayField(Source *source, count_t NumberOfValues):
  Field(source)
{
  value = static_cast<T*>(malloc(sizeof(T) * NumberOfValues));
  if(!value)
  {
    snprintf(errorStr, ERRORSTR_LENGTH,
             "Quickplot ERROR: malloc(%ld) failed: "
             "system error number %d: %s",
             sizeof(T) * NumberOfValues, errno, strerror(errno));
    error = 1;
  }
  // We assume that this many values will be written.
  _numberOfValues = NumberOfValues;
  index = 0;
}


template<class T>
ArrayField<T>::~ArrayField(void)
{
  if(value)
    free(value);
}


template<class T>
void ArrayField<T>::write(T x_in)
{
  // You can't write more than _numberOfValues values.
  value[index++] = x_in;
  
  // assuming T is not the same type as value_t
  value_t x = x_in; // convert to value type.
  if(x > _max)
    _max = x;
  else if(_isIncreasing)
    _isIncreasing = false;

  if(x < _min)
    _min = x;
  else if(_isDecreasing)
    _isDecreasing = false;
}

template<class T>
void *ArrayField<T>::makeDequeuer(count_t number_of_values)
{
  struct Dequeuer *d = (struct Dequeuer *) malloc(sizeof(struct Dequeuer));
  d->numberOfValuesMinus1 =
    ((number_of_values<_numberOfValues)?number_of_values:_numberOfValues)
    -1;
  d->index = 0;
  return (void *) d;
}

template<class T>
void ArrayField<T>::destroyDequeuer(void *dequeuer)
{
  free(dequeuer);
}