From 7251024de50087dede0918c45ab399b864e04c13 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Sun, 1 Mar 2015 12:28:27 +0000 Subject: [PATCH] map: FIXED ! --- src/lol/base/avl_tree.h | 14 +- src/t/base/map.cpp | 463 ++++++++++++++++++++-------------------- 2 files changed, 240 insertions(+), 237 deletions(-) diff --git a/src/lol/base/avl_tree.h b/src/lol/base/avl_tree.h index a528177a..7dd79569 100644 --- a/src/lol/base/avl_tree.h +++ b/src/lol/base/avl_tree.h @@ -257,12 +257,13 @@ protected: int i = -1 + (key < m_key) + 2 * (m_key < key); bool erased = false; + bool suicide = false; if (i < 0) { erase_self(); - delete this; erased = true; + suicide = true; } else if (m_child[i] && m_child[i]->erase(key)) { @@ -270,6 +271,9 @@ protected: erased = true; } + if (suicide) + delete this; + return erased; } @@ -391,8 +395,12 @@ protected: save->m_parent_slot = &this->m_child[i]; } - replacement->m_child[0]->update_balance(); - replacement->m_child[1]->update_balance(); + if (replacement->m_child[0]) + replacement->m_child[0]->update_balance(); + + if (replacement->m_child[1]) + replacement->m_child[1]->update_balance(); + replacement->update_balance(); } } diff --git a/src/t/base/map.cpp b/src/t/base/map.cpp index f7d19e7f..2acac3f1 100644 --- a/src/t/base/map.cpp +++ b/src/t/base/map.cpp @@ -23,91 +23,91 @@ lolunit_declare_fixture(MapTest) void TearDown() {} - // lolunit_declare_test(MapDeclare) - // { - // map m1; - // map m2; - // map m3; - // map m4; - // } - - // lolunit_declare_test(MapSet) - // { - // map m; - - // for (int i = 0; i < 1000; i++) - // m[i] = -1; - - // for (int i = 0; i < 1000; i++) - // m[i] = i; - - // for (int i = 0; i < 1000; i++) - // lolunit_assert_equal(m[i], i); - // } - - // lolunit_declare_test(MapHasKey) - // { - // map m; - - // m[0] = 1; - // m[2] = 2; - - // lolunit_assert(m.has_key(0)); - // lolunit_assert(!m.has_key(1)); - // lolunit_assert(m.has_key(2)); - // } - - // lolunit_declare_test(MapRemove) - // { - // map m; - // array a; - - // for (int i = 0; i < 20; i++) - // { - // a << i; - // m[i] = -1; - // } - // for (int i = 0; i < a.Count(); i++) - // m[i] = i; - // a.Shuffle(); - // for (int i = 0; i < a.Count(); i++) - // m.remove(a[i]); - // } - - // lolunit_declare_test(MapRemoveString) - // { - // map m; - // array a; - - // for (int i = 0; i < 20; i++) - // { - // a << String::Printf("test_str_%i", i); - // m[a.Last()] = -1; - // } - // for (int i = 0; i < a.Count(); i++) - // m[a[i]] = i; - // a.Shuffle(); - // for (int i = 0; i < a.Count(); i++) - // m.remove(a[i]); - // } - - // lolunit_declare_test(MapRemoveBug) - // { - // map m; - - // for (int i = 0; i < 20; i++) - // m[i] = i; - - // m.remove(12); - // m.remove(0); - // m.remove(17); - // m.remove(2); - // m.remove(9); - // m.remove(4); - // m.remove(15); - // m.remove(10); - // lolunit_assert_equal(m[8], 8); - // } + lolunit_declare_test(MapDeclare) + { + map m1; + map m2; + map m3; + map m4; + } + + lolunit_declare_test(MapSet) + { + map m; + + for (int i = 0; i < 1000; i++) + m[i] = -1; + + for (int i = 0; i < 1000; i++) + m[i] = i; + + for (int i = 0; i < 1000; i++) + lolunit_assert_equal(m[i], i); + } + + lolunit_declare_test(MapHasKey) + { + map m; + + m[0] = 1; + m[2] = 2; + + lolunit_assert(m.has_key(0)); + lolunit_assert(!m.has_key(1)); + lolunit_assert(m.has_key(2)); + } + + lolunit_declare_test(MapRemove) + { + map m; + array a; + + for (int i = 0; i < 20; i++) + { + a << i; + m[i] = -1; + } + for (int i = 0; i < a.Count(); i++) + m[i] = i; + a.Shuffle(); + for (int i = 0; i < a.Count(); i++) + m.remove(a[i]); + } + + lolunit_declare_test(MapRemoveString) + { + map m; + array a; + + for (int i = 0; i < 20; i++) + { + a << String::Printf("test_str_%i", i); + m[a.Last()] = -1; + } + for (int i = 0; i < a.Count(); i++) + m[a[i]] = i; + a.Shuffle(); + for (int i = 0; i < a.Count(); i++) + m.remove(a[i]); + } + + lolunit_declare_test(MapRemoveBug) + { + map m; + + for (int i = 0; i < 20; i++) + m[i] = i; + + m.remove(12); + m.remove(0); + m.remove(17); + m.remove(2); + m.remove(9); + m.remove(4); + m.remove(15); + m.remove(10); + lolunit_assert_equal(m[8], 8); + } lolunit_declare_test(MapRandomAddRemove) { @@ -123,11 +123,8 @@ lolunit_declare_fixture(MapTest) presence[i] = 0; } - for (int i = 0 ; i < 198 ; ++i) + for (int i = 0 ; i < 10000 ; ++i) { - // debug output - // std::cout << "i " << i << ", a " << (int)a << ", b " << (int)b << std::endl; - m[a] = b; m.remove(b); @@ -144,8 +141,6 @@ lolunit_declare_fixture(MapTest) unsigned char v; if (presence[j]) { - // debug output - // std::cout << "j " << j << " v " << (int)v << std::endl; lolunit_assert(m.try_get(j, v)); lolunit_assert_equal(value[j], v); } @@ -157,149 +152,149 @@ lolunit_declare_fixture(MapTest) } } - // lolunit_declare_test(StringMap) - // { - // map m; - - // m["foo"] = 42; - // m["bar"] = 12; - // m["baz"] = 2; - - // int foo = m["foo"]; - // int bar = m["bar"]; - // int baz = m["baz"]; - - // lolunit_assert_equal(42, foo); - // lolunit_assert_equal(12, bar); - // lolunit_assert_equal(2, baz); - - // //Big stress test - // array bones = { "RootNode", - // "Cyberano_Ns:Root_$AssimpFbx$_Translation", - // "Cyberano_Ns:Box004_$AssimpFbx$_PreRotation", - // "Cyberano_Ns:Root_$AssimpFbx$_PreRotation", - // "Cyberano_Ns:Box004", - // "Cyberano_Ns:Root_$AssimpFbx$_Rotation", - // "Cyberano_Ns:Root", - // "Cyberano_Ns:Hips", - // "Cyberano_Ns:Spine", - // "Cyberano_Ns:RightUpLeg", - // "Cyberano_Ns:LeftUpLeg", - // "Cyberano_Ns:BeltSheath1", - // "Cyberano_Ns:RightCoat", - // "Cyberano_Ns:LeftCoat", - // "Cyberano_Ns:Spine1", - // "Cyberano_Ns:RightLeg", - // "Cyberano_Ns:RightUpLegRoll", - // "Cyberano_Ns:LeftUpLegRoll", - // "Cyberano_Ns:LeftLeg", - // "Cyberano_Ns:Sheath", - // "Cyberano_Ns:BeltSheath2", - // "Cyberano_Ns:BeltSheath3", - // "Cyberano_Ns:Spine2", - // "Cyberano_Ns:FrontBelt1", - // "Cyberano_Ns:BackBelt1", - // "Cyberano_Ns:RightFoot", - // "Cyberano_Ns:RightLegRoll", - // "Cyberano_Ns:LeftLegRoll", - // "Cyberano_Ns:LeftFoot", - // "Cyberano_Ns:Sword", - // "Cyberano_Ns:Neck", - // "Cyberano_Ns:RightShoulder", - // "Cyberano_Ns:LeftShoulder", - // "Cyberano_Ns:Cloth", - // "Cyberano_Ns:FrontBelt2", - // "Cyberano_Ns:RightToeBase", - // "Cyberano_Ns:LeftToeBase", - // "Cyberano_Ns:Head", - // "Cyberano_Ns:RightArm", - // "Cyberano_Ns:RightSpaulder1", - // "Cyberano_Ns:RightSpaulder2", - // "Cyberano_Ns:LeftArm", - // "Cyberano_Ns:LeftSpaulder1", - // "Cyberano_Ns:LeftCloth01", - // "Cyberano_Ns:MiddleCloth01", - // "Cyberano_Ns:RightCloth01", - // "Cyberano_Ns:FrontBelt3", - // "Cyberano_Ns:RightoeEnd", - // "Cyberano_Ns:LeftoeEnd", - // "Cyberano_Ns:HeadEnd", - // "Cyberano_Ns:Cap", - // "Cyberano_Ns:RightForeArm", - // "Cyberano_Ns:RightArmRoll", - // "Cyberano_Ns:LeftForeArm", - // "Cyberano_Ns:LeftArmRoll", - // "Cyberano_Ns:LeftCloth02", - // "Cyberano_Ns:MiddleCloth02", - // "Cyberano_Ns:RightCloth02", - // "Cyberano_Ns:Feather01", - // "Cyberano_Ns:RightHand", - // "Cyberano_Ns:RightForeArmRoll", - // "Cyberano_Ns:LeftHand", - // "Cyberano_Ns:LeftForeArmRoll", - // "Cyberano_Ns:LeftCloth03", - // "Cyberano_Ns:MiddleCloth03", - // "Cyberano_Ns:RightCloth03", - // "Cyberano_Ns:Feather02", - // "Cyberano_Ns:RightThumb1", - // "Cyberano_Ns:RightIndex1", - // "Cyberano_Ns:RightMiddle1", - // "Cyberano_Ns:RightRing1", - // "Cyberano_Ns:RightCuff", - // "Cyberano_Ns:LeftThumb1", - // "Cyberano_Ns:LeftIndex1", - // "Cyberano_Ns:LeftMiddle1", - // "Cyberano_Ns:LeftRing1", - // "Cyberano_Ns:LeftCloth04", - // "Cyberano_Ns:MiddleCloth04", - // "Cyberano_Ns:RightCloth04", - // "Cyberano_Ns:Feather03", - // "Cyberano_Ns:RightThumb2", - // "Cyberano_Ns:RightIndex2", - // "Cyberano_Ns:RightMiddle2", - // "Cyberano_Ns:RightRing2", - // "Cyberano_Ns:LeftThumb2", - // "Cyberano_Ns:LeftIndex2", - // "Cyberano_Ns:LeftMiddle2", - // "Cyberano_Ns:LeftRing2", - // "Cyberano_Ns:Feather04", - // "Cyberano_Ns:RightThumb3", - // "Cyberano_Ns:RightIndex3", - // "Cyberano_Ns:RightMiddle3", - // "Cyberano_Ns:RightRing3", - // "Cyberano_Ns:LeftThumb3", - // "Cyberano_Ns:LeftIndex3", - // "Cyberano_Ns:LeftMiddle3", - // "Cyberano_Ns:LeftRing3", - // "Cyberano_Ns:Feather05", - // "Cyberano_Ns:RightThumb4", - // "Cyberano_Ns:RightIndex4", - // "Cyberano_Ns:RightMiddle4", - // "Cyberano_Ns:RightRing4", - // "Cyberano_Ns:LeftThumb4", - // "Cyberano_Ns:LeftIndex4", - // "Cyberano_Ns:LeftMiddle4", - // "Cyberano_Ns:LeftRing4", - // "Cyberano_Ns:Feather06", - // "Cyberano_Ns:Feather07", - // "Cyberano_Ns:Feather08", - // "Cyberano_Ns:Feather09", - // "Cyberano_Ns:Feather10", - // "Cyberano_Ns:Feather11", - // "Cyberano_Ns:Feather12", - // "Cyberano_Ns:Feather13", - // "Cyberano_Ns:Feather14", - // "Cyberano_Ns:Feather15", - // "Cyberano_Ns:Feather16", - // "Cyberano_Ns:Feather17" }; - - // map bones_map; - // for (int i = 0; i < bones.Count(); ++i) - // bones_map[bones[i]] = i; - - // for (int i = 0; i < bones.Count(); ++i) - // lolunit_assert_equal(bones_map[bones[i]], i); - // } + lolunit_declare_test(StringMap) + { + map m; + + m["foo"] = 42; + m["bar"] = 12; + m["baz"] = 2; + + int foo = m["foo"]; + int bar = m["bar"]; + int baz = m["baz"]; + + lolunit_assert_equal(42, foo); + lolunit_assert_equal(12, bar); + lolunit_assert_equal(2, baz); + + //Big stress test + array bones = { "RootNode", + "Cyberano_Ns:Root_$AssimpFbx$_Translation", + "Cyberano_Ns:Box004_$AssimpFbx$_PreRotation", + "Cyberano_Ns:Root_$AssimpFbx$_PreRotation", + "Cyberano_Ns:Box004", + "Cyberano_Ns:Root_$AssimpFbx$_Rotation", + "Cyberano_Ns:Root", + "Cyberano_Ns:Hips", + "Cyberano_Ns:Spine", + "Cyberano_Ns:RightUpLeg", + "Cyberano_Ns:LeftUpLeg", + "Cyberano_Ns:BeltSheath1", + "Cyberano_Ns:RightCoat", + "Cyberano_Ns:LeftCoat", + "Cyberano_Ns:Spine1", + "Cyberano_Ns:RightLeg", + "Cyberano_Ns:RightUpLegRoll", + "Cyberano_Ns:LeftUpLegRoll", + "Cyberano_Ns:LeftLeg", + "Cyberano_Ns:Sheath", + "Cyberano_Ns:BeltSheath2", + "Cyberano_Ns:BeltSheath3", + "Cyberano_Ns:Spine2", + "Cyberano_Ns:FrontBelt1", + "Cyberano_Ns:BackBelt1", + "Cyberano_Ns:RightFoot", + "Cyberano_Ns:RightLegRoll", + "Cyberano_Ns:LeftLegRoll", + "Cyberano_Ns:LeftFoot", + "Cyberano_Ns:Sword", + "Cyberano_Ns:Neck", + "Cyberano_Ns:RightShoulder", + "Cyberano_Ns:LeftShoulder", + "Cyberano_Ns:Cloth", + "Cyberano_Ns:FrontBelt2", + "Cyberano_Ns:RightToeBase", + "Cyberano_Ns:LeftToeBase", + "Cyberano_Ns:Head", + "Cyberano_Ns:RightArm", + "Cyberano_Ns:RightSpaulder1", + "Cyberano_Ns:RightSpaulder2", + "Cyberano_Ns:LeftArm", + "Cyberano_Ns:LeftSpaulder1", + "Cyberano_Ns:LeftCloth01", + "Cyberano_Ns:MiddleCloth01", + "Cyberano_Ns:RightCloth01", + "Cyberano_Ns:FrontBelt3", + "Cyberano_Ns:RightoeEnd", + "Cyberano_Ns:LeftoeEnd", + "Cyberano_Ns:HeadEnd", + "Cyberano_Ns:Cap", + "Cyberano_Ns:RightForeArm", + "Cyberano_Ns:RightArmRoll", + "Cyberano_Ns:LeftForeArm", + "Cyberano_Ns:LeftArmRoll", + "Cyberano_Ns:LeftCloth02", + "Cyberano_Ns:MiddleCloth02", + "Cyberano_Ns:RightCloth02", + "Cyberano_Ns:Feather01", + "Cyberano_Ns:RightHand", + "Cyberano_Ns:RightForeArmRoll", + "Cyberano_Ns:LeftHand", + "Cyberano_Ns:LeftForeArmRoll", + "Cyberano_Ns:LeftCloth03", + "Cyberano_Ns:MiddleCloth03", + "Cyberano_Ns:RightCloth03", + "Cyberano_Ns:Feather02", + "Cyberano_Ns:RightThumb1", + "Cyberano_Ns:RightIndex1", + "Cyberano_Ns:RightMiddle1", + "Cyberano_Ns:RightRing1", + "Cyberano_Ns:RightCuff", + "Cyberano_Ns:LeftThumb1", + "Cyberano_Ns:LeftIndex1", + "Cyberano_Ns:LeftMiddle1", + "Cyberano_Ns:LeftRing1", + "Cyberano_Ns:LeftCloth04", + "Cyberano_Ns:MiddleCloth04", + "Cyberano_Ns:RightCloth04", + "Cyberano_Ns:Feather03", + "Cyberano_Ns:RightThumb2", + "Cyberano_Ns:RightIndex2", + "Cyberano_Ns:RightMiddle2", + "Cyberano_Ns:RightRing2", + "Cyberano_Ns:LeftThumb2", + "Cyberano_Ns:LeftIndex2", + "Cyberano_Ns:LeftMiddle2", + "Cyberano_Ns:LeftRing2", + "Cyberano_Ns:Feather04", + "Cyberano_Ns:RightThumb3", + "Cyberano_Ns:RightIndex3", + "Cyberano_Ns:RightMiddle3", + "Cyberano_Ns:RightRing3", + "Cyberano_Ns:LeftThumb3", + "Cyberano_Ns:LeftIndex3", + "Cyberano_Ns:LeftMiddle3", + "Cyberano_Ns:LeftRing3", + "Cyberano_Ns:Feather05", + "Cyberano_Ns:RightThumb4", + "Cyberano_Ns:RightIndex4", + "Cyberano_Ns:RightMiddle4", + "Cyberano_Ns:RightRing4", + "Cyberano_Ns:LeftThumb4", + "Cyberano_Ns:LeftIndex4", + "Cyberano_Ns:LeftMiddle4", + "Cyberano_Ns:LeftRing4", + "Cyberano_Ns:Feather06", + "Cyberano_Ns:Feather07", + "Cyberano_Ns:Feather08", + "Cyberano_Ns:Feather09", + "Cyberano_Ns:Feather10", + "Cyberano_Ns:Feather11", + "Cyberano_Ns:Feather12", + "Cyberano_Ns:Feather13", + "Cyberano_Ns:Feather14", + "Cyberano_Ns:Feather15", + "Cyberano_Ns:Feather16", + "Cyberano_Ns:Feather17" }; + + map bones_map; + for (int i = 0; i < bones.Count(); ++i) + bones_map[bones[i]] = i; + + for (int i = 0; i < bones.Count(); ++i) + lolunit_assert_equal(bones_map[bones[i]], i); + } }; } /* namespace lol */