-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
464 lines (384 loc) · 12.3 KB
/
sort.c
File metadata and controls
464 lines (384 loc) · 12.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "sort.h"
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/* Define sort control common assert block. */
#define SORT_CONTROL_COMMON_ASSERT_BLOCK() \
assert((int)0 <= (int)left); \
assert((int)0 <= (int)right); \
assert((int)left < (int)right); \
assert(package.mem_len); \
assert(package.object_ptr); \
assert((package.object_operator.get_element_ptr || package.object_operator.get_address_ptr) \
? (package.object_operator.get_element_ptr && package.object_operator.get_address_ptr) \
: (true))
/* Define sort control common prepare block. */
#define SORT_CONTROL_COMMON_PREPARE_BLOCK() \
SORT_CONTROL_COMMON_ASSERT_BLOCK(); \
sort_control_common_default_package(&package)
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/**
* @brief This type is the sort algorithm category enum.
*/
void *sort_control_algorithm_address_table[] = {
sort_control_bubble_sort,
sort_control_quick_sort
};
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/**
* @brief This function will get the element of object.
*
* @param package the package of the sort
* @param index the index of the element
*
* @return the element pointer
*/
void *sort_control_common_get_element(struct sort_package_s sort_package,
size_t index);
/**
* @brief This function will get the address of element.
*
* @param package the package of the sort
* @param index the index of the element
*
* @return the address of the element
*/
void *sort_control_common_get_address(struct sort_package_s sort_package,
size_t index);
/**
* @brief This function will default the package.
*
* @param package the package of the sort
*
* @return the error code
*/
static inline errno_t
sort_control_common_default_package(struct sort_package_s *package);
/**
* @brief This function will compare the elements.
*
* @param sort_package the package of the sort
* @param left the index of the left element
* @param right the index of the right element
*
* @return the error code
*/
errno_t sort_control_common_compare(struct sort_package_s package,
size_t left,
size_t right);
/**
* @brief This function will swap the elements.
*
* @param sort_package the package of the sort
* @param left the index of the left element
* @param right the index of the right element
*
* @return the error code
*/
errno_t sort_control_common_swap(struct sort_package_s package,
size_t left,
size_t right);
/**
* @brief This function will partition the object for the quick sort algorithm.
*
* @param package the package of sort algorithm
* @param left the leftmost index of the element
* @param right the rightmost index of the element
*
* @return void
*/
size_t sort_contorl_quick_sort_partition(struct sort_package_s package,
size_t left,
size_t right);
/*
*********************************************************************************************************
* FUNCTIONS
*********************************************************************************************************
*/
/**
* @brief This function will sort the object by the sort algorithm.
*
* @param type the type of sort algorithm
* @param package the package of sort algorithm
* @param left the leftmost index of the element
* @param right the rightmost index of the element
*
* @return void
*/
extern inline errno_t
sort_control(enum sort_algorithm_type type,
struct sort_package_s package,
size_t left,
size_t right)
{
assert(0 <= left);
assert(0 <= right);
assert(left < right);
assert(package.mem_len);
assert(package.object_ptr);
assert((package.object_operator.get_element_ptr || package.object_operator.get_address_ptr)
? (package.object_operator.get_element_ptr && package.object_operator.get_address_ptr)
: (true)); /* Assert if both the operators valid */
sort_t sort = sort_control_algorithm_address_table[type]; /* Get the sort algorithm function address */
return sort(package, left, right);
}
/**
* @brief This function will sort the object by the bubble sort algorithm.
*
* @param package the package of sort algorithm
* @param left the leftmost index of the element
* @param right the rightmost index of the element
*
* @return void
*/
errno_t sort_control_bubble_sort(struct sort_package_s package,
size_t left,
size_t right)
{
SORT_CONTROL_COMMON_PREPARE_BLOCK();
for (size_t cnt = left; cnt < right; cnt++) {
for (size_t ct = left; ct < right - cnt; ct++) {
switch (sort_control_common_compare(package, ct, ct + 1)) { /* Compare the element */
case true:
if (sort_control_common_swap(package, ct, ct + 1)) { /* Swap the element */
return 2;
}
break;
case false:
break;
default:
return 1;
}
}
}
return 0;
}
/**
* @brief This function will sort the object by the quick sort algorithm.
*
* @param package the package of sort algorithm
* @param left the leftmost index of the element
* @param right the rightmost index of the element
*
* @return void
*/
errno_t sort_control_quick_sort(struct sort_package_s package,
size_t left,
size_t right)
{
SORT_CONTROL_COMMON_PREPARE_BLOCK();
size_t
pivot_index = sort_contorl_quick_sort_partition(package, left, right);
if ((int)0 > (int)pivot_index) {
return 1;
}
if ((int)left < (int)pivot_index - 1) {
sort_control_quick_sort(package, left, pivot_index - 1);
}
if ((int)pivot_index + 1 < (int)right) {
sort_control_quick_sort(package, pivot_index + 1, right);
}
return 0;
}
/**
* @brief This function will get the element of object.
*
* @param package the package of the sort
* @param index the index of the element
*
* @return the element pointer
*/
static inline void
*sort_control_common_get_element(struct sort_package_s sort_package,
size_t index)
{
assert(0 <= index);
static void *value;
if (NULL == sort_package.object_operator.get_element_ptr) { /* If the object operator is valid */
value = *(void **)((size_t)sort_package /* Calculate the element */
.object_ptr + index * sort_package.mem_len);
} else {
value = sort_package.object_operator /* Get the element */
.get_element_ptr(sort_package.object_ptr, index);
}
return value;
}
/**
* @brief This function will get the address of element.
*
* @param package the package of the sort
* @param index the index of the element
*
* @return the address of the element
*/
static inline void
*sort_control_common_get_address(struct sort_package_s sort_package,
size_t index)
{
assert(0 <= index);
static void *address;
if (NULL == sort_package.object_operator.get_element_ptr) { /* If the object operator is valid */
address = (void *)((size_t)sort_package /* Calculate the address */
.object_ptr + index * sort_package.mem_len);
} else {
address = sort_package.object_operator /* Get the address */
.get_address_ptr(sort_package.object_ptr, index);
}
return address;
}
/**
* @brief This function will default the package.
*
* @param package the package of the sort
*
* @return the error code
*/
static inline errno_t
sort_control_common_default_package(struct sort_package_s *package)
{
if (0u == package->mem_len_key) {
package->mem_len_key = package->mem_len; /* Default the memory length of the object's compare key */
}
if (NULL == package->compare_ptr
&& NULL == (package->compare_ptr = SORT_CFG_DEFAULT_COMPARE_ADDRESS)) { /* Default the compare function */
return 1;
}
if (NULL == package->swap_ptr
&& NULL == (package->swap_ptr = SORT_CFG_DEFAULT_SWAP_ADDRESS)) { /* Default the swap function */
return 2;
}
return 0;
}
/**
* @brief This function will compare the elements.
*
* @param package the package of the sort
* @param left the index of the left element
* @param right the index of the right element
*
* @return the error code
*/
static inline errno_t
sort_control_common_compare(struct sort_package_s package,
size_t left,
size_t right)
{
assert(0 <= left);
assert(0 <= right);
static char
*value_lhs,
*value_rhs;
value_lhs = sort_control_common_get_element(package, left);
value_rhs = sort_control_common_get_element(package, right);
if (NULL == value_lhs &&
NULL == value_rhs) {
return 0xff;
}
#if (SORT_CFG_DEBUG_EN)
printf("sort_algorithm.common.compare: lhs:\"%s\" rhs:\"%s\" \r\n"
, value_lhs, value_rhs);
#endif // (SORT_CFG_DEBUG_EN)
if (package.compare_ptr(value_lhs, value_rhs, package.mem_len_key)) {
return 1;
}
return 0;
}
/**
* @brief This function will swap the elements.
*
* @param package the package of the sort
* @param left the index of the left element
* @param right the index of the right element
*
* @return the error code
*/
static inline errno_t
sort_control_common_swap(struct sort_package_s package,
size_t left,
size_t right)
{
assert(0 <= left);
assert(0 <= right);
static void
*address_lhs,
*address_rhs;
address_lhs = sort_control_common_get_address(package, left);
address_rhs = sort_control_common_get_address(package, right);
if (NULL == address_lhs &&
NULL == address_rhs) {
return 1;
}
#if (SORT_CFG_DEBUG_EN)
printf("sort_algorithm.common.swap: lhs:%p rhs:%p \r\n"
, address_lhs, address_rhs);
#endif // (SORT_CFG_DEBUG_EN)
if (package.swap_ptr(address_lhs, address_rhs, package.mem_len)) { /* Swap the value */
return 2;
}
return 0;
}
/**
* @brief This function will partition the object for the quick sort algorithm.
*
* @param package the package of sort algorithm
* @param left the leftmost index of the element
* @param right the rightmost index of the element
*
* @return void
*/
size_t sort_contorl_quick_sort_partition(struct sort_package_s package,
size_t left,
size_t right)
{
assert((int)left < (int)right);
size_t
pivot = right,
tail = left - 1;
for (size_t cnt = left; cnt < right; cnt++) {
switch (sort_control_common_compare(package, pivot, cnt)) { /* Compare the element */
case true:
if (sort_control_common_swap(package, ++tail, cnt)) { /* Swap the element */
return -1;
}
break;
case false:
break;
default:
return -2;
}
}
if (sort_control_common_swap(package, tail + 1, pivot)) { /* Swap the pivot behind the tail */
return -3;
}
return tail + 1; /* Return the index of pivot */
}