Changeset 15832 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
- Timestamp:
- 03/08/18 10:54:04 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r15828 r15832 20 20 #region IEquatable 21 21 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); 24 25 } 25 26 … … 29 30 30 31 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; 34 36 } 35 37 36 38 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; 40 42 return Equals((Symbol)obj); 41 43 } … … 48 50 49 51 public class TerminalSymbol : Symbol { 50 public readonly bool IsVariable;51 52 52 public TerminalSymbol(string representation, bool isVariable = false) : base(representation) { 53 IsVariable = isVariable; 54 } 53 public TerminalSymbol(string representation) : base(representation) { } 55 54 } 56 55 56 public class VariableTerminalSymbol : TerminalSymbol { 57 public VariableTerminalSymbol(string representation) : base(representation) { } 58 } 59 60 57 61 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 } 59 67 60 68 public NonterminalSymbol(string representation) : base(representation) { 61 Alternatives = new List<Production>();69 alternatives = new List<Production>(); 62 70 } 63 71 64 72 public void AddProduction(params Symbol[] production) { 65 Alternatives.Add(new Production(production));73 alternatives.Add(new Production(production)); 66 74 } 67 75 } 68 76 69 77 public class VariableSymbol : NonterminalSymbol { // Convenience class 70 public IEnumerable< TerminalSymbol> VariableTerminalSymbols { get; }78 public IEnumerable<VariableTerminalSymbol> VariableTerminalSymbols { get; } 71 79 72 80 public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) { 73 List< TerminalSymbol> createdSymbols = new List<TerminalSymbol>();81 List<VariableTerminalSymbol> createdSymbols = new List<VariableTerminalSymbol>(); 74 82 VariableTerminalSymbols = createdSymbols; 75 83 76 84 foreach (string variableName in variableNames) { 77 TerminalSymbol s = new TerminalSymbol(variableName, isVariable: true);85 VariableTerminalSymbol s = new VariableTerminalSymbol(variableName); 78 86 createdSymbols.Add(s); 79 87 AddProduction(s);
Note: See TracChangeset
for help on using the changeset viewer.