From e1ddd85af54d530a16715d03deea3d6ca130ce98 Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Sun, 6 Oct 2024 08:32:04 +0800 Subject: [PATCH] 1 --- StoryPresent/dag_layout.cpp | 208 +++--------------------------------- StoryPresent/dag_layout.h | 13 +-- 2 files changed, 18 insertions(+), 203 deletions(-) diff --git a/StoryPresent/dag_layout.cpp b/StoryPresent/dag_layout.cpp index 69d94bb..b17bbbe 100644 --- a/StoryPresent/dag_layout.cpp +++ b/StoryPresent/dag_layout.cpp @@ -238,7 +238,7 @@ void DAGGraph::graph_layer_nodes_sort_forward(int layer_index, const QListsortNumber().isNull()) + if (target_node->sortNumber().isNull()) target_node->setSortNumber(idx + 1); } } @@ -261,7 +261,7 @@ void DAGGraph::graph_layer_nodes_sort_forward(int layer_index, const QList ins) { return ins->sortNumber().isNull(); }); - if(nodes_hangout.size()){ + if (nodes_hangout.size()) { auto min_anchor = std::min_element(nodes_fixed.begin(), nodes_fixed.end(), [](std::shared_ptr a, std::shared_ptr b) { return a->sortNumber() < b->sortNumber(); }); @@ -271,7 +271,7 @@ void DAGGraph::graph_layer_nodes_sort_forward(int layer_index, const QListcurrent_nodelist_filling(layer_index, nodes); + this->current_nodelist_filling_indi(layer_index, nodes); } this->nodes_sort_with_above(layer_index, nodes); @@ -279,15 +279,14 @@ void DAGGraph::graph_layer_nodes_sort_forward(int layer_index, const QList>& nodes) +bool dags::DAGGraph::current_nodelist_filling_indi(int layer_index, const QList>& nodes) { // 提取本层次节点 QList> nodes_within_current_layer; std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(nodes_within_current_layer), [=](std::shared_ptr ins) { return ins->layerNumber() == layer_index; }); - if(!nodes_within_current_layer.size()) - return; - + if (!nodes_within_current_layer.size()) + return false; std::sort(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), [](std::shared_ptra, std::shared_ptr b)->bool { @@ -318,6 +317,9 @@ void dags::DAGGraph::current_nodelist_filling(int layer_index, const QList>& nodes) @@ -374,198 +377,18 @@ void dags::DAGGraph::nodes_sort_with_above(int layer_index, const QListsetSortNumber(sort_idx - temp_anchor.second); } } -/* -void dags::DAGGraph::current_nodelist_filling_backward(int layer_index, const QList>& nodes) -{ - // 提取既有排序成果 - QList> ordered_node; - std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(ordered_node), - [=](std::shared_ptr ins) { - return !ins->isFakeNode() && ins->layerNumber() > layer_index; - }); - QList ordered_values; - std::transform(ordered_node.begin(), ordered_node.end(), std::back_inserter(ordered_values), - [=](std::shared_ptr ins) { - return (int) ins->sortNumber(); - }); - ordered_values = ordered_values.toSet().toList(); - std::sort(ordered_values.begin(), ordered_values.end()); - // 提取当前层次节点并排序 - QList> nodes_within_current_layer; - std::copy_if(nodes.begin(), nodes.end(), std::back_inserter(nodes_within_current_layer), - [=](std::shared_ptr ins) { return ins->layerNumber() == layer_index; }); - - std::for_each(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), - [&](std::shared_ptr n) { if (!n->getUpstreamNodes().size()) n->setSortNumber(ordered_values.first() - 1); }); - std::sort(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), - [](std::shared_ptr a, std::shared_ptr b) { return a->sortNumber() < b->sortNumber(); }); - - // 填缝排序 - ordered_values.prepend(ordered_values.first() - 1); - ordered_values << ordered_values.last() + 1; - for (auto idx = 1; idx < ordered_values.size(); ++idx) { - auto prev_sortv = ordered_values[idx - 1]; - auto curr_sortv = ordered_values[idx]; - - QList> pick_ups; - std::copy_if(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), std::back_inserter(pick_ups), - [=](std::shared_ptr ins) { return ins->sortNumber() >= prev_sortv && ins->sortNumber() < curr_sortv; }); - - for (int idx = 0; idx < std::min(curr_sortv - prev_sortv, pick_ups.size()); idx++) { - auto target_node = pick_ups[idx]; - target_node->setSortNumber(prev_sortv + idx); - } - if (curr_sortv - prev_sortv < pick_ups.size()) { - auto npicks = pick_ups.mid(curr_sortv - prev_sortv); - auto inc_span = 1.0 / (npicks.size() + 4); - - for (auto idx = 0; idx < npicks.size(); ++idx) { - auto nsortv = curr_sortv - 1 + (idx + 1) * inc_span; - npicks[idx]->setSortNumber(nsortv); - } - } - } -} - -void dags::DAGGraph::current_nodelist_separate(const QList>& nodes_within_current_layer) -{ - QList sort_values; - std::transform(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), - std::back_inserter(sort_values), [](std::shared_ptr n) { return n->sortNumber(); }); - sort_values = sort_values.toSet().toList(); - - for (auto& sort_v : sort_values) { - QList> pick_items; - std::copy_if(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), - std::back_inserter(pick_items), [=](std::shared_ptr ins) { return ins->sortNumber() == sort_v; }); - - std::sort(pick_items.begin(), pick_items.end(), - [](std::shared_ptra, std::shared_ptr b)->bool { - auto prevs_a = a->getUpstreamNodes(); - auto prevs_b = b->getUpstreamNodes(); - - if (!prevs_a.size() && !prevs_b.size()) - return a < b; - if (!prevs_a.size()) - return false; - if (!prevs_b.size()) - return true; - - auto upnode_a = std::min_element(prevs_a.begin(), prevs_a.end(), - [](std::shared_ptr a, std::shared_ptr b) { return a->sortNumber() < b->sortNumber(); }); - auto upnode_b = std::min_element(prevs_b.begin(), prevs_b.end(), - [](std::shared_ptr a, std::shared_ptr b) { return a->sortNumber() < b->sortNumber(); }); - - return (*upnode_a)->sortNumber() > (*upnode_b)->sortNumber(); - }); - - for (int idx = 0; idx < pick_items.size(); ++idx) { - auto item = pick_items[idx]; - item->setSortNumber(item->sortNumber() - 0.000000001 * idx); - } - } -} - -void dags::DAGGraph::current_layer_separate(int layer_index, const QList>& nodes) -{ - QList> nodes_within_current_layer; - for (auto n : nodes) - if (n->layerNumber() == layer_index) { - nodes_within_current_layer.append(n); - } - - this->current_nodelist_separate(nodes_within_current_layer); -} -*/ void dags::DAGGraph::graph_adjust_after_layout(const QList>& total_nodes) { auto layer_index = this->maxLayerCount() - 1; while (layer_index > -1) { this->layer_adjust_via_next_sibling(layer_index, total_nodes); - this->current_nodelist_filling(layer_index, total_nodes); - this->nodes_sort_with_belows(layer_index, total_nodes); + if (this->current_nodelist_filling_indi(layer_index, total_nodes)) + this->nodes_sort_with_belows(layer_index, total_nodes); layer_index--; }; } -/* -bool dags::DAGGraph::layer_adjust_after_layout(int curr_layer, const QList>& total_nodes) -{ - QList> nodes_within_current_layer; - std::copy_if(total_nodes.begin(), total_nodes.end(), std::back_inserter(nodes_within_current_layer), - [&](std::shared_ptr ins) { return ins->layerNumber() == curr_layer; }); - if (!nodes_within_current_layer.size()) - return false; - - std::sort(nodes_within_current_layer.begin(), nodes_within_current_layer.end(), - [](std::shared_ptr a, std::shared_ptr b) { return a->sortNumber() < b->sortNumber(); }); - - std::shared_ptr prev_sort = nullptr; - for (auto node : nodes_within_current_layer) { - this->node_adjust_after_layout(prev_sort, node, total_nodes); - prev_sort = node; - } - return true; -} - -void dags::DAGGraph::node_adjust_after_layout(std::shared_ptr prev, std::shared_ptr curr_node, const QList>& total_nodes) -{ - // 计算上游调整 - auto prev_layer_nodes = curr_node->getUpstreamNodes(); - QList prev_sort_values; - std::transform(prev_layer_nodes.begin(), prev_layer_nodes.end(), std::back_inserter(prev_sort_values), - [](std::shared_ptr inst) { return inst->sortNumber(); }); - - - - // 计算下游调整 - QList> next_layer_nodes; - std::copy_if(total_nodes.begin(), total_nodes.end(), std::back_inserter(next_layer_nodes), - [&](std::shared_ptr inst) { - if (inst->layerNumber() == curr_node->layerNumber() + 1) { - if (curr_node->isFakeNode()) { - if (inst->isFakeNode()) { - return curr_node->tailsNode() == inst->tailsNode() && curr_node->headsNode() == inst->headsNode(); - } - return curr_node->headsNode() == inst->layerNode(); - } - else { - if (inst->isFakeNode()) { - return curr_node->layerNode() == inst->tailsNode(); - } - else { - auto next_vnodes = curr_node->layerNode()->nextNodes(); - return next_vnodes.contains(inst->layerNode()); - } - } - } - return false; - }); - QList next_sort_values; - std::transform(next_layer_nodes.begin(), next_layer_nodes.end(), std::back_inserter(next_sort_values), - [](std::shared_ptr ins) {return ins->sortNumber(); }); - - - if (prev_sort_values.size() + next_sort_values.size()) { - auto prev_pos_sum = std::accumulate(prev_sort_values.begin(), prev_sort_values.end(), 0.0); - auto next_pos_sum = std::accumulate(next_sort_values.begin(), next_sort_values.end(), 0.0); - auto target_pos = 0.0; - - if (prev_sort_values.size() && next_sort_values.size()) { - target_pos = (prev_pos_sum / prev_sort_values.size() + next_pos_sum / next_sort_values.size()) / 2; - } - else if (prev_sort_values.size()) { - target_pos = prev_pos_sum / prev_sort_values.size(); - } - else { - target_pos = next_pos_sum / next_sort_values.size(); - } - - curr_node->setSortNumber(target_pos); - } -} -*/ bool dags::DAGGraph::layer_adjust_via_next_sibling(int curr_layer, const QList>& total_nodes) { QList> curr_layer_nodes; @@ -581,7 +404,7 @@ bool dags::DAGGraph::layer_adjust_via_next_sibling(int curr_layer, const QList curr_node, const QList>& total_nodes) { - // 计算下游调整 + // 计算下游调整参考节点 QList> next_layer_nodes; std::copy_if(total_nodes.begin(), total_nodes.end(), std::back_inserter(next_layer_nodes), [&](std::shared_ptr inst) { @@ -645,7 +468,6 @@ void dags::DAGGraph::nodes_sort_with_belows(int curr_layer, const QList> sort_seqs; QList> refs; @@ -672,8 +494,8 @@ void dags::DAGGraph::graphLayout() { this->graph_layer_nodes_sort_forward(0, tidy_nodes); this->node_with_layout = tidy_nodes; - int times = 2; - while(times--){ + int times = 1; + while (times--) { this->graph_adjust_after_layout(tidy_nodes); this->graph_layer_nodes_sort_forward(0, tidy_nodes); } diff --git a/StoryPresent/dag_layout.h b/StoryPresent/dag_layout.h index f8769d1..95cee49 100644 --- a/StoryPresent/dag_layout.h +++ b/StoryPresent/dag_layout.h @@ -88,21 +88,14 @@ namespace dags { int node_layering_adj(std::shared_ptr inst); QList> tidy_graph_nodes(); void graph_layer_nodes_sort_forward(int layer_index, const QList> &nodes); - - - //void current_nodelist_filling_backward(int curr_layer, const QList>& total_nodes); - //void current_nodelist_separate(const QList>& total_nodes); - //void current_layer_separate(int layer_index, const QList>& nodes); - void graph_adjust_after_layout(const QList>& total_nodes); - //bool layer_adjust_after_layout(int curr_layer, const QList>& total_nodes); - //void node_adjust_after_layout(std::shared_ptr prev_node, std::shared_ptr curr_node, const QList>& total_nodes); bool layer_adjust_via_next_sibling(int curr_layer, const QList>& total_nodes); void node_adjust_via_next_sibling(std::shared_ptr curr_node, const QList>& total_nodes); - - void current_nodelist_filling(int curr_layer, const QList>& total_nodes); + + bool current_nodelist_filling_indi(int curr_layer, const QList>& total_nodes); void nodes_sort_with_above(int curr_layer, const QList>& total_nodes); void nodes_sort_with_belows(int curr_layer, const QList>& total_nodes); + }; }