diff --git a/include/cppcore/Common/Sort.h b/include/cppcore/Common/Sort.h index fe6e9e1..e90f7ca 100644 --- a/include/cppcore/Common/Sort.h +++ b/include/cppcore/Common/Sort.h @@ -26,8 +26,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. namespace cppcore { + /// @brief The comparison function type typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); + /// @brief Compare two values in ascending order + /// @tparam T The type of the value + /// @param lhs The left hand side value + /// @param rhs The right hand side value + /// @return -1 if lhs < rhs, 0 if lhs == rhs, 1 if lhs > rhs template inline int32_t compAscending(const void *lhs, const void *rhs) { const T _lhs = *static_cast(lhs); @@ -35,11 +41,20 @@ namespace cppcore { return (_lhs > _rhs) - (_lhs < _rhs); } + /// @brief Compare two values in descending order + /// @tparam T The type of the value + /// @param lhs The left hand side value + /// @param rhs The right hand side value + /// @return -1 if lhs > rhs, 0 if lhs == rhs, 1 if lhs < rhs template inline int32_t compDescending(const void *_lhs, const void *_rhs) { return compAscending(_rhs, _lhs); } + /// @brief Swaps two POD values. + /// @tparam T The type of the value. + /// @param v1 The first value + /// @param v2 The second value template inline void swap(T& v1, T& v2) { T tmp = v1; @@ -47,12 +62,18 @@ namespace cppcore { v2 = tmp; } + /// @brief Swaps two uint8 values. + /// @param v1 The first value + /// @param v2 The second value inline void swap(uint8_t &lhs, uint8_t &rhs) { uint8_t tmp = lhs; lhs = rhs; rhs = tmp; } + /// @brief Swaps two POD values bitwise. + /// @param lhs The first value + /// @param rhs The second value inline void swap(void *v1, void *v2, size_t stride) { uint8_t *lhs = (uint8_t*) v1; uint8_t *rhs = (uint8_t*) v2; @@ -62,6 +83,12 @@ namespace cppcore { } } + /// @brief Implements the quicksort algorithm. + /// @param pivot The pivot value to start + /// @param _data The data to sort + /// @param num The number of elements to sort + /// @param stride The stride of the data, i.e. the size of each element + /// @param func The comparison function inline void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) { if (num < 2) { return; @@ -94,11 +121,22 @@ namespace cppcore { quicksortImpl(pivot, &data[g*stride], num - g, stride, func); } + /// @brief Implements the quicksort algorithm. + /// @param _data The data to sort + /// @param num The number of elements to sort + /// @param stride The stride of the data, i.e. the size of each element + /// @param func The comparison function inline void quicksort(void *_data, size_t num, size_t stride, ComparisonFn func) { uint8_t *pivot = (uint8_t*) CPPCORE_STACK_ALLOC(stride); quicksortImpl(pivot, _data, num, stride, func); } + /// @brief Checks if the data is sorted. + /// @param data The data to check + /// @param num The number of elements to check + /// @param stride The stride of the data, i.e. the size of each element + /// @param func The comparison function + /// @return true if the data is sorted, false otherwise bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) { if (num < 2) { return true; @@ -114,12 +152,19 @@ namespace cppcore { return true; } - inline int32_t binSearchImpl(void *key, void *data, size_t num, size_t stride, ComparisonFn func) { + /// @brief Implements a binary search algorithm. + /// @param key The key to search for + /// @param data The data to search in + /// @param num The number of elements to search + /// @param stride The stride of the data, i.e. the size of each element + /// @param func The comparison function + /// @return The index of the key if found, otherwise ~index + inline int32_t binSearchImpl(const void *key, const void *data, size_t num, size_t stride, ComparisonFn func) { size_t offset = 0; - uint8_t *_data = (uint8_t *)data; + const uint8_t *_data = (uint8_t *)data; for (size_t i = num; offset < i;) { size_t idx = (offset + i) / 2; - int32_t result = func(key, &_data[i * stride]); + int32_t result = func(key, &_data[idx * stride]); if (result < 0) { i = idx; } else if (result > 0) { @@ -130,8 +175,16 @@ namespace cppcore { } return ~offset; } - - int32_t binSearch(int32_t key, int32_t* array, size_t num, ComparisonFn func) { - return binSearchImpl(&key, &array[0], num, sizeof(int32_t), func); + + /// @brief Implements a binary search algorithm. + /// @tparam T The type of the value + /// @param key The key to search for + /// @param array The data to search in + /// @param num The number of elements to search + /// @param func The comparison function + /// @return The index of the key if found, otherwise ~index + template + inline int32_t binSearch(const T &key, const void *array, size_t num, ComparisonFn func) { + return binSearchImpl(&key, array, num, sizeof(T), func); } } // namespace cppcore diff --git a/include/cppcore/Memory/TPoolAllocator.h b/include/cppcore/Memory/TPoolAllocator.h index 60f7e38..4857a74 100644 --- a/include/cppcore/Memory/TPoolAllocator.h +++ b/include/cppcore/Memory/TPoolAllocator.h @@ -58,7 +58,8 @@ class TPoolAllocator { /// @brief The class destructor. ~TPoolAllocator(); - /// @brief + /// @brief WIll alloc one item from the pool. + /// @return The pointer to the allocated item. T *alloc(); /// @brief Will release all allocated items.