Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/26/18 22:10:18 (6 years ago)
Author:
bburlacu
Message:

#2950: Refactor HashExtensions: simplify Reduce method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs

    r16255 r16260  
    116116        }
    117117      }
    118       return simplified.UpdateNodeSizes().Sort();
     118      return simplified.UpdateNodeSizes().Reduce().Sort();
    119119    }
    120120
     
    170170      var idx = i - 1;
    171171      for (int j = 0; j < arity; ++j) {
    172         while (!nodes[idx].Enabled) { --idx; } // skip nodes possibly disabled by simplification
    173172        children[j] = idx;
    174173        idx -= 1 + nodes[idx].Size;
     
    192191    }
    193192
    194     /// <summary>
    195     /// Reduce nodes of the same type according to arithmetic rules
    196     /// </summary>
    197     /// <typeparam name="T"></typeparam>
    198     /// <param name="g">The given grammar (controls symbol arities)</param>
    199     /// <param name="nodes">The node array</param>
    200     /// <param name="i">Parent index</param>
    201     /// <param name="j">Child index</param>
    202     private static void Reduce<T>(this HashNode<T>[] nodes, int i, int j) where T : class {
    203       var p = nodes[i]; // parent node
    204       var c = nodes[j]; // child node
    205 
    206       if (c.IsChild)
    207         return;
    208 
    209       foreach (int k in nodes.IterateChildren(j)) {
    210         if (nodes[k].IsChild) continue;
    211         nodes.Reduce(j, k);
    212       }
    213 
    214       // handle commutative symbols (add, mul)
    215       if (p.IsCommutative && p.HashValue == c.HashValue) {
    216         c.Enabled = false;
    217         p.Arity += c.Arity - 1;
    218       }
    219     }
    220 
    221     public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
    222       int i = nodes.Length - 1;
    223       foreach (int c in nodes.IterateChildren(i)) {
    224         if (nodes[c].IsChild) continue;
    225         nodes.Reduce(i, c);
    226       }
     193    private static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
    227194      int count = 0;
    228       foreach (var node in nodes) {
    229         if (!node.Enabled) { ++count; }
     195      for (int i = 0; i < nodes.Length; ++i) {
     196        var node = nodes[i];
     197        if (node.IsChild || !node.IsCommutative) {
     198          continue;
     199        }
     200
     201        foreach (var j in nodes.IterateChildren(i)) {
     202          if (node.HashValue == nodes[j].HashValue) {
     203            nodes[j].Enabled = false;
     204            node.Arity += nodes[j].Arity - 1;
     205            ++count;
     206          }
     207        }
    230208      }
    231209      if (count == 0)
     
    233211
    234212      var reduced = new HashNode<T>[nodes.Length - count];
    235       i = 0;
     213      var idx = 0;
    236214      foreach (var node in nodes) {
    237         if (node.Enabled) { reduced[i++] = node; }
     215        if (node.Enabled) { reduced[idx++] = node; }
    238216      }
    239217      return reduced.UpdateNodeSizes();
Note: See TracChangeset for help on using the changeset viewer.