Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2886: Refactor properties to comply with .NET 4.5.2

File size: 5.6 KB
RevLine 
[15828]1using System;
[15975]2using System.Collections;
[15828]3using System.Collections.Generic;
[15975]4using System.Linq;
[15960]5using HeuristicLab.Common;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[15712]7
8namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
[15960]9  [StorableClass]
10  public abstract class Symbol : DeepCloneable, IEquatable<Symbol> {
11    [Storable]
[15828]12    private readonly int stringRepresentationHash;
13
[15974]14    [Storable]
15    public string StringRepresentation { get; private set; }
[15712]16
17    protected Symbol(string representation) {
[15714]18      StringRepresentation = representation;
[15828]19      stringRepresentationHash = representation.GetHashCode();
[15712]20    }
21
[15960]22    protected Symbol(Symbol original, Cloner cloner) : base(original, cloner) {
23      StringRepresentation = original.StringRepresentation;
24      stringRepresentationHash = original.stringRepresentationHash;
25    }
26
[15963]27    [StorableConstructor]
28    protected Symbol(bool deserializing) { }
29
[15712]30    public override string ToString() {
[15714]31      return StringRepresentation;
[15712]32    }
[15828]33
34    #region IEquatable
35    public static bool operator ==(Symbol s1, Symbol s2) {
[15832]36      if (ReferenceEquals(s1, s2)) return true;
37      if (ReferenceEquals(s1, null) || ReferenceEquals(s2, null)) return false;
38      return s1.Equals(s2);
[15828]39    }
40
41    public static bool operator !=(Symbol s1, Symbol s2) {
42      return !(s1 == s2);
43    }
44
45    public bool Equals(Symbol other) {
[15832]46      if (ReferenceEquals(other, null)) return false;
47      if (ReferenceEquals(other, this)) return true;
48      if (this.GetType() != other.GetType()) return false; // Otherwise, this needs to be reimplemented in derived classes.
49      return StringRepresentation == other.StringRepresentation;
[15828]50    }
51
52    public override bool Equals(object obj) {
[15832]53      if (ReferenceEquals(obj, null)) return false;
54      if (ReferenceEquals(obj, this)) return true;
55      if (this.GetType() != obj.GetType()) return false;
[15828]56      return Equals((Symbol)obj);
57    }
58
59    public override int GetHashCode() {
60      return stringRepresentationHash;
61    }
62    #endregion
[15712]63  }
64
[15960]65  [StorableClass]
[15712]66  public class TerminalSymbol : Symbol {
[15960]67    public TerminalSymbol(string representation) : base(representation) { }
68    public TerminalSymbol(TerminalSymbol original, Cloner cloner) : base(original, cloner) { }
[15812]69
[15960]70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new TerminalSymbol(this, cloner);
72    }
[15963]73
74    [StorableConstructor]
75    protected TerminalSymbol(bool deserializing) : base(deserializing) { }
[15712]76  }
77
[15960]78  [StorableClass]
[15832]79  public class VariableTerminalSymbol : TerminalSymbol {
80    public VariableTerminalSymbol(string representation) : base(representation) { }
[15960]81    public VariableTerminalSymbol(VariableTerminalSymbol original, Cloner cloner) : base(original, cloner) { }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new VariableTerminalSymbol(this, cloner);
85    }
[15963]86
87    [StorableConstructor]
88    protected VariableTerminalSymbol(bool deserializing) : base(deserializing) { }
[15832]89  }
90
[15960]91  [StorableClass]
[15712]92  public class NonterminalSymbol : Symbol {
[15834]93    public NonterminalSymbol(string representation) : base(representation) { }
[15960]94    public NonterminalSymbol(NonterminalSymbol original, Cloner cloner) : base(original, cloner) { }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new NonterminalSymbol(this, cloner);
98    }
[15963]99
100    [StorableConstructor]
101    protected NonterminalSymbol(bool deserializing) : base(deserializing) { }
[15712]102  }
103
[15960]104  [StorableClass]
[15975]105  public class Production : DeepCloneable, IList<Symbol> {
106    [Storable]
107    private List<Symbol> symbols;
[15712]108
[15975]109    public int Count { get { return symbols.Count; } }
[15712]110
[15981]111    public bool IsReadOnly {
112      get { throw new NotImplementedException(); }
113    }
[15712]114
[15981]115    public Symbol this[int index] {
116      get { throw new NotImplementedException(); }
117      set { throw new NotImplementedException(); }
118    }
[15975]119
120    public Production(params Symbol[] symbols) {
121      this.symbols = symbols.ToList();
122    }
123
124    public Production(IEnumerable<Symbol> symbols) {
125      this.symbols = symbols.ToList();
126    }
127
[15963]128    [StorableConstructor]
129    protected Production(bool deserializing) { }
130
[15975]131    protected Production(Production original, Cloner cloner) {
132      symbols = original.symbols.Select(cloner.Clone).ToList();
[15960]133    }
134
[15975]135    public override IDeepCloneable Clone(Cloner cloner) {
[15960]136      return new Production(this, cloner);
137    }
138
[15712]139    public override string ToString() {
140      return string.Join(" ", this);
141    }
[15975]142
143    #region IList<Symbol> methods
144    public IEnumerator<Symbol> GetEnumerator() {
145      return symbols.GetEnumerator();
146    }
147
148    IEnumerator IEnumerable.GetEnumerator() {
149      return GetEnumerator();
150    }
151
152    public int IndexOf(Symbol item) {
153      return symbols.IndexOf(item);
154    }
155
156    public void Insert(int index, Symbol item) {
157      symbols.Insert(index, item);
158    }
159
160    public void RemoveAt(int index) {
161      symbols.RemoveAt(index);
162    }
163
164    public void Add(Symbol item) {
165      symbols.Add(item);
166    }
167
168    public void Clear() {
169      symbols.Clear();
170    }
171
172    public bool Contains(Symbol item) {
173      return symbols.Contains(item);
174    }
175
176    public void CopyTo(Symbol[] array, int arrayIndex) {
177      symbols.CopyTo(array, arrayIndex);
178    }
179
180    public void CopyTo(int index, Symbol[] array, int arrayIndex, int count) {
181      symbols.CopyTo(index, array, arrayIndex, count);
182    }
183
184    public bool Remove(Symbol item) {
185      return symbols.Remove(item);
186    }
187    #endregion
[15712]188  }
189}
Note: See TracBrowser for help on using the repository browser.