summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/NewKit/MutableArray.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-02 19:38:46 +0200
committerGitHub <noreply@github.com>2025-05-02 19:38:46 +0200
commit997be16e5ac9a68d54882ab69529815860d62955 (patch)
tree19d6129c2d776bb1edc5d4a7325e39ca176c3403 /dev/kernel/NewKit/MutableArray.h
parent618104e74c195d7508a18450524f8ed7f9af8cc6 (diff)
parentb3b4b1ebdcd6adeac914869017c86d892b7a8ced (diff)
Merge pull request #28 from nekernel-org/dev
0.0.2
Diffstat (limited to 'dev/kernel/NewKit/MutableArray.h')
-rw-r--r--dev/kernel/NewKit/MutableArray.h394
1 files changed, 179 insertions, 215 deletions
diff --git a/dev/kernel/NewKit/MutableArray.h b/dev/kernel/NewKit/MutableArray.h
index 40d61495..08c8cbf5 100644
--- a/dev/kernel/NewKit/MutableArray.h
+++ b/dev/kernel/NewKit/MutableArray.h
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
#pragma once
@@ -9,231 +9,195 @@
#include <NewKit/Array.h>
#include <NewKit/Defines.h>
-#define TRY_FIND_NODE(NAME, NODE) \
- auto* NAME = NODE; \
- while (NAME) \
- { \
- if (NAME->fIndex == Index) \
- return NAME->fVal; \
- NAME = NAME->fNext; \
- }
-
-#define TRY_FIND_NODE2(NAME, NODE) \
- auto* NAME = NODE; \
- while (NAME) \
- { \
- if (NAME->fIndex == Index) \
- return Ref<T>{NAME->fVal}; \
- NAME = NAME->fNext; \
- }
+#define TRY_FIND_NODE(NAME, NODE) \
+ auto* NAME = NODE; \
+ while (NAME) { \
+ if (NAME->fIndex == Index) return NAME->fVal; \
+ NAME = NAME->fNext; \
+ }
+
+#define TRY_FIND_NODE2(NAME, NODE) \
+ auto* NAME = NODE; \
+ while (NAME) { \
+ if (NAME->fIndex == Index) return Ref<T>{NAME->fVal}; \
+ NAME = NAME->fNext; \
+ }
#define TRY_REMOVE_NODE(NODE) \
- if (NODE && NODE->fIndex == Index) \
- { \
- NODE->fUsed = false; \
- NODE->fIndex = 0; \
+ if (NODE && NODE->fIndex == Index) { \
+ NODE->fUsed = false; \
+ NODE->fIndex = 0; \
\
- return true; \
- }
+ return true; \
+ }
// FIXME: this is a shitty algorithm, which is consumer hungry.
// Remove and occurences of that, and remove that class.
-namespace Kernel
-{
- template <typename T>
- class MutableArray;
-
- template <typename T, T _PlaceHolderValue>
- class NullableMutableArray;
-
- template <typename T>
- class MutableLinkedList
- {
- public:
- T fVal;
- SizeT fIndex{0};
- Boolean fUsed{false};
-
- MutableLinkedList* fPrev{nullptr};
- MutableLinkedList* fNext{nullptr};
- };
-
- template <typename T, T _PlaceHolderValue>
- class NullableMutableArray
- {
- public:
- // explicit this.
- explicit NullableMutableArray()
- : fFirstNode(new MutableLinkedList<T>())
- {
- }
-
- /*
- * We free all the nodes allocated by the array
- * and store the next one inside "NextIt"
- */
-
- virtual ~NullableMutableArray()
- {
- auto* It = fFirstNode;
- MutableLinkedList<T>* NextIt = nullptr;
-
- while (It)
- {
- NextIt = It->fNext;
- delete It;
-
- It = NextIt;
- }
- }
-
- NullableMutableArray& operator=(const NullableMutableArray&) = default;
- NullableMutableArray(const NullableMutableArray&) = default;
-
- operator bool()
- {
- return Count() > 1;
- }
-
- public:
- T operator[](SizeT Index) const
- {
- TRY_FIND_NODE(first, fFirstNode);
- TRY_FIND_NODE(last, fLastNode);
-
- return _PlaceHolderValue;
- }
-
- SizeT Count() const
- {
- return fNodeCount;
- }
-
- public:
- Boolean Remove(SizeT Index)
- {
- TRY_REMOVE_NODE(fFirstNode);
- TRY_REMOVE_NODE(fLastNode);
-
- return false;
- }
-
- Boolean Add(const T val)
- {
- auto* iterationNode = fFirstNode;
- MUST_PASS(iterationNode);
-
- while (iterationNode)
- {
- if (!iterationNode->fUsed)
- {
- iterationNode->fVal = val;
- iterationNode->fIndex = 0;
-
- iterationNode->fUsed = true;
-
- ++fNodeCount;
-
- return true;
- }
-
- iterationNode = iterationNode->fNext;
- }
-
- return false;
- }
-
- private:
- /* Avoid useless lookups */
- MutableLinkedList<T>* fLastNode{nullptr};
- MutableLinkedList<T>* fFirstNode{nullptr};
-
- /* Number of nodes inside of this dynamic array. */
- Kernel::SizeT fNodeCount{0};
-
- private:
- // don't remove that
- friend MutableArray<T>;
- };
-
- template <typename T>
- class MutableArray : public NullableMutableArray<voidPtr, nullptr>
- {
- public:
- // explicit this.
- explicit MutableArray() = default;
- virtual ~MutableArray() = default;
-
- NE_COPY_DEFAULT(MutableArray)
-
- public:
- Boolean Add(const T val)
- {
- auto* iterationNode = fFirstNode;
-
- if (!iterationNode)
- {
- fFirstNode = new MutableLinkedList<T>();
- iterationNode = fFirstNode;
- }
-
- MUST_PASS(iterationNode);
-
- while (iterationNode)
- {
- if (!iterationNode->fUsed)
- {
- iterationNode->fVal = val;
- iterationNode->fIndex = 0;
-
- iterationNode->fUsed = true;
-
- ++fNodeCount;
-
- return true;
- }
-
- iterationNode = iterationNode->fNext;
- }
-
- return false;
- }
+namespace Kernel {
+template <typename T>
+class MutableArray;
+
+template <typename T, T _PlaceHolderValue>
+class NullableMutableArray;
+
+template <typename T>
+class MutableLinkedList {
+ public:
+ T fVal;
+ SizeT fIndex{0};
+ Boolean fUsed{false};
+
+ MutableLinkedList* fPrev{nullptr};
+ MutableLinkedList* fNext{nullptr};
+};
+
+template <typename T, T _PlaceHolderValue>
+class NullableMutableArray {
+ public:
+ // explicit this.
+ explicit NullableMutableArray() : fFirstNode(new MutableLinkedList<T>()) {}
+
+ /*
+ * We free all the nodes allocated by the array
+ * and store the next one inside "NextIt"
+ */
+
+ virtual ~NullableMutableArray() {
+ auto* It = fFirstNode;
+ MutableLinkedList<T>* NextIt = nullptr;
+
+ while (It) {
+ NextIt = It->fNext;
+ delete It;
+
+ It = NextIt;
+ }
+ }
+
+ NullableMutableArray& operator=(const NullableMutableArray&) = default;
+ NullableMutableArray(const NullableMutableArray&) = default;
+
+ operator bool() { return Count() > 1; }
+
+ public:
+ T operator[](SizeT Index) const {
+ TRY_FIND_NODE(first, fFirstNode);
+ TRY_FIND_NODE(last, fLastNode);
+
+ return _PlaceHolderValue;
+ }
+
+ SizeT Count() const { return fNodeCount; }
+
+ public:
+ Boolean Remove(SizeT Index) {
+ TRY_REMOVE_NODE(fFirstNode);
+ TRY_REMOVE_NODE(fLastNode);
+
+ return false;
+ }
+
+ Boolean Add(const T val) {
+ auto* iterationNode = fFirstNode;
+ MUST_PASS(iterationNode);
+
+ while (iterationNode) {
+ if (!iterationNode->fUsed) {
+ iterationNode->fVal = val;
+ iterationNode->fIndex = 0;
+
+ iterationNode->fUsed = true;
+
+ ++fNodeCount;
+
+ return true;
+ }
+
+ iterationNode = iterationNode->fNext;
+ }
+
+ return false;
+ }
+
+ private:
+ /* Avoid useless lookups */
+ MutableLinkedList<T>* fLastNode{nullptr};
+ MutableLinkedList<T>* fFirstNode{nullptr};
+
+ /* Number of nodes inside of this dynamic array. */
+ Kernel::SizeT fNodeCount{0};
+
+ private:
+ // don't remove that
+ friend MutableArray<T>;
+};
+
+template <typename T>
+class MutableArray : public NullableMutableArray<voidPtr, nullptr> {
+ public:
+ // explicit this.
+ explicit MutableArray() = default;
+ virtual ~MutableArray() = default;
+
+ NE_COPY_DEFAULT(MutableArray)
+
+ public:
+ Boolean Add(const T val) {
+ auto* iterationNode = fFirstNode;
+
+ if (!iterationNode) {
+ fFirstNode = new MutableLinkedList<T>();
+ iterationNode = fFirstNode;
+ }
+
+ MUST_PASS(iterationNode);
+
+ while (iterationNode) {
+ if (!iterationNode->fUsed) {
+ iterationNode->fVal = val;
+ iterationNode->fIndex = 0;
+
+ iterationNode->fUsed = true;
+
+ ++fNodeCount;
+
+ return true;
+ }
+
+ iterationNode = iterationNode->fNext;
+ }
+
+ return false;
+ }
+
+ public:
+ Ref<T> operator[](SizeT Index) const {
+ TRY_FIND_NODE2(first, fFirstNode);
+ TRY_FIND_NODE2(last, fLastNode);
- public:
- Ref<T> operator[](SizeT Index) const
- {
- TRY_FIND_NODE2(first, fFirstNode);
- TRY_FIND_NODE2(last, fLastNode);
-
- return {};
- }
+ return {};
+ }
- SizeT Count() const
- {
- return fNodeCount;
- }
+ SizeT Count() const { return fNodeCount; }
- bool Contains(T& value) noexcept
- {
- MutableLinkedList<T>* first = fFirstNode;
+ bool Contains(T& value) noexcept {
+ MutableLinkedList<T>* first = fFirstNode;
- while (first)
- {
- if (first->fVal == value && first->fUsed)
- return true;
+ while (first) {
+ if (first->fVal == value && first->fUsed) return true;
- first = first->fNext;
- }
+ first = first->fNext;
+ }
- return false;
- }
+ return false;
+ }
- private:
- /* Avoid useless lookups */
- MutableLinkedList<T>* fLastNode{nullptr};
- MutableLinkedList<T>* fFirstNode{nullptr};
+ private:
+ /* Avoid useless lookups */
+ MutableLinkedList<T>* fLastNode{nullptr};
+ MutableLinkedList<T>* fFirstNode{nullptr};
- /* Number of nodes inside of this dynamic array. */
- Kernel::SizeT fNodeCount{0};
- };
-} // namespace Kernel
+ /* Number of nodes inside of this dynamic array. */
+ Kernel::SizeT fNodeCount{0};
+};
+} // namespace Kernel