Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Data/3.3/PersistentDataStructures/Adaptations/HistoryList.cs @ 14650

Last change on this file since 14650 was 14650, checked in by epitzer, 8 years ago

#2727 completely replace basic array with array mapped trie in ValueTypeArray and descendants

File size: 3.1 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Data.PersistentDataStructures.Implementations;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Data.PersistentDataStructures.Adaptations {
9
10  [StorableClass]
11  public class HistoryList<T> : IList<T> {
12
13    [Storable]
14    private readonly ArrayMappedTrie<T> amt;
15
16    public int Count { get; private set; }
17
18    [StorableConstructor]
19    protected HistoryList(bool isDesierializing) { }
20
21    public HistoryList() {
22      amt = new ArrayMappedTrie<T>(true);
23      Count = 0;
24    }
25
26    public T this[int index] {
27      get {
28        if (index < 0 || index >= Count) throw new IndexOutOfRangeException();
29        return amt[(UInt32) index];
30      }
31      set {
32        if (index < 0 || index >= Count) throw new IndexOutOfRangeException();
33        amt[(UInt32) index] = value;
34      }
35    }
36
37    public IEnumerator<T> GetEnumerator() {
38      for (int i = 0; i < Count; i++)
39        yield return amt[(UInt32)i];
40    }
41
42    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
43
44    public void Add(T item) {
45      amt[(UInt32)Count] = item;
46      Count++;
47    }
48
49    public void Clear() { amt.Clear(); }
50
51    public bool Contains(T item) { return amt.Contains(item); }
52
53    public void CopyTo(T[] array, int arrayIndex) {
54      for (int i = 0; i < Count && i < array.Length + arrayIndex; i++) {
55        array[i + arrayIndex] = this[i];
56      }
57    }
58
59    public bool Remove(T item) {
60      var oldInternval = amt.SnapshotInterval;
61      amt.SnapshotInterval = 0;
62      bool found = false;
63      var wi = 0;
64      for (int i = 0; i < Count; i++) {
65        var value = amt[(UInt32) i];
66        if (!value.Equals(item)) {
67          if (wi < i) amt[(UInt32) wi] = value;
68          wi++;
69        }
70      }
71      found = wi < Count;
72      while (wi < Count) {
73        amt.Remove((UInt32) wi);
74        wi++;
75        Count--;
76      }
77      amt.SnapshotInterval = oldInternval;
78      if (oldInternval > 0)
79        amt.CreateSnapshot();
80      return found;
81    }
82
83    public bool IsReadOnly { get { return false; } }
84
85    public int IndexOf(T item) {
86      for (int i = 0; i < Count; i++) {
87        if (amt[(UInt32)i].Equals(item)) return i;
88      }
89      return -1;
90    }
91
92    public void Insert(int index, T item) {
93      var oldInterval = amt.SnapshotInterval;
94      amt.SnapshotInterval = 0;
95      for (int i = Count; i > index; i--) {
96        amt[(UInt32)i] = amt[(UInt32)i - 1];
97      }
98      amt[(UInt32) index] = item;
99      Count++;
100      amt.SnapshotInterval = oldInterval;
101      if (oldInterval > 0)
102        amt.CreateSnapshot();
103    }
104
105    public void RemoveAt(int index) {
106      var oldInterval = amt.SnapshotInterval;
107      amt.SnapshotInterval = 0;
108      for (int i = index; i < Count; i++) {
109        amt[(UInt32)i] = amt[(UInt32)i + 1];
110      }
111      amt.Remove((UInt32)Count-1);
112      amt.SnapshotInterval = oldInterval;
113      if (oldInterval > 0)
114        amt.CreateSnapshot();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.