fix many many many bugs

This commit is contained in:
2024-03-26 13:08:59 +00:00
parent 8f169ca426
commit 587b28ffd7

View File

@ -432,6 +432,7 @@ class map {
iterator &operator--() { iterator &operator--() {
if (raw_pointer == nullptr) { if (raw_pointer == nullptr) {
if (domain == nullptr) throw invalid_iterator(); if (domain == nullptr) throw invalid_iterator();
if (domain->tree_root == nullptr) throw invalid_iterator();
raw_pointer = domain->tree_root; raw_pointer = domain->tree_root;
while (raw_pointer->right != nullptr) raw_pointer = raw_pointer->right; while (raw_pointer->right != nullptr) raw_pointer = raw_pointer->right;
return *this; return *this;
@ -464,13 +465,13 @@ class map {
if (raw_pointer == nullptr) throw invalid_iterator(); if (raw_pointer == nullptr) throw invalid_iterator();
return raw_pointer->val; return raw_pointer->val;
} }
bool operator==(const iterator &rhs) const { return raw_pointer == rhs.raw_pointer; } bool operator==(const iterator &rhs) const { return domain == rhs.domain && raw_pointer == rhs.raw_pointer; }
bool operator==(const const_iterator &rhs) const { return raw_pointer == rhs.raw_pointer; } bool operator==(const const_iterator &rhs) const { return domain == rhs.domain && raw_pointer == rhs.raw_pointer; }
/** /**
* some other operator for iterator. * some other operator for iterator.
*/ */
bool operator!=(const iterator &rhs) const { return raw_pointer != rhs.raw_pointer; } bool operator!=(const iterator &rhs) const { return domain != rhs.domain || raw_pointer != rhs.raw_pointer; }
bool operator!=(const const_iterator &rhs) const { return raw_pointer != rhs.raw_pointer; } bool operator!=(const const_iterator &rhs) const { return domain != rhs.domain || raw_pointer != rhs.raw_pointer; }
/** /**
* for the support of it->first. * for the support of it->first.
@ -524,6 +525,7 @@ class map {
const_iterator &operator--() { const_iterator &operator--() {
if (raw_pointer == nullptr) { if (raw_pointer == nullptr) {
if (domain == nullptr) throw invalid_iterator(); if (domain == nullptr) throw invalid_iterator();
if (domain->tree_root == nullptr) throw invalid_iterator();
raw_pointer = domain->tree_root; raw_pointer = domain->tree_root;
while (raw_pointer->right != nullptr) raw_pointer = raw_pointer->right; while (raw_pointer->right != nullptr) raw_pointer = raw_pointer->right;
return *this; return *this;
@ -556,13 +558,13 @@ class map {
if (raw_pointer == nullptr) throw invalid_iterator(); if (raw_pointer == nullptr) throw invalid_iterator();
return raw_pointer->val; return raw_pointer->val;
} }
bool operator==(const iterator &rhs) const { return raw_pointer == rhs.raw_pointer; } bool operator==(const iterator &rhs) const { return domain == rhs.domain && raw_pointer == rhs.raw_pointer; }
bool operator==(const const_iterator &rhs) const { return raw_pointer == rhs.raw_pointer; } bool operator==(const const_iterator &rhs) const { return domain == rhs.domain && raw_pointer == rhs.raw_pointer; }
/** /**
* some other operator for iterator. * some other operator for iterator.
*/ */
bool operator!=(const iterator &rhs) const { return raw_pointer != rhs.raw_pointer; } bool operator!=(const iterator &rhs) const { return domain != rhs.domain || raw_pointer != rhs.raw_pointer; }
bool operator!=(const const_iterator &rhs) const { return raw_pointer != rhs.raw_pointer; } bool operator!=(const const_iterator &rhs) const { return domain != rhs.domain || raw_pointer != rhs.raw_pointer; }
value_type *operator->() const noexcept { return &raw_pointer->val; } value_type *operator->() const noexcept { return &raw_pointer->val; }
}; };
map() : node_count(0), tree_root(nullptr) {} map() : node_count(0), tree_root(nullptr) {}
@ -607,11 +609,13 @@ class map {
* If no such element exists, an exception of type `index_out_of_bound' * If no such element exists, an exception of type `index_out_of_bound'
*/ */
T &at(const Key &key) { T &at(const Key &key) {
if (tree_root == nullptr) throw index_out_of_bound();
RedBlackTreeNodeType *result = tree_root->Find(key); RedBlackTreeNodeType *result = tree_root->Find(key);
if (result == nullptr) throw index_out_of_bound(); if (result == nullptr) throw index_out_of_bound();
return result->val.second; return result->val.second;
} }
const T &at(const Key &key) const { const T &at(const Key &key) const {
if (tree_root == nullptr) throw index_out_of_bound();
RedBlackTreeNodeType *result = tree_root->Find(key); RedBlackTreeNodeType *result = tree_root->Find(key);
if (result == nullptr) throw index_out_of_bound(); if (result == nullptr) throw index_out_of_bound();
return result->val.second; return result->val.second;