Skip to content

Commit 509bb41

Browse files
authored
Add inplace sort (#99)
1 parent fd9c4c4 commit 509bb41

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

include/dsplib/math.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ real_t corr(span_real x, span_real y, Correlation type = Correlation::Pearson);
106106
//result: [sorted array, sort index]
107107
std::pair<arr_real, arr_int> sort(const arr_real& x, Direction dir = Direction::Ascend);
108108

109+
//inplace sort
110+
void sort(inplace_real ix, Direction dir = Direction::Ascend);
111+
109112
//determine if array is sorted
110113
bool issorted(span_real x, Direction dir = Direction::Ascend);
111114

include/dsplib/utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ arr_real from_file(const std::string& file, dtype type = dtype::int16, endian or
8585
long count = std::numeric_limits<long>::max());
8686

8787
//add zeros to the end of the array
88-
arr_real zeropad(span_t<real_t> x, int n);
89-
arr_cmplx zeropad(span_t<cmplx_t> x, int n);
88+
arr_real zeropad(span_real x, int n);
89+
arr_cmplx zeropad(span_cmplx x, int n);
9090

9191
//delays or advances the signal by the number of samples specified in delay
9292
//TODO: fractional delay support
@@ -111,8 +111,8 @@ base_array<T> delayseq(const base_array<T>& data, int delay) {
111111
}
112112

113113
//peak localization
114-
real_t peakloc(const arr_real& x, int idx, bool cyclic = true);
115-
real_t peakloc(const arr_cmplx& x, int idx, bool cyclic = true);
114+
real_t peakloc(span_real x, int idx, bool cyclic = true);
115+
real_t peakloc(span_cmplx x, int idx, bool cyclic = true);
116116

117117
//Estimate delay between signals
118118
int finddelay(span_real x1, span_real x2);

lib/math.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ std::pair<arr_real, arr_int> sort(const arr_real& x, Direction dir) {
188188
return std::make_pair(sorted, index);
189189
}
190190

191+
void sort(inplace_real ix, Direction dir) {
192+
auto x = ix.get();
193+
if (issorted(x, dir)) {
194+
return;
195+
}
196+
197+
if (dir == Direction::Ascend) {
198+
std::sort(x.begin(), x.end());
199+
} else {
200+
std::sort(x.begin(), x.end(), std::greater<int>());
201+
}
202+
}
203+
191204
bool issorted(span_real x, Direction dir) {
192205
if (dir == Direction::Descend) {
193206
auto comp = [&](auto lhs, auto rhs) {

lib/utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ arr_real from_file(const std::string& file, dtype type, endian order, long offse
161161
}
162162

163163
//-------------------------------------------------------------------------------------------------
164-
real_t peakloc(const arr_real& x, int idx, bool cyclic) {
164+
real_t peakloc(span_real x, int idx, bool cyclic) {
165165
const int n = x.size();
166166
if (!cyclic && (idx == 0 || idx == n - 1)) {
167167
return idx;
@@ -177,7 +177,7 @@ real_t peakloc(const arr_real& x, int idx, bool cyclic) {
177177
return idx + q / (2 * a) - 1;
178178
}
179179

180-
real_t peakloc(const arr_cmplx& x, int idx, bool cyclic) {
180+
real_t peakloc(span_cmplx x, int idx, bool cyclic) {
181181
const int n = x.size();
182182
if (!cyclic && (idx == 0 || idx == n - 1)) {
183183
return idx;
@@ -328,11 +328,11 @@ base_array<T> _zeropad(span_t<T> x, int n) {
328328

329329
} // namespace
330330

331-
arr_real zeropad(span_t<real_t> x, int n) {
331+
arr_real zeropad(span_real x, int n) {
332332
return _zeropad<real_t>(x, n);
333333
}
334334

335-
arr_cmplx zeropad(span_t<cmplx_t> x, int n) {
335+
arr_cmplx zeropad(span_cmplx x, int n) {
336336
return _zeropad<cmplx_t>(x, n);
337337
}
338338

tests/math_test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,37 @@ TEST(MathTest, Sort) {
442442
}
443443
}
444444

445+
//-------------------------------------------------------------------------------------------------
446+
TEST(MathTest, InplaceSort) {
447+
{
448+
arr_real x1 = {4, 1, 2, 5, 6, 7, 3, 3};
449+
sort(inplace(x1));
450+
ASSERT_EQ_ARR_REAL(x1, arr_real{1, 2, 3, 3, 4, 5, 6, 7});
451+
ASSERT_TRUE(issorted(x1));
452+
}
453+
{
454+
arr_real x1 = {4, 1, 2, 5, 6, 7, 3, 3};
455+
sort(inplace(x1), Direction::Descend);
456+
ASSERT_EQ_ARR_REAL(x1, arr_real{7, 6, 5, 4, 3, 3, 2, 1});
457+
ASSERT_TRUE(issorted(x1, Direction::Descend));
458+
ASSERT_FALSE(issorted(x1, Direction::Ascend));
459+
}
460+
{
461+
arr_real x1 = {0, 1, 2, 3, 4, 5};
462+
sort(inplace(x1));
463+
ASSERT_EQ_ARR_REAL(x1, arr_real{0, 1, 2, 3, 4, 5});
464+
ASSERT_TRUE(issorted(x1));
465+
ASSERT_FALSE(issorted(x1, Direction::Descend));
466+
}
467+
{
468+
arr_real x1 = {5, 4, 3, 2, 1, 0};
469+
sort(inplace(x1), Direction::Descend);
470+
ASSERT_EQ_ARR_REAL(x1, arr_real{5, 4, 3, 2, 1, 0});
471+
ASSERT_TRUE(issorted(x1, Direction::Descend));
472+
ASSERT_FALSE(issorted(x1, Direction::Ascend));
473+
}
474+
}
475+
445476
//-------------------------------------------------------------------------------------------------
446477
TEST(MathTest, Factor) {
447478
ASSERT_EQ_ARR_REAL(factor(0), arr_int{0});

0 commit comments

Comments
 (0)