From 49d6c709100fa82bbc580aaa3681f8502db2cfa3 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 7 Oct 2015 17:26:54 +0100 Subject: [PATCH] Removed the need for the 'end' node. Reduces the container size by one pointer. --- iforward_list.h | 36 +++++++++++++++++++++--------------- test/test_forward_list.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/iforward_list.h b/iforward_list.h index 472860fc..0709ab99 100644 --- a/iforward_list.h +++ b/iforward_list.h @@ -315,7 +315,7 @@ namespace etl //************************************************************************* iterator end() { - return iterator(end_node); + return iterator(); } //************************************************************************* @@ -323,7 +323,7 @@ namespace etl //************************************************************************* const_iterator end() const { - return const_iterator(static_cast(end_node)); + return const_iterator(); } //************************************************************************* @@ -331,7 +331,7 @@ namespace etl //************************************************************************* const_iterator cend() const { - return const_iterator(static_cast(end_node)); + return const_iterator(); } //************************************************************************* @@ -400,7 +400,7 @@ namespace etl { Data_Node& data_node = allocate_data_node(*first++); join(*p_last_node, data_node); - join(data_node, end_node); + data_node.next = nullptr; p_last_node = &data_node; ++current_size; } @@ -433,7 +433,7 @@ namespace etl { Data_Node& data_node = allocate_data_node(value); join(*p_last_node, data_node); - join(data_node, end_node); + data_node.next = nullptr; p_last_node = &data_node; ++current_size; } @@ -524,11 +524,13 @@ namespace etl { size_t i = 0; iterator i_node = begin(); + iterator i_last_node; // Find where we're currently at. while ((i < n) && (i_node != end())) { ++i; + i_last_node = i_node; ++i_node; } @@ -542,7 +544,7 @@ namespace etl // Increase. while (i < n) { - i_node = insert_after(i_node, value); + i_last_node = insert_after(i_last_node, value); ++i; } } @@ -573,9 +575,9 @@ namespace etl Node* p_current = p_last->next; Node* p_next = p_current->next; - join(*p_current, end_node); + p_current->next = nullptr; - while (p_next != &end_node) + while (p_next != nullptr) { p_last = p_current; p_current = p_next; @@ -706,7 +708,14 @@ namespace etl p_first = p_next; // Move to the next node. } - return ++last; + if (p_next == nullptr) + { + return end(); + } + else + { + return iterator(*p_last); + } } //************************************************************************* @@ -733,7 +742,7 @@ namespace etl Node* last = &get_head(); Node* current = last->next; - while (current != &end_node) + while (current != nullptr) { // Is this value the same as the last? if (isEqual(data_cast(current)->value, data_cast(last)->value)) @@ -858,7 +867,7 @@ namespace etl p_tail = p_node; } - join(*p_tail.p_node, end_node); + p_tail.p_node->next = nullptr; } // Now left has stepped `list_size' places along, and right has too. @@ -934,7 +943,6 @@ namespace etl } Node start_node; ///< The node that acts as the forward_list start. - Node end_node; ///< The node that acts as the forward_list end. private: @@ -1003,7 +1011,6 @@ namespace etl void insert_node_after(Node& position, Node& node) { // Connect to the forward_list. - join(node, *position.next); join(position, node); // One more. @@ -1055,8 +1062,7 @@ namespace etl } current_size = 0; - join(start_node, end_node); - join(end_node, start_node); + start_node.next = nullptr; } //************************************************************************* diff --git a/test/test_forward_list.cpp b/test/test_forward_list.cpp index 5ac26f36..0e653694 100644 --- a/test/test_forward_list.cpp +++ b/test/test_forward_list.cpp @@ -428,12 +428,35 @@ namespace CompareDataNDC::iterator i_compare_data_2 = compare_data.begin(); std::advance(i_compare_data_2, 4); - compare_data.erase_after(i_compare_data_1, i_compare_data_2); + CompareDataNDC::iterator i_compare_result = compare_data.erase_after(i_compare_data_1, i_compare_data_2); - data.erase_after(i_data_1, i_data_2); + DataNDC::iterator i_result = data.erase_after(i_data_1, i_data_2); + + CHECK_EQUAL(*i_compare_result, *i_result); are_equal = std::equal(data.begin(), data.end(), compare_data.begin()); + CHECK(are_equal); + } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_after_range_end) + { + CompareDataNDC compare_data(sorted_data.begin(), sorted_data.end()); + DataNDC data(sorted_data.begin(), sorted_data.end()); + + DataNDC::iterator i_data = data.begin(); + std::advance(i_data, 4); + + CompareDataNDC::iterator i_compare_data = compare_data.begin(); + std::advance(i_compare_data, 4); + + CompareDataNDC::iterator i_compare_result = compare_data.erase_after(i_compare_data, compare_data.end()); + + DataNDC::iterator i_result = data.erase_after(i_data, data.end()); + + CHECK(i_result == data.end()); + + are_equal = std::equal(data.begin(), data.end(), compare_data.begin()); CHECK(are_equal); }