Jo Engine 2026.11.6
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, Johannes Fetz (johannesfetz@gmail.com)
4** All rights reserved.
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are met:
8** * Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** * Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** * Neither the name of the Johannes Fetz nor the
14** names of its contributors may be used to endorse or promote products
15** derived from this software without specific prior written permission.
16**
17** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
34
35#ifndef __JO_LIST_H__
36# define __JO_LIST_H__
37
39typedef struct __jo_node jo_node;
40
42typedef void (*jo_node_callback)(jo_node *node);
43
45typedef bool (*jo_node_any_callback)(jo_node *node, void *extra);
46
48typedef union
49{
50 char c;
51 unsigned char uc;
52 struct
53 {
54 short x;
55 short y;
56 } coord;
57 char str[4];
58 bool booleans[4];
59 void *ptr;
60 char *ch_arr;
63
71
83
87void jo_list_init(jo_list * const list);
88
95jo_node *jo_list_insert_at(jo_list * const list, const jo_list_data data, const int index);
96
103
109static __jo_force_inline jo_node *jo_list_add_ptr(jo_list * const list, void *ptr)
110{
111 return (jo_list_add(list, (jo_list_data)ptr));
112}
113
118void jo_list_remove(jo_list * const list, const jo_node * const node_to_delete);
119
125static __jo_force_inline jo_node *jo_list_at(jo_list * const list, int index)
126{
127 jo_node *tmp;
128 for (tmp = list->first; tmp != JO_NULL && index > 0; tmp = tmp->next)
129 --index;
130 return tmp;
131}
132
137static __jo_force_inline void jo_list_remove_at(jo_list * const list, const int index)
138{
139 jo_list_remove(list, jo_list_at(list, index));
140}
141
146static __jo_force_inline void jo_list_free_and_remove(jo_list * const list, const jo_node * const node_to_delete)
147{
148 if (node_to_delete == JO_NULL) return ;
149 if (node_to_delete->data.ptr != JO_NULL) jo_free(node_to_delete->data.ptr);
150 jo_list_remove(list, node_to_delete);
151}
152
157{
158 while (list->first != JO_NULL)
159 {
160 if (list->first->data.ptr != JO_NULL) jo_free(list->first->data.ptr);
161 jo_list_remove(list, list->first);
162 }
163}
164
169{
170 jo_list_free_and_remove(list, list->last);
171}
172
177{
178 jo_list_remove(list, list->last);
179}
180
185{
186 jo_list_free_and_remove(list, list->first);
187}
188
193{
194 jo_list_remove(list, list->first);
195}
196
200static __jo_force_inline void jo_list_clear(jo_list * const list)
201{
202 while (list->first != JO_NULL) jo_list_remove(list, list->first);
203}
204
209{
210 return (list->first);
211}
212
217{
218 return list->last;
219}
220
226void jo_list_foreach(jo_list * const list, jo_node_callback callback);
227
234bool jo_list_any(jo_list * const list, jo_node_any_callback callback, void *extra);
235
241{
242 list->allocation_behaviour = behaviour;
243}
244
250{
251 jo_node *tmp;
252 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
253 if (tmp->data.integer == data.integer)
254 {
255 jo_list_remove(list, tmp);
256 return (true);
257 }
258 return false;
259}
260
266{
267 while (jo_list_remove_first_value(list, data))
268 ;
269}
270
275static __jo_force_inline void jo_list_append(const jo_list * const list, jo_list * const output)
276{
277 jo_node *tmp;
278 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
279 jo_list_add(output, tmp->data);
280}
281
282#endif /* !__JO_LIST_H__ */
283
284/*
285** END OF FILE
286*/
void * data
Definition entity.h:19
static __jo_force_inline void jo_list_free_and_remove_first(jo_list *const list)
Free and remove the first item from the list.
Definition list.h:184
static __jo_force_inline jo_node * jo_list_at(jo_list *const list, int index)
Get node at index.
Definition list.h:125
bool booleans[4]
Definition list.h:58
jo_node * foreach_next
The next node of the jo_list_foreach() in progress (moved by jo_list_remove(): a callback can remove ...
Definition list.h:81
jo_node * jo_list_add(jo_list *const list, const jo_list_data data)
Add an item on the list.
int integer
Definition list.h:61
static __jo_force_inline jo_node * jo_list_add_ptr(jo_list *const list, void *ptr)
Add a pointer on the list.
Definition list.h:109
static __jo_force_inline void jo_list_remove_at(jo_list *const list, const int index)
Remove an item on the list from index.
Definition list.h:137
static __jo_force_inline jo_node * jo_list_last(jo_list *const list)
Get last item.
Definition list.h:216
jo_list_data data
Definition list.h:67
jo_node * first
Definition list.h:76
char c
Definition list.h:50
char str[4]
Definition list.h:57
jo_node * jo_list_insert_at(jo_list *const list, const jo_list_data data, const int index)
Insert an item on the list at specific index.
static __jo_force_inline void jo_list_free_and_clear(jo_list *const list)
Free node pointers and remove all item.
Definition list.h:156
void jo_list_init(jo_list *const list)
Init a list.
static __jo_force_inline void jo_list_remove_all_value(jo_list *const list, jo_list_data data)
Remove all items on the list that match DATA.
Definition list.h:265
struct __jo_node jo_node
Node type.
Definition list.h:39
static __jo_force_inline void jo_list_remove_last(jo_list *const list)
Free and remove the last item from the list.
Definition list.h:176
bool jo_list_any(jo_list *const list, jo_node_any_callback callback, void *extra)
Find if any element of the list satisfy the condition (callback).
static __jo_force_inline void jo_list_free_and_remove_last(jo_list *const list)
Free and remove the last item from the list.
Definition list.h:168
char * ch_arr
Definition list.h:60
static __jo_force_inline void jo_list_free_and_remove(jo_list *const list, const jo_node *const node_to_delete)
Free node pointer and remove the item from the list.
Definition list.h:146
static __jo_force_inline void jo_list_clear(jo_list *const list)
Remove all item.
Definition list.h:200
static __jo_force_inline void jo_list_set_allocation_behaviour(jo_list *const list, jo_malloc_behaviour behaviour)
Set list memory allocation behaviour.
Definition list.h:240
unsigned char uc
Definition list.h:51
void * ptr
Definition list.h:59
static __jo_force_inline void jo_list_append(const jo_list *const list, jo_list *const output)
Append all items on the list to output list.
Definition list.h:275
static __jo_force_inline void jo_list_remove_first(jo_list *const list)
Free and remove the first item from the list.
Definition list.h:192
jo_node * prev
Definition list.h:68
void jo_list_remove(jo_list *const list, const jo_node *const node_to_delete)
Remove an item on the list.
jo_node * next
Definition list.h:69
void jo_list_foreach(jo_list *const list, jo_node_callback callback)
Iterate on the list.
void(*) jo_node_callback(jo_node *node)
Callback for jo_list_foreach().
Definition list.h:42
static __jo_force_inline jo_node * jo_list_first(jo_list *const list)
Get first item.
Definition list.h:208
int count
Definition list.h:75
bool(*) jo_node_any_callback(jo_node *node, void *extra)
Callback for jo_list_any().
Definition list.h:45
jo_malloc_behaviour allocation_behaviour
Definition list.h:78
jo_node * last
Definition list.h:77
static __jo_force_inline bool jo_list_remove_first_value(jo_list *const list, jo_list_data data)
Remove the first item on the list that match DATA.
Definition list.h:249
Node struct.
Definition list.h:66
List struct.
Definition list.h:74
Node data (4 bytes).
Definition list.h:49
void jo_free(const void *const p)
function frees the memory space pointed to by p
jo_malloc_behaviour
Malloc behaviour JO_MALLOC_TRY_REUSE_SAME_BLOCK_SIZE | ^ | | Speed efficiency | | V | JO_MALLOC_TRY_R...
Definition malloc.h:71
#define JO_NULL
NULL implementation.
Definition tools.h:261
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39