Skip to content

Commit 2c7cd0b

Browse files
authored
Merge pull request #35 from kimkulling/feature/add_sort
Feature/add sort
2 parents 095eec4 + 579710f commit 2c7cd0b

34 files changed

+241
-38
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ SET ( cppcore_common_src
7777
include/cppcore/Common/Hash.h
7878
include/cppcore/Common/TStringBase.h
7979
include/cppcore/Common/Variant.h
80+
include/cppcore/Common/Sort.h
8081
include/cppcore/Common/TBitField.h
8182
include/cppcore/Common/TOptional.h
8283
)
@@ -133,6 +134,7 @@ IF( CPPCORE_BUILD_UNITTESTS )
133134
SET( cppcore_common_test_src
134135
test/common/HashTest.cpp
135136
test/common/VariantTest.cpp
137+
test/common/SortTest.cpp
136138
test/common/TBitFieldTest.cpp
137139
test/common/TOptionalTest.cpp
138140
)

code/cppcore.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/CPPCoreCommon.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929
#include <stddef.h>
3030
#include <stdio.h>
3131
#include <stdarg.h>
32+
#include <malloc.h>
3233

3334
namespace cppcore {
3435

@@ -53,8 +54,10 @@ namespace cppcore {
5354
# endif
5455
// All disabled warnings for windows
5556
# pragma warning( disable : 4251 ) // <class> needs to have dll-interface to be used by clients of class <class>
57+
# define CPPCORE_STACK_ALLOC(size) ::alloca(size)
5658
#else
5759
# define DLL_CPPCORE_EXPORT __attribute__((visibility("default")))
60+
# define CPPCORE_STACK_ALLOC(size) __builtin_alloca(size)
5861
#endif
5962

6063
//-------------------------------------------------------------------------------------------------

include/cppcore/Common/Sort.h

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014-2025 Kim Kulling
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
-----------------------------------------------------------------------------------------------*/
23+
#pragma once
24+
25+
#include <cppcore/CPPCoreCommon.h>
26+
27+
namespace cppcore {
28+
29+
typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs);
30+
31+
template<class T>
32+
inline int32_t compAscending(const void *lhs, const void *rhs) {
33+
const T _lhs = *static_cast<const T *>(lhs);
34+
const T _rhs = *static_cast<const T *>(rhs);
35+
return (_lhs > _rhs) - (_lhs < _rhs);
36+
}
37+
38+
template <typename Ty>
39+
inline int32_t compDescending(const void *_lhs, const void *_rhs) {
40+
return compAscending<Ty>(_rhs, _lhs);
41+
}
42+
43+
template<class T>
44+
inline void swap(T& v1, T& v2) {
45+
T tmp = v1;
46+
v1 = v2;
47+
v2 = tmp;
48+
}
49+
50+
inline void swap(uint8_t &lhs, uint8_t &rhs) {
51+
uint8_t tmp = lhs;
52+
lhs = rhs;
53+
rhs = tmp;
54+
}
55+
56+
inline void swap(void *v1, void *v2, size_t stride) {
57+
uint8_t *lhs = (uint8_t*) v1;
58+
uint8_t *rhs = (uint8_t*) v2;
59+
const uint8_t *end = rhs + stride;
60+
while (rhs != end) {
61+
swap(*lhs++, *rhs++);
62+
}
63+
}
64+
65+
inline void quicksortImpl(void *pivot, void *_data, size_t num, size_t stride, ComparisonFn func) {
66+
if (num < 2) {
67+
return;
68+
}
69+
70+
if (_data == nullptr) {
71+
return;
72+
}
73+
74+
uint8_t *data = (uint8_t*) _data;
75+
memcpy(pivot, &data[0], stride);
76+
77+
size_t l = 0;
78+
size_t g = 1;
79+
for (size_t i=1; i<num;) {
80+
int32_t result = func(&data[i*stride], pivot);
81+
if (result > 0) {
82+
swap(&data[l*stride], &data[i*stride], stride);
83+
++l;
84+
} else if (result == 0) {
85+
swap(&data[g*stride], &data[i*stride], stride);
86+
++g;
87+
++i;
88+
} else {
89+
++i;
90+
}
91+
}
92+
93+
quicksortImpl(pivot, &data[0], l, stride, func);
94+
quicksortImpl(pivot, &data[g*stride], num - g, stride, func);
95+
}
96+
97+
inline void quicksort(void *_data, size_t num, size_t stride, ComparisonFn func) {
98+
uint8_t *pivot = (uint8_t*) CPPCORE_STACK_ALLOC(stride);
99+
quicksortImpl(pivot, _data, num, stride, func);
100+
}
101+
102+
bool isSorted(const void *data, size_t num, size_t stride, ComparisonFn func) {
103+
if (num < 2) {
104+
return true;
105+
}
106+
uint8_t *data_ = (uint8_t *)data;
107+
for (size_t i=1; i<num; ++i) {
108+
const int32_t result = func(&data_[(i-1)*stride], &data_[i * stride]);
109+
if (result == -1) {
110+
return false;
111+
}
112+
}
113+
114+
return true;
115+
}
116+
117+
inline int32_t binSearchImpl(void *key, void *data, size_t num, size_t stride, ComparisonFn func) {
118+
size_t offset = 0;
119+
uint8_t *_data = (uint8_t *)data;
120+
for (size_t i = num; offset < i;) {
121+
size_t idx = (offset + i) / 2;
122+
int32_t result = func(key, &_data[i * stride]);
123+
if (result < 0) {
124+
i = idx;
125+
} else if (result > 0) {
126+
offset = idx + 1;
127+
} else {
128+
return idx;
129+
}
130+
}
131+
return ~offset;
132+
}
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);
136+
}
137+
} // namespace cppcore

include/cppcore/Common/TBitField.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Common/TOptional.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Common/TStringBase.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Common/Variant.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Container/TArray.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Container/THashMap.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Container/TList.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Container/TQueue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Container/TStaticArray.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Memory/MemUtils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Memory/TDefaultAllocator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Memory/TPoolAllocator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Memory/TStackAllocator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

include/cppcore/Random/RandomGenerator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-----------------------------------------------------------------------------------------------
22
The MIT License (MIT)
33
4-
Copyright (c) 2014-2024 Kim Kulling
4+
Copyright (c) 2014-2025 Kim Kulling
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy of
77
this software and associated documentation files (the "Software"), to deal in

test/CPPCoreCommonTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-------------------------------------------------------------------------------------------------
33
The MIT License (MIT)
44
5-
Copyright (c) 2014-2024 Kim Kulling
5+
Copyright (c) 2014-2025 Kim Kulling
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy of
88
this software and associated documentation files (the "Software"), to deal in

test/common/HashTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-------------------------------------------------------------------------------------------------
33
The MIT License (MIT)
44
5-
Copyright (c) 2014-2024 Kim Kulling
5+
Copyright (c) 2014-2025 Kim Kulling
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy of
88
this software and associated documentation files (the "Software"), to deal in

test/common/SortTest.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
-------------------------------------------------------------------------------------------------
3+
The MIT License (MIT)
4+
5+
Copyright (c) 2014-2025 Kim Kulling
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
this software and associated documentation files (the "Software"), to deal in
9+
the Software without restriction, including without limitation the rights to
10+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
the Software, and to permit persons to whom the Software is furnished to do so,
12+
subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
-------------------------------------------------------------------------------------------------
24+
*/
25+
#include <cppcore/Common/Sort.h>
26+
27+
#include "gtest/gtest.h"
28+
29+
using namespace cppcore;
30+
31+
class SortTest : public testing::Test {};
32+
33+
TEST_F(SortTest, swapTest) {
34+
int32_t i1 = 1;
35+
int32_t i2 = 2;
36+
swap(i1, i2);
37+
EXPECT_EQ(i1, 2);
38+
EXPECT_EQ(i2, 1);
39+
}
40+
41+
TEST_F(SortTest, isSortedTest ) {
42+
int32_t arr[] = {1,2,3,4,5};
43+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
44+
EXPECT_TRUE(sorted);
45+
}
46+
47+
TEST_F(SortTest, isNotSortedTest) {
48+
int32_t arr[] = { 1, 2, 3, 5, 4 };
49+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
50+
EXPECT_FALSE(sorted);
51+
}
52+
53+
TEST_F(SortTest, quicksortTest) {
54+
int32_t arr[] = { 1, 2, 3, 5, 4 };
55+
quicksort(arr, 5, sizeof(int32_t), compDescending<int32_t>);
56+
bool sorted = isSorted(arr, 5, sizeof(int32_t), compDescending<int32_t>);
57+
EXPECT_TRUE(sorted);
58+
}
59+
60+
TEST_F(SortTest, binSearchTest) {
61+
int32_t arr[] = { 1, 2, 3, 5, 4 };
62+
quicksort(arr, 5, sizeof(int32_t), compDescending<int32_t>);
63+
int32_t idx = binSearch(3, arr, 5, compDescending<int32_t>);
64+
EXPECT_EQ(idx, 2);
65+
}

0 commit comments

Comments
 (0)