| 602 | using Chunk = HeapSimulator::Chunk; |
| 603 | |
| 604 | void BufferIntervalTree::Add(int64 start, int64 end, const Chunk& chunk) { |
| 605 | node_storage_.emplace_back(BufferIntervalTreeNode{ |
| 606 | start, end, end, chunk, |
| 607 | /*left=*/nullptr, /*right=*/nullptr, /*parent=*/nullptr}); |
| 608 | if (root_ == nullptr) { |
| 609 | root_ = &node_storage_.back(); |
| 610 | // This is root. |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | BufferIntervalTreeNode* parent = root_; |
| 615 | while (true) { |
| 616 | parent->subtree_end = std::max(parent->subtree_end, end); |
| 617 | if (parent->start > start) { |
| 618 | if (parent->left == nullptr) { |
| 619 | parent->left = &node_storage_.back(); |
| 620 | node_storage_.back().parent = parent; |
| 621 | return; |
| 622 | } |
| 623 | parent = parent->left; |
| 624 | } else { |
| 625 | if (parent->right == nullptr) { |
| 626 | parent->right = &node_storage_.back(); |
| 627 | node_storage_.back().parent = parent; |
| 628 | return; |
| 629 | } |
| 630 | parent = parent->right; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | bool BufferIntervalTree::Remove(int64 start, int64 end, const Chunk& chunk) { |
| 636 | BufferIntervalTreeNode* to_delete = root_; |