NeKernel dev
Loading...
Searching...
No Matches
MutableArray.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6#pragma once
7
9#include <NeKit/Array.h>
10#include <NeKit/Config.h>
11
12#define RTL_TRY_FIND_NODE(NAME, NODE) \
13 auto* NAME = NODE; \
14 while (NAME) { \
15 if (NAME->fIndex == Index) return NAME->fVal; \
16 NAME = NAME->fNext; \
17 }
18
19#define RTL_TRY_FIND_NODE2(NAME, NODE) \
20 auto* NAME = NODE; \
21 while (NAME) { \
22 if (NAME->fIndex == Index) return Ref<T>{NAME->fVal}; \
23 NAME = NAME->fNext; \
24 }
25
26#define RTL_TRY_REMOVE_NODE(NODE) \
27 if (NODE && NODE->fIndex == Index) { \
28 NODE->fUsed = false; \
29 NODE->fIndex = 0; \
30 \
31 return true; \
32 }
33
34// FIXME: this is a shitty algorithm, because it is memory heavy.
35// Remove and occurences of that, and remove that class.
36namespace Kernel {
37template <typename T>
38class MutableArray;
39
40template <typename T, T _PlaceHolderValue>
41class NullableMutableArray;
42
43template <typename T>
45 public:
48 Boolean fUsed{false};
49
52};
53
54template <typename T, T _PlaceHolderValue>
56 public:
57 // explicit this.
59
60 /*
61 * We free all the nodes allocated by the array
62 * and store the next one inside "NextIt"
63 */
64
66 auto* It = fFirstNode;
67 MutableLinkedList<T>* NextIt = nullptr;
68
69 while (It) {
70 NextIt = It->fNext;
71 delete It;
72
73 It = NextIt;
74 }
75 }
76
79
80 operator bool() { return Count() > 1; }
81
82 public:
83 T operator[](SizeT Index) const {
86
87 return _PlaceHolderValue;
88 }
89
90 SizeT Count() const { return fNodeCount; }
91
92 public:
96
97 return false;
98 }
99
100 Boolean Add(const T val) {
101 auto* iterationNode = fFirstNode;
102 MUST_PASS(iterationNode);
103
104 while (iterationNode) {
105 if (!iterationNode->fUsed) {
106 iterationNode->fVal = val;
107 iterationNode->fIndex = 0;
108
109 iterationNode->fUsed = true;
110
111 ++fNodeCount;
112
113 return true;
114 }
115
116 iterationNode = iterationNode->fNext;
117 }
118
119 return false;
120 }
121
122 private:
123 /* Avoid useless lookups */
126
127 /* Number of nodes inside of this dynamic array. */
129
130 private:
131 // don't remove that
133};
134
135template <typename T>
136class MutableArray : public NullableMutableArray<voidPtr, nullptr> {
137 public:
138 // explicit this.
139 explicit MutableArray() = default;
140 virtual ~MutableArray() = default;
141
143
144 public:
145 Boolean Add(const T val) {
146 auto* iterationNode = fFirstNode;
147
148 if (!iterationNode) {
150 iterationNode = fFirstNode;
151 }
152
153 MUST_PASS(iterationNode);
154
155 while (iterationNode) {
156 if (!iterationNode->fUsed) {
157 iterationNode->fVal = val;
158 iterationNode->fIndex = 0;
159
160 iterationNode->fUsed = true;
161
162 ++fNodeCount;
163
164 return true;
165 }
166
167 iterationNode = iterationNode->fNext;
168 }
169
170 return false;
171 }
172
173 public:
174 Ref<T> operator[](SizeT Index) const {
177
178 return {};
179 }
180
181 SizeT Count() const { return fNodeCount; }
182
183 bool Contains(T& value) {
185
186 while (first) {
187 if (first->fVal == value && first->fUsed) return true;
188
189 first = first->fNext;
190 }
191
192 return false;
193 }
194
195 private:
196 /* Avoid useless lookups */
199
200 /* Number of nodes inside of this dynamic array. */
202};
203} // namespace Kernel
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
#define MUST_PASS(EXPR)
Definition KernelPanic.h:37
#define RTL_TRY_REMOVE_NODE(NODE)
Definition MutableArray.h:26
#define RTL_TRY_FIND_NODE(NAME, NODE)
Definition MutableArray.h:12
#define RTL_TRY_FIND_NODE2(NAME, NODE)
Definition MutableArray.h:19
Definition MutableArray.h:136
MutableLinkedList< T > * fLastNode
Definition MutableArray.h:197
Boolean Add(const T val)
Definition MutableArray.h:145
bool Contains(T &value)
Definition MutableArray.h:183
Ref< T > operator[](SizeT Index) const
Definition MutableArray.h:174
Kernel::SizeT fNodeCount
Definition MutableArray.h:201
MutableLinkedList< T > * fFirstNode
Definition MutableArray.h:198
SizeT Count() const
Definition MutableArray.h:181
virtual ~MutableArray()=default
Definition MutableArray.h:44
SizeT fIndex
Definition MutableArray.h:47
T fVal
Definition MutableArray.h:46
Boolean fUsed
Definition MutableArray.h:48
MutableLinkedList * fNext
Definition MutableArray.h:51
MutableLinkedList * fPrev
Definition MutableArray.h:50
NullableMutableArray(const NullableMutableArray &)=default
MutableLinkedList< T > * fFirstNode
Definition MutableArray.h:125
T operator[](SizeT Index) const
Definition MutableArray.h:83
SizeT Count() const
Definition MutableArray.h:90
virtual ~NullableMutableArray()
Definition MutableArray.h:65
Boolean Remove(SizeT Index)
Definition MutableArray.h:93
Kernel::SizeT fNodeCount
Definition MutableArray.h:128
NullableMutableArray & operator=(const NullableMutableArray &)=default
MutableLinkedList< T > * fLastNode
Definition MutableArray.h:124
Boolean Add(const T val)
Definition MutableArray.h:100
NullableMutableArray()
Definition MutableArray.h:58
Reference wrapper class. /// =========================================================== ///.
Definition Ref.h:22
UPS inline definitions.
Definition Device.h:12
__SIZE_TYPE__ SizeT
Definition Config.h:60
bool Boolean
Definition Config.h:49