Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs @ 16958

Last change on this file since 16958 was 15366, checked in by pkimmesw, 7 years ago

#2665 Solution Cleanup

File size: 2.0 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3
4  using HeuristicLab.Common;
5  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
7
8  [Serializable]
9  [StorableClass]
10  public abstract class StatefulExpression<T> : Expression, IPooledObject {
11    [Storable]
12    public T State { get; protected set; }
13    [NonSerialized]
14    private int? hashCode;
15    [NonSerialized]
16    private string stringRepresentation;
17
18    protected StatefulExpression(T state) {
19      State = state;
20    }
21
22    [StorableConstructor]
23    protected StatefulExpression(bool deserializing) : base(deserializing) { }
24
25    protected virtual int CalcHashCode() {
26      var hash = 19 * 31 + GetType().FullName.GetHashCode();
27      hash = hash * 31 + State.GetHashCode();
28
29      return hash;
30    }
31
32    protected virtual string GetStringRepresentation() {
33      return State.ToString();
34    }
35
36    public override string StringRepresentation
37    {
38      get
39      {
40        return stringRepresentation ?? (stringRepresentation = GetStringRepresentation());
41      }
42    }
43
44    public override bool Equals(object obj) {
45      if (ReferenceEquals(this, obj))
46        return true;
47
48      if (GetType() != obj.GetType())
49        return false;
50
51      var other = (StatefulExpression<T>)obj;
52
53      return ReferenceEquals(State, other.State) ||
54             GetHashCode() == other.GetHashCode();
55    }
56
57    public bool Equals(StatefulExpression<T> obj) {
58      if (ReferenceEquals(this, obj))
59        return true;
60
61      if (GetType() != obj.GetType())
62        return false;
63
64      return ReferenceEquals(State, obj.State) ||
65             GetHashCode() == obj.GetHashCode();
66    }
67
68    public override int GetHashCode() {
69      if (hashCode == null) hashCode = CalcHashCode();
70      return hashCode.Value;
71    }
72
73    public void Reset() {
74      State = default(T);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.