From b05c252b9622eb6a53d26bc9a8d97eff4a9468b7 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Fri, 17 Oct 2014 08:25:51 +0000 Subject: [PATCH] avl_tree: iterator full implementation + test. --- src/lol/algorithm/avl_tree.h | 72 +++++++++++++++++++----------------- src/t/algorithm/avl_tree.cpp | 17 +++++++++ 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/lol/algorithm/avl_tree.h b/src/lol/algorithm/avl_tree.h index e53beff2..2f9c7807 100644 --- a/src/lol/algorithm/avl_tree.h +++ b/src/lol/algorithm/avl_tree.h @@ -12,7 +12,6 @@ #pragma once - namespace lol { @@ -104,26 +103,36 @@ public: class Iterator; class ConstIterator; - Iterator get_iterator() + Iterator begin() { tree_node * node = nullptr; if (this->m_root) - node = this->m_root->get_min(); + this->m_root->get_min(node); return Iterator(node); } - ConstIterator get_iterator() const + ConstIterator begin() const { tree_node * node = nullptr; if (this->m_root) - node = this->m_root->get_min(); + this->m_root->get_min(node); return ConstIterator(node); } + Iterator end() + { + return Iterator(nullptr); + } + + ConstIterator end() const + { + return ConstIterator(nullptr); + } + protected: class tree_node @@ -234,34 +243,31 @@ protected: return false; } - void get_min(tree_node * & min_node) const + void get_min(tree_node * & min_node) { min_node = this; - while (this->m_child[0]) - min_node = this->m_child[0]; + while (min_node->m_child[0]) + min_node = min_node->m_child[0]; } void get_max(tree_node * & max_node) const { max_node = this; - while (this->m_child[1]) - max_node = this->m_child[1]; + while (max_node->m_child[1]) + max_node = max_node->m_child[1]; } void cascade_delete() { - if (this->m_child[0]) - { - this->m_child[0]->cascade_delete(); - delete this->m_child[0]; - } - - if (this->m_child[1]) + for (int i = 0 ; i < 2 ; ++i) { - this->m_child[1]->cascade_delete(); - delete this->m_child[1]; + if (this->m_child[i]) + { + this->m_child[i]->cascade_delete(); + delete this->m_child[i]; + } } } @@ -384,14 +390,14 @@ public: struct OutputValue { - OutputValue(K const & key, V & value) : - m_key(key), - m_value(value) + OutputValue(K const & _key, V & _value) : + key(_key), + value(_value) { } - K const & m_key; - V & m_value; + K const & key; + V & value; }; class Iterator @@ -446,9 +452,9 @@ public: return OutputValue(this->m_node->get_key(), this->m_node->get_value()); } - bool operator==(Iterator const & that) const + bool operator!=(Iterator const & that) const { - return this->m_node = that->m_node; + return this->m_node != that.m_node; } protected: @@ -458,14 +464,14 @@ public: struct ConstOutputValue { - ConstOutputValue(K const & key, V const & value) : - m_key(key), - m_value(value) + ConstOutputValue(K const & _key, V const & _value) : + key(_key), + value(_value) { } - K const & m_key; - V const & m_value; + K const & key; + V const & value; }; class ConstIterator @@ -520,9 +526,9 @@ public: return ConstOutputValue(this->m_node->get_key(), this->m_node->get_value()); } - bool operator==(ConstIterator const & that) const + bool operator!=(ConstIterator const & that) const { - return this->m_node = that->m_node; + return this->m_node != that.m_node; } protected: diff --git a/src/t/algorithm/avl_tree.cpp b/src/t/algorithm/avl_tree.cpp index 38302a48..c052cf39 100644 --- a/src/t/algorithm/avl_tree.cpp +++ b/src/t/algorithm/avl_tree.cpp @@ -173,6 +173,23 @@ lolunit_declare_fixture(AvlTreeTest) lolunit_assert_equal(tree.try_get_value(67, value_ptr), false); lolunit_assert_equal(*value_ptr, 3); } + + lolunit_declare_test(AvlTreeTestIteratorRead) + { + test_tree tree; + + for (int i = 1 ; i < 100 ; ++i) + tree.insert(i, 2 * i + i % 3); + + int tmp = 0; + + for (auto iterator : tree) + { + lolunit_assert_equal(iterator.key > tmp, true); + lolunit_assert_equal(iterator.value == (iterator.key * 2 + iterator.key % 3), true); + tmp = iterator.key; + } + } }; }