|
15 | 15 | #include <sstream> |
16 | 16 | #include <stack> |
17 | 17 | #include <stdexcept> |
| 18 | +#include <utility> |
18 | 19 | #include <vector> |
19 | 20 |
|
20 | 21 | #ifdef HAVE_OPENSSL |
@@ -747,22 +748,9 @@ namespace merkle |
747 | 748 |
|
748 | 749 | /// @brief Moves a tree |
749 | 750 | /// @param other Tree to move |
750 | | - TreeT(TreeT&& other) noexcept : |
751 | | - leaf_nodes(std::move(other.leaf_nodes)), |
752 | | - uninserted_leaf_nodes(std::move(other.uninserted_leaf_nodes)), |
753 | | - _root(std::move(other._root)), |
754 | | - num_flushed(other.num_flushed), |
755 | | - insertion_stack(std::move(other.insertion_stack)), |
756 | | - hashing_stack(std::move(other.hashing_stack)), |
757 | | - walk_stack(std::move(other.walk_stack)) |
758 | | - { |
759 | | - other.leaf_nodes.clear(); |
760 | | - other.uninserted_leaf_nodes.clear(); |
761 | | - other._root = nullptr; |
762 | | - other.num_flushed = 0; |
763 | | - other.insertion_stack.clear(); |
764 | | - other.hashing_stack.clear(); |
765 | | - other.walk_stack.clear(); |
| 751 | + TreeT(TreeT&& other) noexcept |
| 752 | + { |
| 753 | + move_from(other); |
766 | 754 | } |
767 | 755 |
|
768 | 756 | /// @brief Deserialises a tree |
@@ -790,11 +778,7 @@ namespace merkle |
790 | 778 | /// @brief Deconstructor |
791 | 779 | ~TreeT() |
792 | 780 | { |
793 | | - delete (_root); |
794 | | - for (auto n : uninserted_leaf_nodes) |
795 | | - { |
796 | | - delete (n); |
797 | | - } |
| 781 | + clear(); |
798 | 782 | } |
799 | 783 |
|
800 | 784 | /// @brief Invariant of the tree |
@@ -981,17 +965,7 @@ namespace merkle |
981 | 965 | { |
982 | 966 | return *this; |
983 | 967 | } |
984 | | - leaf_nodes.clear(); |
985 | | - for (auto n : uninserted_leaf_nodes) |
986 | | - { |
987 | | - delete (n); |
988 | | - } |
989 | | - uninserted_leaf_nodes.clear(); |
990 | | - insertion_stack.clear(); |
991 | | - hashing_stack.clear(); |
992 | | - walk_stack.clear(); |
993 | | - delete (_root); |
994 | | - _root = nullptr; |
| 968 | + clear(); |
995 | 969 |
|
996 | 970 | size_t to_skip = (other.num_flushed % 2 == 0) ? 0 : 1; |
997 | 971 | _root = Node::copy_node( |
@@ -1020,33 +994,8 @@ namespace merkle |
1020 | 994 | return *this; |
1021 | 995 | } |
1022 | 996 |
|
1023 | | - leaf_nodes.clear(); |
1024 | | - for (auto n : uninserted_leaf_nodes) |
1025 | | - { |
1026 | | - delete (n); |
1027 | | - } |
1028 | | - uninserted_leaf_nodes.clear(); |
1029 | | - insertion_stack.clear(); |
1030 | | - hashing_stack.clear(); |
1031 | | - walk_stack.clear(); |
1032 | | - delete (_root); |
1033 | | - _root = nullptr; |
1034 | | - |
1035 | | - leaf_nodes = std::move(other.leaf_nodes); |
1036 | | - uninserted_leaf_nodes = std::move(other.uninserted_leaf_nodes); |
1037 | | - _root = other._root; |
1038 | | - num_flushed = other.num_flushed; |
1039 | | - insertion_stack = std::move(other.insertion_stack); |
1040 | | - hashing_stack = std::move(other.hashing_stack); |
1041 | | - walk_stack = std::move(other.walk_stack); |
1042 | | - |
1043 | | - other.leaf_nodes.clear(); |
1044 | | - other.uninserted_leaf_nodes.clear(); |
1045 | | - other._root = nullptr; |
1046 | | - other.num_flushed = 0; |
1047 | | - other.insertion_stack.clear(); |
1048 | | - other.hashing_stack.clear(); |
1049 | | - other.walk_stack.clear(); |
| 997 | + clear(); |
| 998 | + move_from(other); |
1050 | 999 | return *this; |
1051 | 1000 | } |
1052 | 1001 |
|
@@ -1468,17 +1417,7 @@ namespace merkle |
1468 | 1417 | { |
1469 | 1418 | MERKLECPP_TRACE(MERKLECPP_TOUT << "> deserialise " << std::endl;); |
1470 | 1419 |
|
1471 | | - delete (_root); |
1472 | | - leaf_nodes.clear(); |
1473 | | - for (auto n : uninserted_leaf_nodes) |
1474 | | - { |
1475 | | - delete (n); |
1476 | | - } |
1477 | | - uninserted_leaf_nodes.clear(); |
1478 | | - insertion_stack.clear(); |
1479 | | - hashing_stack.clear(); |
1480 | | - walk_stack.clear(); |
1481 | | - _root = nullptr; |
| 1420 | + clear(); |
1482 | 1421 |
|
1483 | 1422 | size_t num_leaf_nodes = deserialise_uint64_t(bytes, position); |
1484 | 1423 | num_flushed = deserialise_uint64_t(bytes, position); |
@@ -1770,15 +1709,40 @@ namespace merkle |
1770 | 1709 | protected: |
1771 | 1710 | void validate_partial_range(size_t from, size_t to) const |
1772 | 1711 | { |
1773 | | - const bool from_out_of_range = from < min_index() || max_index() < from; |
1774 | | - const bool to_out_of_range = to < min_index() || max_index() < to; |
1775 | | - const bool reversed_range = from > to; |
1776 | | - if (empty() || from_out_of_range || to_out_of_range || reversed_range) |
| 1712 | + if ( |
| 1713 | + empty() || !(min_index() <= from && from <= to && to <= max_index())) |
1777 | 1714 | { |
1778 | 1715 | throw std::runtime_error("invalid leaf indices"); |
1779 | 1716 | } |
1780 | 1717 | } |
1781 | 1718 |
|
| 1719 | + void clear() |
| 1720 | + { |
| 1721 | + leaf_nodes.clear(); |
| 1722 | + for (auto n : uninserted_leaf_nodes) |
| 1723 | + { |
| 1724 | + delete (n); |
| 1725 | + } |
| 1726 | + uninserted_leaf_nodes.clear(); |
| 1727 | + insertion_stack.clear(); |
| 1728 | + hashing_stack.clear(); |
| 1729 | + walk_stack.clear(); |
| 1730 | + delete (_root); |
| 1731 | + _root = nullptr; |
| 1732 | + num_flushed = 0; |
| 1733 | + } |
| 1734 | + |
| 1735 | + void move_from(TreeT& other) noexcept |
| 1736 | + { |
| 1737 | + leaf_nodes = std::exchange(other.leaf_nodes, {}); |
| 1738 | + uninserted_leaf_nodes = std::exchange(other.uninserted_leaf_nodes, {}); |
| 1739 | + _root = std::exchange(other._root, nullptr); |
| 1740 | + num_flushed = std::exchange(other.num_flushed, 0); |
| 1741 | + insertion_stack = std::exchange(other.insertion_stack, {}); |
| 1742 | + hashing_stack = std::exchange(other.hashing_stack, {}); |
| 1743 | + walk_stack = std::exchange(other.walk_stack, {}); |
| 1744 | + } |
| 1745 | + |
1782 | 1746 | /// @brief Vector of leaf nodes current in the tree |
1783 | 1747 | std::vector<Node*> leaf_nodes; |
1784 | 1748 |
|
|
0 commit comments