summaryrefslogtreecommitdiff
path: root/core/set.h
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2020-05-14 16:41:43 +0200
committerRémi Verschelde <rverschelde@gmail.com>2020-05-14 21:57:34 +0200
commit0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 (patch)
tree198d4ff7665d89307f6ca2469fa38620a9eb1672 /core/set.h
parent07bc4e2f96f8f47991339654ff4ab16acc19d44f (diff)
Style: Enforce braces around if blocks and loops
Using clang-tidy's `readability-braces-around-statements`. https://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html
Diffstat (limited to 'core/set.h')
-rw-r--r--core/set.h123
1 files changed, 79 insertions, 44 deletions
diff --git a/core/set.h b/core/set.h
index 88eccc7ea8..1bc0a3f41e 100644
--- a/core/set.h
+++ b/core/set.h
@@ -125,13 +125,15 @@ private:
inline void _rotate_left(Element *p_node) {
Element *r = p_node->right;
p_node->right = r->left;
- if (r->left != _data._nil)
+ if (r->left != _data._nil) {
r->left->parent = p_node;
+ }
r->parent = p_node->parent;
- if (p_node == p_node->parent->left)
+ if (p_node == p_node->parent->left) {
p_node->parent->left = r;
- else
+ } else {
p_node->parent->right = r;
+ }
r->left = p_node;
p_node->parent = r;
@@ -140,13 +142,15 @@ private:
inline void _rotate_right(Element *p_node) {
Element *l = p_node->left;
p_node->left = l->right;
- if (l->right != _data._nil)
+ if (l->right != _data._nil) {
l->right->parent = p_node;
+ }
l->parent = p_node->parent;
- if (p_node == p_node->parent->right)
+ if (p_node == p_node->parent->right) {
p_node->parent->right = l;
- else
+ } else {
p_node->parent->left = l;
+ }
l->right = p_node;
p_node->parent = l;
@@ -166,8 +170,9 @@ private:
node = node->parent;
}
- if (node->parent == _data._root)
+ if (node->parent == _data._root) {
return nullptr; // No successor, as p_node = last node
+ }
return node->parent;
}
}
@@ -186,8 +191,9 @@ private:
node = node->parent;
}
- if (node == _data._root)
+ if (node == _data._root) {
return nullptr; // No predecessor, as p_node = first node.
+ }
return node->parent;
}
}
@@ -197,12 +203,13 @@ private:
C less;
while (node != _data._nil) {
- if (less(p_value, node->value))
+ if (less(p_value, node->value)) {
node = node->left;
- else if (less(node->value, p_value))
+ } else if (less(node->value, p_value)) {
node = node->right;
- else
+ } else {
return node; // found
+ }
}
return nullptr;
@@ -216,19 +223,22 @@ private:
while (node != _data._nil) {
prev = node;
- if (less(p_value, node->value))
+ if (less(p_value, node->value)) {
node = node->left;
- else if (less(node->value, p_value))
+ } else if (less(node->value, p_value)) {
node = node->right;
- else
+ } else {
return node; // found
+ }
}
- if (prev == nullptr)
+ if (prev == nullptr) {
return nullptr; // tree empty
+ }
- if (less(prev->value, p_value))
+ if (less(prev->value, p_value)) {
prev = prev->_next;
+ }
return prev;
}
@@ -289,11 +299,11 @@ private:
while (node != _data._nil) {
new_parent = node;
- if (less(p_value, node->value))
+ if (less(p_value, node->value)) {
node = node->left;
- else if (less(node->value, p_value))
+ } else if (less(node->value, p_value)) {
node = node->right;
- else {
+ } else {
return node; // Return existing node
}
}
@@ -313,10 +323,12 @@ private:
new_node->_next = _successor(new_node);
new_node->_prev = _predecessor(new_node);
- if (new_node->_next)
+ if (new_node->_next) {
new_node->_next->_prev = new_node;
- if (new_node->_prev)
+ }
+ if (new_node->_prev) {
new_node->_prev->_next = new_node;
+ }
_data.size_cache++;
_insert_rb_fix(new_node);
@@ -411,10 +423,12 @@ private:
rp->right = p_node->right;
rp->parent = p_node->parent;
rp->color = p_node->color;
- if (p_node->left != _data._nil)
+ if (p_node->left != _data._nil) {
p_node->left->parent = rp;
- if (p_node->right != _data._nil)
+ }
+ if (p_node->right != _data._nil) {
p_node->right->parent = rp;
+ }
if (p_node == p_node->parent->left) {
p_node->parent->left = rp;
@@ -423,10 +437,12 @@ private:
}
}
- if (p_node->_next)
+ if (p_node->_next) {
p_node->_next->_prev = p_node->_prev;
- if (p_node->_prev)
+ }
+ if (p_node->_prev) {
p_node->_prev->_next = p_node->_next;
+ }
memdelete_allocator<Element, A>(p_node);
_data.size_cache--;
@@ -434,19 +450,22 @@ private:
}
void _calculate_depth(Element *p_element, int &max_d, int d) const {
- if (p_element == _data._nil)
+ if (p_element == _data._nil) {
return;
+ }
_calculate_depth(p_element->left, max_d, d + 1);
_calculate_depth(p_element->right, max_d, d + 1);
- if (d > max_d)
+ if (d > max_d) {
max_d = d;
+ }
}
void _cleanup_tree(Element *p_element) {
- if (p_element == _data._nil)
+ if (p_element == _data._nil) {
return;
+ }
_cleanup_tree(p_element->left);
_cleanup_tree(p_element->right);
@@ -463,16 +482,18 @@ private:
public:
const Element *find(const T &p_value) const {
- if (!_data._root)
+ if (!_data._root) {
return nullptr;
+ }
const Element *res = _find(p_value);
return res;
}
Element *find(const T &p_value) {
- if (!_data._root)
+ if (!_data._root) {
return nullptr;
+ }
Element *res = _find(p_value);
return res;
@@ -487,58 +508,70 @@ public:
}
Element *insert(const T &p_value) {
- if (!_data._root)
+ if (!_data._root) {
_data._create_root();
+ }
return _insert(p_value);
}
void erase(Element *p_element) {
- if (!_data._root || !p_element)
+ if (!_data._root || !p_element) {
return;
+ }
_erase(p_element);
- if (_data.size_cache == 0 && _data._root)
+ if (_data.size_cache == 0 && _data._root) {
_data._free_root();
+ }
}
bool erase(const T &p_value) {
- if (!_data._root)
+ if (!_data._root) {
return false;
+ }
Element *e = find(p_value);
- if (!e)
+ if (!e) {
return false;
+ }
_erase(e);
- if (_data.size_cache == 0 && _data._root)
+ if (_data.size_cache == 0 && _data._root) {
_data._free_root();
+ }
return true;
}
Element *front() const {
- if (!_data._root)
+ if (!_data._root) {
return nullptr;
+ }
Element *e = _data._root->left;
- if (e == _data._nil)
+ if (e == _data._nil) {
return nullptr;
+ }
- while (e->left != _data._nil)
+ while (e->left != _data._nil) {
e = e->left;
+ }
return e;
}
Element *back() const {
- if (!_data._root)
+ if (!_data._root) {
return nullptr;
+ }
Element *e = _data._root->left;
- if (e == _data._nil)
+ if (e == _data._nil) {
return nullptr;
+ }
- while (e->right != _data._nil)
+ while (e->right != _data._nil) {
e = e->right;
+ }
return e;
}
@@ -548,8 +581,9 @@ public:
int calculate_depth() const {
// used for debug mostly
- if (!_data._root)
+ if (!_data._root) {
return 0;
+ }
int max_d = 0;
_calculate_depth(_data._root->left, max_d, 0);
@@ -557,8 +591,9 @@ public:
}
void clear() {
- if (!_data._root)
+ if (!_data._root) {
return;
+ }
_cleanup_tree(_data._root->left);
_data._root->left = _data._nil;