-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLinkedMap.h
131 lines (109 loc) · 3.07 KB
/
LinkedMap.h
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
#ifndef OTF_LINKEDMAP_H
#define OTF_LINKEDMAP_H
#define KEY_MAX_LENGTH 100
#if defined(ARDUINO)
#include <Arduino.h>
#else
#include <string.h>
#endif
namespace OTF {
template<class T>
class LinkedMapNode;
template<class T>
class LinkedMap {
friend class OpenThingsFramework;
friend class Response;
private:
LinkedMapNode<T> *head = nullptr;
LinkedMapNode<T> *tail = nullptr;
LinkedMapNode<T> *_findNode(const char *key, bool keyInFlash = false) const {
LinkedMapNode<T> *node = head;
while (node != nullptr) {
#if defined(ARDUINO)
if ((keyInFlash ? strcmp_P(node->key, key) : strcmp(node->key, key)) == 0) {
#else
if (strcmp(node->key, key) == 0) {
#endif
return node;
}
node = node->next;
}
// Indicate the key could not be found.
return nullptr;
}
void _add(LinkedMapNode<T> *node, bool keyInFlash = false) {
LinkedMapNode<T> *existingNode = _findNode(node->key, keyInFlash);
if (existingNode != nullptr) {
// Update the value of the existing node.
existingNode->value = node->value;
delete node;
} else {
// Add the new node to the end of the list.
if (head == nullptr) {
head = node;
tail = head;
} else {
tail->next = node;
tail = tail->next;
}
}
}
T _find(const char *key, bool keyInFlash = false) const {
LinkedMapNode<T> *node = _findNode(key, keyInFlash);
return node != nullptr ? node->value : nullptr;
}
public:
~LinkedMap() {
LinkedMapNode<T> *node = head;
while (node != nullptr) {
LinkedMapNode<T> *next = node->next;
delete node;
node = next;
}
}
void add(const char *key, T value) {
_add(new LinkedMapNode<T>(key, value));
}
#if defined(ARDUINO)
void add(const __FlashStringHelper *key, T value) {
_add(new LinkedMapNode<T>(key, value), true);
}
#endif
T find(const char *key) const {
return _find(key, false);
}
#if defined(ARDUINO)
T find(const __FlashStringHelper *key) const {
return _find((char *) key, true);
}
#endif
};
template<class T>
class LinkedMapNode {
private:
/** Indicates if the key was copied into RAM from flash memory and needs to be freed when the object is destroyed. */
bool keyFromFlash = false;
public:
const char *key = nullptr;
T value;
LinkedMapNode<T> *next = nullptr;
// LinkedMapNode(const __FlashStringHelper *key, T value) {
// keyFromFlash = true;
// char *_key = new char[KEY_MAX_LENGTH];
// strncpy_P(_key, (char *) key, KEY_MAX_LENGTH);
// this->key = (const char *) _key;
// this->value = value;
// }
LinkedMapNode(const char *key, T value) {
this->key = key;
this->value = value;
}
~LinkedMapNode() {
// Delete the key if it was copied into RAM from flash memory.
if (keyFromFlash) {
delete key;
}
}
};
}// namespace OTF
#endif