Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15975 was 15975, checked in by bburlacu, 6 years ago

#2886: address additional serialization issues, make Production implement IList<T> instead of deriving from List<T>

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