#include <Arduino.h>
template <typename K, typename V>
class SimpleMap { // # include <map> from STL
private:
struct Node {
K key;
V value;
Node* next;
Node(const K& k, const V& v) : key(k), value(v), next(nullptr) {}
};
Node* head;
public:
SimpleMap() : head(nullptr) {}
~SimpleMap() {
clear();
}
void insert(const K& key, const V& value) {
Node* newNode = new Node(key, value);
newNode->next = head;
head = newNode;
}
bool find(const K& key, V& value) const {
Node* current = head;
while (current != nullptr) {
if (current->key == key) {
value = current->value;
return true;
}
current = current->next;
}
return false;
}
void remove(const K& key) {
Node* current = head;
Node* prev = nullptr;
while (current != nullptr && current->key != key) {
prev = current;
current = current->next;
}
if (current == nullptr) {
return; // Key not found
}
if (prev == nullptr) {
head = current->next;
} else {
prev->next = current->next;
}
delete current;
}
void clear() {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
bool containsKey(const K& key) const {
Node* current = head;
while (current != nullptr) {
if (current->key == key) {
return true;
}
current = current->next;
}
return false;
}
bool containsValue(const V& value) const {
Node* current = head;
while (current != nullptr) {
if (current->value == value) {
return true;
}
current = current->next;
}
return false;
}
size_t size() const {
size_t count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
bool isEmpty() const {
return head == nullptr;
}
void print() const {
Node* current = head;
while (current != nullptr) {
Serial.print(current->key);
Serial.print(" -> ");
Serial.println(current->value);
current = current->next;
}
}
};
// Einfache Queue-Implementierung
template <typename T, size_t SIZE>
class SimpleQueue { // # include <QueueList.h>
public:
SimpleQueue() : front(0), rear(0), itemCount(0) {}
void push(const T& item) {
if (itemCount < SIZE) {
data[rear] = item;
rear = (rear + 1) % SIZE;
itemCount++;
}
}
T pop() {
if (itemCount > 0) {
T item = data[front];
front = (front + 1) % SIZE;
itemCount--;
return item;
}
return T(); // Return default-constructed item if the queue is empty
}
bool isEmpty() const {
return itemCount == 0;
}
private:
T data[SIZE];
size_t front;
size_t rear;
size_t itemCount;
};
#include <string.h>
struct EmployeeData {
String name;
boolean keyNumber1;
boolean keyNumber2;
boolean keyNumber3;
boolean keyNumber4;
boolean keyNumber5;
boolean keyNumber6;
boolean keyNumber7;
boolean keyNumber8;
boolean keyNumber9;
};
const int maxKeyCount = 50;
SimpleMap<unsigned int, unsigned int> keyMap;
SimpleMap<unsigned int, EmployeeData> employeeMap;
SimpleQueue<const char*, maxKeyCount> getRowQueue(char string[]) {
SimpleQueue<const char*, maxKeyCount> resultQueue;
const char* rowString = strtok(string, "\n");
while (rowString != NULL) {
resultQueue.push(rowString);
rowString = strtok(NULL, "\n");
}
return resultQueue;
}
void insertInKeyMap(const char* rowString) {
const char* keyId = strtok((char*)rowString, ";");
const char* keyNumber = strtok(NULL, ";");
keyMap.insert(atoi(keyId), atoi(keyNumber));
}
void insertInEmplyeeMap(const char* rowString) {
const char* employeeId = strtok((char*)rowString, ";");
const char* employeeName = strtok((char*)rowString, ";");
const char* key1 = strtok((char*)rowString, ";");
const char* key2 = strtok((char*)rowString, ";");
const char* key3 = strtok((char*)rowString, ";");
const char* key4 = strtok((char*)rowString, ";");
const char* key5 = strtok((char*)rowString, ";");
const char* key6 = strtok((char*)rowString, ";");
const char* key7 = strtok((char*)rowString, ";");
const char* key8 = strtok((char*)rowString, ";");
const char* key9 = strtok((char*)rowString, ";");
EmployeeData employeeData = {
.name = employeeName,
.keyNumber1 = atoi(key1),
.keyNumber2 = atoi(key2),
.keyNumber3 = atoi(key3),
.keyNumber4 = atoi(key4),
.keyNumber5 = atoi(key5),
.keyNumber6 = atoi(key6),
.keyNumber7 = atoi(key7),
.keyNumber8 = atoi(key8),
.keyNumber9 = atoi(key9),
};
employeeMap.insert(atoi(employeeId), employeeData);
}
void setup() {
Serial.begin(9600);
char keyInputString[] = "1;2\n3;4\n5;6";
SimpleQueue<const char*, maxKeyCount> keyQueue = getRowQueue(keyInputString);
while (!keyQueue.isEmpty()) {
insertInKeyMap(keyQueue.pop());
}
keyMap.print();
}
void loop() {
// Hier kann Ihr Hauptprogrammcode stehen
}