#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab 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 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab 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 HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Core;
namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common {
public static class ExtensionMethods {
#region Graph walk
///
/// Walks an operator graph in that it jumps from one operator to all its operator parameters and yields each operator it touches.
/// Cycles are detected and not walked twice.
///
/// The operator where the walk starts (is also yielded).
/// An enumeration of all the operators that could be found.
public static IEnumerable Walk(this IOperator initial) {
var open = new Stack();
var visited = new HashSet();
open.Push(initial);
while (open.Any()) {
IOperator current = open.Pop();
if (visited.Contains(current)) continue;
visited.Add(current);
foreach (var parameter in current.Parameters.OfType()) {
if (typeof(IOperator).IsAssignableFrom(parameter.DataType)) {
if (parameter.Value != null)
open.Push((IOperator)parameter.Value);
}
}
yield return current;
}
}
#endregion
}
}