Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15828


Ignore:
Timestamp:
03/07/18 12:52:16 (7 years ago)
Author:
lkammere
Message:

#2886: Implement IEquatable interface in symbols.
Minor performance improvements.

Location:
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15827 r15828  
    166166
    167167      // ADDITION
    168       if (ReferenceEquals(currentSymbol, Addition)) {
     168      if (currentSymbol == Addition) {
    169169        var uniqueChildHashes = new HashSet<THashType>();
    170170
    171171        // First subtree
    172         if (ReferenceEquals(parseStack.Peek(), Addition)) {
     172        if (parseStack.Peek() == Addition) {
    173173          uniqueChildHashes.UnionWith(GetSubtreeHashes(parseStack, aggregateHashes));
    174174        } else {
     
    177177        }
    178178        // Second subtree
    179         if (ReferenceEquals(parseStack.Peek(), Addition)) {
     179        if (parseStack.Peek() == Addition) {
    180180          uniqueChildHashes.UnionWith(GetSubtreeHashes(parseStack, aggregateHashes));
    181181        } else {
     
    190190
    191191      // MULTIPLICATION
    192       if (ReferenceEquals(currentSymbol, Multiplication)) {
     192      if (currentSymbol == Multiplication) {
    193193        var childHashes = new List<THashType>();
    194194
    195195        // First subtree
    196         if (ReferenceEquals(parseStack.Peek(), Multiplication)) {
     196        if (parseStack.Peek() == Multiplication) {
    197197          childHashes.AddRange(GetSubtreeHashes(parseStack, aggregateHashes));
    198198        } else {
     
    200200        }
    201201        // Second subtree
    202         if (ReferenceEquals(parseStack.Peek(), Multiplication)) {
     202        if (parseStack.Peek() == Multiplication) {
    203203          childHashes.AddRange(GetSubtreeHashes(parseStack, aggregateHashes));
    204204        } else {
     
    231231
    232232      // LOG, EXP, SIN, INV
    233       if (ReferenceEquals(currentSymbol, Log) || ReferenceEquals(currentSymbol, Exp) ||
    234           ReferenceEquals(currentSymbol, Sin) || ReferenceEquals(currentSymbol, Cos) ||
    235           ReferenceEquals(currentSymbol, Inv)) {
     233      if (currentSymbol == Log || currentSymbol == Exp ||
     234          currentSymbol == Sin || currentSymbol == Cos ||
     235          currentSymbol == Inv) {
    236236        return new[] { aggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack, aggregateHashes)) };
    237237      }
     
    244244      var hashesArray = hashes.ToArray();
    245245
    246       if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && hashesArray.Count() <= 1) {
     246      if ((operatorSym == Addition || operatorSym == Multiplication) && hashesArray.Length <= 1) {
    247247        return hashesArray[0];
    248248      }
     
    251251      }
    252252
    253       return $"[{hashesArray.Aggregate(operatorSym.StringRepresentation, (result, ti) => string.Concat(result, " ° ", ti))}]";      // TODO: use string join instead of string.Concat
     253      return $"[{operatorSym.StringRepresentation} ° {string.Join(" ° ", hashesArray)}]";
    254254    }
    255255
    256256    private int AggregateIntHashes(Symbol operatorSym, int[] hashes) {
    257257      int start;
    258       if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) &&
    259           hashes.Length <= 1) {
     258      if ((operatorSym == Addition || operatorSym == Multiplication) && hashes.Length <= 1) {
    260259        start = 0;
    261260
     
    302301      ISymbolicExpressionTreeNode parsedSubTree = null;
    303302
    304       if (ReferenceEquals(currentSymbol, Addition)) {
     303      if (currentSymbol == Addition) {
    305304        parsedSubTree = addSy.CreateTreeNode();
    306305        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part
    307306        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part
    308307
    309       } else if (ReferenceEquals(currentSymbol, Multiplication)) {
     308      } else if (currentSymbol == Multiplication) {
    310309        parsedSubTree = mulSy.CreateTreeNode();
    311310        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part
    312311        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part
    313312
    314       } else if (ReferenceEquals(currentSymbol, Log)) {
     313      } else if (currentSymbol == Log) {
    315314        parsedSubTree = logSy.CreateTreeNode();
    316315        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack));
    317316
    318       } else if (ReferenceEquals(currentSymbol, Exp)) {
     317      } else if (currentSymbol == Exp) {
    319318        parsedSubTree = expSy.CreateTreeNode();
    320319        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack));
    321320
    322       } else if (ReferenceEquals(currentSymbol, Sin)) {
     321      } else if (currentSymbol == Sin) {
    323322        parsedSubTree = sinSy.CreateTreeNode();
    324323        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack));
    325324
    326       } else if (ReferenceEquals(currentSymbol, Cos)) {
     325      } else if (currentSymbol ==  Cos) {
    327326        parsedSubTree = cosSy.CreateTreeNode();
    328327        parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack));
    329328
    330       } else if (ReferenceEquals(currentSymbol, Inv)) {
     329      } else if (currentSymbol == Inv) {
    331330        parsedSubTree = divSy.CreateTreeNode();
    332331        ConstantTreeNode dividend = (ConstantTreeNode)constSy.CreateTreeNode();
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs

    r15806 r15828  
    55namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
    66
    7   // TODO: struct not useful here because you are boxing the structs with a new call below
    8   public struct StoredSymbolString {
     7  public class StoredSymbolString {
    98    public readonly int Hash;
    109    public readonly SymbolString SymbolString;
     
    8079      SymbolString result = storedValues[hash];
    8180      storedValues.Remove(hash);
    82       return new StoredSymbolString(hash, result);  // TODO: new for structs creates a box anyway
     81      return new StoredSymbolString(hash, result);
    8382    }
    8483
    8584    public void Store(int hash, SymbolString s) {
    86       storeInternal.Invoke(hash); // TODO: explicit .Invoke() not necessary, just storeInternal(hash) is ok
     85      storeInternal(hash);
    8786      storedValues[hash] = s;
    8887    }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r15812 r15828  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23
    34namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    45
    5   public abstract class Symbol {
     6  public abstract class Symbol : IEquatable<Symbol> {
     7    private readonly int stringRepresentationHash;
     8
    69    public string StringRepresentation { get; }
    710
    811    protected Symbol(string representation) {
    912      StringRepresentation = representation;
     13      stringRepresentationHash = representation.GetHashCode();
    1014    }
    1115
     
    1317      return StringRepresentation;
    1418    }
     19
     20    #region IEquatable
     21    public static bool operator ==(Symbol s1, Symbol s2) {
     22      return !ReferenceEquals(null, s1) && !ReferenceEquals(null, s2) && (ReferenceEquals(s1, s2)
     23        || s1.Equals(s2));
     24    }
     25
     26    public static bool operator !=(Symbol s1, Symbol s2) {
     27      return !(s1 == s2);
     28    }
     29
     30    public bool Equals(Symbol other) {
     31      if (ReferenceEquals(null, other)) return false;
     32      if (ReferenceEquals(this, other)) return true;
     33      return string.Equals(StringRepresentation, other.StringRepresentation);
     34    }
     35
     36    public override bool Equals(object obj) {
     37      if (ReferenceEquals(null, obj)) return false;
     38      if (ReferenceEquals(this, obj)) return true;
     39      if (obj.GetType() != this.GetType()) return false;
     40      return Equals((Symbol)obj);
     41    }
     42
     43    public override int GetHashCode() {
     44      return stringRepresentationHash;
     45    }
     46    #endregion
    1547  }
    1648
Note: See TracChangeset for help on using the changeset viewer.