Jo Engine  9
Jo Sega Saturn Engine
list.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2017, 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  struct
51  {
52  short x;
53  short y;
54  } coord;
55  char str[4];
56  bool booleans[4];
57  void *ptr;
58  int integer;
59 } jo_list_data;
60 
62 struct __jo_node
63 {
67 };
68 
70 typedef struct
71 {
72  int count;
76 } jo_list;
77 
81 void jo_list_init(jo_list * const list);
82 
88 jo_node *jo_list_add(jo_list * const list, const jo_list_data data);
89 
95 static __jo_force_inline jo_node *jo_list_add_ptr(jo_list * const list, void *ptr)
96 {
97  return (jo_list_add(list, (jo_list_data)ptr));
98 }
99 
104 void jo_list_remove(jo_list * const list, const jo_node * const node_to_delete);
105 
110 static __jo_force_inline void jo_list_free_and_remove(jo_list * const list, const jo_node * const node_to_delete)
111 {
112  if (node_to_delete->data.ptr != JO_NULL) jo_free(node_to_delete->data.ptr);
113  jo_list_remove(list, node_to_delete);
114 }
115 
120 {
121  while (list->first != JO_NULL)
122  {
123  if (list->first->data.ptr != JO_NULL) jo_free(list->first->data.ptr);
124  jo_list_remove(list, list->first);
125  }
126 }
127 
132 {
133  jo_list_free_and_remove(list, list->last);
134 }
135 
140 {
141  jo_list_remove(list, list->last);
142 }
143 
148 {
149  jo_list_free_and_remove(list, list->first);
150 }
151 
156 {
157  jo_list_remove(list, list->first);
158 }
159 
163 static __jo_force_inline void jo_list_clear(jo_list * const list)
164 {
165  while (list->first != JO_NULL) jo_list_remove(list, list->first);
166 }
167 
172 {
173  return (list->first);
174 }
175 
180 {
181  return list->last;
182 }
183 
188 static __jo_force_inline void jo_list_foreach(jo_list * const list, jo_node_callback callback)
189 {
190  jo_node *tmp;
191  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) callback(tmp);
192 }
193 
200 static __jo_force_inline bool jo_list_any(jo_list * const list, jo_node_any_callback callback, void *extra)
201 {
202  jo_node *tmp;
203  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next) if (callback(tmp, extra)) return true;
204  return false;
205 }
206 
212 {
213  list->allocation_behaviour = behaviour;
214 }
215 
221 {
222  jo_node *tmp;
223  for (tmp = list->first; tmp != JO_NULL; tmp = tmp->next)
224  if (tmp->data.integer == data.integer)
225  {
226  jo_list_remove(list, tmp);
227  return (true);
228  }
229  return false;
230 }
231 
237 {
238  while (jo_list_remove_first_value(list, data))
239  ;
240 }
241 
242 #endif /* !__JO_LIST_H__ */
243 
244 /*
245 ** END OF FILE
246 */
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:200
jo_node * next
Definition: list.h:66
jo_list_data data
Definition: list.h:64
void jo_list_remove(jo_list *const list, const jo_node *const node_to_delete)
Remove an item on the list.
void * ptr
Definition: list.h:57
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:139
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:131
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
static __jo_force_inline void jo_list_foreach(jo_list *const list, jo_node_callback callback)
Iterate on the list.
Definition: list.h:188
List struct.
Definition: list.h:70
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:95
void(* jo_node_callback)(jo_node *node)
Callback for jo_list_foreach()
Definition: list.h:42
jo_malloc_behaviour allocation_behaviour
Definition: list.h:75
static __jo_force_inline jo_node * jo_list_first(jo_list *const list)
Get first item.
Definition: list.h:171
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:236
static __jo_force_inline jo_node * jo_list_last(jo_list *const list)
Get last item.
Definition: list.h:179
jo_malloc_behaviour
Malloc behaviour JO_MALLOC_TRY_REUSE_SAME_BLOCK_SIZE | ^ | | Speed efficiency | | V | JO_MALLOC_TRY_R...
Definition: malloc.h:64
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:220
jo_node * jo_list_add(jo_list *const list, const jo_list_data data)
Add an item on the list.
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:155
static __jo_force_inline void jo_list_clear(jo_list *const list)
Remove all item.
Definition: list.h:163
void jo_free(const void *const p)
function frees the memory space pointed to by ptr, which must have been returned by a previous call t...
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:147
bool(* jo_node_any_callback)(jo_node *node, void *extra)
Callback for jo_list_any()
Definition: list.h:45
jo_node * prev
Definition: list.h:65
jo_node * first
Definition: list.h:73
#define JO_NULL
NULL implementation.
Definition: tools.h:150
jo_node * last
Definition: list.h:74
static __jo_force_inline void jo_list_free_and_clear(jo_list *const list)
Free node pointers and remove all item.
Definition: list.h:119
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:211
int count
Definition: list.h:72
Node struct.
Definition: list.h:62
Node data (4 bytes)
Definition: list.h:48
int integer
Definition: list.h:58
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:110
void jo_list_init(jo_list *const list)
Init a list.