#include "object_forest.hpp" #include #include #include namespace object_forest { namespace { constexpr int BAD_HANDLE = -9; constexpr int INVALID = -22; constexpr int EXISTS = -17; } void Forest::preorder(Handle handle, std::vector &out) const { out.push_back(handle); const Node &node = nodes_[static_cast(locate(handle))]; for (Handle child : node.children) { preorder(child, out); } } void Forest::postorder(Handle handle, std::vector &out) const { const Node &node = nodes_[static_cast(locate(handle))]; for (Handle child : node.children) { postorder(child, out); } out.push_back(handle); } int Forest::move(Handle handle, Handle parent, std::size_t position) { const int source_slot = locate(handle); if (source_slot < 0) { return BAD_HANDLE; } if (!valid_parent(parent)) { return BAD_HANDLE; } const Node &source = nodes_[static_cast(source_slot)]; const Handle previous_parent = source.parent; const std::size_t available = siblings(parent).size() - (previous_parent == parent ? 1 : 0); if (position > available) { return INVALID; } Handle ancestor = parent; while (ancestor != 0) { if (ancestor == handle) { return INVALID; } ancestor = nodes_[static_cast(locate(ancestor))].parent; } if (duplicate_name(parent, source.name, handle)) { return EXISTS; } Forest draft = *this; std::vector &old_siblings = draft.siblings(previous_parent); const auto old_position = std::find(old_siblings.begin(), old_siblings.end(), handle); const std::size_t old_index = static_cast(old_position - old_siblings.begin()); old_siblings.erase(old_position); if (previous_parent == parent && old_index < position) { --position; } std::vector &destination = draft.siblings(parent); destination.insert(destination.begin() + static_cast(position), handle); draft.nodes_[static_cast(source_slot)].parent = parent; return commit(draft); } int Forest::clone(Handle source, Handle parent, const std::string &new_name, Handle &out) { if (locate(source) < 0) { return BAD_HANDLE; } if (!valid_parent(parent)) { return BAD_HANDLE; } if (!valid_name(new_name)) { return INVALID; } if (duplicate_name(parent, new_name)) { return EXISTS; } std::vector originals; preorder(source, originals); Forest draft = *this; std::vector> mapping; mapping.reserve(originals.size()); Handle root_copy = 0; for (Handle original : originals) { const Node &node = nodes_[static_cast(locate(original))]; Handle destination_parent = parent; if (original != source) { const auto entry = std::find_if(mapping.begin(), mapping.end(), [&](const std::pair &item) { return item.first == node.parent; }); destination_parent = entry->second; } const std::string &name = original == source ? new_name : node.name; Handle copied = 0; const int result = draft.reserve_node(destination_parent, name, node.properties, copied); if (result != 0) { return result; } mapping.push_back({original, copied}); if (original == source) { root_copy = copied; } } const int result = commit(draft); if (result != 0) { return result; } out = root_copy; return 0; } int Forest::retire(Handle handle, std::vector &trace) { const int slot = locate(handle); if (slot < 0) { return BAD_HANDLE; } std::vector retired; preorder(handle, retired); Forest draft = *this; const Handle parent = nodes_[static_cast(slot)].parent; std::vector &owner = draft.siblings(parent); owner.erase(std::find(owner.begin(), owner.end(), handle)); for (Handle child : retired) { const int removed = draft.locate(child); draft.nodes_[static_cast(removed)] = Node{}; } const int result = commit(draft); if (result != 0) { return result; } trace.swap(retired); return 0; } int Forest::clear(std::vector &trace) { std::vector retired; for (Handle root : roots_) { postorder(root, retired); } Forest draft = *this; for (Node &node : draft.nodes_) { node = Node{}; } draft.roots_.clear(); const int result = commit(draft); if (result != 0) { return result; } trace.swap(retired); return 0; } }