summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JSupport/JSUList.cpp
diff options
context:
space:
mode:
authorLuke Street <luke@street.dev>2026-03-01 15:35:36 -0700
committerGitHub <noreply@github.com>2026-03-01 14:35:36 -0800
commit9649319ec4926457ef85e6094f8207474c977c2d (patch)
tree1c5cd4c7cc4c1aee03f28921213da47f2775444d /libs/JSystem/src/JSupport/JSUList.cpp
parent6e149819e13b1f70ecbf8a4c66b030c17ffed2bb (diff)
Reorganize library code into `libs/` (#3119)
* Reorganize files into libs/{dolphin,JSystem,PowerPC_EABI_Support,revolution,TRK_MINNOW_DOLPHIN} * Update configure.py and project.py for new libs structure * Refactor `#include <dolphin/x.h>` -> `<x.h>` * Remove `__REVOLUTION_SDK__` forwards from dolphin * Fix dolphin/ references in revolution * Wrap `#include <dolphin.h>` in `!__REVOLUTION_SDK__` * Always build TRK against dolphin headers * Resolve revolution SDK header resolution issues
Diffstat (limited to 'libs/JSystem/src/JSupport/JSUList.cpp')
-rw-r--r--libs/JSystem/src/JSupport/JSUList.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/libs/JSystem/src/JSupport/JSUList.cpp b/libs/JSystem/src/JSupport/JSUList.cpp
new file mode 100644
index 0000000000..516b37bb7b
--- /dev/null
+++ b/libs/JSystem/src/JSupport/JSUList.cpp
@@ -0,0 +1,155 @@
+#include "JSystem/JSystem.h" // IWYU pragma: keep
+
+#include "JSystem/JSupport/JSUList.h"
+
+JSUPtrLink::JSUPtrLink(void* object) {
+ mList = NULL;
+ mObject = object;
+ mPrev = NULL;
+ mNext = NULL;
+}
+
+JSUPtrLink::~JSUPtrLink() {
+ if (mList != NULL) {
+ mList->remove(this);
+ }
+}
+
+JSUPtrList::JSUPtrList(bool init) {
+ if (init) {
+ initiate();
+ }
+}
+
+JSUPtrList::~JSUPtrList() {
+ JSUPtrLink* node = mHead;
+ for (int i = 0; i < mLength; i++) {
+ node->mList = NULL;
+ node = node->mNext;
+ }
+}
+
+void JSUPtrList::initiate() {
+ mHead = NULL;
+ mTail = NULL;
+ mLength = 0;
+}
+
+void JSUPtrList::setFirst(JSUPtrLink* first) {
+ first->mList = this;
+ first->mPrev = NULL;
+ first->mNext = NULL;
+ mTail = first;
+ mHead = first;
+ mLength = 1;
+}
+
+bool JSUPtrList::append(JSUPtrLink* ptr) {
+ bool result = ptr->mList == NULL;
+ if (!result) {
+ result = ptr->mList->remove(ptr);
+ }
+
+ if (result) {
+ if (mLength == 0) {
+ setFirst(ptr);
+ } else {
+ ptr->mList = this;
+ ptr->mPrev = mTail;
+ ptr->mNext = NULL;
+ mTail->mNext = ptr;
+ mTail = ptr;
+ mLength++;
+ }
+ }
+
+ return result;
+}
+
+bool JSUPtrList::prepend(JSUPtrLink* ptr) {
+ bool result = ptr->mList == NULL;
+ if (!result) {
+ result = ptr->mList->remove(ptr);
+ }
+
+ if (result) {
+ if (mLength == 0) {
+ setFirst(ptr);
+ } else {
+ ptr->mList = this;
+ ptr->mPrev = NULL;
+ ptr->mNext = mHead;
+ mHead->mPrev = ptr;
+ mHead = ptr;
+ mLength++;
+ }
+ }
+
+ return result;
+}
+
+bool JSUPtrList::insert(JSUPtrLink* before, JSUPtrLink* ptr) {
+ if (before == mHead) {
+ return prepend(ptr);
+ } else if (before == NULL) {
+ return append(ptr);
+ }
+
+ if (before->mList != this) {
+ return false;
+ }
+
+ bool result = ptr->mList == NULL;
+ if (!result) {
+ result = ptr->mList->remove(ptr);
+ }
+
+ if (result) {
+ JSUPtrLink* prev = before->mPrev;
+ ptr->mList = this;
+ ptr->mPrev = prev;
+ ptr->mNext = before;
+ prev->mNext = ptr;
+ before->mPrev = ptr;
+ mLength++;
+ }
+
+ return result;
+}
+
+bool JSUPtrList::remove(JSUPtrLink* ptr) {
+ bool is_parent = (ptr->mList == this);
+ if (is_parent) {
+ if (mLength == 1) {
+ mHead = NULL;
+ mTail = NULL;
+ } else if (ptr == mHead) {
+ ptr->mNext->mPrev = NULL;
+ mHead = ptr->mNext;
+ } else if (ptr == mTail) {
+ ptr->mPrev->mNext = NULL;
+ mTail = ptr->mPrev;
+ } else {
+ ptr->mPrev->mNext = ptr->mNext;
+ ptr->mNext->mPrev = ptr->mPrev;
+ }
+
+ ptr->mList = NULL;
+ mLength--;
+ }
+
+ return is_parent;
+}
+
+JSUPtrLink* JSUPtrList::getNthLink(u32 index) const {
+ if (index >= mLength) {
+ return NULL;
+ }
+
+ JSUPtrLink* node = mHead;
+ for (u32 i = 0; i < index; i++) {
+ node = node->mNext;
+ }
+
+ return node;
+}