Skip to content

Commit 633f0c8

Browse files
authored
Issues #172/173/174/175 Fix src/library/containers tests (#253)
1 parent bff3cd9 commit 633f0c8

File tree

14 files changed

+166
-140
lines changed

14 files changed

+166
-140
lines changed

src/library/containers/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
2-
# This file was generated by the build system used internally in the Yandex monorepo.
3-
# Only simple modifications are allowed (adding source-files to targets, adding simple properties
4-
# like target_include_directories). These modifications will be ported to original
5-
# ya.make files by maintainers. Any complex modifications which can't be ported back to the
6-
# original buildsystem will not be accepted.
7-
8-
91
add_subdirectory(disjoint_interval_tree)
102
add_subdirectory(intrusive_rb_tree)
113
add_subdirectory(paged_vector)

src/library/containers/disjoint_interval_tree/disjoint_interval_tree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
template <class T>
99
class TDisjointIntervalTree {
1010
private:
11-
static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value");
11+
static_assert(std::is_integral_v<T>, "expect std::is_integral_v<T>");
1212

1313
using TTree = std::map<T, T>; // [key, value)
1414
using TIterator = typename TTree::iterator;
@@ -32,7 +32,7 @@ class TDisjointIntervalTree {
3232
// we assume that none of elements from [begin, end) belong to tree.
3333
void InsertInterval(const T begin, const T end) {
3434
InsertIntervalImpl(begin, end);
35-
NumElements += (size_t)(end - begin);
35+
NumElements += static_cast<size_t>(end - begin);
3636
}
3737

3838
bool Has(const T t) const {

src/library/containers/disjoint_interval_tree/ut/ya.make

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/library/containers/intrusive_rb_tree/rb_tree.h

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,32 @@ struct TRbTreeNodeBase {
3131
}
3232

3333
static TBasePtr MinimumNode(TBasePtr x) {
34-
while (x->Left_ != nullptr)
34+
while (x->Left_ != nullptr) {
3535
x = x->Left_;
36-
36+
}
3737
return x;
3838
}
3939

4040
static TBasePtr MaximumNode(TBasePtr x) {
41-
while (x->Right_ != nullptr)
41+
while (x->Right_ != nullptr) {
4242
x = x->Right_;
43-
43+
}
4444
return x;
4545
}
4646

4747
static TBasePtr ByIndex(TBasePtr x, size_t index) {
4848
if (x->Left_ != nullptr) {
49-
if (index < x->Left_->Children_)
49+
if (index < x->Left_->Children_) {
5050
return ByIndex(x->Left_, index);
51+
}
5152
index -= x->Left_->Children_;
5253
}
53-
if (0 == index)
54+
if (0 == index) {
5455
return x;
55-
if (!x->Right_)
56+
}
57+
if (!x->Right_) {
5658
ythrow yexception() << "index not found";
59+
}
5760
return ByIndex(x->Right_, index - 1);
5861
}
5962
};
@@ -335,12 +338,13 @@ class TRbTree {
335338
TBasePtr y = nullptr;
336339
TBasePtr x = Root(); // Current node.
337340

338-
while (x != nullptr)
339-
if (!KeyCompare_(ValueNode(x), k))
341+
while (x != nullptr) {
342+
if (!KeyCompare_(ValueNode(x), k)) {
340343
y = x, x = LeftNode(x);
341-
else
344+
} else {
342345
x = RightNode(x);
343-
346+
}
347+
}
344348
if (y) {
345349
if (KeyCompare_(k, ValueNode(y))) {
346350
y = nullptr;
@@ -375,12 +379,13 @@ class TRbTree {
375379
TBasePtr y = const_cast<TBasePtr>(&this->Data_); /* Last node which is not less than k. */
376380
TBasePtr x = Root(); /* Current node. */
377381

378-
while (x != nullptr)
379-
if (!KeyCompare_(ValueNode(x), k))
382+
while (x != nullptr) {
383+
if (!KeyCompare_(ValueNode(x), k)) {
380384
y = x, x = LeftNode(x);
381-
else
385+
} else {
382386
x = RightNode(x);
383-
387+
}
388+
}
384389
return y;
385390
}
386391

@@ -389,12 +394,13 @@ class TRbTree {
389394
TBasePtr y = const_cast<TBasePtr>(&this->Data_); /* Last node which is greater than k. */
390395
TBasePtr x = Root(); /* Current node. */
391396

392-
while (x != nullptr)
393-
if (KeyCompare_(k, ValueNode(x)))
397+
while (x != nullptr) {
398+
if (KeyCompare_(k, ValueNode(x))) {
394399
y = x, x = LeftNode(x);
395-
else
400+
} else {
396401
x = RightNode(x);
397-
402+
}
403+
}
398404
return y;
399405
}
400406

@@ -457,14 +463,16 @@ class TRbTree {
457463
KeyCompare_(ValueNode(val), ValueNode(parent))))
458464
{
459465
LeftNode(parent) = new_node;
460-
if (parent == LeftMost())
466+
if (parent == LeftMost()) {
461467
// maintain LeftMost() pointing to min node
462468
LeftMost() = new_node;
469+
}
463470
} else {
464471
RightNode(parent) = new_node;
465-
if (parent == RightMost())
472+
if (parent == RightMost()) {
466473
// maintain RightMost() pointing to max node
467474
RightMost() = new_node;
475+
}
468476
}
469477
ParentNode(new_node) = parent;
470478
TRbGlobalInst::Rebalance(new_node, this->Data_.Parent_);
@@ -532,16 +540,18 @@ template <class TDummy>
532540
void TRbGlobal<TDummy>::RotateLeft(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
533541
TRbTreeNodeBase* y = x->Right_;
534542
x->Right_ = y->Left_;
535-
if (y->Left_ != nullptr)
543+
if (y->Left_ != nullptr) {
536544
y->Left_->Parent_ = x;
545+
}
537546
y->Parent_ = x->Parent_;
538547

539-
if (x == root)
548+
if (x == root) {
540549
root = y;
541-
else if (x == x->Parent_->Left_)
550+
} else if (x == x->Parent_->Left_) {
542551
x->Parent_->Left_ = y;
543-
else
552+
} else {
544553
x->Parent_->Right_ = y;
554+
}
545555
y->Left_ = x;
546556
x->Parent_ = y;
547557
y->Children_ = x->Children_;
@@ -552,16 +562,18 @@ template <class TDummy>
552562
void TRbGlobal<TDummy>::RotateRight(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
553563
TRbTreeNodeBase* y = x->Left_;
554564
x->Left_ = y->Right_;
555-
if (y->Right_ != nullptr)
565+
if (y->Right_ != nullptr) {
556566
y->Right_->Parent_ = x;
567+
}
557568
y->Parent_ = x->Parent_;
558569

559-
if (x == root)
570+
if (x == root) {
560571
root = y;
561-
else if (x == x->Parent_->Right_)
572+
} else if (x == x->Parent_->Right_) {
562573
x->Parent_->Right_ = y;
563-
else
574+
} else {
564575
x->Parent_->Left_ = y;
576+
}
565577
y->Right_ = x;
566578
x->Parent_ = y;
567579
y->Children_ = x->Children_;
@@ -633,15 +645,13 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
633645
TRbTreeNodeBase* x;
634646
TRbTreeNodeBase* x_parent;
635647

636-
if (y->Left_ == nullptr) // z has at most one non-null child. y == z.
637-
x = y->Right_; // x might be null.
638-
else {
639-
if (y->Right_ == nullptr) // z has exactly one non-null child. y == z.
640-
x = y->Left_; // x is not null.
641-
else { // z has two non-null children. Set y to
642-
y = TRbTreeNodeBase::MinimumNode(y->Right_); // z's successor. x might be null.
643-
x = y->Right_;
644-
}
648+
if (y->Left_ == nullptr) { // z has at most one non-null child. y == z.
649+
x = y->Right_; // x might be null.
650+
} else if (y->Right_ == nullptr) { // z has exactly one non-null child. y == z.
651+
x = y->Left_; // x is not null.
652+
} else { // z has two non-null children. Set y to
653+
y = TRbTreeNodeBase::MinimumNode(y->Right_); // z's successor. x might be null.
654+
x = y->Right_;
645655
}
646656

647657
if (y != z) {
@@ -650,19 +660,22 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
650660
y->Left_ = z->Left_;
651661
if (y != z->Right_) {
652662
x_parent = y->Parent_;
653-
if (x)
663+
if (x) {
654664
x->Parent_ = y->Parent_;
665+
}
655666
y->Parent_->Left_ = x; // y must be a child of mLeft
656667
y->Right_ = z->Right_;
657668
z->Right_->Parent_ = y;
658-
} else
669+
} else {
659670
x_parent = y;
660-
if (root == z)
671+
}
672+
if (root == z) {
661673
root = y;
662-
else if (z->Parent_->Left_ == z)
674+
} else if (z->Parent_->Left_ == z) {
663675
z->Parent_->Left_ = y;
664-
else
676+
} else {
665677
z->Parent_->Right_ = y;
678+
}
666679
y->Parent_ = z->Parent_;
667680
DoSwap(y->Color_, z->Color_);
668681

@@ -678,31 +691,35 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
678691
} else {
679692
// y == z
680693
x_parent = y->Parent_;
681-
if (x)
694+
if (x) {
682695
x->Parent_ = y->Parent_;
683-
if (root == z)
696+
}
697+
if (root == z) {
684698
root = x;
685-
else {
686-
if (z->Parent_->Left_ == z)
699+
} else {
700+
if (z->Parent_->Left_ == z) {
687701
z->Parent_->Left_ = x;
688-
else
702+
} else {
689703
z->Parent_->Right_ = x;
704+
}
690705
DecrementChildrenUntilRoot(z->Parent_, root); // we lost y
691706
}
692707

693708
if (leftmost == z) {
694-
if (z->Right_ == nullptr) // z->mLeft must be null also
709+
if (z->Right_ == nullptr) { // z->mLeft must be null also
695710
leftmost = z->Parent_;
696711
// makes leftmost == _M_header if z == root
697-
else
712+
} else {
698713
leftmost = TRbTreeNodeBase::MinimumNode(x);
714+
}
699715
}
700716
if (rightmost == z) {
701-
if (z->Left_ == nullptr) // z->mRight must be null also
717+
if (z->Left_ == nullptr) { // z->mRight must be null also
702718
rightmost = z->Parent_;
703719
// makes rightmost == _M_header if z == root
704-
else // x == z->mLeft
720+
} else { // x == z->mLeft
705721
rightmost = TRbTreeNodeBase::MaximumNode(x);
722+
}
706723
}
707724
}
708725

@@ -726,16 +743,18 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
726743
x_parent = x_parent->Parent_;
727744
} else {
728745
if (w->Right_ == nullptr || w->Right_->Color_ == RBTreeBlack) {
729-
if (w->Left_)
746+
if (w->Left_) {
730747
w->Left_->Color_ = RBTreeBlack;
748+
}
731749
w->Color_ = RBTreeRed;
732750
RotateRight(w, root);
733751
w = x_parent->Right_;
734752
}
735753
w->Color_ = x_parent->Color_;
736754
x_parent->Color_ = RBTreeBlack;
737-
if (w->Right_)
755+
if (w->Right_) {
738756
w->Right_->Color_ = RBTreeBlack;
757+
}
739758
RotateLeft(x_parent, root);
740759
break;
741760
}
@@ -758,31 +777,34 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
758777
x_parent = x_parent->Parent_;
759778
} else {
760779
if (w->Left_ == nullptr || w->Left_->Color_ == RBTreeBlack) {
761-
if (w->Right_)
780+
if (w->Right_) {
762781
w->Right_->Color_ = RBTreeBlack;
782+
}
763783
w->Color_ = RBTreeRed;
764784
RotateLeft(w, root);
765785
w = x_parent->Left_;
766786
}
767787
w->Color_ = x_parent->Color_;
768788
x_parent->Color_ = RBTreeBlack;
769-
if (w->Left_)
789+
if (w->Left_) {
770790
w->Left_->Color_ = RBTreeBlack;
791+
}
771792
RotateRight(x_parent, root);
772793
break;
773794
}
774795
}
775-
if (x)
796+
if (x) {
776797
x->Color_ = RBTreeBlack;
798+
}
777799
}
778800
return y;
779801
}
780802

781803
template <class TDummy>
782804
TRbTreeNodeBase* TRbGlobal<TDummy>::DecrementNode(TRbTreeNodeBase* Node_) {
783-
if (Node_->Color_ == RBTreeRed && Node_->Parent_->Parent_ == Node_)
805+
if (Node_->Color_ == RBTreeRed && Node_->Parent_->Parent_ == Node_) {
784806
Node_ = Node_->Right_;
785-
else if (Node_->Left_ != nullptr) {
807+
} else if (Node_->Left_ != nullptr) {
786808
Node_ = TRbTreeNodeBase::MaximumNode(Node_->Left_);
787809
} else {
788810
TBasePtr y = Node_->Parent_;
@@ -808,8 +830,9 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::IncrementNode(TRbTreeNodeBase* Node_) {
808830
// check special case: This is necessary if mNode is the
809831
// _M_head and the tree contains only a single node y. In
810832
// that case parent, left and right all point to y!
811-
if (Node_->Right_ != y)
833+
if (Node_->Right_ != y) {
812834
Node_ = y;
835+
}
813836
}
814837
return Node_;
815838
}

src/library/containers/intrusive_rb_tree/ut/ya.make

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)