Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/08/18 10:54:04 (7 years ago)
Author:
lkammere
Message:

#2886: Fix Equals methods in Symbols.
Move semantical hashing of phrases to separate class.

File:
1 edited

Legend:

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

    r15828 r15832  
    2020    #region IEquatable
    2121    public static bool operator ==(Symbol s1, Symbol s2) {
    22       return !ReferenceEquals(null, s1) && !ReferenceEquals(null, s2) && (ReferenceEquals(s1, s2)
    23         || s1.Equals(s2));
     22      if (ReferenceEquals(s1, s2)) return true;
     23      if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
     24      return s1.Equals(s2);
    2425    }
    2526
     
    2930
    3031    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);
     32      if (ReferenceEquals(other, null)) return false;
     33      if (ReferenceEquals(other, this)) return true;
     34      if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
     35      return StringRepresentation == other.StringRepresentation;
    3436    }
    3537
    3638    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;
     39      if (ReferenceEquals(obj, null)) return false;
     40      if (ReferenceEquals(obj, this)) return true;
     41      if (this.GetType() != obj.GetType()) return false;
    4042      return Equals((Symbol)obj);
    4143    }
     
    4850
    4951  public class TerminalSymbol : Symbol {
    50     public readonly bool IsVariable;
    5152
    52     public TerminalSymbol(string representation, bool isVariable = false) : base(representation) {
    53       IsVariable = isVariable;
    54     }
     53    public TerminalSymbol(string representation) : base(representation) { }
    5554  }
    5655
     56  public class VariableTerminalSymbol : TerminalSymbol {
     57    public VariableTerminalSymbol(string representation) : base(representation) { }
     58  }
     59
     60
    5761  public class NonterminalSymbol : Symbol {
    58     public List<Production> Alternatives { get; }
     62    private readonly List<Production> alternatives;
     63
     64    public IReadOnlyList<Production> Alternatives {
     65      get { return alternatives.AsReadOnly(); }
     66    }
    5967
    6068    public NonterminalSymbol(string representation) : base(representation) {
    61       Alternatives = new List<Production>();
     69      alternatives = new List<Production>();
    6270    }
    6371
    6472    public void AddProduction(params Symbol[] production) {
    65       Alternatives.Add(new Production(production));
     73      alternatives.Add(new Production(production));
    6674    }
    6775  }
    6876
    6977  public class VariableSymbol : NonterminalSymbol { // Convenience class
    70     public IEnumerable<TerminalSymbol> VariableTerminalSymbols { get; }
     78    public IEnumerable<VariableTerminalSymbol> VariableTerminalSymbols { get; }
    7179
    7280    public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) {
    73       List<TerminalSymbol> createdSymbols = new List<TerminalSymbol>();
     81      List<VariableTerminalSymbol> createdSymbols = new List<VariableTerminalSymbol>();
    7482      VariableTerminalSymbols = createdSymbols;
    7583
    7684      foreach (string variableName in variableNames) {
    77         TerminalSymbol s = new TerminalSymbol(variableName, isVariable: true);
     85        VariableTerminalSymbol s = new VariableTerminalSymbol(variableName);
    7886        createdSymbols.Add(s);
    7987        AddProduction(s);
Note: See TracChangeset for help on using the changeset viewer.