-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
146 lines (110 loc) · 3.82 KB
/
list.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* list.h - header file of the list_t module
* Copyright (c) 2017 Löwenware Ltd (https://lowenware.com)
*
* REPOSITORY:
* https://github.com/lowenware.com:cStuff.git
* MAINTAINER:
* Ilja Kartaschoff <ik@lowenware.com>
*
* LICENSE and DISCLAIMER:
* All code stored in this repository is designed to solve
* very common and widely meet development tasks. We are not about to patent
* wheels here, so all code you can find in this repository is FREE:
* you can use, redistribute and/or modify it without any limits or
* restrictions.
*
* All code described above is distributed in hope to be useful for somebody
* else WITHOUT ANY WARRANTY, without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* In case of questions or suggestions, feel free to contact maintainer.
*
* */
#ifndef _CSTUFF_LIST_H_
#define _CSTUFF_LIST_H_
#include <stdint.h>
/* MODULE: list_t
* Dynamic storage for pointers */
/* structure ---------------------------------------------------------------- */
struct list
{
void **list; /* list itself */
int size; /* number of bytes allocated for list */
int count; /* number of defined items in list */
};
typedef struct list * list_t;
/* callback to free memory used by stored item ------------------------------ */
typedef void (* list_destructor_t)(void * list_item);
/* functions ---------------------------------------------------------------- */
/* get list item by index macro
* @self : list_t instance
* @index : item index
* @result : stored pointer
* */
#define list_index(self, index) (self->list[index])
/* -------------------------------------------------------------------------- */
/* create new list_t object
* @size : initial list size
* @result new list_t instance
* */
list_t
list_new(int size);
/* -------------------------------------------------------------------------- */
/* free resources allocated for list_t instance
* @self : list_t instance
* @destructor : if set, destructor method will be called for each not-null
* list entry
* */
void
list_free(list_t self, list_destructor_t destructor);
/* -------------------------------------------------------------------------- */
#ifdef CSTUFF_LIST_WITH_INSERT
/* insert new item into list at some position,
* memory will be allocated if necessary
* @self : list_t instance
* @item : pointer to be inserted
* @position : number from 0(prepend) to list size (append)
* */
int
list_insert( list_t self, void * item, int position );
#endif
/* -------------------------------------------------------------------------- */
#ifdef CSTUFF_LIST_WITH_APPEND
/* append item to queue list
* @self : list_t object
* @item : pointer to be appended
* @result : position at which pointer was stored
* */
int
list_append(list_t self, void * item);
#endif
/* -------------------------------------------------------------------------- */
#ifdef CSTUFF_LIST_WITH_REMOVE
/* remove pointer from list
* @self : list_t object
* @item : to be removed
* */
void
list_remove( list_t self, void * item );
#endif
/* -------------------------------------------------------------------------- */
#ifdef CSTUFF_LIST_WITH_REMOVE_INDEX
/* remove pointer from list by index
* @self : list_t object
* @index : index of pointer to be removed
* */
void *
list_remove_index( list_t self, int index );
#endif
/* -------------------------------------------------------------------------- */
#ifdef CSTUFF_LIST_WITH_SET_ITEM
/* set pointer by index. if element does not exist, list won't be extended
* @self : list_t object
* @index : index of pointer to be removed
* @value : value to be set
* */
void
list_set_item(list_t self, int index, void * value);
#endif
/* -------------------------------------------------------------------------- */
#endif