Debian Codegraph @

Descent3/list.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "list.h"
20 // #include <malloc.h>
21 #include "mem.h"
22
23 // Allocates space for a new list node, returning the pointer to it
24 listnode *NewListNode(void) {
25 listnode *node;
26
27 node = (listnode *)mem_malloc(sizeof(listnode));
28 if (node == NULL) {
29 mprintf(0, "Not enough memory for a new listnode!\n");
30 Int3();
31 return NULL;
32 }
33
34 node->data = NULL;
35 node->next = NULL;
36 node->prev = NULL;
37
38 return node;
39 }
40
41 // Adds an item to a list
42 // Returns 1 if everything is ok, else 0
43 int AddListItem(listnode **listp, void *item) {
44 listnode *newnode, *curr;
45
46 newnode = NewListNode();
47
48 if (newnode == NULL) {
49 mprintf(0, "There was a problem mallocing list node memory!\n");
50 Int3();
51 return 0;
52 }
53
54 if (*listp == NULL) // If this list is empty, construct a new head
55 {
56 newnode->data = item;
57 *listp = newnode;
58 return 1;
59 } else {
60 // Go through until the end of the list and add the new item there
61 for (curr = *listp; curr->next != NULL; curr = curr->next) {
62 if (curr->data == item) {
63 Int3();
64 return 0;
65 }
66 }
67
68 newnode->data = item;
69 curr->next = newnode;
70 newnode->next = NULL;
71 newnode->prev = curr;
72 }
73
74 return 1;
75 }
76
77 // Removes an item from a list
78 int RemoveListItem(listnode **listp, void *item) {
79 listnode *curr, *node;
80 int inlist = 0;
81
82 for (curr = *listp; curr != NULL; curr = curr->next) {
83 if (curr->data == item) {
84 inlist = 1;
85 node = curr;
86 break;
87 }
88 }
89
90 if (!inlist) {
91 mprintf(0, "RemoveItem: Warning, item not found in list\n");
92 return 0;
93 }
94
95 // Head of list is a special case
96 if (node == *listp) {
97 *listp = node->next;
98 if (*listp != NULL)
99 (*listp)->prev = NULL;
100 mem_free(node);
101 return 1;
102 } else {
103 if (node->prev != NULL)
104 node->prev->next = node->next;
105 if (node->next != NULL)
106 node->next->prev = node->prev;
107 mem_free(node);
108 }
109 return 1;
110 }
111
112 // Destroys all the nodes in a list
113 // The items must be freed in another routine
114 void DestroyList(listnode **listp) {
115 listnode *node, *next;
116
117 for (node = *listp; node != NULL; node = next) {
118 next = node->next;
119 mem_free(node);
120 }
121 }
122
123 // Returns the number of items in a list
124 int CountListItems(listnode **listp) {
125 listnode *node;
126 int count = 0;
127
128 for (node = *listp; node != NULL; node = node->next)
129 count++;
130
131 return count;
132 }
133
134 // returns a pointer the given item index in a list
135 // Returns NULL if point not found
136 void *GetListItem(listnode **listp, int index) {
137 listnode *node;
138
139 for (node = *listp; node != NULL; node = node->next) {
140 if (index-- == 0)
141 return node->data;
142 }
143
144 return NULL;
145 }
146
147 // Returns how far from the head of the list a given item is
148 // Returns -1 if not found
149 int GetListItemIndex(listnode **listp, void *item) {
150 listnode *node;
151 int index = 0;
152
153 for (node = *listp; node != NULL; node = node->next) {
154 if (node->data == item)
155 return index;
156 index++;
157 }
158
159 return -1;
160 }