Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9038


Ignore:
Timestamp:
12/12/12 13:09:21 (11 years ago)
Author:
mkommend
Message:

#1996: Added OperatorGraph.Iterate method which returns all reachable operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs

    r7259 r9038  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Collections;
     
    244245    }
    245246    #endregion
     247
     248    // <summary>
     249    /// Iterates an operator graph so that it jumps from the intial operator to all other operators and yields each operator it touches.
     250    /// Cycles are detected and not iterated twice.
     251    /// </summary>
     252    /// <returns>An enumeration of all the operators that could be found.</returns>
     253    public IEnumerable<IOperator> Iterate() {
     254      if (InitialOperator == null) yield break;
     255
     256      var open = new Stack<IOperator>();
     257      var visited = new HashSet<IOperator>();
     258      open.Push(InitialOperator);
     259
     260      while (open.Any()) {
     261        IOperator current = open.Pop();
     262        if (visited.Contains(current)) continue;
     263        visited.Add(current);
     264
     265        foreach (var parameter in current.Parameters.OfType<IValueParameter>()) {
     266          if (!typeof(IOperator).IsAssignableFrom(parameter.DataType)) continue;
     267          if (parameter.Value == null) continue;
     268
     269          open.Push((IOperator)parameter.Value);
     270        }
     271
     272        yield return current;
     273      }
     274    }
    246275  }
    247276}
Note: See TracChangeset for help on using the changeset viewer.