Subversion Repositories HelenOS-historic

Rev

Rev 814 | Rev 1264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
333 bondari 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
 
814 palkovsky 29
#include <mm/slab.h>
331 bondari 30
#include <memstr.h>
31
#include <sort.h>
335 bondari 32
#include <panic.h>
331 bondari 33
 
349 jermar 34
#define EBUFSIZE    32
35
 
351 jermar 36
void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot);
37
void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot);
38
 
39
/** Quicksort wrapper
40
 *
41
 * This is only a wrapper that takes care of memory allocations for storing
42
 * the pivot and temporary elements for generic quicksort algorithm.
43
 *
822 palkovsky 44
 * This function _can_ sleep
45
 *
351 jermar 46
 * @param data Pointer to data to be sorted.
47
 * @param n Number of elements to be sorted.
48
 * @param e_size Size of one element.
49
 * @param cmp Comparator function.
50
 *
51
 */
350 jermar 52
void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
53
{
349 jermar 54
    __u8 buf_tmp[EBUFSIZE];
55
    __u8 buf_pivot[EBUFSIZE];
56
    void * tmp = buf_tmp;
57
    void * pivot = buf_pivot;
58
 
59
    if (e_size > EBUFSIZE) {
822 palkovsky 60
        pivot = (void *) malloc(e_size, 0);
61
        tmp = (void *) malloc(e_size, 0);
335 bondari 62
    }
63
 
351 jermar 64
    _qsort(data, n, e_size, cmp, tmp, pivot);
65
 
66
    if (e_size > EBUFSIZE) {
67
        free(tmp);
68
        free(pivot);
69
    }
70
}
71
 
72
/** Quicksort
73
 *
74
 * Apply generic quicksort algorithm on supplied data, using pre-allocated buffers.
75
 *
76
 * @param data Pointer to data to be sorted.
77
 * @param n Number of elements to be sorted.
78
 * @param e_size Size of one element.
79
 * @param cmp Comparator function.
80
 * @param tmp Pointer to scratch memory buffer e_size bytes long.
81
 * @param pivot Pointer to scratch memory buffer e_size bytes long.
82
 *
83
 */
84
void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot)
85
{
350 jermar 86
    if (n > 4) {
87
        int i = 0, j = n - 1;
88
 
89
        memcpy(pivot, data, e_size);
90
 
91
        while (1) {
92
            while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++;
93
            while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--;
94
            if (i<j) {
95
                memcpy(tmp, data + i * e_size, e_size);
96
                memcpy(data + i * e_size, data + j * e_size, e_size);
97
                memcpy(data + j * e_size, tmp, e_size);
98
            } else {
99
                break;
100
            }
101
        }
102
 
351 jermar 103
        _qsort(data, j + 1, e_size, cmp, tmp, pivot);
104
        _qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot);
350 jermar 105
    } else {
351 jermar 106
        _bubblesort(data, n, e_size, cmp, tmp);
350 jermar 107
    }
335 bondari 108
}
109
 
351 jermar 110
/** Bubblesort wrapper
111
 *
112
 * This is only a wrapper that takes care of memory allocation for storing
113
 * the slot element for generic bubblesort algorithm.
114
 *
115
 * @param data Pointer to data to be sorted.
116
 * @param n Number of elements to be sorted.
117
 * @param e_size Size of one element.
118
 * @param cmp Comparator function.
119
 *
120
 */
350 jermar 121
void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
122
{
349 jermar 123
    __u8 buf_slot[EBUFSIZE];
124
    void * slot = buf_slot;
125
 
126
    if (e_size > EBUFSIZE) {
822 palkovsky 127
        slot = (void *) malloc(e_size, 0);
349 jermar 128
    }
335 bondari 129
 
351 jermar 130
    _bubblesort(data, n, e_size, cmp, slot);
131
 
132
    if (e_size > EBUFSIZE) {
133
        free(slot);
134
    }
135
}
136
 
137
/** Bubblesort
138
 *
139
 * Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer.
140
 *
141
 * @param data Pointer to data to be sorted.
142
 * @param n Number of elements to be sorted.
143
 * @param e_size Size of one element.
144
 * @param cmp Comparator function.
145
 * @param slot Pointer to scratch memory buffer e_size bytes long.
146
 *
147
 */
148
void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot)
149
{
150
    bool done = false;
151
    void * p;
152
 
335 bondari 153
    while (!done) {
154
        done = true;
155
        for (p = data; p < data + e_size * (n - 1); p = p + e_size) {
156
            if (cmp(p, p + e_size) == 1) {
157
                memcpy(slot, p, e_size);
158
                memcpy(p, p + e_size, e_size);
159
                memcpy(p + e_size, slot, e_size);
160
                done = false;
161
            }
162
        }
163
    }
351 jermar 164
 
335 bondari 165
}
166
 
331 bondari 167
/*
168
 * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b
169
 */
350 jermar 170
int int_cmp(void * a, void * b)
171
{
333 bondari 172
    return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0;
331 bondari 173
}
174
 
350 jermar 175
int __u8_cmp(void * a, void * b)
176
{
333 bondari 177
    return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0;
331 bondari 178
}
179
 
350 jermar 180
int __u16_cmp(void * a, void * b)
181
{
333 bondari 182
    return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0;
331 bondari 183
}
184
 
350 jermar 185
int __u32_cmp(void * a, void * b)
186
{
333 bondari 187
    return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0;
331 bondari 188
}