Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefulExpression.cs @ 16003

Last change on this file since 16003 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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