Skip to content

Commit 87d1636

Browse files
author
Miao Mico
committed
增加 _common_get_element,为原 _get_value;
增加 _common_get_address,为原 _get_value; 增加 _common_compare; 增加 _common_swap; 减小增加算法的不舒适度。
1 parent 7047f00 commit 87d1636

2 files changed

Lines changed: 174 additions & 107 deletions

File tree

Algorithm/algorithm_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef errno_t(*swap_t)(void *lhs, void *rhs, size_t len);
7575
*/
7676

7777
struct sort_package_object_operator_s {
78-
void *(*get_value_ptr)(void *object, size_t loc);
78+
void *(*get_element_ptr)(void *object, size_t loc);
7979
void *(*get_address_ptr)(void *object, size_t loc);
8080
};
8181

Algorithm/sort.c

Lines changed: 173 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,56 @@ void *sort_control_algorithm_address_table[] = {
5151
*/
5252

5353
/**
54-
* @brief This function will get the value of object.
54+
* @brief This function will get the element of object.
5555
*
56-
* @param sort_package the package of the sort information
57-
* @param pos the position of the value
58-
* @param value_lhs the pointer to the left side value
59-
* @param value_rhs the pointer to the right side value
56+
* @param package the package of the sort
57+
* @param index the index of the element
6058
*
61-
* @return if get the value of object successful
62-
* - true yes
63-
* - false no
59+
* @return the element pointer
6460
*/
6561

66-
static inline bool
67-
sort_control_get_value(struct sort_package_s sort_package,
68-
size_t pos,
69-
void **value_lhs,
70-
void **value_rhs);
62+
void *sort_control_common_get_element(struct sort_package_s sort_package,
63+
size_t index);
64+
65+
/**
66+
* @brief This function will get the address of element.
67+
*
68+
* @param package the package of the sort
69+
* @param index the index of the element
70+
*
71+
* @return the address of the element
72+
*/
73+
74+
void *sort_control_common_get_address(struct sort_package_s sort_package,
75+
size_t index);
76+
77+
/**
78+
* @brief This function will compare the elements.
79+
*
80+
* @param sort_package the package of the sort
81+
* @param left the index of the left element
82+
* @param right the index of the right element
83+
*
84+
* @return the error code
85+
*/
86+
87+
errno_t sort_control_common_compare(struct sort_package_s package,
88+
size_t left,
89+
size_t right);
90+
91+
/**
92+
* @brief This function will swap the elements.
93+
*
94+
* @param sort_package the package of the sort
95+
* @param left the index of the left element
96+
* @param right the index of the right element
97+
*
98+
* @return the error code
99+
*/
100+
101+
errno_t sort_control_common_swap(struct sort_package_s package,
102+
size_t left,
103+
size_t right);
71104

72105
/*
73106
*********************************************************************************************************
@@ -93,8 +126,8 @@ sort_control(enum sort_algorithm_type type,
93126
assert(package.left < package.right);
94127
assert(package.mem_len);
95128
assert(package.object_ptr);
96-
assert((package.object_operator.get_value_ptr || package.object_operator.get_address_ptr)
97-
? (package.object_operator.get_value_ptr && package.object_operator.get_address_ptr)
129+
assert((package.object_operator.get_element_ptr || package.object_operator.get_address_ptr)
130+
? (package.object_operator.get_element_ptr && package.object_operator.get_address_ptr)
98131
: (true)); /* Assert if both the operators valid */
99132

100133
sort_t sort = sort_control_algorithm_address_table[type]; /* Get the sort algorithm function address */
@@ -103,85 +136,147 @@ sort_control(enum sort_algorithm_type type,
103136
}
104137

105138
/**
106-
* @brief This function will get the value of object.
139+
* @brief This function will get the element of object.
107140
*
108-
* @param sort_package the package of the sort information
109-
* @param pos the position of the value
110-
* @param value_lhs the pointer to the left side value
111-
* @param value_rhs the pointer to the right side value
141+
* @param package the package of the sort
142+
* @param index the index of the element
112143
*
113-
* @return if get the value of object successful
114-
* - true yes
115-
* - false no
144+
* @return the element pointer
116145
*/
117146

118-
static inline bool
119-
sort_control_get_value(struct sort_package_s sort_package,
120-
size_t pos,
121-
void **value_lhs,
122-
void **value_rhs)
147+
static inline void
148+
*sort_control_common_get_element(struct sort_package_s sort_package,
149+
size_t index)
123150
{
124-
assert(0 <= pos);
151+
assert(0 <= index);
152+
153+
static void *value;
125154

126-
if (NULL == sort_package.object_operator.get_value_ptr) { /* If the object operator is valid */
127-
*value_lhs = *(void **)((size_t)sort_package /* Calculate the value */
128-
.object_ptr + pos * sort_package.mem_len);
129-
*value_rhs = *(void **)((size_t)sort_package
130-
.object_ptr + (pos + 1) * sort_package.mem_len);
155+
if (NULL == sort_package.object_operator.get_element_ptr) { /* If the object operator is valid */
156+
value = *(void **)((size_t)sort_package /* Calculate the element */
157+
.object_ptr + index * sort_package.mem_len);
131158
} else {
132-
*value_lhs = sort_package.object_operator /* Get the value */
133-
.get_value_ptr(sort_package.object_ptr, pos);
134-
*value_rhs = sort_package.object_operator
135-
.get_value_ptr(sort_package.object_ptr, pos + 1);
159+
value = sort_package.object_operator /* Get the element */
160+
.get_element_ptr(sort_package.object_ptr, index);
136161
}
137162

138-
if (NULL == *value_lhs &&
139-
NULL == *value_rhs) {
140-
return false;
163+
return value;
164+
}
165+
166+
/**
167+
* @brief This function will get the address of element.
168+
*
169+
* @param package the package of the sort
170+
* @param index the index of the element
171+
*
172+
* @return the address of the element
173+
*/
174+
175+
static inline void
176+
*sort_control_common_get_address(struct sort_package_s sort_package,
177+
size_t index)
178+
{
179+
assert(0 <= index);
180+
181+
static void *address;
182+
183+
if (NULL == sort_package.object_operator.get_element_ptr) { /* If the object operator is valid */
184+
address = (void *)((size_t)sort_package /* Calculate the address */
185+
.object_ptr + index * sort_package.mem_len);
186+
} else {
187+
address = sort_package.object_operator /* Get the address */
188+
.get_address_ptr(sort_package.object_ptr, index);
141189
}
142190

143-
return true;
191+
return address;
144192
}
145193

146194
/**
147-
* @brief This function will get the value of object.
195+
* @brief This function will compare the elements.
148196
*
149-
* @param sort_package the package of the sort information
150-
* @param pos the position of the value
151-
* @param value_lhs the pointer to the left side value
152-
* @param value_rhs the pointer to the right side value
197+
* @param sort_package the package of the sort
198+
* @param left the index of the left element
199+
* @param right the index of the right element
153200
*
154-
* @return if get the value of object successful
155-
* - true yes
156-
* - false no
201+
* @return the error code
157202
*/
158203

159-
static inline bool
160-
sort_control_get_address(struct sort_package_s sort_package,
161-
size_t pos,
162-
void **address_lhs,
163-
void **address_rhs)
204+
static inline errno_t
205+
sort_control_common_compare(struct sort_package_s package,
206+
size_t left,
207+
size_t right)
164208
{
165-
assert(0 <= pos);
209+
assert(0 <= left);
210+
assert(0 <= right);
166211

167-
if (NULL == sort_package.object_operator.get_address_ptr) {
168-
*address_lhs = (void *)((size_t)sort_package /* If the object operator is valid */
169-
.object_ptr + pos * sort_package.mem_len); /* Calculate the address of the value */
170-
*address_rhs = (void *)((size_t)sort_package
171-
.object_ptr + (pos + 1) * sort_package.mem_len);
172-
} else {
173-
*address_lhs = sort_package.object_operator
174-
.get_address_ptr(sort_package.object_ptr, pos); /* Get the address of the value */
175-
*address_rhs = sort_package.object_operator
176-
.get_address_ptr(sort_package.object_ptr, pos + 1);
212+
static void
213+
*value_lhs,
214+
*value_rhs;
215+
216+
value_lhs = sort_control_common_get_element(package, left);
217+
value_rhs = sort_control_common_get_element(package, right);
218+
219+
if (NULL == value_lhs &&
220+
NULL == value_rhs) {
221+
return 0xff;
177222
}
178223

179-
if (NULL == *address_lhs &&
180-
NULL == *address_rhs) {
181-
return false;
224+
#if (SORT_CFG_DEBUG_EN)
225+
226+
printf("sort_algorithm.compare: lhs:\"%s\" rhs:\"%s\" \r\n"
227+
, value_lhs, value_rhs);
228+
229+
#endif // (SORT_CFG_DEBUG_EN)
230+
231+
if (package.compare_ptr(value_lhs, value_rhs, package.mem_len_key)) {
232+
return 1;
182233
}
183234

184-
return true;
235+
return 0;
236+
}
237+
238+
/**
239+
* @brief This function will swap the elements.
240+
*
241+
* @param sort_package the package of the sort
242+
* @param left the index of the left element
243+
* @param right the index of the right element
244+
*
245+
* @return the error code
246+
*/
247+
248+
static inline errno_t
249+
sort_control_common_swap(struct sort_package_s package,
250+
size_t left,
251+
size_t right)
252+
{
253+
assert(0 <= left);
254+
assert(0 <= right);
255+
256+
static void
257+
*address_lhs,
258+
*address_rhs;
259+
260+
address_lhs = sort_control_common_get_address(package, left);
261+
address_rhs = sort_control_common_get_address(package, right);
262+
263+
if (NULL == address_lhs &&
264+
NULL == address_rhs) {
265+
return 1;
266+
}
267+
268+
#if (SORT_CFG_DEBUG_EN)
269+
270+
printf("sort_algorithm.swap: lhs:%p rhs:%p \r\n"
271+
, address_lhs, address_rhs);
272+
273+
#endif // (SORT_CFG_DEBUG_EN)
274+
275+
if (package.swap_ptr(address_lhs, address_rhs, package.mem_len)) { /* Swap the value */
276+
return 2;
277+
}
278+
279+
return 0;
185280
}
186281

187282
/**
@@ -199,8 +294,8 @@ errno_t sort_control_bubble_sort(struct sort_package_s package)
199294
assert(package.left < package.right);
200295
assert(package.mem_len);
201296
assert(package.object_ptr);
202-
assert((package.object_operator.get_value_ptr || package.object_operator.get_address_ptr)
203-
? (package.object_operator.get_value_ptr && package.object_operator.get_address_ptr)
297+
assert((package.object_operator.get_element_ptr || package.object_operator.get_address_ptr)
298+
? (package.object_operator.get_element_ptr && package.object_operator.get_address_ptr)
204299
: (true)); /* Assert if both the operators valid */
205300

206301
static char
@@ -224,46 +319,18 @@ errno_t sort_control_bubble_sort(struct sort_package_s package)
224319

225320
for (size_t cnt = package.left; cnt < package.right; cnt++) {
226321
for (size_t ct = package.left; ct < package.right - cnt; ct++) {
227-
if (!sort_control_get_value(package, ct, &value_lhs, &value_rhs)) { /* Get the value */
228-
return 1;
229-
}
230-
231-
#if (SORT_CFG_DEBUG_EN)
232-
233-
printf("sort_algorithm.bubble_sort.compare: lhs:\"%s\" rhs:\"%s\" \r\n"
234-
, value_lhs, value_rhs);
235-
236-
#endif // (SORT_CFG_DEBUG_EN)
322+
errno_t error_compare
323+
= sort_control_common_compare(package, ct, ct + 1); /* Compare the value */
237324

238-
if (package.compare_ptr(value_lhs, value_rhs, package.mem_len_key)) { /* Compare the value */
239-
if (!sort_control_get_address(package, ct, &address_lhs, &address_rhs)) {
325+
if (true == error_compare) {
326+
if (!sort_control_common_swap(package, ct, ct + 1)) { /* Swap the value */
240327
return 2;
241328
}
242-
243-
#if (SORT_CFG_DEBUG_EN)
244-
245-
printf("sort_algorithm.bubble_sort.swap: lhs:%p rhs:%p \r\n"
246-
, address_lhs, address_rhs);
247-
248-
#endif // (SORT_CFG_DEBUG_EN)
249-
250-
package.swap_ptr(address_lhs, address_rhs, package.mem_len); /* Swap the value */
329+
} else if (0xff == error_compare) {
330+
return 1;
251331
}
252332
}
253333
}
254334

255335
return 0;
256-
}
257-
258-
/**
259-
* @brief This function will sort the object by the quick sort algorithm.
260-
*
261-
* @param sort_package the information package of the sort
262-
*
263-
* @return void
264-
*/
265-
266-
errno_t sort_control_quick_sort(struct sort_package_s package)
267-
{
268-
269336
}

0 commit comments

Comments
 (0)