NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
list.cpp
2
3namespace nw4hbm {
4namespace ut {
5
6#define GET_OBJ_NODE(list, obj) ((Link*)(((u32)(obj)) + (list)->mOffset))
7
8void List_Init(List *list, u16 offset) {
9 list->mpHead = nullptr;
10 list->mpTail = nullptr;
11 list->mCount = 0;
12 list->mOffset = offset;
13}
14
15void List_Append(List *list, void *obj) {
16 // If only one node is in the list, set it as both head and tail
17 // Else place it at the end
18 if (list->mpHead == nullptr) {
19 Link *node = GET_OBJ_NODE(list, obj);
20 node->mpNext = nullptr;
21 node->mpPrev = nullptr;
22 list->mpHead = obj;
23 list->mpTail = obj;
24 list->mCount++;
25
26 } else {
27 Link *node = GET_OBJ_NODE(list, obj);
28 node->mpPrev = list->mpTail;
29 node->mpNext = nullptr;
30
31 // Link the previous node
32 GET_OBJ_NODE(list, list->mpTail)->mpNext = obj;
33
34 // Set the new tail
35 list->mpTail = obj;
36 list->mCount++;
37 }
38}
39
40void List_Remove(List *list, void *obj) {
41 Link *node = GET_OBJ_NODE(list, obj);
42
43 // If the node is the list head, set the next node as the new head
44 // Else punch a hole in the list
45 if (node->mpPrev == nullptr) {
46 list->mpHead = node->mpNext;
47 } else {
48 GET_OBJ_NODE(list, node->mpPrev)->mpNext = node->mpNext;
49 }
50
51 // If the node is the list tail, set the previous node as the new tail
52 // Else punch a hole in the list
53 if (node->mpNext == nullptr) {
54 list->mpTail = node->mpPrev;
55 } else {
56 GET_OBJ_NODE(list, node->mpNext)->mpPrev = node->mpPrev;
57 }
58
59 // Reset the node
60 node->mpPrev = nullptr;
61 node->mpNext = nullptr;
62
63 // Update the list
64 list->mCount--;
65}
66
67void *List_GetNext(const List *list, const void *obj) {
68 if (obj == nullptr) {
69 return list->mpHead;
70 }
71
72 return GET_OBJ_NODE(list, obj)->mpNext;
73}
74
75void *List_GetNth(const List *list, u16 n) {
76 void *node;
77 int i;
78
79 for (i = 0, node = nullptr; node = List_GetNext(list, node); i++) {
80 if (n == i) {
81 return node;
82 }
83 }
84
85 return nullptr;
86}
87
88} // namespace ut
89} // namespace nw4hbm