Skip to content

Commit

Permalink
feat(DataTomeAnalysis): ✨ add highest and lowest mode methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreHiroyuki committed Apr 13, 2024
1 parent 091833e commit e82cb17
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 20 deletions.
110 changes: 95 additions & 15 deletions src/DataTomeAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
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);
}
memcpy(temp, this->_array, current_size * sizeof(TypeOfArray));

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

Expand All @@ -68,28 +66,67 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
}

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

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

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

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

size_t max_count = 0;
TypeOfArray mode = temp[0];

size_t count = 0;
TypeOfArray current_number = temp[0];

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

if (count > max_count) {
max_count = count;
mode = current_number;
}
}

free(temp);

return mode;
}

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

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

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

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

size_t max_count = 0;
TypeOfArray mode = temp[0];

size_t count = 0;
TypeOfArray current_number = temp[0];

for (size_t i = 0; i < current_size; i++) {
size_t count = 0;
for (size_t j = 0; j < current_size; j++) {
if (temp[j] == temp[i]) count++;
if (temp[i] == current_number) {
count++;
} else {
current_number = temp[i];
count = 1;
}

if (count > max_count) {
if (count >= max_count) {
max_count = count;
mode = temp[i];
mode = current_number;
}
}

Expand Down Expand Up @@ -167,7 +204,6 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
}

TypeOfArray partial_lowest_mode(size_t partial_id) {
TypeOfArray mode = 0;
size_t current_size = this->partial_point_count(partial_id);

TypeOfArray *temp =
Expand All @@ -180,15 +216,59 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
std::sort(temp, temp + current_size);

size_t max_count = 0;
TypeOfArray mode = temp[0];

size_t count = 0;
TypeOfArray current_number = temp[0];

for (size_t i = 0; i < current_size; i++) {
size_t count = 0;
for (size_t j = 0; j < current_size; j++) {
if (temp[j] == temp[i]) count++;
if (temp[i] == current_number) {
count++;
} else {
current_number = temp[i];
count = 1;
}

if (count > max_count) {
max_count = count;
mode = temp[i];
mode = current_number;
}
}

free(temp);

return mode;
}

TypeOfArray partial_highest_mode(size_t partial_id) {
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);

size_t max_count = 0;
TypeOfArray mode = temp[0];

size_t count = 0;
TypeOfArray current_number = temp[0];

for (size_t i = 0; i < current_size; i++) {
if (temp[i] == current_number) {
count++;
} else {
current_number = temp[i];
count = 1;
}

if (count >= max_count) {
max_count = count;
mode = current_number;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/DataTomeMvAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

template <typename TypeOfArray, typename TypeOfSum = TypeOfArray>
class DataTomeMvAvg {
private:
protected:
size_t _array_size;
size_t _current_index;
size_t _average_counter;
Expand Down Expand Up @@ -217,7 +217,7 @@ class DataTomeMvAvg {

size_t partials_memory() {
return sizeof(_partial_sums_counter) + sizeof(_partial_sums) +
sizeof(_partial_sum_sizes);
sizeof(_partial_sum_sizes);
}
};

Expand Down
32 changes: 29 additions & 3 deletions test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void tearDown(void) {} // after test

long int data[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

long int mode_data[10] = {1, 1, 2, 3, 3, 3, 3, 4, 4, 5};
long int mode_data[10] = {1, 2, 3, 3, 3, 3, 4, 4, 4, 4};

void test_minAndMax(void) {
DataTomeAnalysis<int, long int> TestAnalysis(10);
Expand Down Expand Up @@ -40,6 +40,16 @@ void test_lowestMode(void) {
TEST_ASSERT_EQUAL(3, TestAnalysis.lowest_mode());
}

void test_highestMode(void) {
DataTomeAnalysis<int, long int> TestAnalysis(10);

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

TEST_ASSERT_EQUAL(4, TestAnalysis.highest_mode());
}

void test_getVarianceAndStandardDeviation(void) {
DataTomeAnalysis<int, long int> TestAnalysis(10);
size_t data_count = 5;
Expand Down Expand Up @@ -106,7 +116,21 @@ void test_partialLowestMode(void) {
TestAnalysis.push(mode_data[i]);
}

TEST_ASSERT_EQUAL(1, TestAnalysis.partial_lowest_mode(partial_id));
TEST_ASSERT_EQUAL(3, TestAnalysis.partial_lowest_mode(partial_id));
}

void test_partialHighestMode(void) {
DataTomeAnalysis<int, long int> TestAnalysis(10);
size_t partial_size = 5;
size_t data_count = 10;

size_t partial_id = TestAnalysis.partial_create(partial_size);

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

TEST_ASSERT_EQUAL(4, TestAnalysis.partial_highest_mode(partial_id));
}

void test_partialVarianceAndStandardDeviation(void) {
Expand Down Expand Up @@ -145,11 +169,13 @@ void process() {
RUN_TEST(test_minAndMax);
RUN_TEST(test_median);
RUN_TEST(test_lowestMode);
RUN_TEST(test_highestMode);
RUN_TEST(test_getVarianceAndStandardDeviation);

RUN_TEST(test_partialMinAndMax);
RUN_TEST(test_partialMedian);
RUN_TEST(test_partialLowestMode);
RUN_TEST(test_partialHighestMode);
RUN_TEST(test_partialVarianceAndStandardDeviation);

UNITY_END();
Expand Down

0 comments on commit e82cb17

Please sign in to comment.