adding to map seems to work now

This commit is contained in:
Pagwin 2024-11-23 20:52:08 -05:00
parent 79fd8550db
commit 62cdce3f2e
No known key found for this signature in database
GPG key ID: 81137023740CA260
2 changed files with 60 additions and 12 deletions

39
Map.hpp
View file

@ -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 <typename Key_T, typename Mapped_T> class Map {
@ -62,6 +65,7 @@ template <typename Key_T, typename Mapped_T> 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 <typename Key_T, typename Mapped_T> class Map {
this->val = rhs.val;
this->left = std::make_unique<Node>(*rhs.left);
this->right = std::make_unique<Node>(*rhs.right);
this->color = rhs.color;
if (this->left) {
this->left->parent = this;
this->left->restore_ordering();
@ -89,6 +93,7 @@ template <typename Key_T, typename Mapped_T> 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 <typename Key_T, typename Mapped_T> class Map {
std::unique_ptr<Node> &set_child(Direction dir,
std::unique_ptr<Node> new_child) {
new_child->parent = this;
if (new_child) {
new_child->parent = this;
}
switch (dir) {
case Direction::Left:
@ -284,13 +291,15 @@ template <typename Key_T, typename Mapped_T> 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 <bool trace = false>
@ -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};

33
t.cpp
View file

@ -3,6 +3,9 @@
template class cs440::Map<int, int>;
int main(void) {
cs440::Map<int, int> 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;
}