Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14575


Ignore:
Timestamp:
01/15/17 14:12:11 (7 years ago)
Author:
bburlacu
Message:

#1772: Minor refactoring in the TraceCalculator (use Tuple.Create factory method to make the code more readable).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs

    r13877 r14575  
    3535  public class TraceCalculator : Item {
    3636    private Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>> nodeListCache;
     37    // the trace cache consits of tuples of the current and last vertices, the subtree index to be traced in the current vertex, and the last subtree and fragment indexes
    3738    private HashSet<Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, int, IGenealogyGraphNode<ISymbolicExpressionTree>, int, int>> traceCache;
    3839    public IGenealogyGraph<ISymbolicExpressionTree> TraceGraph { get; private set; }
     
    105106    private void TraceRecursive(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, IGenealogyGraphNode<ISymbolicExpressionTree> last = null, int lastSi = -1, int lastFi = -1) {
    106107      var g = node;
    107       int si = subtreeIndex; // subtree index
    108       int fi = 0; // fragment index
     108      var si = subtreeIndex; // subtree index
     109      var fi = 0; // fragment index
    109110      while (((List<IArc>)((IVertex)g).InArcs).Count > 0) {
    110111        if (!(si < g.Data.Length)) throw new ArgumentOutOfRangeException("The subtree index exceeds the size of the tree.");
     
    148149              var n = AddTraceNode(g); // current node becomes "last" as we restart tracing from the parent
    149150              if (CacheTraceNodes) {
    150                 var t0 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, int, IGenealogyGraphNode<ISymbolicExpressionTree>, int, int>(parent0, si, n, si, fi);
    151                 var t1 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, int, IGenealogyGraphNode<ISymbolicExpressionTree>, int, int>(parent1, fragment.Index2, n, si, fi);
    152                 if (!traceCache.Contains(t0)) {
    153                   traceCache.Add(t0);
     151                var t0 = Tuple.Create(parent0, si, n, si, fi);
     152                var t1 = Tuple.Create(parent1, fragment.Index2, n, si, fi);
     153                // trace subtree
     154                if (traceCache.Add(t0))
    154155                  TraceRecursive(parent0, si, n, si, fi);
    155                 } else {
     156                else
    156157                  TraceCacheHits++;
    157                 }
    158                 if (!traceCache.Contains(t1)) {
    159                   traceCache.Add(t1);
     158                // trace fragment
     159                if (traceCache.Add(t1))
    160160                  TraceRecursive(parent1, fragment.Index2, n, si, fi);
    161                 } else {
     161                else
    162162                  TraceCacheHits++;
    163                 }
    164163              } else {
    165164                TraceRecursive(parent0, si, n, si, fi);
     
    182181          Debug.Assert(fragment.Index1 == fragment.Index2);
    183182          // check if the subtree and the fragment overlap => branch out
     183          // optionally we could ignore the case when the fragment contains the subtree:
     184          // ((si == fi) || fi < si && si < fi + fl)
    184185          if ((si == fi) || (si < fi && fi < si + sl) || (fi < si && si < fi + fl)) {
    185186            var n = AddTraceNode(g); // current node becomes "last" as we restart tracing from the parent
    186187            int i = si < fi ? si : fi;
    187             var t = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, int, IGenealogyGraphNode<ISymbolicExpressionTree>, int, int>(parent0, i, n, si, fi);
    188             if (!(CacheTraceNodes && traceCache.Contains(t))) {
     188            var t = Tuple.Create(parent0, i, n, si, fi);
     189            if (CacheTraceNodes) {
     190              if (traceCache.Add(t))
     191                TraceRecursive(parent0, i, n, si, fi);
     192              else
     193                TraceCacheHits++;
     194            } else {
    189195              TraceRecursive(parent0, i, n, si, fi);
    190               traceCache.Add(t);
    191196            }
    192197            break;
     
    198203            continue;
    199204          }
     205
    200206        }
    201207        #endregion
Note: See TracChangeset for help on using the changeset viewer.