interval.cpp
1.69 KB
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
#include "interval.h"
#include <assert.h>
namespace Shared {
Interval::Interval() :
m_numberOfElements(0),
m_start(0),
m_end(0),
m_step(0),
m_needCompute(false)
{
}
int Interval::numberOfElements() {
computeElements();
return m_numberOfElements;
}
void Interval::deleteElementAtIndex(int index) {
m_numberOfElements--;
for (int k = index; k < m_numberOfElements; k++) {
m_intervalBuffer[k] = m_intervalBuffer[k+1];
}
m_intervalBuffer[m_numberOfElements] = 0.0f;
}
double Interval::element(int i) {
assert(i >= 0 && i < numberOfElements());
computeElements();
return m_intervalBuffer[i];
}
double Interval::start() {
return m_start;
}
double Interval::end() {
return m_end;
}
double Interval::step() {
return m_step;
}
void Interval::setStart(double f) {
m_start = f;
m_needCompute = true;
}
void Interval::setEnd(double f) {
m_end = f;
m_needCompute = true;
}
void Interval::setStep(double f) {
m_step = f;
m_needCompute = true;
}
void Interval::setElement(int i, double f) {
assert(i <= numberOfElements() && i < k_maxNumberOfElements);
computeElements();
m_intervalBuffer[i] = f;
if (i == numberOfElements()) {
m_numberOfElements++;
}
}
void Interval::computeElements() {
if (!m_needCompute) {
return;
}
if ( m_start > m_end) {
m_numberOfElements = 0;
} else {
m_numberOfElements = m_step > 0 ? 1 + (m_end - m_start)/m_step : k_maxNumberOfElements;
m_numberOfElements = m_numberOfElements > k_maxNumberOfElements || m_numberOfElements < 0 ? k_maxNumberOfElements : m_numberOfElements;
}
for (int i = 0; i < m_numberOfElements; i += 1) {
m_intervalBuffer[i] = m_start + i * m_step;
}
m_needCompute = false;
}
}