Jo Engine 2026.11.1
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
80
84void jo_list_init(jo_list * const list);
85
92jo_node *jo_list_insert_at(jo_list * const list, const jo_list_data data, const int index);
93
99jo_node *jo_list_add(jo_list * const list, const jo_list_data data);
100
106static __jo_force_inline jo_node *jo_list_add_ptr(jo_list * const list, void *ptr)
107{
108 return (jo_list_add(list, (jo_list_data)ptr));
109}
110
115void jo_list_remove(jo_list * const list, const jo_node * const node_to_delete);
116
122static __jo_force_inline jo_node *jo_list_at(jo_list * const list, int index)
123{
124 jo_node *tmp;
125 for (tmp = list->first; tmp != JO_NULL && index > 0; tmp = tmp->next)
126 --index;
127 return tmp;
128}
129
134static __jo_force_inline void jo_list_remove_at(jo_list * const list, const int index)
135{
136 jo_list_remove(list, jo_list_at(list, index));
137}
138
143static __jo_force_inline void jo_list_free_and_remove(jo_list * const list, const jo_node * const node_to_delete)
144{
145 if (node_to_delete == JO_NULL) return ;
146 if (node_to_delete->data.ptr != JO_NULL) jo_free(node_to_delete->data.ptr);
147 jo_list_remove(list, node_to_delete);
148}
149
154{
155 while (list->first != JO_NULL)
156 {
157 if (list->first->data.ptr != JO_NULL) jo_free(list->first->data.ptr);
158 jo_list_remove(list, list->first);
159 }
160}
161
166{
167 jo_list_free_and_remove(list, list->last);
168}
169
174{
175 jo_list_remove(list, list->last);
176}
177
182{
183 jo_list_free_and_remove(list, list->first);
184}
185
190{
191 jo_list_remove(list, list->first);
192}
193
197static __jo_force_inline void jo_list_clear(jo_list * const list)
198{
199 while (list->first != JO_NULL) jo_list_remove(list, list->first);
200}
201
206{
207 return (list->first);
208}
209
214{
215 return list->last;
216}
217
223{
224 jo_node *tmp;
225 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) callback(tmp);
226}
227
234static __jo_force_inline bool jo_list_any(jo_list * const list, jo_node_any_callback callback, void *extra)
235{
236 jo_node *tmp;
237 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) if (callback(tmp, extra)) return true;
238 return false;
239}
240
246{
247 list->allocation_behaviour = behaviour;
248}
249
255{
256 jo_node *tmp;
257 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
258 if (tmp->data.integer == data.integer)
259 {
260 jo_list_remove(list, tmp);
261 return (true);
262 }
263 return false;
264}
265
271{
272 while (jo_list_remove_first_value(list, data))
273 ;
274}
275
280static __jo_force_inline void jo_list_append(const jo_list * const list, jo_list * const output)
281{
282 jo_node *tmp;
283 for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
284 jo_list_add(output, tmp->data);
285}
286
287#endif /* !__JO_LIST_H__ */
288
289/*
290** END OF FILE
291*/
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:181
static __jo_force_inline jo_node * jo_list_at(jo_list *const list, int index)
Get node at index.
Definition list.h:122
bool booleans[4]
Definition list.h:58
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:106
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:134
static __jo_force_inline jo_node * jo_list_last(jo_list *const list)
Get last item.
Definition list.h:213
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:153
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:270
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:173
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:165
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:143
static __jo_force_inline void jo_list_clear(jo_list *const list)
Remove all item.
Definition list.h:197
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:245
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:280
static __jo_force_inline 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).
Definition list.h:234
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:189
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_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:205
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:254
static __jo_force_inline void jo_list_foreach(jo_list *const list, jo_node_callback callback)
Iterate on the list.
Definition list.h:222
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:239
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39