Jo Engine  2023.08.26
Jo Sega Saturn Engine
list.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, 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 */
35 #ifndef __JO_LIST_H__
36 # define __JO_LIST_H__
37 
39 typedef struct __jo_node jo_node;
40 
42 typedef void (*jo_node_callback)(jo_node *node);
43 
45 typedef bool (*jo_node_any_callback)(jo_node *node, void *extra);
46 
48 typedef 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;
61  int integer;
62 } jo_list_data;
63 
65 struct __jo_node
66 {
70 };
71 
73 typedef struct
74 {
75  int count;
79 } jo_list;
80 
84 void jo_list_init(jo_list * const list);
85 
92 jo_node *jo_list_insert_at(jo_list * const list, const jo_list_data data, const int index);
93 
99 jo_node *jo_list_add(jo_list * const list, const jo_list_data data);
100 
106 static __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 
115 void jo_list_remove(jo_list * const list, const jo_node * const node_to_delete);
116 
122 static __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 
134 static __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 
143 static __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->data.ptr != JO_NULL) jo_free(node_to_delete->data.ptr);
146  jo_list_remove(list, node_to_delete);
147 }
148 
153 {
154  while (list->first != JO_NULL)
155  {
156  if (list->first->data.ptr != JO_NULL) jo_free(list->first->data.ptr);
157  jo_list_remove(list, list->first);
158  }
159 }
160 
165 {
166  jo_list_free_and_remove(list, list->last);
167 }
168 
173 {
174  jo_list_remove(list, list->last);
175 }
176 
181 {
182  jo_list_free_and_remove(list, list->first);
183 }
184 
189 {
190  jo_list_remove(list, list->first);
191 }
192 
196 static __jo_force_inline void jo_list_clear(jo_list * const list)
197 {
198  while (list->first != JO_NULL) jo_list_remove(list, list->first);
199 }
200 
205 {
206  return (list->first);
207 }
208 
213 {
214  return list->last;
215 }
216 
221 static __jo_force_inline void jo_list_foreach(jo_list * const list, jo_node_callback callback)
222 {
223  jo_node *tmp;
224  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) callback(tmp);
225 }
226 
233 static __jo_force_inline bool jo_list_any(jo_list * const list, jo_node_any_callback callback, void *extra)
234 {
235  jo_node *tmp;
236  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) if (callback(tmp, extra)) return true;
237  return false;
238 }
239 
245 {
246  list->allocation_behaviour = behaviour;
247 }
248 
254 {
255  jo_node *tmp;
256  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
257  if (tmp->data.integer == data.integer)
258  {
259  jo_list_remove(list, tmp);
260  return (true);
261  }
262  return false;
263 }
264 
270 {
271  while (jo_list_remove_first_value(list, data))
272  ;
273 }
274 
279 static __jo_force_inline void jo_list_append(const jo_list * const list, jo_list * const output)
280 {
281  jo_node *tmp;
282  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
283  jo_list_add(output, tmp->data);
284 }
285 
286 #endif /* !__JO_LIST_H__ */
287 
288 /*
289 ** END OF FILE
290 */
jo_list_remove_last
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:172
jo_list_first
static __jo_force_inline jo_node * jo_list_first(jo_list *const list)
Get first item.
Definition: list.h:204
jo_node_callback
void(* jo_node_callback)(jo_node *node)
Callback for jo_list_foreach()
Definition: list.h:42
jo_list::allocation_behaviour
jo_malloc_behaviour allocation_behaviour
Definition: list.h:78
jo_list_free_and_clear
static __jo_force_inline void jo_list_free_and_clear(jo_list *const list)
Free node pointers and remove all item.
Definition: list.h:152
__jo_node::prev
jo_node * prev
Definition: list.h:68
jo_list_init
void jo_list_init(jo_list *const list)
Init a list.
jo_list_remove_all_value
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:269
jo_list_last
static __jo_force_inline jo_node * jo_list_last(jo_list *const list)
Get last item.
Definition: list.h:212
jo_malloc_behaviour
jo_malloc_behaviour
Malloc behaviour JO_MALLOC_TRY_REUSE_SAME_BLOCK_SIZE | ^ | | Speed efficiency | | V | JO_MALLOC_TRY_R...
Definition: malloc.h:71
jo_list_remove_first_value
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:253
jo_list_insert_at
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.
jo_list_data::integer
int integer
Definition: list.h:61
jo_list_remove
void jo_list_remove(jo_list *const list, const jo_node *const node_to_delete)
Remove an item on the list.
jo_list_any
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:233
jo_list_data
Node data (4 bytes)
Definition: list.h:49
jo_list_append
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:279
jo_list_clear
static __jo_force_inline void jo_list_clear(jo_list *const list)
Remove all item.
Definition: list.h:196
jo_list_free_and_remove_first
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:180
jo_list_add
jo_node * jo_list_add(jo_list *const list, const jo_list_data data)
Add an item on the list.
__jo_node::data
jo_list_data data
Definition: list.h:67
jo_list_remove_at
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
jo_list::first
jo_node * first
Definition: list.h:76
jo_list::last
jo_node * last
Definition: list.h:77
jo_list_set_allocation_behaviour
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:244
JO_NULL
#define JO_NULL
NULL implementation.
Definition: tools.h:170
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
jo_list_foreach
static __jo_force_inline void jo_list_foreach(jo_list *const list, jo_node_callback callback)
Iterate on the list.
Definition: list.h:221
jo_list_data::ch_arr
char * ch_arr
Definition: list.h:60
jo_list_data::uc
unsigned char uc
Definition: list.h:51
jo_list
List struct.
Definition: list.h:74
jo_list_free_and_remove_last
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:164
jo_list::count
int count
Definition: list.h:75
jo_list_add_ptr
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
__jo_node
Node struct.
Definition: list.h:66
jo_free
void jo_free(const void *const p)
function frees the memory space pointed to by p
jo_list_free_and_remove
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
jo_node_any_callback
bool(* jo_node_any_callback)(jo_node *node, void *extra)
Callback for jo_list_any()
Definition: list.h:45
__jo_node::next
jo_node * next
Definition: list.h:69
jo_list_data::c
char c
Definition: list.h:50
jo_list_data::ptr
void * ptr
Definition: list.h:59
jo_list_remove_first
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:188
jo_list_at
static __jo_force_inline jo_node * jo_list_at(jo_list *const list, int index)
Get node at index.
Definition: list.h:122