summaryrefslogtreecommitdiff
path: root/src/libc/vendor/sortix/qsort_r.c
blob: 8aff8074b9c859a51e883703f74bbaa5ed310594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2012, 2014, 2021 Jonas 'Sortie' Termansen.
 * Copyright (c) 2021 Juhani 'nortti' Krekelä.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * stdlib/qsort_r.c
 * Sort an array. Implemented using heapsort, which is not a stable sort.
 */

#include <stdbool.h>
#include <stdlib.h>

static void memswap(unsigned char* a, unsigned char* b, size_t size)
{
	unsigned char tmp;
	for ( size_t i = 0; i < size; i++ )
	{
		tmp = a[i];
		a[i] = b[i];
		b[i] = tmp;
	}
}

static unsigned char* array_index(unsigned char* base,
                                  size_t element_size,
                                  size_t index)
{
	return base + element_size * index;
}

void qsort_r(void* base_ptr,
             size_t num_elements,
             size_t element_size,
             int (*compare)(const void*, const void*, void*),
             void* arg)
{
	unsigned char* base = base_ptr;

	if ( !element_size || num_elements < 2 )
		return;

	// Incrementally left-to-right transform the array into a max-heap, where
	// each element has up to two children that aren't bigger than the element.
	for ( size_t i = 0; i < num_elements; i++ )
	{
		// Grow the heap by inserting another element into it (implicit, done by
		// moving the boundary between the heap and the yet-to-be-processed
		// array elements) and swapping it up the parent chain as long as it's
		// bigger than its parent.
		size_t element = i;
		while ( element > 0 )
		{
			unsigned char* ptr = array_index(base, element_size, element);
			size_t parent = (element - 1) / 2;
			unsigned char* parent_ptr = array_index(base, element_size, parent);
			if ( compare(parent_ptr, ptr, arg) < 0 )
			{
				memswap(parent_ptr, ptr, element_size);
				element = parent;
			}
			else
				break;
		}
	}

	// The array is split into two sections, first the max-heap and then the
	// part of the array that has been sorted.
	// Sorting progresses right-to-left, taking the biggest value of the heap
	// (at its root), swapping it with the last element of the heap, and
	// adjusting the boundary between the two sections such that the old biggest
	// value is now part of the sorted section.
	// After this, the max-heap property is restored by swapping the old last
	// element down as long as it's smaller than one of its children.
	for ( size_t size = num_elements; --size; )
	{
		memswap(array_index(base, element_size, size), base, element_size);

		size_t first_without_left = size / 2;
		size_t first_without_right = (size - 1) / 2;

		size_t element = 0;
		while ( element < size )
		{
			unsigned char* ptr = array_index(base, element_size, element);

			size_t left = 2 * element + 1;
			unsigned char* left_ptr = NULL;
			bool left_bigger = false;
			if ( element < first_without_left )
			{
				left_ptr = array_index(base, element_size, left);
				left_bigger = compare(ptr, left_ptr, arg) < 0;
			}

			size_t right = 2 * element + 2;
			unsigned char* right_ptr = NULL;
			bool right_bigger = false;
			if ( element < first_without_right )
			{
				right_ptr = array_index(base, element_size, right);
				right_bigger = compare(ptr, right_ptr, arg) < 0;
			}

			if ( left_bigger && right_bigger )
			{
				// If both the left and right child are bigger than the element,
				// then swap the element with whichever of the left and right
				// child is bigger.
				if ( compare(left_ptr, right_ptr, arg) < 0 )
				{
					memswap(ptr, right_ptr, element_size);
					element = right;
				}
				else
				{
					memswap(ptr, left_ptr, element_size);
					element = left;
				}
			}
			else if ( left_bigger )
			{
				memswap(ptr, left_ptr, element_size);
				element = left;
			}
			else if ( right_bigger )
			{
				memswap(ptr, right_ptr, element_size);
				element = right;
			}
			else
				break;
		}
	}
}