Added binarySearch, and isSorted functions.

This commit is contained in:
Бранимир Караџић
2022-08-07 19:26:28 -07:00
parent 74b5b7ef11
commit 81025f36da
3 changed files with 127 additions and 10 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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) );
}