Rev 1264 | Rev 1757 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1264 | Rev 1702 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Sergey Bondari |
2 | * Copyright (C) 2005 Sergey Bondari |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
- | 29 | /** @addtogroup generic |
|
- | 30 | * @{ |
|
- | 31 | */ |
|
- | 32 | ||
29 | /** |
33 | /** |
30 | * @file sort.c |
34 | * @file |
31 | * @brief Sorting functions. |
35 | * @brief Sorting functions. |
32 | * |
36 | * |
33 | * This files contains functions implementing several sorting |
37 | * This files contains functions implementing several sorting |
34 | * algorithms (e.g. quick sort and bubble sort). |
38 | * algorithms (e.g. quick sort and bubble sort). |
35 | */ |
39 | */ |
36 | 40 | ||
37 | #include <mm/slab.h> |
41 | #include <mm/slab.h> |
38 | #include <memstr.h> |
42 | #include <memstr.h> |
39 | #include <sort.h> |
43 | #include <sort.h> |
40 | #include <panic.h> |
44 | #include <panic.h> |
41 | 45 | ||
42 | #define EBUFSIZE 32 |
46 | #define EBUFSIZE 32 |
43 | 47 | ||
44 | void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot); |
48 | void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot); |
45 | void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot); |
49 | void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot); |
46 | 50 | ||
47 | /** Quicksort wrapper |
51 | /** Quicksort wrapper |
48 | * |
52 | * |
49 | * This is only a wrapper that takes care of memory allocations for storing |
53 | * This is only a wrapper that takes care of memory allocations for storing |
50 | * the pivot and temporary elements for generic quicksort algorithm. |
54 | * the pivot and temporary elements for generic quicksort algorithm. |
51 | * |
55 | * |
52 | * This function _can_ sleep |
56 | * This function _can_ sleep |
53 | * |
57 | * |
54 | * @param data Pointer to data to be sorted. |
58 | * @param data Pointer to data to be sorted. |
55 | * @param n Number of elements to be sorted. |
59 | * @param n Number of elements to be sorted. |
56 | * @param e_size Size of one element. |
60 | * @param e_size Size of one element. |
57 | * @param cmp Comparator function. |
61 | * @param cmp Comparator function. |
58 | * |
62 | * |
59 | */ |
63 | */ |
60 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) |
64 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) |
61 | { |
65 | { |
62 | __u8 buf_tmp[EBUFSIZE]; |
66 | __u8 buf_tmp[EBUFSIZE]; |
63 | __u8 buf_pivot[EBUFSIZE]; |
67 | __u8 buf_pivot[EBUFSIZE]; |
64 | void * tmp = buf_tmp; |
68 | void * tmp = buf_tmp; |
65 | void * pivot = buf_pivot; |
69 | void * pivot = buf_pivot; |
66 | 70 | ||
67 | if (e_size > EBUFSIZE) { |
71 | if (e_size > EBUFSIZE) { |
68 | pivot = (void *) malloc(e_size, 0); |
72 | pivot = (void *) malloc(e_size, 0); |
69 | tmp = (void *) malloc(e_size, 0); |
73 | tmp = (void *) malloc(e_size, 0); |
70 | } |
74 | } |
71 | 75 | ||
72 | _qsort(data, n, e_size, cmp, tmp, pivot); |
76 | _qsort(data, n, e_size, cmp, tmp, pivot); |
73 | 77 | ||
74 | if (e_size > EBUFSIZE) { |
78 | if (e_size > EBUFSIZE) { |
75 | free(tmp); |
79 | free(tmp); |
76 | free(pivot); |
80 | free(pivot); |
77 | } |
81 | } |
78 | } |
82 | } |
79 | 83 | ||
80 | /** Quicksort |
84 | /** Quicksort |
81 | * |
85 | * |
82 | * Apply generic quicksort algorithm on supplied data, using pre-allocated buffers. |
86 | * Apply generic quicksort algorithm on supplied data, using pre-allocated buffers. |
83 | * |
87 | * |
84 | * @param data Pointer to data to be sorted. |
88 | * @param data Pointer to data to be sorted. |
85 | * @param n Number of elements to be sorted. |
89 | * @param n Number of elements to be sorted. |
86 | * @param e_size Size of one element. |
90 | * @param e_size Size of one element. |
87 | * @param cmp Comparator function. |
91 | * @param cmp Comparator function. |
88 | * @param tmp Pointer to scratch memory buffer e_size bytes long. |
92 | * @param tmp Pointer to scratch memory buffer e_size bytes long. |
89 | * @param pivot Pointer to scratch memory buffer e_size bytes long. |
93 | * @param pivot Pointer to scratch memory buffer e_size bytes long. |
90 | * |
94 | * |
91 | */ |
95 | */ |
92 | void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot) |
96 | void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot) |
93 | { |
97 | { |
94 | if (n > 4) { |
98 | if (n > 4) { |
95 | int i = 0, j = n - 1; |
99 | int i = 0, j = n - 1; |
96 | 100 | ||
97 | memcpy(pivot, data, e_size); |
101 | memcpy(pivot, data, e_size); |
98 | 102 | ||
99 | while (1) { |
103 | while (1) { |
100 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
104 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
101 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
105 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
102 | if (i<j) { |
106 | if (i<j) { |
103 | memcpy(tmp, data + i * e_size, e_size); |
107 | memcpy(tmp, data + i * e_size, e_size); |
104 | memcpy(data + i * e_size, data + j * e_size, e_size); |
108 | memcpy(data + i * e_size, data + j * e_size, e_size); |
105 | memcpy(data + j * e_size, tmp, e_size); |
109 | memcpy(data + j * e_size, tmp, e_size); |
106 | } else { |
110 | } else { |
107 | break; |
111 | break; |
108 | } |
112 | } |
109 | } |
113 | } |
110 | 114 | ||
111 | _qsort(data, j + 1, e_size, cmp, tmp, pivot); |
115 | _qsort(data, j + 1, e_size, cmp, tmp, pivot); |
112 | _qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot); |
116 | _qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot); |
113 | } else { |
117 | } else { |
114 | _bubblesort(data, n, e_size, cmp, tmp); |
118 | _bubblesort(data, n, e_size, cmp, tmp); |
115 | } |
119 | } |
116 | } |
120 | } |
117 | 121 | ||
118 | /** Bubblesort wrapper |
122 | /** Bubblesort wrapper |
119 | * |
123 | * |
120 | * This is only a wrapper that takes care of memory allocation for storing |
124 | * This is only a wrapper that takes care of memory allocation for storing |
121 | * the slot element for generic bubblesort algorithm. |
125 | * the slot element for generic bubblesort algorithm. |
122 | * |
126 | * |
123 | * @param data Pointer to data to be sorted. |
127 | * @param data Pointer to data to be sorted. |
124 | * @param n Number of elements to be sorted. |
128 | * @param n Number of elements to be sorted. |
125 | * @param e_size Size of one element. |
129 | * @param e_size Size of one element. |
126 | * @param cmp Comparator function. |
130 | * @param cmp Comparator function. |
127 | * |
131 | * |
128 | */ |
132 | */ |
129 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) |
133 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) |
130 | { |
134 | { |
131 | __u8 buf_slot[EBUFSIZE]; |
135 | __u8 buf_slot[EBUFSIZE]; |
132 | void * slot = buf_slot; |
136 | void * slot = buf_slot; |
133 | 137 | ||
134 | if (e_size > EBUFSIZE) { |
138 | if (e_size > EBUFSIZE) { |
135 | slot = (void *) malloc(e_size, 0); |
139 | slot = (void *) malloc(e_size, 0); |
136 | } |
140 | } |
137 | 141 | ||
138 | _bubblesort(data, n, e_size, cmp, slot); |
142 | _bubblesort(data, n, e_size, cmp, slot); |
139 | 143 | ||
140 | if (e_size > EBUFSIZE) { |
144 | if (e_size > EBUFSIZE) { |
141 | free(slot); |
145 | free(slot); |
142 | } |
146 | } |
143 | } |
147 | } |
144 | 148 | ||
145 | /** Bubblesort |
149 | /** Bubblesort |
146 | * |
150 | * |
147 | * Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer. |
151 | * Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer. |
148 | * |
152 | * |
149 | * @param data Pointer to data to be sorted. |
153 | * @param data Pointer to data to be sorted. |
150 | * @param n Number of elements to be sorted. |
154 | * @param n Number of elements to be sorted. |
151 | * @param e_size Size of one element. |
155 | * @param e_size Size of one element. |
152 | * @param cmp Comparator function. |
156 | * @param cmp Comparator function. |
153 | * @param slot Pointer to scratch memory buffer e_size bytes long. |
157 | * @param slot Pointer to scratch memory buffer e_size bytes long. |
154 | * |
158 | * |
155 | */ |
159 | */ |
156 | void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot) |
160 | void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot) |
157 | { |
161 | { |
158 | bool done = false; |
162 | bool done = false; |
159 | void * p; |
163 | void * p; |
160 | 164 | ||
161 | while (!done) { |
165 | while (!done) { |
162 | done = true; |
166 | done = true; |
163 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
167 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
164 | if (cmp(p, p + e_size) == 1) { |
168 | if (cmp(p, p + e_size) == 1) { |
165 | memcpy(slot, p, e_size); |
169 | memcpy(slot, p, e_size); |
166 | memcpy(p, p + e_size, e_size); |
170 | memcpy(p, p + e_size, e_size); |
167 | memcpy(p + e_size, slot, e_size); |
171 | memcpy(p + e_size, slot, e_size); |
168 | done = false; |
172 | done = false; |
169 | } |
173 | } |
170 | } |
174 | } |
171 | } |
175 | } |
172 | 176 | ||
173 | } |
177 | } |
174 | 178 | ||
175 | /* |
179 | /* |
176 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
180 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
177 | */ |
181 | */ |
178 | int int_cmp(void * a, void * b) |
182 | int int_cmp(void * a, void * b) |
179 | { |
183 | { |
180 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
184 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
181 | } |
185 | } |
182 | 186 | ||
183 | int __u8_cmp(void * a, void * b) |
187 | int __u8_cmp(void * a, void * b) |
184 | { |
188 | { |
185 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
189 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
186 | } |
190 | } |
187 | 191 | ||
188 | int __u16_cmp(void * a, void * b) |
192 | int __u16_cmp(void * a, void * b) |
189 | { |
193 | { |
190 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
194 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
191 | } |
195 | } |
192 | 196 | ||
193 | int __u32_cmp(void * a, void * b) |
197 | int __u32_cmp(void * a, void * b) |
194 | { |
198 | { |
195 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
199 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
196 | } |
200 | } |
- | 201 | ||
- | 202 | /** @} |
|
- | 203 | */ |
|
- | 204 | ||
197 | 205 |