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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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      }
Note: See TracChangeset for help on using the changeset viewer.