[8e3fb24c] | 1 | /*
|
---|
| 2 | * Copyright (C) 2005 Sergey Bondari
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[8491c48] | 29 | #include <mm/heap.h>
|
---|
| 30 | #include <memstr.h>
|
---|
| 31 | #include <sort.h>
|
---|
[788ccb04] | 32 | #include <panic.h>
|
---|
[8491c48] | 33 |
|
---|
[01e48c1] | 34 | #define EBUFSIZE 32
|
---|
| 35 |
|
---|
[0a50f59] | 36 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
|
---|
| 37 | {
|
---|
[01e48c1] | 38 | __u8 buf_tmp[EBUFSIZE];
|
---|
| 39 | __u8 buf_pivot[EBUFSIZE];
|
---|
| 40 | void * tmp = buf_tmp;
|
---|
| 41 | void * pivot = buf_pivot;
|
---|
| 42 |
|
---|
| 43 | if (e_size > EBUFSIZE) {
|
---|
| 44 | pivot = (void *) malloc(e_size);
|
---|
| 45 | tmp = (void *) malloc(e_size);
|
---|
[788ccb04] | 46 |
|
---|
[01e48c1] | 47 | if (!tmp || !pivot) {
|
---|
| 48 | panic("Cannot allocate memory\n");
|
---|
| 49 | }
|
---|
[788ccb04] | 50 | }
|
---|
| 51 |
|
---|
[0a50f59] | 52 | if (n > 4) {
|
---|
| 53 | int i = 0, j = n - 1;
|
---|
| 54 |
|
---|
| 55 | memcpy(pivot, data, e_size);
|
---|
| 56 |
|
---|
| 57 | while (1) {
|
---|
| 58 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++;
|
---|
| 59 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--;
|
---|
| 60 | if (i<j) {
|
---|
| 61 | memcpy(tmp, data + i * e_size, e_size);
|
---|
| 62 | memcpy(data + i * e_size, data + j * e_size, e_size);
|
---|
| 63 | memcpy(data + j * e_size, tmp, e_size);
|
---|
| 64 | } else {
|
---|
| 65 | break;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | qsort(data, j + 1, e_size, cmp);
|
---|
| 70 | qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp);
|
---|
| 71 | } else {
|
---|
| 72 | bubblesort(data, n, e_size, cmp);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[788ccb04] | 75 |
|
---|
[01e48c1] | 76 | if (e_size > EBUFSIZE) {
|
---|
| 77 | free(tmp);
|
---|
| 78 | free(pivot);
|
---|
| 79 | }
|
---|
[788ccb04] | 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
[0a50f59] | 83 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
|
---|
| 84 | {
|
---|
[01e48c1] | 85 | __u8 buf_slot[EBUFSIZE];
|
---|
[788ccb04] | 86 | bool done = false;
|
---|
| 87 | void * p;
|
---|
[01e48c1] | 88 | void * slot = buf_slot;
|
---|
| 89 |
|
---|
| 90 | if (e_size > EBUFSIZE) {
|
---|
| 91 | slot = (void *) malloc(e_size);
|
---|
| 92 |
|
---|
| 93 | if (!slot) {
|
---|
| 94 | panic("Cannot allocate memory\n");
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[788ccb04] | 97 |
|
---|
| 98 | while (!done) {
|
---|
| 99 | done = true;
|
---|
| 100 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) {
|
---|
| 101 | if (cmp(p, p + e_size) == 1) {
|
---|
| 102 | memcpy(slot, p, e_size);
|
---|
| 103 | memcpy(p, p + e_size, e_size);
|
---|
| 104 | memcpy(p + e_size, slot, e_size);
|
---|
| 105 | done = false;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[01e48c1] | 110 | if (e_size > EBUFSIZE) {
|
---|
| 111 | free(slot);
|
---|
| 112 | }
|
---|
[788ccb04] | 113 | }
|
---|
| 114 |
|
---|
[8491c48] | 115 | /*
|
---|
| 116 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b
|
---|
| 117 | */
|
---|
[0a50f59] | 118 | int int_cmp(void * a, void * b)
|
---|
| 119 | {
|
---|
[8e3fb24c] | 120 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0;
|
---|
[8491c48] | 121 | }
|
---|
| 122 |
|
---|
[0a50f59] | 123 | int __u8_cmp(void * a, void * b)
|
---|
| 124 | {
|
---|
[8e3fb24c] | 125 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0;
|
---|
[8491c48] | 126 | }
|
---|
| 127 |
|
---|
[0a50f59] | 128 | int __u16_cmp(void * a, void * b)
|
---|
| 129 | {
|
---|
[8e3fb24c] | 130 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0;
|
---|
[8491c48] | 131 | }
|
---|
| 132 |
|
---|
[0a50f59] | 133 | int __u32_cmp(void * a, void * b)
|
---|
| 134 | {
|
---|
[8e3fb24c] | 135 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0;
|
---|
[8491c48] | 136 | }
|
---|