NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
ut_list.cpp
1#include <nw4r/ut.h>
2
3namespace nw4r {
4namespace ut {
5
6void List_Init(List *list, u16 offset) {
7 list->headObject = nullptr;
8 list->tailObject = nullptr;
9 list->numObjects = 0;
10 list->offset = offset;
11}
12
13void List_Append(List *list, void *obj) {
14 // If only one node is in the list, set it as both head and tail
15 // Else place it at the end
16 if (list->headObject == nullptr) {
17 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
18 node->nextObject = nullptr;
19 node->prevObject = nullptr;
20 list->headObject = obj;
21 list->tailObject = obj;
22 list->numObjects++;
23
24 } else {
25 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
26 node->prevObject = list->tailObject;
27 node->nextObject = nullptr;
28
29 // Link the previous node
30 NW4R_UT_LIST_GET_LINK(*list, list->tailObject)->nextObject = obj;
31
32 // Set the new tail
33 list->tailObject = obj;
34 list->numObjects++;
35 }
36}
37
38void List_Prepend(List *list, void *obj) {
39 // If only one node is in the list, set it as both head and tail
40 // Else place it at the start
41 if (list->headObject == nullptr) {
42 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
43 node->nextObject = nullptr;
44 node->prevObject = nullptr;
45 list->headObject = obj;
46 list->tailObject = obj;
47 list->numObjects++;
48
49 } else {
50 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
51 node->prevObject = nullptr;
52 node->nextObject = list->headObject;
53
54 // Link the following node
55 NW4R_UT_LIST_GET_LINK(*list, list->headObject)->prevObject = obj;
56
57 // Set the new head
58 list->headObject = obj;
59 list->numObjects++;
60 }
61}
62
63void List_Insert(List *list, void *target, void *obj) {
64 // If the target is null, insert the object at the end of the list
65 // Else if the target is the list head, insert the object at the beginning
66 // Else perform regular insertion
67 if (target == nullptr) {
68 List_Append(list, obj);
69
70 } else if (target == list->headObject) {
71 List_Prepend(list, obj);
72
73 } else {
74 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
75 void *prevObj = NW4R_UT_LIST_GET_LINK(*list, target)->prevObject;
76 Link *prevNode = NW4R_UT_LIST_GET_LINK(*list, prevObj);
77
78 // Update the node to be inserted
79 node->prevObject = prevObj;
80 node->nextObject = target;
81
82 // Update its neighbors
83 prevNode->nextObject = obj;
84 NW4R_UT_LIST_GET_LINK(*list, target)->prevObject = obj;
85
86 // Update the list
87 list->numObjects++;
88 }
89}
90
91void List_Remove(List *list, void *obj) {
92 Link *node = NW4R_UT_LIST_GET_LINK(*list, obj);
93
94 // If the node is the list head, set the next node as the new head
95 // Else punch a hole in the list
96 if (node->prevObject == nullptr) {
97 list->headObject = node->nextObject;
98 } else {
99 NW4R_UT_LIST_GET_LINK(*list, node->prevObject)->nextObject = node->nextObject;
100 }
101
102 // If the node is the list tail, set the previous node as the new tail
103 // Else punch a hole in the list
104 if (node->nextObject == nullptr) {
105 list->tailObject = node->prevObject;
106 } else {
107 NW4R_UT_LIST_GET_LINK(*list, node->nextObject)->prevObject = node->prevObject;
108 }
109
110 // Reset the node
111 node->prevObject = nullptr;
112 node->nextObject = nullptr;
113
114 // Update the list
115 list->numObjects--;
116}
117
118void *List_GetNext(const List *list, const void *obj) {
119 if (obj == nullptr) {
120 return list->headObject;
121 }
122
123 return NW4R_UT_LIST_GET_LINK(*list, obj)->nextObject;
124}
125
126void *List_GetPrev(const List *list, const void *obj) {
127 if (obj == nullptr) {
128 return list->tailObject;
129 }
130
131 return NW4R_UT_LIST_GET_LINK(*list, obj)->prevObject;
132}
133
134} // namespace ut
135} // namespace nw4r
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4