Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs @ 15834

Last change on this file since 15834 was 15834, checked in by lkammere, 6 years ago

#2886: Store production rules in grammar instead of nonterminal symbols.

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
5
6  public abstract class Symbol : IEquatable<Symbol> {
7    private readonly int stringRepresentationHash;
8
9    public string StringRepresentation { get; }
10
11    protected Symbol(string representation) {
12      StringRepresentation = representation;
13      stringRepresentationHash = representation.GetHashCode();
14    }
15
16    public override string ToString() {
17      return StringRepresentation;
18    }
19
20    #region IEquatable
21    public static bool operator ==(Symbol s1, Symbol s2) {
22      if (ReferenceEquals(s1, s2)) return true;
23      if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
24      return s1.Equals(s2);
25    }
26
27    public static bool operator !=(Symbol s1, Symbol s2) {
28      return !(s1 == s2);
29    }
30
31    public bool Equals(Symbol other) {
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;
36    }
37
38    public override bool Equals(object obj) {
39      if (ReferenceEquals(obj, null)) return false;
40      if (ReferenceEquals(obj, this)) return true;
41      if (this.GetType() != obj.GetType()) return false;
42      return Equals((Symbol)obj);
43    }
44
45    public override int GetHashCode() {
46      return stringRepresentationHash;
47    }
48    #endregion
49  }
50
51  public class TerminalSymbol : Symbol {
52
53    public TerminalSymbol(string representation) : base(representation) { }
54  }
55
56  public class VariableTerminalSymbol : TerminalSymbol {
57    public VariableTerminalSymbol(string representation) : base(representation) { }
58  }
59
60
61  public class NonterminalSymbol : Symbol {
62    public NonterminalSymbol(string representation) : base(representation) { }
63  }
64
65  public class Production : List<Symbol> {
66
67    public Production(params Symbol[] symbols) : base(symbols) { }
68
69    public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
70
71    public override string ToString() {
72      return string.Join(" ", this);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.