Procházet zdrojové kódy

avl_tree: iterator full implementation + test.

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> před 10 roky
rodič
revize
b05c252b96
2 změnil soubory, kde provedl 56 přidání a 33 odebrání
  1. +39
    -33
      src/lol/algorithm/avl_tree.h
  2. +17
    -0
      src/t/algorithm/avl_tree.cpp

+ 39
- 33
src/lol/algorithm/avl_tree.h Zobrazit soubor

@@ -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:


+ 17
- 0
src/t/algorithm/avl_tree.cpp Zobrazit soubor

@@ -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;
}
}
};

}

Načítá se…
Zrušit
Uložit