Jo Engine  2020.06.22
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  int integer;
61 } jo_list_data;
62 
64 struct __jo_node
65 {
69 };
70 
72 typedef struct
73 {
74  int count;
78 } jo_list;
79 
83 void jo_list_init(jo_list * const list);
84 
91 jo_node *jo_list_insert_at(jo_list * const list, const jo_list_data data, const int index);
92 
98 jo_node *jo_list_add(jo_list * const list, const jo_list_data data);
99 
105 static __jo_force_inline jo_node *jo_list_add_ptr(jo_list * const list, void *ptr)
106 {
107  return (jo_list_add(list, (jo_list_data)ptr));
108 }
109 
114 void jo_list_remove(jo_list * const list, const jo_node * const node_to_delete);
115 
121 static __jo_force_inline jo_node *jo_list_at(jo_list * const list, int index)
122 {
123  jo_node *tmp;
124  for (tmp = list->first; tmp != JO_NULL && index > 0; tmp = tmp->next)
125  --index;
126  return tmp;
127 }
128 
133 static __jo_force_inline void jo_list_remove_at(jo_list * const list, const int index)
134 {
135  jo_list_remove(list, jo_list_at(list, index));
136 }
137 
142 static __jo_force_inline void jo_list_free_and_remove(jo_list * const list, const jo_node * const node_to_delete)
143 {
144  if (node_to_delete->data.ptr != JO_NULL) jo_free(node_to_delete->data.ptr);
145  jo_list_remove(list, node_to_delete);
146 }
147 
152 {
153  while (list->first != JO_NULL)
154  {
155  if (list->first->data.ptr != JO_NULL) jo_free(list->first->data.ptr);
156  jo_list_remove(list, list->first);
157  }
158 }
159 
164 {
165  jo_list_free_and_remove(list, list->last);
166 }
167 
172 {
173  jo_list_remove(list, list->last);
174 }
175 
180 {
181  jo_list_free_and_remove(list, list->first);
182 }
183 
188 {
189  jo_list_remove(list, list->first);
190 }
191 
195 static __jo_force_inline void jo_list_clear(jo_list * const list)
196 {
197  while (list->first != JO_NULL) jo_list_remove(list, list->first);
198 }
199 
204 {
205  return (list->first);
206 }
207 
212 {
213  return list->last;
214 }
215 
220 static __jo_force_inline void jo_list_foreach(jo_list * const list, jo_node_callback callback)
221 {
222  jo_node *tmp;
223  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) callback(tmp);
224 }
225 
232 static __jo_force_inline bool jo_list_any(jo_list * const list, jo_node_any_callback callback, void *extra)
233 {
234  jo_node *tmp;
235  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) if (callback(tmp, extra)) return true;
236  return false;
237 }
238 
244 {
245  list->allocation_behaviour = behaviour;
246 }
247 
253 {
254  jo_node *tmp;
255  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
256  if (tmp->data.integer == data.integer)
257  {
258  jo_list_remove(list, tmp);
259  return (true);
260  }
261  return false;
262 }
263 
269 {
270  while (jo_list_remove_first_value(list, data))
271  ;
272 }
273 
278 static __jo_force_inline void jo_list_append(const jo_list * const list, jo_list * const output)
279 {
280  jo_node *tmp;
281  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
282  jo_list_add(output, tmp->data);
283 }
284 
285 #endif /* !__JO_LIST_H__ */
286 
287 /*
288 ** END OF FILE
289 */
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:171
jo_list_first
static __jo_force_inline jo_node * jo_list_first(jo_list *const list)
Get first item.
Definition: list.h:203
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:77
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:151
__jo_node::prev
jo_node * prev
Definition: list.h:67
jo_list_init
void jo_list_init(jo_list *const list)
Init a list.
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
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:268
jo_list_last
static __jo_force_inline jo_node * jo_list_last(jo_list *const list)
Get last item.
Definition: list.h:211
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:65
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:252
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:60
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:232
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:278
jo_list_clear
static __jo_force_inline void jo_list_clear(jo_list *const list)
Remove all item.
Definition: list.h:195
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:179
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:66
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:133
jo_list::first
jo_node * first
Definition: list.h:75
jo_list::last
jo_node * last
Definition: list.h:76
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:243
JO_NULL
#define JO_NULL
NULL implementation.
Definition: tools.h:170
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:220
jo_list_data::uc
unsigned char uc
Definition: list.h:51
jo_list
List struct.
Definition: list.h:73
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:163
jo_list::count
int count
Definition: list.h:74
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:105
__jo_node
Node struct.
Definition: list.h:65
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:142
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:68
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:187
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:121