NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
list.cpp
2
3namespace nw4r {
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_Prepend(List *list, void *obj) {
41 // If only one node is in the list, set it as both head and tail
42 // Else place it at the start
43 if (list->mpHead == nullptr) {
44 Link *node = GET_OBJ_NODE(list, obj);
45 node->mpNext = nullptr;
46 node->mpPrev = nullptr;
47 list->mpHead = obj;
48 list->mpTail = obj;
49 list->mCount++;
50
51 } else {
52 Link *node = GET_OBJ_NODE(list, obj);
53 node->mpPrev = nullptr;
54 node->mpNext = list->mpHead;
55
56 // Link the following node
57 GET_OBJ_NODE(list, list->mpHead)->mpPrev = obj;
58
59 // Set the new head
60 list->mpHead = obj;
61 list->mCount++;
62 }
63}
64
65void List_Insert(List *list, void *target, void *obj) {
66 // If the target is null, insert the object at the end of the list
67 // Else if the target is the list head, insert the object at the beginning
68 // Else perform regular insertion
69 if (target == nullptr) {
70 List_Append(list, obj);
71
72 } else if (target == list->mpHead) {
73 List_Prepend(list, obj);
74
75 } else {
76 Link *node = GET_OBJ_NODE(list, obj);
77 void *prevObj = GET_OBJ_NODE(list, target)->mpPrev;
78 Link *prevNode = GET_OBJ_NODE(list, prevObj);
79
80 // Update the node to be inserted
81 node->mpPrev = prevObj;
82 node->mpNext = target;
83
84 // Update its neighbors
85 prevNode->mpNext = obj;
86 GET_OBJ_NODE(list, target)->mpPrev = obj;
87
88 // Update the list
89 list->mCount++;
90 }
91}
92
93void List_Remove(List *list, void *obj) {
94 Link *node = GET_OBJ_NODE(list, obj);
95
96 // If the node is the list head, set the next node as the new head
97 // Else punch a hole in the list
98 if (node->mpPrev == nullptr) {
99 list->mpHead = node->mpNext;
100 } else {
101 GET_OBJ_NODE(list, node->mpPrev)->mpNext = node->mpNext;
102 }
103
104 // If the node is the list tail, set the previous node as the new tail
105 // Else punch a hole in the list
106 if (node->mpNext == nullptr) {
107 list->mpTail = node->mpPrev;
108 } else {
109 GET_OBJ_NODE(list, node->mpNext)->mpPrev = node->mpPrev;
110 }
111
112 // Reset the node
113 node->mpPrev = nullptr;
114 node->mpNext = nullptr;
115
116 // Update the list
117 list->mCount--;
118}
119
120void *List_GetNext(const List *list, const void *obj) {
121 if (obj == nullptr) {
122 return list->mpHead;
123 }
124
125 return GET_OBJ_NODE(list, obj)->mpNext;
126}
127
128void *List_GetPrev(const List *list, const void *obj) {
129 if (obj == nullptr) {
130 return list->mpTail;
131 }
132
133 return GET_OBJ_NODE(list, obj)->mpPrev;
134}
135
136} // namespace ut
137} // namespace nw4r
void List_Append(List *list, void *obj)
Adds an object to the end of the list.
Definition list.cpp:15
void List_Prepend(List *list, void *obj)
Adds an object to the beginning of the list.
Definition list.cpp:40
void List_Remove(List *list, void *obj)
Deletes an object from the list.
Definition list.cpp:93
void * List_GetPrev(const List *list, const void *obj)
Gets the object linked before the given one.
Definition list.cpp:128
void List_Init(List *list, u16 offset)
Initializes the list.
Definition list.cpp:8
void * List_GetNext(const List *list, const void *obj)
Gets the object linked after the given one.
Definition list.cpp:120
void List_Insert(List *list, void *target, void *obj)
Inserts an object at a specified position in the list.
Definition list.cpp:65
A doubly-linked list container. See Link.
Definition list.hpp:15
void * mpHead
The first linked object.
Definition list.hpp:16
u16 mCount
The linked object count.
Definition list.hpp:18
u16 mOffset
The offset of the Link structure in each object.
Definition list.hpp:19
void * mpTail
The last linked object.
Definition list.hpp:17