File: LinearField.h

package info (click to toggle)
quickplot 0.8.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 1,019
  • sloc: cpp: 10,051; sh: 7,597; makefile: 176
file content (107 lines) | stat: -rw-r--r-- 2,679 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
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
class Source;
class Field;

class LinearField : public Field
{
 public:

  // value = count * step + offset
  inline LinearField(Source *source,
                     value_t step_in=(value_t)1.0,
                     value_t offset_in=(value_t)0.0):
    Field(source),
    step(step_in), offset(offset_in)
    {
      _isIncreasing = (step > 0.0)? true: false;
      _numberOfValues = MAXCOUNT;
      _max = (_numberOfValues-1)*step + offset;
      _min = offset;
      // assuming that count_t is unsigned so that -1 is the maxmum
      // value
      index = 0;

      setLabel("linear field");
      char label[64];
      snprintf(label, 64, "step=%g offset=%g", step, offset);
      setName(label);
    }
  
  // read the list and advance the pointer to the next value in the
  // series.
  inline value_t read(void)
    {
      return step*(index++) + offset;
    }

  // find the minimum 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)
    {
      if(i_max == ((count_t) -1))
        i_max = _numberOfValues - 1;
      if(step > (value_t) 0.0)
        return step*(i_max) + offset;
      else
        return step*(i_min) + offset;
    }
  
  // find the maximum 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)
    {
      if(i_max == ((count_t) -1))
        i_max = _numberOfValues - 1;
      if(step < (value_t) 0.0)
        return step*(i_max) + offset;
      else
        return step*(i_min) + offset;
    }

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

  struct Dequeuer
  {
    count_t index, numberOfValuesMinus1;
  };

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

  
  // You can use this to read.
  inline value_t read(void *dequeuer)
    {
      struct Dequeuer *d = (struct Dequeuer *) dequeuer;
      value_t val = step*(d->index) + offset;
      if(d->index < d->numberOfValuesMinus1)
        (d->index)++;
      return val;
    }

  // You can use this to read backwards.
  inline value_t readBack(void *dequeuer)
    {
      struct Dequeuer *d = (struct Dequeuer *) dequeuer;
      value_t val = step*(d->index) + offset;
      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.
  count_t index;
  value_t step, offset;

};