Subversion Repositories HelenOS

Rev

Rev 2745 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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