| 1687 | |
| 1688 | template <typename P> |
| 1689 | void btree_node<P>::swap(btree_node *x, allocator_type *alloc) { |
| 1690 | using std::swap; |
| 1691 | assert(leaf() == x->leaf()); |
| 1692 | |
| 1693 | // Determine which is the smaller/larger node. |
| 1694 | btree_node *smaller = this, *larger = x; |
| 1695 | if (smaller->count() > larger->count()) { |
| 1696 | swap(smaller, larger); |
| 1697 | } |
| 1698 | |
| 1699 | // Swap the values. |
| 1700 | std::swap_ranges(smaller->slot(0), smaller->slot(smaller->count()), |
| 1701 | larger->slot(0)); |
| 1702 | |
| 1703 | // Move values that can't be swapped. |
| 1704 | const size_type to_move = larger->count() - smaller->count(); |
| 1705 | larger->uninitialized_move_n(to_move, smaller->count(), smaller->count(), |
| 1706 | smaller, alloc); |
| 1707 | larger->value_destroy_n(smaller->count(), to_move, alloc); |
| 1708 | |
| 1709 | if (!leaf()) { |
| 1710 | // Swap the child pointers. |
| 1711 | auto* smaller_begin = &smaller->mutable_child(0); |
| 1712 | auto* larger_begin = &larger->mutable_child(0); |
| 1713 | auto count = smaller->count() + 1; |
| 1714 | std::swap_ranges(smaller_begin, |
| 1715 | smaller_begin + count, |
| 1716 | larger_begin); |
| 1717 | // Update swapped children's parent pointers. |
| 1718 | int i = 0; |
| 1719 | for (; i <= smaller->count(); ++i) { |
| 1720 | smaller->child(i)->set_parent(smaller); |
| 1721 | larger->child(i)->set_parent(larger); |
| 1722 | } |
| 1723 | // Move the child pointers that couldn't be swapped. |
| 1724 | for (; i <= larger->count(); ++i) { |
| 1725 | smaller->init_child(i, larger->child(i)); |
| 1726 | larger->clear_child(i); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | // Swap the counts. |
| 1731 | swap(mutable_count(), x->mutable_count()); |
| 1732 | } |
| 1733 | |
| 1734 | //// |
| 1735 | // btree_iterator methods |
no test coverage detected