@@ -26,33 +26,54 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727namespace cppcore {
2828
29+ // / @brief The comparison function type
2930 typedef int32_t (*ComparisonFn)(const void * _lhs, const void * _rhs);
3031
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
3137 template <class T >
3238 inline int32_t compAscending (const void *lhs, const void *rhs) {
3339 const T _lhs = *static_cast <const T *>(lhs);
3440 const T _rhs = *static_cast <const T *>(rhs);
3541 return (_lhs > _rhs) - (_lhs < _rhs);
3642 }
3743
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
3849 template <typename Ty>
3950 inline int32_t compDescending (const void *_lhs, const void *_rhs) {
4051 return compAscending<Ty>(_rhs, _lhs);
4152 }
4253
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
4358 template <class T >
4459 inline void swap (T& v1, T& v2) {
4560 T tmp = v1;
4661 v1 = v2;
4762 v2 = tmp;
4863 }
4964
65+ // / @brief Swaps two uint8 values.
66+ // / @param v1 The first value
67+ // / @param v2 The second value
5068 inline void swap (uint8_t &lhs, uint8_t &rhs) {
5169 uint8_t tmp = lhs;
5270 lhs = rhs;
5371 rhs = tmp;
5472 }
5573
74+ // / @brief Swaps two POD values bitwise.
75+ // / @param lhs The first value
76+ // / @param rhs The second value
5677 inline void swap (void *v1, void *v2, size_t stride) {
5778 uint8_t *lhs = (uint8_t *) v1;
5879 uint8_t *rhs = (uint8_t *) v2;
@@ -62,6 +83,12 @@ namespace cppcore {
6283 }
6384 }
6485
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
6592 inline void quicksortImpl (void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) {
6693 if (num < 2 ) {
6794 return ;
@@ -94,11 +121,22 @@ namespace cppcore {
94121 quicksortImpl (pivot, &data[g*stride], num - g, stride, func);
95122 }
96123
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
97129 inline void quicksort (void *_data, size_t num, size_t stride, ComparisonFn func) {
98130 uint8_t *pivot = (uint8_t *) CPPCORE_STACK_ALLOC (stride);
99131 quicksortImpl (pivot, _data, num, stride, func);
100132 }
101133
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
102140 bool isSorted (const void *data, size_t num, size_t stride, ComparisonFn func) {
103141 if (num < 2 ) {
104142 return true ;
@@ -114,12 +152,19 @@ namespace cppcore {
114152 return true ;
115153 }
116154
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) {
118163 size_t offset = 0 ;
119- uint8_t *_data = (uint8_t *)data;
164+ const uint8_t *_data = (uint8_t *)data;
120165 for (size_t i = num; offset < i;) {
121166 size_t idx = (offset + i) / 2 ;
122- int32_t result = func (key, &_data[i * stride]);
167+ int32_t result = func (key, &_data[idx * stride]);
123168 if (result < 0 ) {
124169 i = idx;
125170 } else if (result > 0 ) {
@@ -130,8 +175,16 @@ namespace cppcore {
130175 }
131176 return ~offset;
132177 }
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);
136189 }
137190} // namespace cppcore
0 commit comments