From 81025f36dab8408cea226b24297689bb71d4ba43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sun, 7 Aug 2022 19:26:28 -0700 Subject: [PATCH] Added binarySearch, and isSorted functions. --- include/bx/sort.h | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/sort.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ tests/sort_test.cpp | 44 +++++++++++++++++++++++++++++++---------- 3 files changed, 127 insertions(+), 10 deletions(-) diff --git a/include/bx/sort.h b/include/bx/sort.h index 8a648ae..408a0b7 100644 --- a/include/bx/sort.h +++ b/include/bx/sort.h @@ -10,9 +10,21 @@ namespace bx { + /// The function compares the `_lhs` and `_rhs` values. + /// + /// @returns Returns value: + /// - less than zero if `_lhs` is less than `_rhs` + /// - zero if `_lhs` is equivalent to `_rhs` + /// - greater than zero if `_lhs` is greater than `_rhs` /// typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); + /// Performs sort (Quick Sort algorithm). + /// + /// @param _data Pointer to sorted array data. + /// @param _num Number of elements. + /// @param _stride Element stride in bytes. + /// @param _fn Comparison function. /// void quickSort( void* _data @@ -55,6 +67,42 @@ namespace bx , uint32_t _size ); + /// Performs check if array is sorted. + /// + /// @param _data Pointer to sorted array data. + /// @param _num Number of elements. + /// @param _stride Element stride in bytes. + /// @param _fn Comparison function. + /// + /// @returns Returns `true` if array is sorted, otherwise returns `false`. + /// + bool isSorted( + const void* _data + , uint32_t _num + , uint32_t _stride + , const ComparisonFn _fn + ); + + /// Performs binary search of a sorted array. + /// + /// @param _key Pointer to the key to search for. + /// @param _data Pointer to sorted array data. + /// @param _num Number of elements. + /// @param _stride Element stride in bytes. + /// @param _fn Comparison function. + /// + /// @remarks Array must be sorted! + /// + /// @returns Returns index of element or -1 if the key is not found in sorted array. + /// + int32_t binarySearch( + const void* _key + , const void* _data + , uint32_t _num + , uint32_t _stride + , const ComparisonFn _fn + ); + } // namespace bx #include "inline/sort.inl" diff --git a/src/sort.cpp b/src/sort.cpp index 0717564..25f7ea9 100644 --- a/src/sort.cpp +++ b/src/sort.cpp @@ -51,5 +51,50 @@ namespace bx quickSortR(pivot, _data, _num, _stride, _fn); } + bool isSorted(const void* _data, uint32_t _num, uint32_t _stride, const ComparisonFn _fn) + { + const uint8_t* data = (uint8_t*)_data; + + for (uint32_t ii = 1; ii < _num; ++ii) + { + int32_t result = _fn(&data[(ii-1)*_stride], &data[ii*_stride]); + + if (0 < result) + { + return false; + } + } + + return true; + } + + int32_t binarySearch(const void* _key, const void* _data, uint32_t _num, uint32_t _stride, const ComparisonFn _fn) + { + uint32_t offset = 0; + const uint8_t* data = (uint8_t*)_data; + + for (uint32_t ll = _num; offset < ll;) + { + const uint32_t idx = (offset + ll) / 2; + + int32_t result = _fn(_key, &data[idx * _stride]); + + if (result < 0) + { + ll = idx; + } + else if (result > 0) + { + offset = idx + 1; + } + else + { + return idx; + } + } + + return -1; + } + } // namespace bx diff --git a/tests/sort_test.cpp b/tests/sort_test.cpp index 7a8c1cc..57551e2 100644 --- a/tests/sort_test.cpp +++ b/tests/sort_test.cpp @@ -18,19 +18,45 @@ TEST_CASE("quickSort", "") "jagoda", }; - bx::quickSort(str, BX_COUNTOF(str), sizeof(void*) - , [](const void* _lhs, const void* _rhs) + auto strCmpFn = [](const void* _lhs, const void* _rhs) { const char* lhs = *(const char**)_lhs; const char* rhs = *(const char**)_rhs; return bx::strCmp(lhs, rhs); - }); + }; + + REQUIRE(!bx::isSorted(str, BX_COUNTOF(str), sizeof(str[0]), strCmpFn) ); + + bx::quickSort(str, BX_COUNTOF(str), sizeof(str[0]), strCmpFn); REQUIRE(0 == bx::strCmp(str[0], "jabuka") ); REQUIRE(0 == bx::strCmp(str[1], "jagoda") ); REQUIRE(0 == bx::strCmp(str[2], "kruska") ); REQUIRE(0 == bx::strCmp(str[3], "malina") ); + REQUIRE(bx::isSorted(str, BX_COUNTOF(str), sizeof(str[0]), strCmpFn) ); + + auto bsearchStrCmpFn = [](const void* _lhs, const void* _rhs) + { + const char* lhs = (const char*)_lhs; + const char* rhs = *(const char**)_rhs; + return bx::strCmp(lhs, rhs); + }; + + REQUIRE(-1 == bx::binarySearch("sljiva", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + REQUIRE( 0 == bx::binarySearch("jabuka", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + REQUIRE( 1 == bx::binarySearch("jagoda", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + REQUIRE( 2 == bx::binarySearch("kruska", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + REQUIRE( 3 == bx::binarySearch("malina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + REQUIRE(-1 == bx::binarySearch("kupina", str, BX_COUNTOF(str), sizeof(str[0]), bsearchStrCmpFn) ); + + auto byteCmpFn = [](const void* _lhs, const void* _rhs) + { + int8_t lhs = *(const int8_t*)_lhs; + int8_t rhs = *(const int8_t*)_rhs; + return lhs - rhs; + }; + int8_t byte[128]; bx::RngMwc rng; for (uint32_t ii = 0; ii < BX_COUNTOF(byte); ++ii) @@ -38,16 +64,14 @@ TEST_CASE("quickSort", "") byte[ii] = rng.gen()&0xff; } - bx::quickSort(byte, BX_COUNTOF(byte), 1 - , [](const void* _lhs, const void* _rhs) - { - int8_t lhs = *(const int8_t*)_lhs; - int8_t rhs = *(const int8_t*)_rhs; - return lhs - rhs; - }); + REQUIRE(!bx::isSorted(byte, BX_COUNTOF(byte), sizeof(byte[0]), byteCmpFn) ); + + bx::quickSort(byte, BX_COUNTOF(byte), sizeof(byte[0]), byteCmpFn); for (uint32_t ii = 1; ii < BX_COUNTOF(byte); ++ii) { REQUIRE(byte[ii-1] <= byte[ii]); } + + REQUIRE(bx::isSorted(byte, BX_COUNTOF(byte), sizeof(byte[0]), byteCmpFn) ); }