cartesian_function_store.cpp
3.28 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
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
#include "cartesian_function_store.h"
extern "C" {
#include <assert.h>
#include <stddef.h>
}
#include <ion.h>
namespace Graph {
constexpr int CartesianFunctionStore::k_maxNumberOfFunctions;
constexpr KDColor CartesianFunctionStore::k_defaultColors[k_maxNumberOfFunctions];
constexpr const char * CartesianFunctionStore::k_functionNames[k_maxNumberOfFunctions];
CartesianFunctionStore::CartesianFunctionStore() :
Shared::FunctionStore()
{
addEmptyFunction();
}
uint32_t CartesianFunctionStore::storeChecksum() {
size_t dataLengthInBytes = k_maxNumberOfFunctions*sizeof(uint32_t);
assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
uint32_t checksums[k_maxNumberOfFunctions];
for (int i = 0; i < k_maxNumberOfFunctions; i++) {
checksums[i] = m_functions[i].checksum();
}
return Ion::crc32((uint32_t *)checksums, dataLengthInBytes/sizeof(uint32_t));
}
CartesianFunction * CartesianFunctionStore::functionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
return &m_functions[i];
}
CartesianFunction * CartesianFunctionStore::activeFunctionAtIndex(int i) {
return (CartesianFunction *)Shared::FunctionStore::activeFunctionAtIndex(i);
}
CartesianFunction * CartesianFunctionStore::definedFunctionAtIndex(int i) {
return (CartesianFunction *)Shared::FunctionStore::definedFunctionAtIndex(i);
}
CartesianFunction * CartesianFunctionStore::addEmptyFunction() {
assert(m_numberOfFunctions < k_maxNumberOfFunctions);
const char * name = firstAvailableName();
KDColor color = firstAvailableColor();
CartesianFunction addedFunction(name, color);
m_functions[m_numberOfFunctions] = addedFunction;
CartesianFunction * result = &m_functions[m_numberOfFunctions];
m_numberOfFunctions++;
return result;
}
void CartesianFunctionStore::removeFunction(Shared::Function * f) {
int i = 0;
while (&m_functions[i] != f && i < m_numberOfFunctions) {
i++;
}
assert(i>=0 && i<m_numberOfFunctions);
m_numberOfFunctions--;
for (int j = i; j<m_numberOfFunctions; j++) {
m_functions[j] = m_functions[j+1];
}
CartesianFunction emptyFunction("", KDColorBlack);
m_functions[m_numberOfFunctions] = emptyFunction;
}
int CartesianFunctionStore::maxNumberOfFunctions() {
return k_maxNumberOfFunctions;
}
char CartesianFunctionStore::symbol() const {
return 'x';
}
const char * CartesianFunctionStore::firstAvailableName() {
for (int k = 0; k < k_maxNumberOfFunctions; k++) {
int j = 0;
while (j < m_numberOfFunctions) {
if (m_functions[j].name() == k_functionNames[k]) {
break;
}
j++;
}
if (j == m_numberOfFunctions) {
return k_functionNames[k];
}
}
return k_functionNames[0];
}
const KDColor CartesianFunctionStore::firstAvailableColor() {
for (int k = 0; k < k_maxNumberOfFunctions; k++) {
int j = 0;
while (j < m_numberOfFunctions) {
if (m_functions[j].color() == k_defaultColors[k]) {
break;
}
j++;
}
if (j == m_numberOfFunctions) {
return k_defaultColors[k];
}
}
return k_defaultColors[0];
}
void CartesianFunctionStore::removeAll() {
for (int i = 0; i < m_numberOfFunctions; i++) {
CartesianFunction emptyFunction("", KDColorBlack);
m_functions[i] = emptyFunction;
}
m_numberOfFunctions = 0;
addEmptyFunction();
}
}