Depsgraph: Simplify some loops using foreach()

This commit is contained in:
Sergey Sharybin 2016-05-25 13:51:35 +02:00
parent f83f7bb988
commit cec629ae42
7 changed files with 86 additions and 127 deletions

@ -55,6 +55,7 @@ extern "C" {
#include "depsnode_operation.h"
#include "depsnode_component.h"
#include "depsgraph_intern.h"
#include "depsgraph_util_foreach.h"
static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
@ -295,11 +296,7 @@ void Depsgraph::remove_subgraph_node(SubgraphDepsNode *subgraph_node)
void Depsgraph::clear_subgraph_nodes()
{
for (Subgraphs::iterator it = subgraphs.begin();
it != subgraphs.end();
++it)
{
SubgraphDepsNode *subgraph_node = *it;
foreach (SubgraphDepsNode *subgraph_node, subgraphs) {
OBJECT_GUARDED_DELETE(subgraph_node, SubgraphDepsNode);
}
subgraphs.clear();

@ -100,6 +100,7 @@ extern "C" {
#include "depsgraph_intern.h"
#include "depsgraph_util_cycle.h"
#include "depsgraph_util_foreach.h"
#include "depsgraph_util_transitive.h"
/* ****************** */
@ -184,18 +185,10 @@ static void deg_graph_build_finalize(Depsgraph *graph)
{
std::stack<OperationDepsNode *> stack;
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
foreach (OperationDepsNode *node, graph->operations) {
node->done = 0;
node->num_links_pending = 0;
for (OperationDepsNode::Relations::const_iterator it_rel = node->inlinks.begin();
it_rel != node->inlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (DepsRelation *rel, node->inlinks) {
if ((rel->from->type == DEPSNODE_TYPE_OPERATION) &&
(rel->flag & DEPSREL_FLAG_CYCLIC) == 0)
{
@ -212,11 +205,7 @@ static void deg_graph_build_finalize(Depsgraph *graph)
while (!stack.empty()) {
OperationDepsNode *node = stack.top();
if (node->done == 0 && node->outlinks.size() != 0) {
for (OperationDepsNode::Relations::const_iterator it_rel = node->outlinks.begin();
it_rel != node->outlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (DepsRelation *rel, node->outlinks) {
if (rel->to->type == DEPSNODE_TYPE_OPERATION) {
OperationDepsNode *to = (OperationDepsNode *)rel->to;
if ((rel->flag & DEPSREL_FLAG_CYCLIC) == 0) {
@ -233,11 +222,7 @@ static void deg_graph_build_finalize(Depsgraph *graph)
else {
stack.pop();
IDDepsNode *id_node = node->owner->owner;
for (OperationDepsNode::Relations::const_iterator it_rel = node->outlinks.begin();
it_rel != node->outlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (DepsRelation *rel, node->outlinks) {
if (rel->to->type == DEPSNODE_TYPE_OPERATION) {
OperationDepsNode *to = (OperationDepsNode *)rel->to;
IDDepsNode *id_to = to->owner->owner;

@ -55,6 +55,7 @@ extern "C" {
#include "depsnode_component.h"
#include "depsnode_operation.h"
#include "depsgraph_intern.h"
#include "depsgraph_util_foreach.h"
/* ****************** */
/* Graphviz Debugging */
@ -1018,33 +1019,18 @@ bool DEG_debug_scene_relations_validate(Main *bmain,
bool DEG_debug_consistency_check(Depsgraph *graph)
{
/* Validate links exists in both directions. */
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
for (OperationDepsNode::Relations::const_iterator it_rel = node->outlinks.begin();
it_rel != node->outlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (OperationDepsNode *node, graph->operations) {
foreach (DepsRelation *rel, node->outlinks) {
int counter1 = 0;
for (OperationDepsNode::Relations::const_iterator tmp_rel = node->outlinks.begin();
tmp_rel != node->outlinks.end();
++tmp_rel)
{
if (*tmp_rel == rel) {
foreach (DepsRelation *tmp_rel, node->outlinks) {
if (tmp_rel == rel) {
++counter1;
}
}
int counter2 = 0;
for (OperationDepsNode::Relations::const_iterator tmp_rel = rel->to->inlinks.begin();
tmp_rel != rel->to->inlinks.end();
++tmp_rel)
{
if (*tmp_rel == rel) {
foreach (DepsRelation *tmp_rel, rel->to->inlinks) {
if (tmp_rel == rel) {
++counter2;
}
}
@ -1057,33 +1043,18 @@ bool DEG_debug_consistency_check(Depsgraph *graph)
}
}
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
for (OperationDepsNode::Relations::const_iterator it_rel = node->inlinks.begin();
it_rel != node->inlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (OperationDepsNode *node, graph->operations) {
foreach (DepsRelation *rel, node->inlinks) {
int counter1 = 0;
for (OperationDepsNode::Relations::const_iterator tmp_rel = node->inlinks.begin();
tmp_rel != node->inlinks.end();
++tmp_rel)
{
if (*tmp_rel == rel) {
foreach (DepsRelation *tmp_rel, node->inlinks) {
if (tmp_rel == rel) {
++counter1;
}
}
int counter2 = 0;
for (OperationDepsNode::Relations::const_iterator tmp_rel = rel->from->outlinks.begin();
tmp_rel != rel->from->outlinks.end();
++tmp_rel)
{
if (*tmp_rel == rel) {
foreach (DepsRelation *tmp_rel, rel->from->outlinks) {
if (tmp_rel == rel) {
++counter2;
}
}
@ -1096,30 +1067,18 @@ bool DEG_debug_consistency_check(Depsgraph *graph)
}
/* Validate node valency calculated in both directions. */
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
foreach (OperationDepsNode *node, graph->operations) {
node->num_links_pending = 0;
node->done = 0;
}
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
foreach (OperationDepsNode *node, graph->operations) {
if (node->done) {
printf("Node %s is twice in the operations!\n",
node->identifier().c_str());
return false;
}
for (OperationDepsNode::Relations::const_iterator it_rel = node->outlinks.begin();
it_rel != node->outlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (DepsRelation *rel, node->outlinks) {
if (rel->to->type == DEPSNODE_TYPE_OPERATION) {
OperationDepsNode *to = (OperationDepsNode *)rel->to;
BLI_assert(to->num_links_pending < to->inlinks.size());
@ -1129,17 +1088,9 @@ bool DEG_debug_consistency_check(Depsgraph *graph)
node->done = 1;
}
for (Depsgraph::OperationNodes::const_iterator it_op = graph->operations.begin();
it_op != graph->operations.end();
++it_op)
{
OperationDepsNode *node = *it_op;
foreach (OperationDepsNode *node, graph->operations) {
int num_links_pending = 0;
for (OperationDepsNode::Relations::const_iterator it_rel = node->inlinks.begin();
it_rel != node->inlinks.end();
++it_rel)
{
DepsRelation *rel = *it_rel;
foreach (DepsRelation *rel, node->inlinks) {
if (rel->from->type == DEPSNODE_TYPE_OPERATION) {
++num_links_pending;
}

@ -51,6 +51,7 @@ extern "C" {
#include "depsnode_component.h"
#include "depsnode_operation.h"
#include "depsgraph_debug.h"
#include "depsgraph_util_foreach.h"
#ifdef WITH_LEGACY_DEPSGRAPH
static bool use_legacy_depsgraph = true;
@ -302,11 +303,7 @@ static void calculate_eval_priority(OperationDepsNode *node)
/* NOOP nodes have no cost */
node->eval_priority = node->is_noop() ? cost : 0.0f;
for (OperationDepsNode::Relations::const_iterator it = node->outlinks.begin();
it != node->outlinks.end();
++it)
{
DepsRelation *rel = *it;
foreach (DepsRelation *rel, node->outlinks) {
OperationDepsNode *to = (OperationDepsNode *)rel->to;
BLI_assert(to->type == DEPSNODE_TYPE_OPERATION);
calculate_eval_priority(to);
@ -362,11 +359,7 @@ static void schedule_graph(TaskPool *pool,
Depsgraph *graph,
const int layers)
{
for (Depsgraph::OperationNodes::const_iterator it = graph->operations.begin();
it != graph->operations.end();
++it)
{
OperationDepsNode *node = *it;
foreach (OperationDepsNode *node, graph->operations) {
schedule_node(pool, graph, layers, node, false, 0);
}
}
@ -429,21 +422,13 @@ void DEG_evaluate_on_refresh_ex(EvaluationContext *eval_ctx,
calculate_pending_parents(graph, layers);
/* Clear tags. */
for (Depsgraph::OperationNodes::const_iterator it = graph->operations.begin();
it != graph->operations.end();
++it)
{
OperationDepsNode *node = *it;
foreach (OperationDepsNode *node, graph->operations) {
node->done = 0;
}
/* Calculate priority for operation nodes. */
#ifdef USE_EVAL_PRIORITY
for (Depsgraph::OperationNodes::const_iterator it = graph->operations.begin();
it != graph->operations.end();
++it)
{
OperationDepsNode *node = *it;
foreach (OperationDepsNode *node, graph->operations) {
calculate_eval_priority(node);
}
#endif

@ -60,6 +60,7 @@ extern "C" {
#include "depsnode_component.h"
#include "depsnode_operation.h"
#include "depsgraph_intern.h"
#include "depsgraph_util_foreach.h"
/* *********************** */
/* Update Tagging/Flushing */
@ -304,11 +305,7 @@ void DEG_graph_flush_updates(Main *bmain, Depsgraph *graph)
* NOTE: Count how many nodes we need to handle - entry nodes may be
* component nodes which don't count for this purpose!
*/
for (Depsgraph::EntryTags::const_iterator it = graph->entry_tags.begin();
it != graph->entry_tags.end();
++it)
{
OperationDepsNode *node = *it;
foreach (OperationDepsNode *node, graph->entry_tags) {
IDDepsNode *id_node = node->owner->owner;
queue.push(node);
if (id_node->done == 0) {
@ -349,11 +346,7 @@ void DEG_graph_flush_updates(Main *bmain, Depsgraph *graph)
}
/* Flush to nodes along links... */
for (OperationDepsNode::Relations::const_iterator it = node->outlinks.begin();
it != node->outlinks.end();
++it)
{
DepsRelation *rel = *it;
foreach (DepsRelation *rel, node->outlinks) {
OperationDepsNode *to_node = (OperationDepsNode *)rel->to;
if (to_node->scheduled == false) {
to_node->flag |= DEPSOP_FLAG_NEEDS_UPDATE;

@ -46,6 +46,7 @@ extern "C" {
#include "depsnode_component.h"
#include "depsnode_operation.h"
#include "depsgraph_intern.h"
#include "depsgraph_util_foreach.h"
/* *************** */
/* Node Management */
@ -100,11 +101,7 @@ string DepsNode::identifier() const
void TimeSourceDepsNode::tag_update(Depsgraph *graph)
{
for (DepsNode::Relations::const_iterator it = outlinks.begin();
it != outlinks.end();
++it)
{
DepsRelation *rel = *it;
foreach (DepsRelation *rel, outlinks) {
DepsNode *node = rel->to;
node->tag_update(graph);
}

@ -0,0 +1,51 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2016 Blender Foundation.
* All rights reserved.
*
* Original Author: Sergey Sharybin
* Contributor(s):
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/depsgraph/util/depsgraph_util_foreach.h
* \ingroup depsgraph
*/
#ifndef __DEPSGRAPH_UTIL_FOREACH_H__
#define __DEPSGRAPH_UTIL_FOREACH_H__
#if (__cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
# define foreach(x, y) for(x : y)
#elif defined(HAVE_BOOST_FUNCTION_BINDINGS)
# include <boost/foreach.hpp>
# define foreach BOOST_FOREACH
#else
#pragma message("No available foreach() implementation. Using stub instead, disabling new depsgraph")
#ifndef WITH_LEGACY_DEPSGRAPH
# error "Unable to build new depsgraph and legacy one is disabled."
#endif
#define DISABLE_NEW_DEPSGRAPH
# define foreach(x, y) for (x; false; (void)y)
#endif
#endif /* __DEPSGRAPH_UTIL_FOREACH_H__ */