Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/10/17 15:13:29 (7 years ago)
Author:
epitzer
Message:

#2727 add generic MetaInfo to AMT e.g. for tracking Count in facades

Location:
branches/PersistentDataStructures/HeuristicLab.Data/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Adaptations/HistoryArray.cs

    r14650 r14657  
    1313    [Storable]
    1414    private ArrayMappedTrie<T> amt;
    15     [Storable]
    16     public int Count { get; private set; }
     15
     16    public int Count {
     17      get { return (int?) amt.MetaInfo ?? 0; }
     18      private set { amt.MetaInfo = value; }
     19    }
     20
    1721    public int Length { get { return Count; } }
    1822
    1923    [StorableConstructor]
    2024    protected HistoryArray(bool isDeserializing) { }
     25
     26    private HistoryArray(ArrayMappedTrie<T> amt) {
     27      this.amt = amt.Clone();
     28      Count = (int?) amt.MetaInfo ?? 0;
     29    }
    2130
    2231    protected HistoryArray(HistoryArray<T> orig) {
     
    2635
    2736    public HistoryArray(int size) {
    28       amt = new ArrayMappedTrie<T>();
     37      amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 };
    2938      Count = size;
    3039    }
    3140
     41    [Obsolete]
    3242    public HistoryArray(IEnumerable<T> elements) {
    33       amt = new ArrayMappedTrie<T>();
     43      amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 };
    3444      Count = 0;
    3545      foreach (var element in elements) {
     
    125135        int hash = (int)2166136261;
    126136        for (int i = 0; i < Count; i++) {
    127           hash = (hash * 16777619) ^ (this[i]?.GetHashCode() ?? 0);
     137          hash = (hash * 16777619) ^ (this[i] == null ? 0 : this[i].GetHashCode());
    128138        }
    129139        return hash;
     
    137147      Count = newSize;
    138148    }
     149
     150    public void CreateSnapshot() {
     151      amt.CreateSnapshot();
     152    }
     153
     154    public IEnumerable<HistoryArray<T>> GetHistory() {
     155      yield return this;
     156      var prev = amt.Rollback();
     157      while (prev != null) {
     158        yield return new HistoryArray<T>(prev);
     159        prev = prev.Rollback();
     160      }
     161    }
    139162  }
    140163}
  • branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Adaptations/HistoryList.cs

    r14650 r14657  
    99
    1010  [StorableClass]
    11   public class HistoryList<T> : IList<T> {
     11  public class HistoryList<T> : IList<T>, ICloneable {
    1212
    1313    [Storable]
    1414    private readonly ArrayMappedTrie<T> amt;
    1515
    16     public int Count { get; private set; }
     16    public int Count {
     17      get { return (int) amt.MetaInfo; }
     18      private set { amt.MetaInfo = value; }
     19    }
    1720
    1821    [StorableConstructor]
    1922    protected HistoryList(bool isDesierializing) { }
    2023
     24    private HistoryList(ArrayMappedTrie<T> amt) { this.amt = amt; }
     25
    2126    public HistoryList() {
    22       amt = new ArrayMappedTrie<T>(true);
     27      amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 };
    2328      Count = 0;
     29    }
     30
     31    public HistoryList(HistoryList<T> orig) {
     32      amt = orig.amt.Clone();
     33      Count = orig.Count;
     34    }
     35
     36
     37    [Obsolete]
     38    public HistoryList(IEnumerable<T> values) {
     39      amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 };
     40      Count = 0;
     41      foreach (var value in values) Add(value);
    2442    }
    2543
     
    114132        amt.CreateSnapshot();
    115133    }
     134
     135    public void CreateSnapshot() {
     136      amt.CreateSnapshot();
     137    }
     138
     139    public IEnumerable<HistoryList<T>> GetHistory() {
     140      yield return this;
     141      var prev = amt.Rollback();
     142      while (prev != null) {
     143        yield return new HistoryList<T>(prev);
     144        prev = prev.Rollback();
     145      }
     146    }
     147
     148    public object Clone() { return new HistoryList<T>(this); }
    116149  }
    117150}
  • branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Implementations/ArrayMappedTrie.cs

    r14650 r14657  
    55using System.Diagnostics;
    66using System.Linq;
     7using HeuristicLab.Persistence.Core.Tokens;
    78using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    89
    910namespace HeuristicLab.Data.PersistentDataStructures.Implementations {
     11
    1012
    1113  [StorableClass]
     
    8587    }
    8688
    87     [Storable]
    88     private Node root;
    89 
    90     [Storable]
    91     private List<Node> oldRoots;
    92 
    93     [Storable]
    94     private UInt32 stepsSinceSnapshot = UInt32.MaxValue;
    95 
    96     [Storable]
    97     private readonly bool assumeFilledWithDefaultValue;
     89    [Storable] private readonly bool assumeFilledWithDefaultValue;
     90    [Storable] private Node root;
     91    [Storable] private List<Tuple<Node, ValueType>> oldRoots;
     92    [Storable] private UInt32 stepsSinceSnapshot = UInt32.MaxValue;
     93    [Storable] public UInt32 SnapshotInterval { get; set; }
     94    [Storable] public bool TrackClonedFrom { get; set; }
     95    [Storable] public ArrayMappedTrie<T> ClonedFrom { get; private set; }
     96    [Storable] public ValueType MetaInfo { get; set; }
     97
     98    public UInt32 Count { get { return DoCount(root); } }
     99
     100    protected ArrayMappedTrie() { }
    98101
    99102    public ArrayMappedTrie(bool assumeFilledWithDefaultValue = true) {
     
    110113    }
    111114
    112     public UInt32 Count { get { return DoCount(root); } }
    113 
    114     public UInt32 SnapshotInterval { get; set; }
    115 
    116     public bool TrackClonedFrom { get; set; }
    117 
    118     public ArrayMappedTrie<T> ClonedFrom { get; private set; }
    119 
    120115    public void CreateSnapshot() {
    121       if (oldRoots == null) oldRoots = new List<Node>();
    122       oldRoots.Add(root);
     116      if (oldRoots == null) oldRoots = new List<Tuple<Node, ValueType>>();
     117      oldRoots.Add(Tuple.Create(root, MetaInfo));
    123118      stepsSinceSnapshot = 0;
     119    }
     120
     121    public ArrayMappedTrie<T> Rollback() {
     122      if (oldRoots != null && oldRoots.Count > 0) {
     123        return new ArrayMappedTrie<T>(assumeFilledWithDefaultValue) {
     124          root = oldRoots.Last().Item1,
     125          MetaInfo = oldRoots.Last().Item2,
     126          oldRoots = oldRoots.GetRange(0, oldRoots.Count - 1),
     127          SnapshotInterval = SnapshotInterval,
     128          TrackClonedFrom = TrackClonedFrom,
     129          ClonedFrom = ClonedFrom
     130        };
     131      } else {
     132        return ClonedFrom;
     133      }
    124134    }
    125135
     
    237247      if (oldRoots != null) {
    238248        foreach (var node in oldRoots) {
    239           AddNodeSizeRecursively(node, nodes);
     249          AddNodeSizeRecursively(node.Item1, nodes);
    240250        }
    241251      }
     
    257267
    258268    public ArrayMappedTrie<T> Clone() {
    259       return new ArrayMappedTrie<T> {
    260         TrackClonedFrom = this.TrackClonedFrom,
     269      return new ArrayMappedTrie<T>(assumeFilledWithDefaultValue) {
     270        root = root,
     271        MetaInfo = MetaInfo,
     272        oldRoots = null,
     273        SnapshotInterval = SnapshotInterval,
     274        TrackClonedFrom = TrackClonedFrom,
    261275        ClonedFrom = TrackClonedFrom ? this : null,
    262         oldRoots = new List<Node> {root},
    263         root = root
    264276      };
    265277    }
     
    277289
    278290    private static IEnumerable<Node> GetChildrenRecursively(Node n, int level) {
    279       if (level == 0) yield return n;
    280       foreach (Node child in n) {
    281         foreach (var grandchild in GetChildrenRecursively(child, level-1)) {
    282           yield return grandchild;
     291      if (level == 0) {
     292        yield return n;
     293      } else {
     294        foreach (Node child in n) {
     295          foreach (var grandchild in GetChildrenRecursively(child, level - 1)) {
     296            yield return grandchild;
     297          }
    283298        }
    284299      }
  • branches/PersistentDataStructures/HeuristicLab.Data/3.3/ValueTypeArray.cs

    r14650 r14657  
    4444    protected T[] oldArrayValuePersistence { set { historyArray = new HistoryArray<T>(value); } }
    4545
     46    [Storable(AllowOneWay = true, Name = "elementNames")]
     47    protected List<string> oldElementNamesValuePersistence { set { historyElementNames = new HistoryList<string>(value); } }
     48
    4649    [Storable]
    4750    protected HistoryArray<T> historyArray;
    4851
    4952    [Storable]
    50     protected List<string> elementNames;
     53    protected HistoryList<string> historyElementNames;
     54
    5155    public virtual IEnumerable<string> ElementNames {
    52       get { return this.elementNames; }
     56      get { return this.historyElementNames; }
    5357      set {
    5458        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
    5559        if (value == null || !value.Any())
    56           elementNames = new List<string>();
     60          historyElementNames = new HistoryList<string>();
    5761        else if (value.Count() > Length)
    5862          throw new ArgumentException("The number of element names must not exceed the array length.");
    59         else
    60           elementNames = new List<string>(value);
     63        else 
     64          historyElementNames = new HistoryList<string>(value);
    6165        OnElementNamesChanged();
    6266      }
     
    7175        if (value != Length) {
    7276          historyArray.Resize(value);
    73           while (elementNames.Count > value)
    74             elementNames.RemoveAt(elementNames.Count - 1);
     77          while (historyElementNames.Count > value)
     78            historyElementNames.RemoveAt(historyElementNames.Count - 1);
    7579          OnElementNamesChanged();
    7680          OnReset();
     
    112116    [StorableHook(HookType.AfterDeserialization)]
    113117    private void AfterDeserialization() {
    114       if (elementNames == null) { elementNames = new List<string>(); }
     118      if (historyElementNames == null) { historyElementNames = new HistoryList<string>(); }
    115119    }
    116120
     
    122126      this.readOnly = original.readOnly;
    123127      this.resizable = original.resizable;
    124       this.elementNames = new List<string>(original.elementNames);
     128      this.historyElementNames = (HistoryList<string>)original.historyElementNames.Clone();
    125129    }
    126130    protected ValueTypeArray() {
     
    128132      readOnly = false;
    129133      resizable = true;
    130       elementNames = new List<string>();
     134      historyElementNames = new HistoryList<string>();
    131135    }
    132136    protected ValueTypeArray(int length) {
     
    134138      readOnly = false;
    135139      resizable = true;
    136       elementNames = new List<string>();
     140      historyElementNames = new HistoryList<string>();
    137141    }
    138142    protected ValueTypeArray(T[] elements) {
     
    141145      readOnly = false;
    142146      resizable = true;
    143       elementNames = new List<string>();
     147      historyElementNames = new HistoryList<string>();
     148    }
     149    private ValueTypeArray(HistoryArray<T> values, HistoryList<string> historyElementNames) {
     150      this.historyArray = values;
     151      this.historyElementNames = historyElementNames;
    144152    }
    145153
     
    205213      OnToStringChanged();
    206214    }
     215
     216    public void CreateSnapshot() {
     217      historyArray.CreateSnapshot();
     218      historyElementNames.CreateSnapshot();
     219    }
     220
     221    public IEnumerable<ValueTypeArray<T>> GetHistory() {
     222      foreach (var vta in historyArray.GetHistory().Zip(historyElementNames.GetHistory(), (v, n) => new {v, n})) {
     223        var clone = (ValueTypeArray<T>)Clone();
     224        clone.historyArray = vta.v;
     225        clone.historyElementNames = vta.n;
     226        yield return clone;
     227      }
     228    }
    207229  }
    208230}
Note: See TracChangeset for help on using the changeset viewer.