Subversion Repositories HelenOS

Rev

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