-
Notifications
You must be signed in to change notification settings - Fork 0
/
jak_l.h
91 lines (72 loc) · 2.09 KB
/
jak_l.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef jak_l_h
#define jak_l_h
/**
* @file
* @brief Double linked list class
*
* All functions that return int return 0 on success if not specified otherwise
*/
/**
* The main list element type. This type should normally be used as readonly.
* It is only public so looping is easier.
*/
typedef struct jak_l_el {
void * val;
struct jak_l_el * next;
struct jak_l_el * prev;
} jak_l_el_t;
/**
* The main list element type. This type should normally be used as readonly.
* It is only public so looping is easier.
*/
typedef struct {
jak_l_el_t * first;
jak_l_el_t * last;
void (*destructor)( void * );
} jak_l_t;
/**
* Creates a new list.
* \Return NULL on error.
* \param value_destructor A destructor that gets called for each (jak_l_el_t *)->val. Can be NULL.
*/
jak_l_t * jak_l_new( void (*value_destructor)( void * ) );
/**
* Frees all memory associated with this list calling all the destructors.
*/
void jak_l_free( jak_l_t * l );
/**
* Pushes a new element to the end of the list
*/
int jak_l_push( jak_l_t * l, void * val );
/**
* Deletes the last element of the list, calling its destructor if set.
*/
void jak_l_pop( jak_l_t * l );
/**
* Adds a new element to the beginning of the list.
*/
int jak_l_unshift( jak_l_t * l, void * val );
/**
* Deletes the first element of the list and calls its destructor if set.
*/
void jak_l_shift( jak_l_t * l );
/**
* Deletes the element with the same pointer for val as this functions argument.
*/
void jak_l_remove( jak_l_t * l, void * val );
/**
* A macro for easier looping trough the list.
*/
#define JAK_L_FOREACH( L, V ) \
{ \
jak_l_el_t * jak_l_p_el = L->first; \
for( V = ( jak_l_p_el ? jak_l_p_el->val : NULL ); jak_l_p_el; ( jak_l_p_el = jak_l_p_el->next ), ( V = ( jak_l_p_el ? jak_l_p_el->val : NULL ) ) )
#define JAK_L_FOREACH_END }
/**
* A macro for easier reverse looping trough the list.
*/
#define JAK_L_FOREACH_REV( L, V ) \
{ \
jak_l_el_t * jak_l_p_el = L->last; \
for( V = ( jak_l_p_el ? jak_l_p_el->val : NULL ); jak_l_p_el; ( jak_l_p_el = jak_l_p_el->prev ), ( V = ( jak_l_p_el ? jak_l_p_el->val : NULL ) ) )
#endif