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
86104void 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
102120void * 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 }
0 commit comments