-
Notifications
You must be signed in to change notification settings - Fork 30
/
arraylist.h
185 lines (167 loc) · 5.26 KB
/
arraylist.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @file arraylist.h
*
* @author hutusi ([email protected])
*
* @brief Automatically resizing array(stack).
*
* ArrayLists are arrays of pointers which automatically increase in
* size.
*
* To create an ArrayList, use @ref arraylist_new.
* To destroy an ArrayList, use @ref arraylist_free.
*
* To add a value to an ArrayList, use @ref arraylist_prepend,
* @ref arraylist_append, or @ref arraylist_insert.
*
* To remove a value from an ArrayList, use @ref arraylist_remove
* or @ref arraylist_remove_range.
*
* ArrayList can be treated as stack, use @ref arraylist_push,
* @ref arraylist_pop to push or pop element value.
*
* @date 2019-07-20
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_ARRAYLIST_H
#define RETHINK_C_ARRAYLIST_H
/**
* @brief The type of a value to be stored in an @ref ArrayList.
* (void *) can be changed to int, long, or other types if needed.
*/
typedef void *ArrayListValue;
/**
* @brief Array List free value callback function.
*
*/
typedef void (*ArrayListFreeValueFunc)(ArrayListValue value);
/**
* @brief Definition of an @ref ArrayList.
*/
typedef struct _ArrayList {
/** Entries in the array */
ArrayListValue *data;
/** Length of the array */
unsigned int length;
/** Allocated length of the array.
* (Private data and should not be accessed) */
unsigned int _allocated;
/** Free value call back function. */
ArrayListFreeValueFunc _free_value_func;
} ArrayList, Stack;
/**
* @brief Allocated a new ArrayList for use.
*
* @param free_value_func Free value callback function.
* @param length The initiate length of ArrayList.
* @return ArrayList*
*/
ArrayList *arraylist_new(ArrayListFreeValueFunc free_value_func,
unsigned int length);
/**
* @brief Destroy an ArrayList and free back the memory.
*
* @param arraylist The arraylist to free.
*/
void arraylist_free(ArrayList *arraylist);
/**
* @brief Append a value to the end of an ArrayList.
*
* @param arraylist The ArrayList.
* @param data The value to append.
* @return int 0 if success.
*/
int arraylist_append(ArrayList *arraylist, ArrayListValue data);
/**
* @brief Prepend a value to the beginning of an ArrayList.
*
* @param arraylist The ArrayList.
* @param data The value to prepend.
* @return int 0 if success.
*/
int arraylist_prepend(ArrayList *arraylist, ArrayListValue data);
/**
* @brief Insert a value into an ArrayList.
*
* @param arraylist The ArrayList.
* @param index The index to be insert.
* @param data The value to insert.
* @return int 0 if success.
*/
int arraylist_insert(ArrayList *arraylist,
unsigned int index,
ArrayListValue data);
/**
* @brief Push a value to the end of an ArrayList.
*
* @param arraylist The ArrayList.
* @param data The value to push.
* @return int 0 if success.
*/
int arraylist_push(ArrayList *arraylist, ArrayListValue data);
/**
* @brief Pop a value from the end of an ArrayList.
*
* @param arraylist The ArrayList.
* @return ArrayListValue Value if success, otherwise return NULL.
*/
ArrayListValue arraylist_pop(ArrayList *arraylist);
/**
* @brief Remove the entry at the specified location in an ArrayList.
*
* @param arraylist The ArrayList.
* @param index The index of entry to remove.
* @return int 0 if success.
*/
int arraylist_remove(ArrayList *arraylist, unsigned int index);
/**
* @brief Remove a range of entries at the specified location in an ArrayList.
*
* @param arraylist The ArrayList.
* @param index The index of the start of the range to remove.
* @param length The length of the range to remove.
* @return int 0 if success.
*/
int arraylist_remove_range(ArrayList *arraylist,
unsigned int index,
unsigned int length);
/**
* @brief Clear all entries of an ArrayList.
*
* @param arraylist The ArrayList.
*/
void arraylist_clear(ArrayList *arraylist);
/**
* @brief Compare two values in an ArrayList.
*
* @param left The first value.
* @param right The second value.
* @return A negative number if left should be sorted before right,
* a positive number if right should be sorted before left,
* zero if the two values are equal.
*/
typedef int (*ArrayListValueCompareFunc)(ArrayListValue left,
ArrayListValue right);
/**
* @brief Find the index of a particular value in an ArrayList.
*
* @param arraylist The ArrayList.
* @param callback The compare function callback.
* @param data The value to search for.
* @return int The index of the value if found, or -1 if not found.
*/
int arraylist_index_of(const ArrayList *arraylist,
ArrayListValueCompareFunc callback,
ArrayListValue data);
/**
* @brief Sort the values in an ArrayList.
*
* @param arraylist The ArrayList.
* @param compare_func Function callback used to compare values in sorting.
* @return int 0 if success.
*/
int arraylist_sort(ArrayList *arraylist,
ArrayListValueCompareFunc compare_func);
#endif /* #ifndef RETHINK_C_ARRAYLIST_H */