Skip to content

Commit 5afa2ff

Browse files
author
Miao Mico
committed
更改 binary_tree_family 为 tree_family;
提取公共部分构造 tree_family_control_search/insert/init_node...
1 parent 5c6545b commit 5afa2ff

17 files changed

Lines changed: 1631 additions & 2126 deletions

Algorithm/compare.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ bool compare_control_lesser(void *lhs, void *rhs, size_t len)
6565
assert(rhs);
6666
assert(len);
6767

68-
for (size_t cnt = 0; cnt < len; cnt++) {
69-
if (*((char *)lhs + cnt) < *((char *)rhs + cnt)) {
68+
for (size_t cnt = 1; cnt <= len; cnt++) {
69+
if (*((char *)lhs + len - cnt) < *((char *)rhs + len - cnt)) {
7070
return true;
7171
}
7272
}
@@ -91,8 +91,8 @@ bool compare_control_greater(void *lhs, void *rhs, size_t len)
9191
assert(rhs);
9292
assert(len);
9393

94-
for (size_t cnt = 0; cnt < len; cnt++) {
95-
if (*((char *)lhs + cnt) > *((char *)rhs + cnt)) {
94+
for (size_t cnt = 1; cnt <= len; cnt++) {
95+
if (*((char *)lhs + len - cnt) > *((char *)rhs + len - cnt)) {
9696
return true;
9797
}
9898
}
@@ -132,4 +132,4 @@ bool compare_control_equal(void *lhs, void *rhs, size_t len)
132132
} else {
133133
return false;
134134
}
135-
}
135+
}

Algorithm/compare.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @brief This struct is the compare structure module
3535
*/
3636

37-
typedef bool compare_t(void * lhs, void *rhs, size_t len);
37+
typedef bool compare_t(void *lhs, void *rhs, size_t len);
3838

3939
/*
4040
*********************************************************************************************************

Algorithm/sort.c

Lines changed: 115 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,48 @@
4343
*/
4444

4545
/**
46-
* @brief This function will sort the object by the comp.
47-
*
48-
* @param data the pointer to the data list will give
49-
*
50-
* @return if the data match the rule
51-
* - true yes
52-
* - false no
53-
*/
54-
55-
void sort_algorithm_quick_sort(struct sort_pack_s sort_package,
56-
compare_t *comp);
46+
* @brief This function will get the value of object.
47+
*
48+
* @param sort_package the package of the sort information
49+
* @param pos the position of the value
50+
* @param value_lhs the pointer to the left side value
51+
* @param value_rhs the pointer to the right side value
52+
*
53+
* @return if get the value of object successful
54+
* - true yes
55+
* - false no
56+
*/
57+
58+
static inline bool sort_algorithm_control_get_value(struct sort_package_s sort_package,
59+
size_t pos,
60+
void **value_lhs,
61+
void **value_rhs);
5762

5863
/**
59-
* @brief This function will sort the object by the comp.
60-
*
61-
* @param data the pointer to the data list will give
62-
*
63-
* @return if the data match the rule
64-
* - true yes
65-
* - false no
66-
*/
64+
* @brief This function will sort the object by the comp.
65+
*
66+
* @param data the pointer to the data list will give
67+
*
68+
* @return if the data match the rule
69+
* - true yes
70+
* - false no
71+
*/
72+
73+
void sort_algorithm_control_quick_sort(struct sort_package_s sort_package,
74+
compare_t *compare);
6775

68-
void sort_algorithm_bubble_sort(struct sort_pack_s sort_package,
69-
compare_t *comp);
76+
/**
77+
* @brief This function will sort the object by the comp.
78+
*
79+
* @param data the pointer to the data list will give
80+
*
81+
* @return if the data match the rule
82+
* - true yes
83+
* - false no
84+
*/
85+
86+
void sort_algorithm_control_bubble_sort(struct sort_package_s sort_package,
87+
compare_t *compare);
7088

7189
/*
7290
*********************************************************************************************************
@@ -75,40 +93,40 @@ void sort_algorithm_bubble_sort(struct sort_pack_s sort_package,
7593
*/
7694

7795
/**
78-
* @brief This function will sort the object by the comp and the sort algorithm is distinguished by
79-
* the sort_algorithm_addr that will get by xxx_convert_type_to_func_addr_table().
80-
*
81-
* @param data the pointer to the data list will give
82-
*
83-
* @return void
84-
*/
96+
* @brief This function will sort the object by the comp and the sort algorithm is distinguished by
97+
* the sort_algorithm_addr that will get by xxx_convert_type_to_func_addr_table().
98+
*
99+
* @param data the pointer to the data list will give
100+
*
101+
* @return void
102+
*/
85103

86104
void sort_algorithm_control(void *sort_algorithm_addr,
87-
struct sort_pack_s sort_package, compare_t *comp)
105+
struct sort_package_s sort_package, compare_t *comp)
88106
{
89-
void (*sort_algorithm)(struct sort_pack_s, compare_t * comp) = sort_algorithm_addr;
107+
void (*sort_algorithm)(struct sort_package_s, compare_t * comp) = sort_algorithm_addr;
90108

91109
sort_algorithm(sort_package, comp);
92110
}
93111

94112
/**
95-
* @brief This function will return the specified sort algorithm's function address.
96-
*
97-
* @param data the pointer to the data list will give
98-
*
99-
* @return the specified sort algorithm's function address
100-
*/
113+
* @brief This function will return the specified sort algorithm's function address.
114+
*
115+
* @param data the pointer to the data list will give
116+
*
117+
* @return the specified sort algorithm's function address
118+
*/
101119

102120
void *sort_algorithm_control_convert_type_to_func_addr(enum sort_algorithm_type type)
103121
{
104122
void *func_addr_table = NULL;
105123

106124
switch (type) {
107125
case QUICK_SORT:
108-
func_addr_table = sort_algorithm_quick_sort;
126+
func_addr_table = sort_algorithm_control_quick_sort;
109127
break;
110128
case BUBBLE_SORT:
111-
func_addr_table = sort_algorithm_bubble_sort;
129+
func_addr_table = sort_algorithm_control_bubble_sort;
112130
break;
113131
default:
114132
break;
@@ -118,56 +136,83 @@ void *sort_algorithm_control_convert_type_to_func_addr(enum sort_algorithm_type
118136
}
119137

120138
/**
121-
* @brief This function will sort the object by the comp.
122-
*
123-
* @param data the pointer to the data list will give
124-
*
125-
* @return if the data match the rule
126-
* - true yes
127-
* - false no
128-
*/
129-
130-
void sort_algorithm_quick_sort(struct sort_pack_s sort_package,
131-
compare_t *comp)
139+
* @brief This function will get the value of object.
140+
*
141+
* @param sort_package the package of the sort information
142+
* @param pos the position of the value
143+
* @param value_lhs the pointer to the left side value
144+
* @param value_rhs the pointer to the right side value
145+
*
146+
* @return if get the value of object successful
147+
* - true yes
148+
* - false no
149+
*/
150+
151+
static inline bool sort_algorithm_control_get_value(struct sort_package_s sort_package,
152+
size_t pos,
153+
void **value_lhs,
154+
void **value_rhs)
132155
{
156+
if (NULL == sort_package.get_value_method) { /* Get the value */
157+
*value_lhs = (void *)((size_t)sort_package.object + pos * sort_package.mem_len);
158+
*value_rhs = (void *)((size_t)sort_package.object + (pos + 1) * sort_package.mem_len);
159+
} else {
160+
*value_lhs = sort_package.get_value_method(sort_package.object, pos);
161+
*value_rhs = sort_package.get_value_method(sort_package.object, pos + 1);
162+
}
163+
164+
if (NULL == *value_lhs &&
165+
NULL == *value_rhs) {
166+
return false;
167+
}
168+
169+
return true;
133170
}
134171

135172
/**
136-
* @brief This function will sort the object by the comp.
137-
*
138-
* @param data the pointer to the data list will give
139-
*
140-
* @return if the data match the rule
141-
* - true yes
142-
* - false no
143-
*/
173+
* @brief This function will sort the object by the compare().
174+
*
175+
* @param sort_package the information package of the sort
176+
* @param compare the compare() function
177+
*
178+
* @return void
179+
*/
180+
181+
void sort_algorithm_control_quick_sort(struct sort_package_s sort_package,
182+
compare_t *compare)
183+
{
184+
}
144185

145-
void sort_algorithm_bubble_sort(struct sort_pack_s sort_package,
146-
compare_t *comp)
186+
/**
187+
* @brief This function will sort the object by the compare().
188+
*
189+
* @param sort_package the information package of the sort
190+
* @param compare the compare() function
191+
*
192+
* @return void
193+
*/
194+
195+
void sort_algorithm_control_bubble_sort(struct sort_package_s sort_package,
196+
compare_t *compare)
147197
{
148198
char
149199
*value_lhs = NULL,
150200
*value_rhs = NULL;
151201

152202
for (size_t cnt = 0; cnt < sort_package.len - 1; cnt++) {
153203
for (size_t ct = 0; ct < sort_package.len - cnt - 1; ct++) {
154-
if (NULL == sort_package.get_value_method) { /* Get the value */
155-
value_lhs = (void *)((size_t)sort_package.object + ct * sort_package.mem_len);
156-
value_rhs = (void *)((size_t)sort_package.object + (ct + 1) * sort_package.mem_len);
157-
} else {
158-
value_lhs = sort_package.get_value_method(sort_package.object, ct);
159-
value_rhs = sort_package.get_value_method(sort_package.object, ct + 1);
160-
}
161-
162-
if (NULL == value_lhs ||
163-
NULL == value_rhs) {
204+
if (!sort_algorithm_control_get_value(sort_package, ct, &value_lhs, &value_rhs)) { /* Get the value */
164205
return;
165206
}
166207

167-
if (comp(value_lhs, value_rhs, sort_package.mem_len)) { /* Compare the value */
208+
if (compare(value_lhs, value_rhs, sort_package.mem_len)) { /* Compare the value */
209+
#if (SORT_ALGORITHM_CFG_DEBUG_EN)
210+
168211
printf("sort_algorithm.bubble_sort.no.%d-%d: %d \"%s\" swap %d \"%s\" \r\n",
169212
cnt, ct, ct, value_lhs, ct + 1, value_rhs);
170213

214+
#endif // (SORT_ALGORITHM_CFG_DEBUG_EN)
215+
171216
sort_package.swap_method(sort_package.object, ct, ct + 1);
172217
}
173218
}

Algorithm/sort.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*********************************************************************************************************
2727
*/
2828

29+
#define SORT_ALGORITHM_CFG_DEBUG_EN 0
30+
2931
/*
3032
*********************************************************************************************************
3133
* DATA TYPES
@@ -36,15 +38,17 @@
3638
* @brief This struct will contain the necessary information that sort needed.
3739
*/
3840

39-
struct sort_pack_s {
41+
struct sort_package_s {
4042
void *object;
4143

4244
size_t len;
4345
size_t mem_len;
4446

45-
void (*swap_method)(void *, size_t, size_t);
47+
void *(*get_value_method)(void *object, size_t loc);
48+
49+
void (*swap_method)(void *object, size_t lhs, size_t rhs);
4650

47-
void *(*get_value_method)(void *, size_t);
51+
compare_t *compare_method;
4852
};
4953

5054
/*
@@ -73,7 +77,7 @@ void *sort_algorithm_control_convert_type_to_func_addr(enum sort_algorithm_type
7377
*/
7478

7579
void sort_algorithm_control(void *sort_algorithm_addr,
76-
struct sort_pack_s sort_package, compare_t *comp);
80+
struct sort_package_s sort_package, compare_t *comp);
7781

7882
/*
7983
*********************************************************************************************************

0 commit comments

Comments
 (0)