Skip to content

Commit

Permalink
feat(DataTomeAnalysis): ✨ add median
Browse files Browse the repository at this point in the history
Add median and partial_median methods.

#6
  • Loading branch information
AlexandreHiroyuki committed Mar 25, 2024
1 parent ebe5152 commit 77e3366
Showing 2 changed files with 75 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/DataTomeAnalysis.h
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@

#include <DataTomeMvAvg.h>

#include <algorithm>
#include <cmath>
#include <cstdlib>

@@ -42,6 +43,30 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
return max;
}

TypeOfArray median() {
TypeOfArray median = 0;
size_t current_size = this->point_count();

TypeOfArray *temp =
(typeof(temp))malloc(current_size * sizeof(typeof(temp)));

for (size_t i = 0; i < current_size; i++) {
temp[i] = this->at_index(i);
}

std::sort(temp, temp + current_size);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
} else {
median = temp[current_size / 2];
}

free(temp);

return median;
}

TypeOfArray var() {
TypeOfArray average = this->get();

@@ -86,6 +111,30 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
return max;
}

TypeOfArray partial_median(size_t partial_id) {
TypeOfArray median = 0;
size_t current_size = this->partial_point_count(partial_id);

TypeOfArray *temp =
(typeof(temp))malloc(current_size * sizeof(typeof(temp)));

for (size_t i = 0; i < current_size; i++) {
temp[i] = (*this)[i];
}

std::sort(temp, temp + current_size);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
} else {
median = temp[current_size / 2];
}

free(temp);

return median;
}

TypeOfArray partial_var(size_t partial_id) {
TypeOfArray average = this->partial_get(partial_id);

26 changes: 26 additions & 0 deletions test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp
Original file line number Diff line number Diff line change
@@ -18,6 +18,16 @@ void test_minAndMax(void) {
TEST_ASSERT_EQUAL(data[9], TestMV.max());
}

void test_median(void) {
DataTomeAnalysis<int, long int> TestMV(10);

for (size_t i = 0; i < 10; i++) {
TestMV.push(data[i]);
}

TEST_ASSERT_EQUAL((data[4] + data[5]) / 2, TestMV.median());
}

void test_getVarianceAndStandardDeviation(void) {
DataTomeAnalysis<int, long int> TestMV(10);
size_t data_count = 5;
@@ -59,6 +69,20 @@ void test_partialMinAndMax(void) {
TEST_ASSERT_EQUAL(data[4], TestMV.partial_max(partial_id));
}

void test_partialMedian(void) {
DataTomeAnalysis<int, long int> TestMV(10);
size_t partial_size = 3;
size_t data_count = 5;

size_t partial_id = TestMV.partial_create(partial_size);

for (size_t i = 0; i < data_count; i++) {
TestMV.push(data[i]);
}

TEST_ASSERT_EQUAL(data[3], TestMV.partial_median(partial_id));
}

void test_partialVarianceAndStandardDeviation(void) {
DataTomeAnalysis<int, long int> TestMV(10);
size_t partial_size = 3;
@@ -93,8 +117,10 @@ void process() {
UNITY_BEGIN();

RUN_TEST(test_minAndMax);
RUN_TEST(test_median);
RUN_TEST(test_getVarianceAndStandardDeviation);
RUN_TEST(test_partialMinAndMax);
RUN_TEST(test_partialMedian);
RUN_TEST(test_partialVarianceAndStandardDeviation);

UNITY_END();

0 comments on commit 77e3366

Please sign in to comment.