Skip to content

Commit

Permalink
fix(DataTomeAnalysis): 🐛 fix qsort callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreHiroyuki committed Aug 5, 2024
1 parent a285fbf commit eb71f55
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/DataTome.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

#include <DataTomeAnalysis.h>
#include <DataTomeMvAvg.h>
#include <DataTomeUtils.h>

#endif // DATA_TOME_H
31 changes: 7 additions & 24 deletions src/DataTomeAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define DATA_TOME_ANALYSIS_H

#include <DataTomeMvAvg.h>
#include <DataTomeUtils.h>
#include <math.h>
#include <stdlib.h>

Expand Down Expand Up @@ -43,10 +44,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
Expand All @@ -67,10 +65,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -105,10 +100,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {

memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -180,10 +172,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
Expand All @@ -206,10 +195,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down Expand Up @@ -246,10 +232,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
temp[i] = (*this)[i];
}

qsort(temp, current_size, sizeof(TypeOfArray),
[](const void *a, const void *b) -> int {
return (*(TypeOfArray *)a - *(TypeOfArray *)b);
});
qsort(temp, current_size, sizeof(TypeOfArray), sort_ascend<TypeOfArray>);

size_t max_count = 0;
TypeOfArray mode = temp[0];
Expand Down
17 changes: 17 additions & 0 deletions src/DataTomeUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/***************************************************************
DataTomeUtils.h
Created by Alexandre Hiroyuki Yamauchi, August 05, 2024.
***************************************************************/

#ifndef DATA_TOME_UTILS_H
#define DATA_TOME_UTILS_H

template <typename TypeOfArray>
int sort_ascend(const void *cmp1, const void *cmp2) {
TypeOfArray b = *((TypeOfArray *)cmp2);
TypeOfArray a = *((TypeOfArray *)cmp1);

return (int)a - b;
}

#endif // DATA_TOME_UTILS_H

0 comments on commit eb71f55

Please sign in to comment.