Line | Branch | Exec | Source |
---|---|---|---|
1 | /*********************************************************************************/ | ||
2 | /* Copyright 2009-2024 Barcelona Supercomputing Center */ | ||
3 | /* */ | ||
4 | /* This file is part of the DLB library. */ | ||
5 | /* */ | ||
6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
9 | /* (at your option) any later version. */ | ||
10 | /* */ | ||
11 | /* DLB is distributed in the hope that it will be useful, */ | ||
12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
14 | /* GNU Lesser General Public License for more details. */ | ||
15 | /* */ | ||
16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
18 | /*********************************************************************************/ | ||
19 | |||
20 | /* Subset of GSList from GLIB to implement a stack: */ | ||
21 | |||
22 | /* GLIB - Library of useful routines for C programming | ||
23 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald | ||
24 | * | ||
25 | * SPDX-License-Identifier: LGPL-2.1-or-later | ||
26 | * | ||
27 | * This library is free software; you can redistribute it and/or | ||
28 | * modify it under the terms of the GNU Lesser General Public | ||
29 | * License as published by the Free Software Foundation; either | ||
30 | * version 2.1 of the License, or (at your option) any later version. | ||
31 | * | ||
32 | * This library is distributed in the hope that it will be useful, | ||
33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
35 | * Lesser General Public License for more details. | ||
36 | * | ||
37 | * You should have received a copy of the GNU Lesser General Public | ||
38 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | ||
39 | */ | ||
40 | |||
41 | /* | ||
42 | * Modified by the GLib Team and others 1997-2000. See the AUTHORS | ||
43 | * file for a list of people on the GLib Team. See the ChangeLog | ||
44 | * files for a list of changes. These files are distributed with | ||
45 | * GLib at ftp://ftp.gtk.org/pub/gtk/. | ||
46 | */ | ||
47 | |||
48 | /* | ||
49 | * MT safe | ||
50 | */ | ||
51 | |||
52 | #include "gslist.h" | ||
53 | |||
54 | #include <stdlib.h> | ||
55 | |||
56 | |||
57 | /** | ||
58 | * g_slist_free: | ||
59 | * @list: the first link of a #GSList | ||
60 | * | ||
61 | * DLB: free all elements in list | ||
62 | **/ | ||
63 | void | ||
64 | 119 | g_slist_free (GSList *list) | |
65 | { | ||
66 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 119 times.
|
184 | while (list) |
67 | { | ||
68 | 65 | GSList *next = list->next; | |
69 | 65 | free(list); | |
70 | 65 | list = next; | |
71 | } | ||
72 | 119 | } | |
73 | |||
74 | /** | ||
75 | * g_slist_prepend: | ||
76 | * @list: a #GSList | ||
77 | * @data: the data for the new element | ||
78 | * | ||
79 | * Adds a new element on to the start of the list. | ||
80 | * | ||
81 | * The return value is the new start of the list, which | ||
82 | * may have changed, so make sure you store the new value. | ||
83 | * | ||
84 | * |[<!-- language="C" --> | ||
85 | * // Notice that it is initialized to the empty list. | ||
86 | * GSList *list = NULL; | ||
87 | * list = g_slist_prepend (list, "last"); | ||
88 | * list = g_slist_prepend (list, "first"); | ||
89 | * ]| | ||
90 | * | ||
91 | * Returns: the new start of the #GSList | ||
92 | */ | ||
93 | GSList* | ||
94 | 1514 | g_slist_prepend (GSList *list, | |
95 | gpointer data) | ||
96 | { | ||
97 | GSList *new_list; | ||
98 | |||
99 | 1514 | new_list = malloc(sizeof(GSList)); | |
100 | 1514 | new_list->data = data; | |
101 | 1514 | new_list->next = list; | |
102 | |||
103 | 1514 | return new_list; | |
104 | } | ||
105 | |||
106 | /** | ||
107 | * g_slist_remove: | ||
108 | * @list: a #GSList | ||
109 | * @data: the data of the element to remove | ||
110 | * | ||
111 | * Removes an element from a #GSList. | ||
112 | * If two elements contain the same data, only the first is removed. | ||
113 | * If none of the elements contain the data, the #GSList is unchanged. | ||
114 | * | ||
115 | * Returns: the new start of the #GSList | ||
116 | */ | ||
117 | GSList* | ||
118 | 1449 | g_slist_remove (GSList *list, | |
119 | gconstpointer data) | ||
120 | { | ||
121 | 1449 | GSList *tmp = NULL; | |
122 | 1449 | GSList **previous_ptr = &list; | |
123 | |||
124 |
1/2✓ Branch 0 taken 1450 times.
✗ Branch 1 not taken.
|
1450 | while (*previous_ptr) |
125 | { | ||
126 | 1450 | tmp = *previous_ptr; | |
127 |
2/2✓ Branch 0 taken 1449 times.
✓ Branch 1 taken 1 times.
|
1450 | if (tmp->data == data) |
128 | { | ||
129 | 1449 | *previous_ptr = tmp->next; | |
130 | 1449 | free(tmp); | |
131 | 1449 | break; | |
132 | } | ||
133 | else | ||
134 | { | ||
135 | 1 | previous_ptr = &tmp->next; | |
136 | } | ||
137 | } | ||
138 | |||
139 | 1449 | return list; | |
140 | } | ||
141 | |||
142 | /** | ||
143 | * g_slist_reverse: | ||
144 | * @list: a #GSList | ||
145 | * | ||
146 | * Reverses a #GSList. | ||
147 | * | ||
148 | * Returns: the start of the reversed #GSList | ||
149 | */ | ||
150 | GSList* | ||
151 | 60 | g_slist_reverse (GSList *list) | |
152 | { | ||
153 | 60 | GSList *prev = NULL; | |
154 | |||
155 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 60 times.
|
92 | while (list) |
156 | { | ||
157 | 32 | GSList *next = list->next; | |
158 | |||
159 | 32 | list->next = prev; | |
160 | |||
161 | 32 | prev = list; | |
162 | 32 | list = next; | |
163 | } | ||
164 | |||
165 | 60 | return prev; | |
166 | } | ||
167 | |||
168 | /** | ||
169 | * g_slist_length: | ||
170 | * @list: a #GSList | ||
171 | * | ||
172 | * Gets the number of elements in a #GSList. | ||
173 | * | ||
174 | * This function iterates over the whole list to | ||
175 | * count its elements. To check whether the list is non-empty, it is faster to | ||
176 | * check @list against %NULL. | ||
177 | * | ||
178 | * Returns: the number of elements in the #GSList | ||
179 | */ | ||
180 | guint | ||
181 | ✗ | g_slist_length (GSList *list) | |
182 | { | ||
183 | guint length; | ||
184 | |||
185 | ✗ | length = 0; | |
186 | ✗ | while (list) | |
187 | { | ||
188 | ✗ | length++; | |
189 | ✗ | list = list->next; | |
190 | } | ||
191 | |||
192 | ✗ | return length; | |
193 | } | ||
194 | |||
195 | /** | ||
196 | * g_slist_foreach: | ||
197 | * @list: a #GSList | ||
198 | * @func: (scope call): the function to call with each element's data | ||
199 | * @user_data: user data to pass to the function | ||
200 | * | ||
201 | * Calls a function for each element of a #GSList. | ||
202 | * | ||
203 | * It is safe for @func to remove the element from @list, but it must | ||
204 | * not modify any part of the list after that element. | ||
205 | */ | ||
206 | void | ||
207 | ✗ | g_slist_foreach (GSList *list, | |
208 | GFunc func, | ||
209 | gpointer user_data) | ||
210 | { | ||
211 | ✗ | while (list) | |
212 | { | ||
213 | ✗ | GSList *next = list->next; | |
214 | ✗ | (*func) (list->data, user_data); | |
215 | ✗ | list = next; | |
216 | } | ||
217 | } | ||
218 |