source: mainline/uspace/lib/c/generic/gsort.c@ 74017ce

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 74017ce was 16bfcd3, checked in by jzr <zarevucky.jiri@…>, 8 years ago

Fix up headers.

  • Property mode set to 100644
File size: 3.6 KB
Line 
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
29/** @addtogroup libc
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Sorting functions.
36 *
37 * This files contains functions implementing several sorting
38 * algorithms (e.g. quick sort and gnome sort).
39 *
40 */
41
42#include <gsort.h>
43#include <inttypes.h>
44#include <mem.h>
45#include <malloc.h>
46
47/** Immediate buffer size.
48 *
49 * For small buffer sizes avoid doing malloc()
50 * and use the stack.
51 *
52 */
53#define IBUF_SIZE 32
54
55/** Array accessor.
56 *
57 */
58#define INDEX(buf, i, elem_size) ((buf) + (i) * (elem_size))
59
60/** Gnome sort
61 *
62 * Apply generic gnome sort algorithm on supplied data,
63 * using pre-allocated buffer.
64 *
65 * @param data Pointer to data to be sorted.
66 * @param cnt Number of elements to be sorted.
67 * @param elem_size Size of one element.
68 * @param cmp Comparator function.
69 * @param arg 3rd argument passed to cmp.
70 * @param slot Pointer to scratch memory buffer
71 * elem_size bytes long.
72 *
73 */
74static void _gsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp,
75 void *arg, void *slot)
76{
77 size_t i = 0;
78
79 while (i < cnt) {
80 if ((i != 0) &&
81 (cmp(INDEX(data, i, elem_size),
82 INDEX(data, i - 1, elem_size), arg) == -1)) {
83 memcpy(slot, INDEX(data, i, elem_size), elem_size);
84 memcpy(INDEX(data, i, elem_size), INDEX(data, i - 1, elem_size),
85 elem_size);
86 memcpy(INDEX(data, i - 1, elem_size), slot, elem_size);
87 i--;
88 } else
89 i++;
90 }
91}
92
93/** Gnome sort wrapper
94 *
95 * This is only a wrapper that takes care of memory
96 * allocations for storing the slot element for generic
97 * gnome sort algorithm.
98 *
99 * @param data Pointer to data to be sorted.
100 * @param cnt Number of elements to be sorted.
101 * @param elem_size Size of one element.
102 * @param cmp Comparator function.
103 * @param arg 3rd argument passed to cmp.
104 *
105 * @return True if sorting succeeded.
106 *
107 */
108bool gsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp, void *arg)
109{
110 uint8_t ibuf_slot[IBUF_SIZE];
111 void *slot;
112
113 if (elem_size > IBUF_SIZE) {
114 slot = (void *) malloc(elem_size);
115 if (!slot)
116 return false;
117 } else
118 slot = (void *) ibuf_slot;
119
120 _gsort(data, cnt, elem_size, cmp, arg, slot);
121
122 if (elem_size > IBUF_SIZE)
123 free(slot);
124
125 return true;
126}
127
128/** @}
129 */
Note: See TracBrowser for help on using the repository browser.