Rev 335 | Rev 349 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 335 | Rev 336 | ||
---|---|---|---|
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 | #include <mm/heap.h> |
29 | #include <mm/heap.h> |
30 | #include <memstr.h> |
30 | #include <memstr.h> |
31 | #include <sort.h> |
31 | #include <sort.h> |
32 | #include <panic.h> |
32 | #include <panic.h> |
33 | 33 | ||
34 | static void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void * pivot, void * tmp); |
34 | static void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void * pivot, void * tmp); |
35 | 35 | ||
36 | /* |
36 | /* |
37 | * Wrapper method for quicksort algorithm to decrease amount of allocations |
37 | * Wrapper method for quicksort algorithm to decrease amount of allocations |
38 | */ |
38 | */ |
39 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
39 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
40 | void * tmp = (void *) malloc(e_size); |
40 | void * tmp = (void *) malloc(e_size); |
41 | void * pivot = (void *) malloc(e_size); |
41 | void * pivot = (void *) malloc(e_size); |
42 | 42 | ||
43 | if (!tmp || !pivot) { |
43 | if (!tmp || !pivot) { |
44 | panic("qsort(): Cannot allocate memory\n"); |
44 | panic("Cannot allocate memory\n"); |
45 | } |
45 | } |
46 | 46 | ||
47 | _qsort(data, n, e_size, cmp, pivot, tmp); |
47 | _qsort(data, n, e_size, cmp, pivot, tmp); |
48 | 48 | ||
49 | free(tmp); |
49 | free(tmp); |
50 | free(pivot); |
50 | free(pivot); |
51 | } |
51 | } |
52 | 52 | ||
53 | 53 | ||
54 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
54 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
55 | bool done = false; |
55 | bool done = false; |
56 | void * p; |
56 | void * p; |
57 | void * slot = (void *) malloc(e_size); |
57 | void * slot = (void *) malloc(e_size); |
58 | 58 | ||
59 | while (!done) { |
59 | while (!done) { |
60 | done = true; |
60 | done = true; |
61 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
61 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
62 | if (cmp(p, p + e_size) == 1) { |
62 | if (cmp(p, p + e_size) == 1) { |
63 | memcpy(slot, p, e_size); |
63 | memcpy(slot, p, e_size); |
64 | memcpy(p, p + e_size, e_size); |
64 | memcpy(p, p + e_size, e_size); |
65 | memcpy(p + e_size, slot, e_size); |
65 | memcpy(p + e_size, slot, e_size); |
66 | done = false; |
66 | done = false; |
67 | } |
67 | } |
68 | } |
68 | } |
69 | } |
69 | } |
70 | 70 | ||
71 | free(slot); |
71 | free(slot); |
72 | } |
72 | } |
73 | 73 | ||
74 | static void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void * pivot, void * tmp) { |
74 | static void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void * pivot, void * tmp) { |
75 | if (n > 4) { |
75 | if (n > 4) { |
76 | int i = 0, j = n - 1; |
76 | int i = 0, j = n - 1; |
77 | 77 | ||
78 | memcpy(pivot, data, e_size); |
78 | memcpy(pivot, data, e_size); |
79 | 79 | ||
80 | while (1) { |
80 | while (1) { |
81 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
81 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
82 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
82 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
83 | if (i<j) { |
83 | if (i<j) { |
84 | memcpy(tmp, data + i * e_size, e_size); |
84 | memcpy(tmp, data + i * e_size, e_size); |
85 | memcpy(data + i * e_size, data + j * e_size, e_size); |
85 | memcpy(data + i * e_size, data + j * e_size, e_size); |
86 | memcpy(data + j * e_size, tmp, e_size); |
86 | memcpy(data + j * e_size, tmp, e_size); |
87 | } else { |
87 | } else { |
88 | break; |
88 | break; |
89 | } |
89 | } |
90 | } |
90 | } |
91 | 91 | ||
92 | qsort(data, j + 1, e_size, cmp); |
92 | qsort(data, j + 1, e_size, cmp); |
93 | qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp); |
93 | qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp); |
94 | } else { |
94 | } else { |
95 | bubblesort(data, n, e_size, cmp); |
95 | bubblesort(data, n, e_size, cmp); |
96 | } |
96 | } |
97 | } |
97 | } |
98 | 98 | ||
99 | 99 | ||
100 | 100 | ||
101 | /* |
101 | /* |
102 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
102 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
103 | */ |
103 | */ |
104 | int int_cmp(void * a, void * b) { |
104 | int int_cmp(void * a, void * b) { |
105 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
105 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
106 | } |
106 | } |
107 | 107 | ||
108 | int __u8_cmp(void * a, void * b) { |
108 | int __u8_cmp(void * a, void * b) { |
109 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
109 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
110 | } |
110 | } |
111 | 111 | ||
112 | int __u16_cmp(void * a, void * b) { |
112 | int __u16_cmp(void * a, void * b) { |
113 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
113 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
114 | } |
114 | } |
115 | 115 | ||
116 | int __u32_cmp(void * a, void * b) { |
116 | int __u32_cmp(void * a, void * b) { |
117 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
117 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
118 | } |
118 | } |
119 | 119 | ||
120 | 120 |