@@ -26,33 +26,54 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
26
27
27
namespace cppcore {
28
28
29
+ // / @brief The comparison function type
29
30
typedef int32_t (*ComparisonFn)(const void * _lhs, const void * _rhs);
30
31
32
+ // / @brief Compare two values in ascending order
33
+ // / @tparam T The type of the value
34
+ // / @param lhs The left hand side value
35
+ // / @param rhs The right hand side value
36
+ // / @return -1 if lhs < rhs, 0 if lhs == rhs, 1 if lhs > rhs
31
37
template <class T >
32
38
inline int32_t compAscending (const void *lhs, const void *rhs) {
33
39
const T _lhs = *static_cast <const T *>(lhs);
34
40
const T _rhs = *static_cast <const T *>(rhs);
35
41
return (_lhs > _rhs) - (_lhs < _rhs);
36
42
}
37
43
44
+ // / @brief Compare two values in descending order
45
+ // / @tparam T The type of the value
46
+ // / @param lhs The left hand side value
47
+ // / @param rhs The right hand side value
48
+ // / @return -1 if lhs > rhs, 0 if lhs == rhs, 1 if lhs < rhs
38
49
template <typename Ty>
39
50
inline int32_t compDescending (const void *_lhs, const void *_rhs) {
40
51
return compAscending<Ty>(_rhs, _lhs);
41
52
}
42
53
54
+ // / @brief Swaps two POD values.
55
+ // / @tparam T The type of the value.
56
+ // / @param v1 The first value
57
+ // / @param v2 The second value
43
58
template <class T >
44
59
inline void swap (T& v1, T& v2) {
45
60
T tmp = v1;
46
61
v1 = v2;
47
62
v2 = tmp;
48
63
}
49
64
65
+ // / @brief Swaps two uint8 values.
66
+ // / @param v1 The first value
67
+ // / @param v2 The second value
50
68
inline void swap (uint8_t &lhs, uint8_t &rhs) {
51
69
uint8_t tmp = lhs;
52
70
lhs = rhs;
53
71
rhs = tmp;
54
72
}
55
73
74
+ // / @brief Swaps two POD values bitwise.
75
+ // / @param lhs The first value
76
+ // / @param rhs The second value
56
77
inline void swap (void *v1, void *v2, size_t stride) {
57
78
uint8_t *lhs = (uint8_t *) v1;
58
79
uint8_t *rhs = (uint8_t *) v2;
@@ -62,6 +83,12 @@ namespace cppcore {
62
83
}
63
84
}
64
85
86
+ // / @brief Implements the quicksort algorithm.
87
+ // / @param pivot The pivot value to start
88
+ // / @param _data The data to sort
89
+ // / @param num The number of elements to sort
90
+ // / @param stride The stride of the data, i.e. the size of each element
91
+ // / @param func The comparison function
65
92
inline void quicksortImpl (void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) {
66
93
if (num < 2 ) {
67
94
return ;
@@ -94,11 +121,22 @@ namespace cppcore {
94
121
quicksortImpl (pivot, &data[g*stride], num - g, stride, func);
95
122
}
96
123
124
+ // / @brief Implements the quicksort algorithm.
125
+ // / @param _data The data to sort
126
+ // / @param num The number of elements to sort
127
+ // / @param stride The stride of the data, i.e. the size of each element
128
+ // / @param func The comparison function
97
129
inline void quicksort (void *_data, size_t num, size_t stride, ComparisonFn func) {
98
130
uint8_t *pivot = (uint8_t *) CPPCORE_STACK_ALLOC (stride);
99
131
quicksortImpl (pivot, _data, num, stride, func);
100
132
}
101
133
134
+ // / @brief Checks if the data is sorted.
135
+ // / @param data The data to check
136
+ // / @param num The number of elements to check
137
+ // / @param stride The stride of the data, i.e. the size of each element
138
+ // / @param func The comparison function
139
+ // / @return true if the data is sorted, false otherwise
102
140
bool isSorted (const void *data, size_t num, size_t stride, ComparisonFn func) {
103
141
if (num < 2 ) {
104
142
return true ;
@@ -114,12 +152,19 @@ namespace cppcore {
114
152
return true ;
115
153
}
116
154
117
- inline int32_t binSearchImpl (void *key, void *data, size_t num, size_t stride, ComparisonFn func) {
155
+ // / @brief Implements a binary search algorithm.
156
+ // / @param key The key to search for
157
+ // / @param data The data to search in
158
+ // / @param num The number of elements to search
159
+ // / @param stride The stride of the data, i.e. the size of each element
160
+ // / @param func The comparison function
161
+ // / @return The index of the key if found, otherwise ~index
162
+ inline int32_t binSearchImpl (const void *key, const void *data, size_t num, size_t stride, ComparisonFn func) {
118
163
size_t offset = 0 ;
119
- uint8_t *_data = (uint8_t *)data;
164
+ const uint8_t *_data = (uint8_t *)data;
120
165
for (size_t i = num; offset < i;) {
121
166
size_t idx = (offset + i) / 2 ;
122
- int32_t result = func (key, &_data[i * stride]);
167
+ int32_t result = func (key, &_data[idx * stride]);
123
168
if (result < 0 ) {
124
169
i = idx;
125
170
} else if (result > 0 ) {
@@ -130,8 +175,16 @@ namespace cppcore {
130
175
}
131
176
return ~offset;
132
177
}
133
-
134
- int32_t binSearch (int32_t key, int32_t * array, size_t num, ComparisonFn func) {
135
- return binSearchImpl (&key, &array[0 ], num, sizeof (int32_t ), func);
178
+
179
+ // / @brief Implements a binary search algorithm.
180
+ // / @tparam T The type of the value
181
+ // / @param key The key to search for
182
+ // / @param array The data to search in
183
+ // / @param num The number of elements to search
184
+ // / @param func The comparison function
185
+ // / @return The index of the key if found, otherwise ~index
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);
136
189
}
137
190
} // namespace cppcore
0 commit comments