cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
Sort.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27namespace cppcore {
28
30 typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs);
31
37 template<class T>
38 inline int32_t compAscending(const void *lhs, const void *rhs) {
39 const T _lhs = *static_cast<const T *>(lhs);
40 const T _rhs = *static_cast<const T *>(rhs);
41 return (_lhs > _rhs) - (_lhs < _rhs);
42 }
43
49 template <typename Ty>
50 inline int32_t compDescending(const void *_lhs, const void *_rhs) {
51 return compAscending<Ty>(_rhs, _lhs);
52 }
53
58 template<class T>
59 inline void swap(T& v1, T& v2) {
60 T tmp = v1;
61 v1 = v2;
62 v2 = tmp;
63 }
64
68 inline void swap(uint8_t &lhs, uint8_t &rhs) {
69 uint8_t tmp = lhs;
70 lhs = rhs;
71 rhs = tmp;
72 }
73
77 inline void swap(void *v1, void *v2, size_t stride) {
78 auto *lhs = (uint8_t*) v1;
79 auto *rhs = (uint8_t*) v2;
80 const auto *end = rhs + stride;
81 while (rhs != end) {
82 swap(*lhs++, *rhs++);
83 }
84 }
85
92 inline void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) {
93 if (num < 2) {
94 return;
95 }
96
97 if (_data == nullptr) {
98 return;
99 }
100
101 auto *data = (uint8_t*) _data;
102 memcpy(pivot, &data[0], stride);
103
104 size_t l = 0;
105 size_t g = 1;
106 for (size_t i=1; i<num;) {
107 int32_t result = func(&data[i*stride], pivot);
108 if (result > 0) {
109 swap(&data[l*stride], &data[i*stride], stride);
110 ++l;
111 } else if (result == 0) {
112 swap(&data[g*stride], &data[i*stride], stride);
113 ++g;
114 ++i;
115 } else {
116 ++i;
117 }
118 }
119
120 quicksortImpl(pivot, &data[0], l, stride, func);
121 quicksortImpl(pivot, &data[g*stride], num - g, stride, func);
122 }
123
129 inline void quicksort(void *_data, size_t num, size_t stride, ComparisonFn func) {
130 auto *pivot = (uint8_t*) CPPCORE_STACK_ALLOC(stride);
131 quicksortImpl(pivot, _data, num, stride, func);
132 }
133
140 bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) {
141 if (num < 2) {
142 return true;
143 }
144 auto *data_ = (uint8_t *)data;
145 for (size_t i=1; i<num; ++i) {
146 const int32_t result = func(&data_[(i-1)*stride], &data_[i * stride]);
147 if (result == -1) {
148 return false;
149 }
150 }
151
152 return true;
153 }
154
162 inline int32_t binSearchImpl(const void *key, const void *data, size_t num, size_t stride, ComparisonFn func) {
163 size_t offset = 0l;
164 const uint8_t *_data = (uint8_t *)data;
165 for (size_t i = num; offset < i;) {
166 size_t idx = (offset + i) / 2;
167 int32_t result = func(key, &_data[idx * stride]);
168 if (result < 0) {
169 i = idx;
170 } else if (result > 0) {
171 offset = idx + 1;
172 } else {
173 return idx;
174 }
175 }
176 return ~offset;
177 }
178
186 template<class T>
187 inline int32_t binSearch(const T &key, const void *array, size_t num, ComparisonFn func) {
188 return binSearchImpl(&key, array, num, sizeof(T), func);
189 }
190} // namespace cppcore
#define CPPCORE_STACK_ALLOC(size)
int32_t(* ComparisonFn)(const void *_lhs, const void *_rhs)
The comparison function type.
Definition Sort.h:30
void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func)
Implements the quicksort algorithm.
Definition Sort.h:92
int32_t compDescending(const void *_lhs, const void *_rhs)
Compare two values in descending order.
Definition Sort.h:50
int32_t binSearchImpl(const void *key, const void *data, size_t num, size_t stride, ComparisonFn func)
Implements a binary search algorithm.
Definition Sort.h:162
int32_t compAscending(const void *lhs, const void *rhs)
Compare two values in ascending order.
Definition Sort.h:38
void quicksort(void *_data, size_t num, size_t stride, ComparisonFn func)
Implements the quicksort algorithm.
Definition Sort.h:129
int32_t binSearch(const T &key, const void *array, size_t num, ComparisonFn func)
Implements a binary search algorithm.
Definition Sort.h:187
bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func)
Checks if the data is sorted.
Definition Sort.h:140
void swap(T &v1, T &v2)
Swaps two POD values.
Definition Sort.h:59