Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/24/16 14:20:17 (8 years ago)
Author:
bburlacu
Message:

#2685: Minor refactor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/GrammarUtils.cs

    r14356 r14357  
    4444    }
    4545
    46     private static IEnumerable<ISymbol> IterateBreadthReverse(this ISymbolicExpressionGrammarBase grammar, ISymbol topSymbol) {
     46    private static IReadOnlyCollection<ISymbol> IterateBreadthReverse(this ISymbolicExpressionGrammarBase grammar, ISymbol topSymbol) {
    4747      // sort symbols in reverse breadth order (starting from the topSymbol)
    4848      // each symbol is visited only once (this avoids infinite recursion)
    49       var numberedSymbols = new List<Tuple<ISymbol, int>> { Tuple.Create(topSymbol, 0) };
     49      var symbols = new List<ISymbol> { topSymbol };
    5050      var visited = new HashSet<ISymbol> { topSymbol };
    51       int i = 0, index = 0;
    52       while (i < numberedSymbols.Count) {
    53         var symbol = numberedSymbols[i].Item1;
     51      int i = 0;
     52      while (i < symbols.Count) {
     53        var symbol = symbols[i];
    5454        var minSubtreeCount = grammar.GetMinimumSubtreeCount(symbol);
    5555
     
    6060
    6161            if (visited.Add(childSymbol))
    62               numberedSymbols.Add(Tuple.Create(childSymbol, ++index));
     62              symbols.Add(childSymbol);
    6363          }
    6464        }
    6565        ++i;
    6666      }
    67       numberedSymbols.Reverse();
    68       return numberedSymbols.Select(x => x.Item1);
     67      symbols.Reverse();
     68      return symbols;
    6969    }
    7070
     
    7777      minimumExpressionLengths.Clear();
    7878      //terminal symbols => minimum expression length = 1
    79       foreach (var s in grammar.Symbols.Where(x => grammar.GetMinimumSubtreeCount(x) == 0))
    80         minimumExpressionLengths[s.Name] = 1;
     79      foreach (var s in grammar.Symbols) {
     80        if (grammar.GetMinimumSubtreeCount(s) == 0)
     81          minimumExpressionLengths[s.Name] = 1;
     82        else
     83          minimumExpressionLengths[s.Name] = int.MaxValue;
     84      }
    8185
    8286      foreach (var topSymbol in grammar.GetTopmostSymbols()) {
    8387        // get all symbols below in reverse breadth order
    8488        // this way we ensure lengths are calculated bottom-up
    85         var symbols = grammar.IterateBreadthReverse(topSymbol).ToList();
     89        var symbols = grammar.IterateBreadthReverse(topSymbol);
    8690        foreach (var symbol in symbols) {
    8791          long minLength = 1;
     
    112116        }
    113117      }
    114 
    115       //set minLength to int.MaxValue for all symbols that are not reacheable
    116       foreach (var remainingSymbols in grammar.Symbols) {
    117         if (!minimumExpressionLengths.ContainsKey(remainingSymbols.Name))
    118           minimumExpressionLengths[remainingSymbols.Name] = int.MaxValue;
    119       }
    120118    }
    121119
     
    125123      minimumExpressionDepths.Clear();
    126124      //terminal symbols => minimum expression depth = 1
    127       foreach (var s in grammar.Symbols.Where(s => grammar.GetMinimumSubtreeCount(s) == 0))
    128         minimumExpressionDepths[s.Name] = 1;
     125      foreach (var s in grammar.Symbols) {
     126        if (grammar.GetMinimumSubtreeCount(s) == 0)
     127          minimumExpressionDepths[s.Name] = 1;
     128        else
     129          minimumExpressionDepths[s.Name] = int.MaxValue;
     130      }
    129131
    130132      foreach (var topSymbol in grammar.GetTopmostSymbols()) {
    131133        // get all symbols below in reverse breadth order
    132134        // this way we ensure lengths are calculated bottom-up
    133         var symbols = grammar.IterateBreadthReverse(topSymbol).ToList();
     135        var symbols = grammar.IterateBreadthReverse(topSymbol);
    134136        foreach (var symbol in symbols) {
    135137          long minDepth = -1;
     
    160162        }
    161163      }
    162 
    163       //set minDepth to int.Maxvalue for all symbols that are not reacheable
    164       foreach (var remainingSymbols in grammar.Symbols) {
    165         if (!minimumExpressionDepths.ContainsKey(remainingSymbols.Name))
    166           minimumExpressionDepths[remainingSymbols.Name] = int.MaxValue;
    167       }
    168164    }
    169165  }
Note: See TracChangeset for help on using the changeset viewer.