aboutsummaryrefslogtreecommitdiff
path: root/src/task.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/task.h')
-rw-r--r--src/task.h78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/task.h b/src/task.h
new file mode 100644
index 0000000..381f628
--- /dev/null
+++ b/src/task.h
@@ -0,0 +1,78 @@
+#pragma once
+
+/**
+ * Representation of a task in the system
+ */
+struct Task {
+ const char *name;
+ int id;
+ int priority;
+ int burst;
+};
+
+#ifdef __cplusplus
+template <typename T> struct Queue {
+ struct Item {
+ Item(T *node) : node(node) {}
+ T *node;
+ Item *next = nullptr;
+
+ [[nodiscard]] bool
+ operator==(const T *other) const
+ {
+ return node == other;
+ }
+ };
+
+ ~Queue() noexcept
+ {
+ for (auto *it = head; it != nullptr;) {
+ auto *current = it;
+ it = it->next;
+ delete current->node;
+ delete current;
+ }
+ }
+
+ /// Insert item at the end of the queue
+ void
+ insert(T *item)
+ {
+ if (head == nullptr) {
+ head = new Item(item);
+ tail = head;
+ }
+ else {
+ tail->next = new Item(item);
+ tail = tail->next;
+ }
+ }
+
+ void
+ remove(T *item)
+ {
+ if (head == nullptr) return;
+ if (item == head->node) {
+ auto *it = head;
+ head = head->next;
+ if (*tail == item) tail = nullptr;
+ delete it;
+ return;
+ }
+
+ Item *prev = nullptr;
+ for (auto *it = head; it != nullptr; it = it->next) {
+ if (it->node == item) {
+ if (prev) { prev->next = it->next; }
+ if (tail == it) { tail = prev; }
+ delete it;
+ return;
+ }
+ prev = it;
+ }
+ }
+
+ Item *head = nullptr;
+ Item *tail = nullptr;
+};
+#endif