From ed3f57b1e3b9d7d75baadb63e45e8a3440216194 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 1 Apr 2025 15:53:00 +0200 Subject: [PATCH 1/4] Add first quicksort --- CMakeLists.txt | 2 + include/cppcore/Common/Sort.h | 73 +++++++++++++++++++++++++++++++++++ test/common/SortTest.cpp | 43 +++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 include/cppcore/Common/Sort.h create mode 100644 test/common/SortTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 06ba38f..4b095a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ SET ( cppcore_common_src include/cppcore/Common/Hash.h include/cppcore/Common/TStringBase.h include/cppcore/Common/Variant.h + include/cppcore/Common/Sort.h include/cppcore/Common/TBitField.h include/cppcore/Common/TOptional.h ) @@ -133,6 +134,7 @@ IF( CPPCORE_BUILD_UNITTESTS ) SET( cppcore_common_test_src test/common/HashTest.cpp test/common/VariantTest.cpp + test/common/SortTest.cpp test/common/TBitFieldTest.cpp test/common/TOptionalTest.cpp ) diff --git a/include/cppcore/Common/Sort.h b/include/cppcore/Common/Sort.h new file mode 100644 index 0000000..ecd5daf --- /dev/null +++ b/include/cppcore/Common/Sort.h @@ -0,0 +1,73 @@ +#pragma once + +#include + +namespace cppcore { + + typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); + + int32_t int_comp(const void *lhs, const void *rhs) { + int32_t _lhs=0, _rhs=0; + memcpy(&_lhs, lhs, sizeof(int32_t)); + memcpy(&_rhs, rhs, sizeof(int32_t)); + if (_lhs > _rhs) { + return 1; + } else if (lhs == rhs) { + return 0; + } + return -1; + } + + template + void swap(T *v1, T *v2) { + T tmp = *v1; + *v1 = *v2; + *v2 = *tmp; + } + + inline void quicksort(void *pivot, void *_data, size_t num, ComparisonFn func=int_comp) { + if (num < 2) { + return; + } + + if (_data == nullptr) { + return; + } + + size_t l = 0; + size_t g = 1; + uint8_t *data = (uint8_t*) _data; + for (size_t i=1; i 0) { + swap(&data[l], &data[i]); + ++l; + } else if (result == 0) { + swap(&data[g], &data[i]); + ++g; + ++i; + } else { + ++i; + } + } + + quicksort(pivot, &data[0], l, func); + quicksort(pivot, &data[g], num-g, func); + } + + bool isSorted(void *data, size_t num, ComparisonFn func) { + if (num < 2) { + return true; + } + + for (size_t i=0; i + +#include "gtest/gtest.h" + +using namespace cppcore; + +class SortTest : public testing::Test { +public: + +protected: + // empty +}; + +TEST_F(SortTest, isSortedTest ) { + int32_t arr[] = {1,2,3,4,5}; + bool sorted = isSorted(arr, 5, int_comp); + EXPECT_TRUE(sorted); +} + From af3d12613a35460d8f047355630fbd9665e2dc32 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 2 Apr 2025 00:03:13 +0200 Subject: [PATCH 2/4] Add more tests --- include/cppcore/Common/Sort.h | 51 ++++++++++++++++++----------------- test/common/SortTest.cpp | 14 +++++++++- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/include/cppcore/Common/Sort.h b/include/cppcore/Common/Sort.h index ecd5daf..400d85d 100644 --- a/include/cppcore/Common/Sort.h +++ b/include/cppcore/Common/Sort.h @@ -6,26 +6,29 @@ namespace cppcore { typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); - int32_t int_comp(const void *lhs, const void *rhs) { - int32_t _lhs=0, _rhs=0; - memcpy(&_lhs, lhs, sizeof(int32_t)); - memcpy(&_rhs, rhs, sizeof(int32_t)); - if (_lhs > _rhs) { - return 1; - } else if (lhs == rhs) { - return 0; - } - return -1; + template + int32_t compAscending(const void *lhs, const void *rhs) { + const T _lhs = *static_cast(lhs); + const T _rhs = *static_cast(rhs); + return (_lhs > _rhs) - (_lhs < _rhs); } - template - void swap(T *v1, T *v2) { - T tmp = *v1; - *v1 = *v2; - *v2 = *tmp; + inline void swap(uint8_t &lhs, uint8_t &rhs) { + uint8_t tmp = lhs; + lhs = rhs; + rhs = tmp; } - inline void quicksort(void *pivot, void *_data, size_t num, ComparisonFn func=int_comp) { + inline void swap(void *v1, void *v2, size_t stride) { + uint8_t *lhs = (uint8_t*) v1; + uint8_t *rhs = (uint8_t*) v2; + const uint8_t *end = rhs + stride; + while (rhs != end) { + swap(*lhs++, *rhs++); + } + } + + inline void quicksort(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) { if (num < 2) { return; } @@ -40,10 +43,10 @@ namespace cppcore { for (size_t i=1; i 0) { - swap(&data[l], &data[i]); + swap(&data[l*stride], &data[i*stride], stride); ++l; } else if (result == 0) { - swap(&data[g], &data[i]); + swap(&data[g*stride], &data[i*stride], stride); ++g; ++i; } else { @@ -51,17 +54,17 @@ namespace cppcore { } } - quicksort(pivot, &data[0], l, func); - quicksort(pivot, &data[g], num-g, func); + quicksort(pivot, &data[0], l, stride, func); + quicksort(pivot, &data[g], num - g, stride, func); } - bool isSorted(void *data, size_t num, ComparisonFn func) { + bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) { if (num < 2) { return true; } - - for (size_t i=0; i); EXPECT_TRUE(sorted); } +TEST_F(SortTest, isNotSortedTest) { + int32_t arr[] = { 1, 2, 3, 5, 4 }; + bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending); + EXPECT_FALSE(sorted); +} + +TEST_F(SortTest, quicksortTest) { + int32_t arr[] = { 1, 2, 3, 5, 4 }; + quicksort(&arr[0], &arr[0], 5, sizeof(int32_t), compAscending); + bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending); + EXPECT_TRUE(sorted); +} From 4cc6253c09faa4fff275331b473a6d3870ca93b2 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 2 Apr 2025 16:09:24 +0200 Subject: [PATCH 3/4] Fix quicksort --- code/cppcore.cpp | 2 +- include/cppcore/CPPCoreCommon.h | 3 ++ include/cppcore/Common/Sort.h | 53 +++++++++++++++++++--- include/cppcore/Common/TBitField.h | 2 +- include/cppcore/Common/TOptional.h | 2 +- include/cppcore/Common/TStringBase.h | 2 +- include/cppcore/Common/Variant.h | 2 +- include/cppcore/Container/TArray.h | 2 +- include/cppcore/Container/THashMap.h | 2 +- include/cppcore/Container/TList.h | 2 +- include/cppcore/Container/TQueue.h | 2 +- include/cppcore/Container/TStaticArray.h | 2 +- include/cppcore/Memory/MemUtils.h | 2 +- include/cppcore/Memory/TDefaultAllocator.h | 2 +- include/cppcore/Memory/TPoolAllocator.h | 2 +- include/cppcore/Memory/TStackAllocator.h | 2 +- include/cppcore/Random/RandomGenerator.h | 2 +- test/CPPCoreCommonTest.cpp | 2 +- test/common/HashTest.cpp | 2 +- test/common/SortTest.cpp | 25 +++++----- test/common/TBitFieldTest.cpp | 2 +- test/common/TOptionalTest.cpp | 2 +- test/common/VariantTest.cpp | 2 +- test/container/TArrayTest.cpp | 2 +- test/container/THashMapTest.cpp | 6 +-- test/container/TListTest.cpp | 2 +- test/container/TQueueTest.cpp | 2 +- test/container/TStaticArrayTest.cpp | 2 +- test/io/FileSystemTest.cpp | 2 +- test/memory/TPoolAllocatorTest.cpp | 7 ++- test/memory/TScratchAllocatorTest.cpp | 5 +- test/memory/TStackAllocatorTest.cpp | 2 +- test/random/RandomGeneratorTest.cpp | 2 +- 33 files changed, 98 insertions(+), 55 deletions(-) diff --git a/code/cppcore.cpp b/code/cppcore.cpp index 9e9df2c..aa13166 100644 --- a/code/cppcore.cpp +++ b/code/cppcore.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/CPPCoreCommon.h b/include/cppcore/CPPCoreCommon.h index 3eeeaf8..613c8e6 100644 --- a/include/cppcore/CPPCoreCommon.h +++ b/include/cppcore/CPPCoreCommon.h @@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include namespace cppcore { @@ -53,8 +54,10 @@ namespace cppcore { # endif // All disabled warnings for windows # pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class +# define CPPCORE_STACK_ALLOC(size) ::alloca(size) #else # define DLL_CPPCORE_EXPORT __attribute__((visibility("default"))) +# define CPPCORE_STACK_ALLOC(size) __builtin_alloca(size) #endif //------------------------------------------------------------------------------------------------- diff --git a/include/cppcore/Common/Sort.h b/include/cppcore/Common/Sort.h index 400d85d..2e3ed9a 100644 --- a/include/cppcore/Common/Sort.h +++ b/include/cppcore/Common/Sort.h @@ -1,3 +1,25 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2025 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ #pragma once #include @@ -7,12 +29,24 @@ namespace cppcore { typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs); template - int32_t compAscending(const void *lhs, const void *rhs) { + inline int32_t compAscending(const void *lhs, const void *rhs) { const T _lhs = *static_cast(lhs); const T _rhs = *static_cast(rhs); return (_lhs > _rhs) - (_lhs < _rhs); } + template + inline int32_t compDescending(const void *_lhs, const void *_rhs) { + return compAscending(_rhs, _lhs); + } + + template + inline void swap(T& v1, T& v2) { + T tmp = v1; + v1 = v2; + v2 = tmp; + } + inline void swap(uint8_t &lhs, uint8_t &rhs) { uint8_t tmp = lhs; lhs = rhs; @@ -28,7 +62,7 @@ namespace cppcore { } } - inline void quicksort(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) { + inline void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) { if (num < 2) { return; } @@ -37,11 +71,13 @@ namespace cppcore { return; } + uint8_t *data = (uint8_t*) _data; + memcpy(pivot, &data[0], stride); + size_t l = 0; size_t g = 1; - uint8_t *data = (uint8_t*) _data; for (size_t i=1; i 0) { swap(&data[l*stride], &data[i*stride], stride); ++l; @@ -54,8 +90,13 @@ namespace cppcore { } } - quicksort(pivot, &data[0], l, stride, func); - quicksort(pivot, &data[g], num - g, stride, func); + quicksortImpl(pivot, &data[0], l, stride, func); + quicksortImpl(pivot, &data[g*stride], num - g, stride, func); + } + + 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); } bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) { diff --git a/include/cppcore/Common/TBitField.h b/include/cppcore/Common/TBitField.h index 742d512..9e7fc4d 100644 --- a/include/cppcore/Common/TBitField.h +++ b/include/cppcore/Common/TBitField.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Common/TOptional.h b/include/cppcore/Common/TOptional.h index c1a9e97..4d449a4 100644 --- a/include/cppcore/Common/TOptional.h +++ b/include/cppcore/Common/TOptional.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Common/TStringBase.h b/include/cppcore/Common/TStringBase.h index 0305b8a..c6cce64 100644 --- a/include/cppcore/Common/TStringBase.h +++ b/include/cppcore/Common/TStringBase.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Common/Variant.h b/include/cppcore/Common/Variant.h index bfc58bb..b220254 100644 --- a/include/cppcore/Common/Variant.h +++ b/include/cppcore/Common/Variant.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Container/TArray.h b/include/cppcore/Container/TArray.h index b7bc463..443059e 100644 --- a/include/cppcore/Container/TArray.h +++ b/include/cppcore/Container/TArray.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Container/THashMap.h b/include/cppcore/Container/THashMap.h index bb20a2f..33a29d1 100644 --- a/include/cppcore/Container/THashMap.h +++ b/include/cppcore/Container/THashMap.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Container/TList.h b/include/cppcore/Container/TList.h index 2a16cde..cd1213b 100644 --- a/include/cppcore/Container/TList.h +++ b/include/cppcore/Container/TList.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Container/TQueue.h b/include/cppcore/Container/TQueue.h index 6c40f12..2e5255d 100644 --- a/include/cppcore/Container/TQueue.h +++ b/include/cppcore/Container/TQueue.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Container/TStaticArray.h b/include/cppcore/Container/TStaticArray.h index dc13f57..a24ab04 100644 --- a/include/cppcore/Container/TStaticArray.h +++ b/include/cppcore/Container/TStaticArray.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Memory/MemUtils.h b/include/cppcore/Memory/MemUtils.h index e6e681f..2bc9103 100644 --- a/include/cppcore/Memory/MemUtils.h +++ b/include/cppcore/Memory/MemUtils.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Memory/TDefaultAllocator.h b/include/cppcore/Memory/TDefaultAllocator.h index 583e3f4..bcc74f7 100644 --- a/include/cppcore/Memory/TDefaultAllocator.h +++ b/include/cppcore/Memory/TDefaultAllocator.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Memory/TPoolAllocator.h b/include/cppcore/Memory/TPoolAllocator.h index d212776..60f7e38 100644 --- a/include/cppcore/Memory/TPoolAllocator.h +++ b/include/cppcore/Memory/TPoolAllocator.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Memory/TStackAllocator.h b/include/cppcore/Memory/TStackAllocator.h index ece9b0c..fd28292 100644 --- a/include/cppcore/Memory/TStackAllocator.h +++ b/include/cppcore/Memory/TStackAllocator.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Random/RandomGenerator.h b/include/cppcore/Random/RandomGenerator.h index 797af28..7247176 100644 --- a/include/cppcore/Random/RandomGenerator.h +++ b/include/cppcore/Random/RandomGenerator.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/CPPCoreCommonTest.cpp b/test/CPPCoreCommonTest.cpp index 7065947..a435936 100644 --- a/test/CPPCoreCommonTest.cpp +++ b/test/CPPCoreCommonTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/common/HashTest.cpp b/test/common/HashTest.cpp index 80a4e9a..dd40c9b 100644 --- a/test/common/HashTest.cpp +++ b/test/common/HashTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/common/SortTest.cpp b/test/common/SortTest.cpp index f3fe721..73e5089 100644 --- a/test/common/SortTest.cpp +++ b/test/common/SortTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -28,28 +28,31 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using namespace cppcore; -class SortTest : public testing::Test { -public: - -protected: - // empty -}; +class SortTest : public testing::Test {}; + +TEST_F(SortTest, swapTest) { + int32_t i1 = 1; + int32_t i2 = 2; + swap(i1, i2); + EXPECT_EQ(i1, 2); + EXPECT_EQ(i2, 1); +} TEST_F(SortTest, isSortedTest ) { int32_t arr[] = {1,2,3,4,5}; - bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending); + bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending); EXPECT_TRUE(sorted); } TEST_F(SortTest, isNotSortedTest) { int32_t arr[] = { 1, 2, 3, 5, 4 }; - bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending); + bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending); EXPECT_FALSE(sorted); } TEST_F(SortTest, quicksortTest) { int32_t arr[] = { 1, 2, 3, 5, 4 }; - quicksort(&arr[0], &arr[0], 5, sizeof(int32_t), compAscending); - bool sorted = isSorted(arr, 5, sizeof(int32_t), compAscending); + quicksort(arr, 5, sizeof(int32_t), compDescending); + bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending); EXPECT_TRUE(sorted); } diff --git a/test/common/TBitFieldTest.cpp b/test/common/TBitFieldTest.cpp index 0a5c5f9..f86fccc 100644 --- a/test/common/TBitFieldTest.cpp +++ b/test/common/TBitFieldTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/common/TOptionalTest.cpp b/test/common/TOptionalTest.cpp index dce1787..980facb 100644 --- a/test/common/TOptionalTest.cpp +++ b/test/common/TOptionalTest.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/common/VariantTest.cpp b/test/common/VariantTest.cpp index 76745dc..de17f87 100644 --- a/test/common/VariantTest.cpp +++ b/test/common/VariantTest.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/container/TArrayTest.cpp b/test/container/TArrayTest.cpp index d4fc005..ead6072 100644 --- a/test/container/TArrayTest.cpp +++ b/test/container/TArrayTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/container/THashMapTest.cpp b/test/container/THashMapTest.cpp index 2a5c944..9aba9c7 100644 --- a/test/container/THashMapTest.cpp +++ b/test/container/THashMapTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -28,9 +28,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using namespace ::cppcore; -class THashMapTest : public ::testing::Test { - // empty -}; +class THashMapTest : public ::testing::Test {}; TEST_F( THashMapTest, constructTest ) { bool ok( true ); diff --git a/test/container/TListTest.cpp b/test/container/TListTest.cpp index 2a1566d..901e476 100644 --- a/test/container/TListTest.cpp +++ b/test/container/TListTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/container/TQueueTest.cpp b/test/container/TQueueTest.cpp index 2dbd94d..7072f20 100644 --- a/test/container/TQueueTest.cpp +++ b/test/container/TQueueTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/container/TStaticArrayTest.cpp b/test/container/TStaticArrayTest.cpp index 03ac3f9..c3a580b 100644 --- a/test/container/TStaticArrayTest.cpp +++ b/test/container/TStaticArrayTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/io/FileSystemTest.cpp b/test/io/FileSystemTest.cpp index 8885e53..f25f3ad 100644 --- a/test/io/FileSystemTest.cpp +++ b/test/io/FileSystemTest.cpp @@ -2,7 +2,7 @@ ------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/memory/TPoolAllocatorTest.cpp b/test/memory/TPoolAllocatorTest.cpp index eb6d5e3..09f0127 100644 --- a/test/memory/TPoolAllocatorTest.cpp +++ b/test/memory/TPoolAllocatorTest.cpp @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -34,8 +34,7 @@ class TPoolAllocatorTest : public testing::Test { struct PoolItem { int id; - PoolItem() - : id(0) { + PoolItem() : id(0) { // empty } }; @@ -50,7 +49,7 @@ TEST_F(TPoolAllocatorTest, createTest) { EXPECT_TRUE( ok ); } -static const size_t NumItems = 500; +static constexpr size_t NumItems = 500; TEST_F(TPoolAllocatorTest, alloc_access_Test) { TPoolAllocator allocator; diff --git a/test/memory/TScratchAllocatorTest.cpp b/test/memory/TScratchAllocatorTest.cpp index e85de85..48d29e3 100644 --- a/test/memory/TScratchAllocatorTest.cpp +++ b/test/memory/TScratchAllocatorTest.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -28,7 +28,7 @@ using namespace cppcore; class TScratchAllocatorTest : public testing::Test { public: - const size_t BufferSize = 1024u; + static constexpr size_t BufferSize = 1024u; }; TEST_F(TScratchAllocatorTest, CreateTest) { @@ -64,4 +64,3 @@ TEST_F(TScratchAllocatorTest, ClearTest) { EXPECT_EQ(myAllocator.capacity(), 0u); EXPECT_EQ(myAllocator.freeMem(), 0u); } - diff --git a/test/memory/TStackAllocatorTest.cpp b/test/memory/TStackAllocatorTest.cpp index dad0c99..e1f41f8 100644 --- a/test/memory/TStackAllocatorTest.cpp +++ b/test/memory/TStackAllocatorTest.cpp @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/test/random/RandomGeneratorTest.cpp b/test/random/RandomGeneratorTest.cpp index a06656c..20e8ed4 100644 --- a/test/random/RandomGeneratorTest.cpp +++ b/test/random/RandomGeneratorTest.cpp @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in From 579710f676a859eca317006bce8794eb55e88c25 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Wed, 2 Apr 2025 16:32:48 +0200 Subject: [PATCH 4/4] First version of binsearch --- include/cppcore/Common/Sort.h | 20 ++++++++++++++++++++ test/common/SortTest.cpp | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/include/cppcore/Common/Sort.h b/include/cppcore/Common/Sort.h index 2e3ed9a..fe6e9e1 100644 --- a/include/cppcore/Common/Sort.h +++ b/include/cppcore/Common/Sort.h @@ -114,4 +114,24 @@ namespace cppcore { return true; } + inline int32_t binSearchImpl(void *key, void *data, size_t num, size_t stride, ComparisonFn func) { + size_t offset = 0; + 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]); + if (result < 0) { + i = idx; + } else if (result > 0) { + offset = idx + 1; + } else { + return idx; + } + } + 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); + } } // namespace cppcore diff --git a/test/common/SortTest.cpp b/test/common/SortTest.cpp index 73e5089..dc56bef 100644 --- a/test/common/SortTest.cpp +++ b/test/common/SortTest.cpp @@ -56,3 +56,10 @@ TEST_F(SortTest, quicksortTest) { bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending); EXPECT_TRUE(sorted); } + +TEST_F(SortTest, binSearchTest) { + int32_t arr[] = { 1, 2, 3, 5, 4 }; + quicksort(arr, 5, sizeof(int32_t), compDescending); + int32_t idx = binSearch(3, arr, 5, compDescending); + EXPECT_EQ(idx, 2); +}