Skip to content

Commit 68b9649

Browse files
committed
Fix the nullptr warning in red/black tree impl.
1 parent a8f7859 commit 68b9649

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

vm/vm/main/dictionary-decl.hh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ private:
4242
enum Color { clBlack, clRed };
4343

4444
struct Node {
45-
bool safeIsBlack() {
46-
return (this == nullptr) || (color == clBlack);
47-
}
48-
49-
bool safeIsRed() {
50-
return (this != nullptr) && (color == clRed);
51-
}
52-
5345
Node* getOneChild() {
5446
if (left == nullptr) {
5547
return right;
@@ -183,6 +175,14 @@ private:
183175
inline
184176
void rotateRight(Node* parent, Node* child);
185177

178+
bool isBlack(Node* n) {
179+
return (n == nullptr) || (n->color == clBlack);
180+
}
181+
182+
bool isRed(Node* n) {
183+
return (n != nullptr) && (n->color == clRed);
184+
}
185+
186186
private:
187187
Node* root;
188188
};

vm/vm/main/dictionary.hh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void NodeDictionary::fixInsert(VM vm, Node* node) {
179179

180180
Node* uncle = grandParent->getTheOtherChild(parent);
181181

182-
if (uncle->safeIsRed()) {
182+
if (isRed(uncle)) {
183183
// Case 3
184184
parent->color = clBlack;
185185
uncle->color = clBlack;
@@ -265,7 +265,7 @@ void NodeDictionary::fixRemove(VM vm, Node* node, Node* parent) {
265265
Node* siblingLeft = sibling->left;
266266
Node* siblingRight = sibling->right;
267267

268-
if (siblingLeft->safeIsBlack() && siblingRight->safeIsBlack()) {
268+
if (isBlack(siblingLeft) && isBlack(siblingRight)) {
269269
if (parent->color == clBlack) {
270270
// Case 3
271271
sibling->color = clRed;
@@ -277,12 +277,12 @@ void NodeDictionary::fixRemove(VM vm, Node* node, Node* parent) {
277277
}
278278
} else {
279279
// Case 5
280-
if ((node == parent->left) && siblingRight->safeIsBlack()) {
281-
assert(siblingLeft->safeIsRed());
280+
if ((node == parent->left) && isBlack(siblingRight)) {
281+
assert(isRed(siblingLeft));
282282
rotateRight(sibling, siblingLeft);
283283
std::swap(sibling, siblingLeft);
284-
} else if ((node == parent->right) && siblingLeft->safeIsBlack()) {
285-
assert(siblingRight->safeIsRed());
284+
} else if ((node == parent->right) && isBlack(siblingLeft)) {
285+
assert(isRed(siblingRight));
286286
rotateLeft(sibling, siblingRight);
287287
std::swap(sibling, siblingRight);
288288
}

0 commit comments

Comments
 (0)