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