Changeset 14657
- Timestamp:
- 02/10/17 15:13:29 (8 years ago)
- 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 13 13 [Storable] 14 14 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 17 21 public int Length { get { return Count; } } 18 22 19 23 [StorableConstructor] 20 24 protected HistoryArray(bool isDeserializing) { } 25 26 private HistoryArray(ArrayMappedTrie<T> amt) { 27 this.amt = amt.Clone(); 28 Count = (int?) amt.MetaInfo ?? 0; 29 } 21 30 22 31 protected HistoryArray(HistoryArray<T> orig) { … … 26 35 27 36 public HistoryArray(int size) { 28 amt = new ArrayMappedTrie<T> ();37 amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 }; 29 38 Count = size; 30 39 } 31 40 41 [Obsolete] 32 42 public HistoryArray(IEnumerable<T> elements) { 33 amt = new ArrayMappedTrie<T> ();43 amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 }; 34 44 Count = 0; 35 45 foreach (var element in elements) { … … 125 135 int hash = (int)2166136261; 126 136 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()); 128 138 } 129 139 return hash; … … 137 147 Count = newSize; 138 148 } 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 } 139 162 } 140 163 } -
branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Adaptations/HistoryList.cs
r14650 r14657 9 9 10 10 [StorableClass] 11 public class HistoryList<T> : IList<T> {11 public class HistoryList<T> : IList<T>, ICloneable { 12 12 13 13 [Storable] 14 14 private readonly ArrayMappedTrie<T> amt; 15 15 16 public int Count { get; private set; } 16 public int Count { 17 get { return (int) amt.MetaInfo; } 18 private set { amt.MetaInfo = value; } 19 } 17 20 18 21 [StorableConstructor] 19 22 protected HistoryList(bool isDesierializing) { } 20 23 24 private HistoryList(ArrayMappedTrie<T> amt) { this.amt = amt; } 25 21 26 public HistoryList() { 22 amt = new ArrayMappedTrie<T> (true);27 amt = new ArrayMappedTrie<T> { SnapshotInterval = 0 }; 23 28 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); 24 42 } 25 43 … … 114 132 amt.CreateSnapshot(); 115 133 } 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); } 116 149 } 117 150 } -
branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Implementations/ArrayMappedTrie.cs
r14650 r14657 5 5 using System.Diagnostics; 6 6 using System.Linq; 7 using HeuristicLab.Persistence.Core.Tokens; 7 8 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 9 9 10 namespace HeuristicLab.Data.PersistentDataStructures.Implementations { 11 10 12 11 13 [StorableClass] … … 85 87 } 86 88 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() { } 98 101 99 102 public ArrayMappedTrie(bool assumeFilledWithDefaultValue = true) { … … 110 113 } 111 114 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 120 115 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)); 123 118 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 } 124 134 } 125 135 … … 237 247 if (oldRoots != null) { 238 248 foreach (var node in oldRoots) { 239 AddNodeSizeRecursively(node , nodes);249 AddNodeSizeRecursively(node.Item1, nodes); 240 250 } 241 251 } … … 257 267 258 268 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, 261 275 ClonedFrom = TrackClonedFrom ? this : null, 262 oldRoots = new List<Node> {root},263 root = root264 276 }; 265 277 } … … 277 289 278 290 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 } 283 298 } 284 299 } -
branches/PersistentDataStructures/HeuristicLab.Data/3.3/ValueTypeArray.cs
r14650 r14657 44 44 protected T[] oldArrayValuePersistence { set { historyArray = new HistoryArray<T>(value); } } 45 45 46 [Storable(AllowOneWay = true, Name = "elementNames")] 47 protected List<string> oldElementNamesValuePersistence { set { historyElementNames = new HistoryList<string>(value); } } 48 46 49 [Storable] 47 50 protected HistoryArray<T> historyArray; 48 51 49 52 [Storable] 50 protected List<string> elementNames; 53 protected HistoryList<string> historyElementNames; 54 51 55 public virtual IEnumerable<string> ElementNames { 52 get { return this. elementNames; }56 get { return this.historyElementNames; } 53 57 set { 54 58 if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only."); 55 59 if (value == null || !value.Any()) 56 elementNames = newList<string>();60 historyElementNames = new HistoryList<string>(); 57 61 else if (value.Count() > Length) 58 62 throw new ArgumentException("The number of element names must not exceed the array length."); 59 else 60 elementNames = newList<string>(value);63 else 64 historyElementNames = new HistoryList<string>(value); 61 65 OnElementNamesChanged(); 62 66 } … … 71 75 if (value != Length) { 72 76 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); 75 79 OnElementNamesChanged(); 76 80 OnReset(); … … 112 116 [StorableHook(HookType.AfterDeserialization)] 113 117 private void AfterDeserialization() { 114 if ( elementNames == null) { elementNames = newList<string>(); }118 if (historyElementNames == null) { historyElementNames = new HistoryList<string>(); } 115 119 } 116 120 … … 122 126 this.readOnly = original.readOnly; 123 127 this.resizable = original.resizable; 124 this. elementNames = new List<string>(original.elementNames);128 this.historyElementNames = (HistoryList<string>)original.historyElementNames.Clone(); 125 129 } 126 130 protected ValueTypeArray() { … … 128 132 readOnly = false; 129 133 resizable = true; 130 elementNames = newList<string>();134 historyElementNames = new HistoryList<string>(); 131 135 } 132 136 protected ValueTypeArray(int length) { … … 134 138 readOnly = false; 135 139 resizable = true; 136 elementNames = newList<string>();140 historyElementNames = new HistoryList<string>(); 137 141 } 138 142 protected ValueTypeArray(T[] elements) { … … 141 145 readOnly = false; 142 146 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; 144 152 } 145 153 … … 205 213 OnToStringChanged(); 206 214 } 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 } 207 229 } 208 230 }
Note: See TracChangeset
for help on using the changeset viewer.