Files
tp/libs/JSystem/JSupport/JSUList.cpp
T

174 lines
4.0 KiB
C++
Raw Normal View History

#include "JSystem/JSupport/JSUList/JSUList.h"
2020-11-29 11:53:10 -05:00
#include "global.h"
// #include "JSupport/asm/func_802DBDFC.s"
JSUPtrLink::JSUPtrLink(void* object) {
this->mList = NULL;
this->mObject = object;
this->mPrev = NULL;
this->mNext = NULL;
2020-11-29 11:53:10 -05:00
}
// #include "JSupport/asm/func_802DBE14.s"
JSUPtrLink::~JSUPtrLink() {
if (this->mList != NULL) {
this->mList->remove(this);
2020-11-29 11:53:10 -05:00
}
}
//
//
//
// #include "JSupport/asm/func_802DBE74.s"
JSUPtrList::JSUPtrList(bool init) {
if (init) {
2020-11-29 11:53:10 -05:00
this->initiate();
}
}
// #include "JSupport/asm/func_802DBEAC.s"
JSUPtrList::~JSUPtrList() {
JSUPtrLink* node = this->mHead;
2020-11-29 11:53:10 -05:00
s32 removed = 0;
while (this->mLength > removed) {
node->mList = NULL;
node = node->getNext();
2020-11-29 11:53:10 -05:00
removed += 1;
}
}
// #include "JSupport/asm/func_802DBF14.s"
void JSUPtrList::initiate() {
this->mHead = NULL;
this->mTail = NULL;
this->mLength = 0;
2020-11-29 11:53:10 -05:00
}
// #include "JSupport/asm/func_802DBF28.s"
void JSUPtrList::setFirst(JSUPtrLink* first) {
first->mList = this;
first->mPrev = NULL;
first->mNext = NULL;
this->mTail = first;
this->mHead = first;
this->mLength = 1;
2020-11-29 11:53:10 -05:00
}
// #include "JSupport/asm/func_802DBF4C.s"
bool JSUPtrList::append(JSUPtrLink* ptr) {
JSUPtrList* list = ptr->mList;
bool result = (NULL == list);
2020-11-29 11:53:10 -05:00
if (!result) {
result = list->remove(ptr);
}
if (result) {
if (this->mLength == 0) {
2020-11-29 11:53:10 -05:00
this->setFirst(ptr);
} else {
ptr->mList = this;
ptr->mPrev = this->mTail;
ptr->mNext = NULL;
this->mTail->mNext = ptr;
this->mTail = ptr;
this->mLength++;
}
2020-11-29 11:53:10 -05:00
}
return result;
}
// #include "JSupport/asm/func_802DBFF0.s"
bool JSUPtrList::prepend(JSUPtrLink* ptr) {
JSUPtrList* list = ptr->mList;
bool result = (NULL == list);
2020-11-29 11:53:10 -05:00
if (!result) {
result = list->remove(ptr);
}
if (result) {
if (this->mLength == 0) {
2020-11-29 11:53:10 -05:00
this->setFirst(ptr);
} else {
ptr->mList = this;
ptr->mPrev = NULL;
ptr->mNext = this->mHead;
this->mHead->mPrev = ptr;
this->mHead = ptr;
this->mLength++;
}
2020-11-29 11:53:10 -05:00
}
return result;
}
// #include "JSupport/asm/func_802DC094.s"
bool JSUPtrList::insert(JSUPtrLink* before, JSUPtrLink* ptr) {
if (before == this->mHead) {
return this->prepend(ptr);
} else if (before == NULL) {
2020-11-29 11:53:10 -05:00
return this->append(ptr);
}
if (before->mList != this) {
2020-11-29 11:53:10 -05:00
return false;
}
bool result = (NULL == ptr->mList);
2020-11-29 11:53:10 -05:00
if (!result) {
result = ptr->mList->remove(ptr);
2020-11-29 11:53:10 -05:00
}
if (result) {
JSUPtrLink* prev = before->mPrev;
ptr->mList = this;
ptr->mPrev = prev;
ptr->mNext = before;
prev->mNext = ptr;
before->mPrev = ptr;
this->mLength++;
2020-11-29 11:53:10 -05:00
}
return result;
}
// #include "JSupport/asm/func_802DC15C.s"
bool JSUPtrList::remove(JSUPtrLink* ptr) {
bool is_parent = (ptr->mList == this);
2020-11-29 11:53:10 -05:00
if (is_parent) {
if (this->mLength == 1) {
this->mHead = NULL;
this->mTail = NULL;
} else if (ptr == this->mHead) {
ptr->mNext->mPrev = NULL;
this->mHead = ptr->mNext;
} else if (ptr == this->mTail) {
ptr->mPrev->mNext = NULL;
this->mTail = ptr->mPrev;
2020-11-29 11:53:10 -05:00
} else {
ptr->mPrev->mNext = ptr->mNext;
ptr->mNext->mPrev = ptr->mPrev;
2020-11-29 11:53:10 -05:00
}
ptr->mList = NULL;
this->mLength--;
2020-11-29 11:53:10 -05:00
}
return is_parent;
}
// #include "JSupport/asm/func_802DC20C.s"
JSUPtrLink* JSUPtrList::getNthLink(u32 index) const {
if (index >= this->mLength) {
return NULL;
2020-11-29 11:53:10 -05:00
}
JSUPtrLink* node = this->mHead;
for (u32 i = 0; i < index; i++) {
node = node->getNext();
2020-11-29 11:53:10 -05:00
}
return node;
}