From 62cdce3f2e1dc118104ecf81cc85d867f3e8ec2f Mon Sep 17 00:00:00 2001 From: Pagwin Date: Sat, 23 Nov 2024 20:52:08 -0500 Subject: [PATCH] adding to map seems to work now --- Map.hpp | 39 +++++++++++++++++++++++++++------------ t.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/Map.hpp b/Map.hpp index 6f9a943..b68f8bd 100644 --- a/Map.hpp +++ b/Map.hpp @@ -20,6 +20,9 @@ Direction operator!(Direction dir) { assert(false); } } +int x5 = 5; +int x6 = 6; +int x7 = 7; enum class Color { Red, Black }; } // namespace template class Map { @@ -62,6 +65,7 @@ template class Map { this->right->parent = this; } } + ~Node() {} Node &operator=(const Node &rhs) { // retain parent as is, common case is the copy or move is happening due // to a rotation where parent can get wonky @@ -69,7 +73,7 @@ template class Map { this->val = rhs.val; this->left = std::make_unique(*rhs.left); this->right = std::make_unique(*rhs.right); - + this->color = rhs.color; if (this->left) { this->left->parent = this; this->left->restore_ordering(); @@ -89,6 +93,7 @@ template class Map { this->val = rhs.val; this->left = std::move(rhs.left); this->right = std::move(rhs.right); + this->color = rhs.color; if (this->left) { this->left->parent = this; this->left->restore_ordering(); @@ -126,7 +131,9 @@ template class Map { std::unique_ptr &set_child(Direction dir, std::unique_ptr new_child) { - new_child->parent = this; + if (new_child) { + new_child->parent = this; + } switch (dir) { case Direction::Left: @@ -284,13 +291,15 @@ template class Map { parent = self->parent; } // case 6 + + // recolor first so we aren't recoloring a dropped reference or smth + parent->color = Color::Black; + grandparent->color = Color::Red; if (grandparent->parent == nullptr) { map->rotate_root(!dir); } else { grandparent->rotate(!dir); } - parent->color = Color::Black; - grandparent->color = Color::Red; return; } @@ -353,6 +362,7 @@ private: root.value() = std::move(*new_root); + old_root->set_child(!dir, std::move(root.value().uchild(dir))); root.value().set_child(dir, std::move(old_root)); } template @@ -367,21 +377,26 @@ private: return std::make_pair(nullptr, ret_dir); } if constexpr (trace) { - std::cerr << "root->"; + std::cerr << "root"; } // value is in root if (this->root.value().val.first == key) { + + if constexpr (trace) { + std::cerr << "->found" << std::endl; + } + return std::make_pair(nullptr, ret_dir); } ret_parent = &this->root.value(); if (key < ret_parent->val.first) { if constexpr (trace) { - std::cerr << "left->"; + std::cerr << "->left"; } ret_dir = Direction::Left; } else { if constexpr (trace) { - std::cerr << "right->"; + std::cerr << "->right"; } ret_dir = Direction::Right; } @@ -390,18 +405,18 @@ private: ret_parent = ret_parent->child(ret_dir); if (key < ret_parent->val.first) { if constexpr (trace) { - std::cerr << "left->"; + std::cerr << "->left"; } ret_dir = Direction::Left; } else { if constexpr (trace) { - std::cerr << "right->"; + std::cerr << "->right"; } ret_dir = Direction::Right; } } if constexpr (trace) { - std::cerr << "found" << std::endl; + std::cerr << "->found" << std::endl; } return std::make_pair(ret_parent, ret_dir); } @@ -415,8 +430,8 @@ public: if (this->root.value().val.first == key) { return Iterator{&this->root.value()}; } - return this->end(); } + return this->end(); } if (parent->child(dir) != nullptr) { return Iterator{parent->child(dir), nullptr}; @@ -430,8 +445,8 @@ public: if (this->root.value().val.first == key) { return Iterator{&this->root.value()}; } - return this->end(); } + return this->end(); } if (parent->child(dir) != nullptr) { return Iterator{parent->child(dir), nullptr}; diff --git a/t.cpp b/t.cpp index f238d1d..c23c742 100644 --- a/t.cpp +++ b/t.cpp @@ -3,6 +3,9 @@ template class cs440::Map; int main(void) { cs440::Map a; + int x5 = 5; + int x6 = 6; + int x7 = 7; a.insert({1, 1}); a.insert({2, 2}); a.insert({3, 3}); @@ -27,5 +30,35 @@ int main(void) { a.insert({22, 22}); a.insert({23, 23}); a.insert({24, 24}); + a.insert({25, 25}); + a.insert({26, 26}); + a.insert({27, 27}); + a.insert({28, 28}); + a.insert({29, 29}); + a.insert({30, 30}); + a.insert({31, 31}); + a.insert({32, 32}); + a.insert({33, 33}); + a.insert({34, 34}); + a.insert({35, 35}); + a.insert({36, 36}); + a.insert({37, 37}); + a.insert({38, 38}); + a.insert({39, 39}); + a.insert({40, 40}); + a.insert({41, 41}); + a.insert({42, 42}); + a.insert({43, 43}); + a.insert({44, 44}); + a.insert({45, 45}); + a.insert({46, 46}); + a.insert({47, 47}); + a.insert({48, 48}); + a.insert({49, 49}); + a.insert({50, 50}); + for (std::size_t i = 1; i <= 50; i++) { + std::cout << i << "\t"; + a.find_trace(i); + } return 0; }