diff --git a/src/DataTomeAnalysis.h b/src/DataTomeAnalysis.h index 98b1966..f780d2d 100644 --- a/src/DataTomeAnalysis.h +++ b/src/DataTomeAnalysis.h @@ -50,9 +50,7 @@ class DataTomeAnalysis : public DataTomeMvAvg { 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); @@ -68,28 +66,67 @@ class DataTomeAnalysis : public DataTomeMvAvg { } 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; } } @@ -167,7 +204,6 @@ class DataTomeAnalysis : public DataTomeMvAvg { } TypeOfArray partial_lowest_mode(size_t partial_id) { - TypeOfArray mode = 0; size_t current_size = this->partial_point_count(partial_id); TypeOfArray *temp = @@ -180,15 +216,59 @@ class DataTomeAnalysis : public DataTomeMvAvg { 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; } } diff --git a/src/DataTomeMvAvg.h b/src/DataTomeMvAvg.h index 227df86..5aa4916 100644 --- a/src/DataTomeMvAvg.h +++ b/src/DataTomeMvAvg.h @@ -10,7 +10,7 @@ template class DataTomeMvAvg { - private: + protected: size_t _array_size; size_t _current_index; size_t _average_counter; @@ -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); } }; diff --git a/test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp b/test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp index 3d57f80..ad50ad1 100644 --- a/test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp +++ b/test/test_DataTomeAnalysis/DataTomeAnalysis.test.cpp @@ -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 TestAnalysis(10); @@ -40,6 +40,16 @@ void test_lowestMode(void) { TEST_ASSERT_EQUAL(3, TestAnalysis.lowest_mode()); } +void test_highestMode(void) { + DataTomeAnalysis 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 TestAnalysis(10); size_t data_count = 5; @@ -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 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) { @@ -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();