NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
ut_list.cpp
1#include <lib/nw4r/ut/list.h>
2
3namespace nw4r {
4namespace ut {
5
6#define GET_OBJ_NODE(list, obj) ((Link *)(((u32)(obj)) + (list)->offset))
7
8void List_Init(List *list, u16 offset) {
9 list->headObject = nullptr;
10 list->tailObject = nullptr;
11 list->numObjects = 0;
12 list->offset = 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->headObject == nullptr) {
19 Link *node = GET_OBJ_NODE(list, obj);
20 node->nextObject = nullptr;
21 node->prevObject = nullptr;
22 list->headObject = obj;
23 list->tailObject = obj;
24 list->numObjects++;
25
26 } else {
27 Link *node = GET_OBJ_NODE(list, obj);
28 node->prevObject = list->tailObject;
29 node->nextObject = nullptr;
30
31 // Link the previous node
32 GET_OBJ_NODE(list, list->tailObject)->nextObject = obj;
33
34 // Set the new tail
35 list->tailObject = obj;
36 list->numObjects++;
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->headObject == nullptr) {
44 Link *node = GET_OBJ_NODE(list, obj);
45 node->nextObject = nullptr;
46 node->prevObject = nullptr;
47 list->headObject = obj;
48 list->tailObject = obj;
49 list->numObjects++;
50
51 } else {
52 Link *node = GET_OBJ_NODE(list, obj);
53 node->prevObject = nullptr;
54 node->nextObject = list->headObject;
55
56 // Link the following node
57 GET_OBJ_NODE(list, list->headObject)->prevObject = obj;
58
59 // Set the new head
60 list->headObject = obj;
61 list->numObjects++;
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->headObject) {
73 List_Prepend(list, obj);
74
75 } else {
76 Link *node = GET_OBJ_NODE(list, obj);
77 void *prevObj = GET_OBJ_NODE(list, target)->prevObject;
78 Link *prevNode = GET_OBJ_NODE(list, prevObj);
79
80 // Update the node to be inserted
81 node->prevObject = prevObj;
82 node->nextObject = target;
83
84 // Update its neighbors
85 prevNode->nextObject = obj;
86 GET_OBJ_NODE(list, target)->prevObject = obj;
87
88 // Update the list
89 list->numObjects++;
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->prevObject == nullptr) {
99 list->headObject = node->nextObject;
100 } else {
101 GET_OBJ_NODE(list, node->prevObject)->nextObject = node->nextObject;
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->nextObject == nullptr) {
107 list->tailObject = node->prevObject;
108 } else {
109 GET_OBJ_NODE(list, node->nextObject)->prevObject = node->prevObject;
110 }
111
112 // Reset the node
113 node->prevObject = nullptr;
114 node->nextObject = nullptr;
115
116 // Update the list
117 list->numObjects--;
118}
119
120void *List_GetNext(const List *list, const void *obj) {
121 if (obj == nullptr) {
122 return list->headObject;
123 }
124
125 return GET_OBJ_NODE(list, obj)->nextObject;
126}
127
128void *List_GetPrev(const List *list, const void *obj) {
129 if (obj == nullptr) {
130 return list->tailObject;
131 }
132
133 return GET_OBJ_NODE(list, obj)->prevObject;
134}
135
136} // namespace ut
137} // namespace nw4r
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4
void List_Append(List *list, void *obj)
Adds an object to the end of the list.
Definition ut_list.cpp:15
void List_Prepend(List *list, void *obj)
Adds an object to the beginning of the list.
Definition ut_list.cpp:40
void List_Remove(List *list, void *obj)
Deletes an object from the list.
Definition ut_list.cpp:93
void * List_GetPrev(const List *list, const void *obj)
Gets the object linked before the given one.
Definition ut_list.cpp:128
void List_Init(List *list, u16 offset)
Initializes the list.
Definition ut_list.cpp:8
void * List_GetNext(const List *list, const void *obj)
Gets the object linked after the given one.
Definition ut_list.cpp:120
void List_Insert(List *list, void *target, void *obj)
Inserts an object at a specified position in the list.
Definition ut_list.cpp:65
A doubly-linked list container. See Link.
Definition list.h:15
void * headObject
The first linked object.
Definition list.h:16
void * tailObject
The last linked object.
Definition list.h:17
u16 numObjects
The linked object count.
Definition list.h:18
u16 offset
The offset of the Link structure in each object.
Definition list.h:19