-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_list.c
More file actions
272 lines (247 loc) · 5.61 KB
/
array_list.c
File metadata and controls
272 lines (247 loc) · 5.61 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* @Description:
* @LastEditors: hsjfans
* @Email: hsjfans.scholar@gmail.com
* @Date: 2019-02-28 14:21:46
* @LastEditTime: 2019-04-23 23:13:44
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/array_list.h"
/**
* build an empty list
**/
ArrayList *empty_list()
{
return array_list_new(LIST_INIT_CAPACITY);
}
/**
* build an array whose capacity is cap
**/
ArrayList *array_list_new(unsigned int cap)
{
if (cap == 0)
{
cap = LIST_INIT_CAPACITY;
}
ArrayList *temp;
// allocate the data array
temp = (ArrayList *)malloc(sizeof(ArrayList));
if (temp == NULL)
{
return NULL;
}
temp->capacity = cap;
temp->length = 0;
// allocate the data array
temp->elements = (Element *)malloc(cap * sizeof(Element));
if (temp->elements == NULL)
{
// release the space
free(temp);
return NULL;
}
return temp;
}
unsigned int len(ArrayList *array)
{
return array->length;
}
/**
* return the capacity of array
**/
unsigned int cap(ArrayList *array)
{
return array->capacity;
}
/**
* return 1 or 0 if it's empty
**/
boolean is_empty(ArrayList *array)
{
return array->length == 0 ? 1 : 0;
}
/**
* return 1 or 0 if idx is the last index of array
* */
boolean is_last(unsigned int idx, ArrayList *array)
{
return array->length == 1 + idx ? 1 : 0;
}
/**
* return the index of the element or -1 if not exitsis;
* O(N)
* */
int index_of_array(Element e, ArrayList *array, compare_func cmp)
{
for (int i = 0; i < array->length; ++i)
{
if (cmp(e, array->elements[i]) == 0)
{
return i;
}
}
return -1;
}
/**
* @description:
* @param {type}
* @return:
*/
Element find(unsigned int index, ArrayList *array)
{
if (index >= array->length)
return NULL;
return array->elements[index];
}
/**
* @description: remove one element from an array by index ;
* @param {int index} the index
* @param {ArrayList *array}
* @param {compare_func cmp}
* @return:
*/
void delete_one(unsigned int index, ArrayList *array)
{
if (index >= array->length)
{
return;
}
return delete_many(index, 1, array);
}
/**
* @description: remove value from an array list if exists ;
* @param {Element e} the value expect to be removed;
* @param {ArrayList *array}
* @param {compare_func cmp} compare function
* @return:
*/
void delete_value(Element e, ArrayList *array, compare_func cmp)
{
int idx = index_of_array(e, array, cmp);
if (idx == -1)
{
return;
}
delete_one((unsigned int)idx, array);
}
/**
* @description: delete a range from array
* @param {int index}
* @param {int offset} always is positive number
* @param {ArrayList *array}
* @param {compare_func cmp} compare function
* @return:
*/
void delete_many(unsigned int index, unsigned int offset, ArrayList *array)
{
if (index < 0 || (offset + index) > array->length)
return;
/* Move back the entries following the range to be removed */
memmove(array->elements[index], array->elements[index + offset], (array->length - (index + offset)) * sizeof(Element));
array->length -= offset;
}
/**
* insert the element in array; always in tail;
* */
void push_back(Element e, ArrayList *array)
{
return push_index(e, array->length, array);
}
/**
* @description:
* @param {type}
* @return:
*/
void push_front(Element e, ArrayList *array)
{
return push_index(e, 0, array);
}
/**
* insert the element in array;
* */
void push_index(Element e, unsigned int index, ArrayList *array)
{
if (array->length == array->capacity)
{
array_list_extend(array, array->length + (array->length >> 1));
}
/* Move back the entries following the range to be removed */
memmove(array->elements[index + 1], array->elements[index], (array->length - index) * sizeof(Element));
array->elements[index] = e;
++array->length;
}
void array_list_extend(ArrayList *array, unsigned int new_cap)
{
if (new_cap <= array->capacity)
{
return;
}
Element *data;
data = (Element *)realloc(array->elements, new_cap * sizeof(Element));
if (data == NULL)
{
return;
}
else
{
array->elements = data;
array->capacity = new_cap;
}
}
/**
* clear data and release the space ;
* */
void clear(ArrayList *array)
{
if (array != NULL)
{
free(array->elements);
free(array);
}
}
/**
* @description: array reverse ; two pointer to array
* @param {ArrayList *array}
* @return:
*/
void reverse(ArrayList *array)
{
unsigned int left = 0, right = array->length;
while (left <= right)
{
Swap(array->elements[left], array->elements[right]);
left++;
right--;
}
}
/**
* @description: simple q sort
* @param {type}
* @return:
*/
void q_sort(ArrayList *array, unsigned int left, unsigned int right, compare_func cmp)
{
if (left >= right)
return;
// first select the mid one as the datum mark
unsigned int mid = (left + right) / 2, startIndex = left;
// swap
Swap(array->elements[right], array->elements[mid]);
for (unsigned int i = left; i < right; i++)
{
if (cmp(array->elements[i], array->elements[right]) < 0)
{
Swap(array->elements[startIndex++], array->elements[i]);
}
}
// swap the
Swap(array->elements[right], array->elements[startIndex]);
q_sort(array, left, startIndex - 1, cmp);
q_sort(array, startIndex + 1, right, cmp);
}
void sort(ArrayList *array, compare_func cmp)
{
q_sort(array, 0, array->length - 1, cmp);
}