| | | 1 | | using System.Collections.Generic; |
| | | 2 | | |
| | | 3 | | namespace DirectSight.Parser.Analysis.LineCoverage; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// An implementation of <see cref="ILineInfo{T}"/> that uses a dictionary to store the line information. This implement |
| | | 7 | | /// </summary> |
| | | 8 | | /// <typeparam name="T">The type of the elements in the line information.</typeparam> |
| | | 9 | | internal class DictionaryBasedLineInfo<T> : ILineInfo<T> |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The internal dictionary that holds the line information. The keys of this dictionary represent the line indices, |
| | | 13 | | /// </summary> |
| | | 14 | | private readonly Dictionary<long, T> data; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The total number of elements in the line information. This value is specified in the constructor and is used to |
| | | 18 | | /// </summary> |
| | | 19 | | private readonly long length; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The default value used when retrieving line information for indices that are not present in the dictionary. This |
| | | 23 | | /// </summary> |
| | | 24 | | private readonly T defaultValue; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="DictionaryBasedLineInfo<T>"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="length">The number of elements to allocate for the internal data array. Must be non-negative.</para |
| | | 30 | | /// <param name="defaultValue">The default value to initialize each element with.</param> |
| | 0 | 31 | | public DictionaryBasedLineInfo(long length, T defaultValue) |
| | 0 | 32 | | { |
| | 0 | 33 | | this.length = length; |
| | 0 | 34 | | this.defaultValue = defaultValue; |
| | | 35 | | |
| | 0 | 36 | | this.data = []; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | public int Length |
| | | 41 | | { |
| | | 42 | | get |
| | 0 | 43 | | { |
| | 0 | 44 | | return (int)this.length; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public long LongLength |
| | | 50 | | { |
| | | 51 | | get |
| | 0 | 52 | | { |
| | 0 | 53 | | return this.length; |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public T this[long index] |
| | | 59 | | { |
| | | 60 | | get |
| | 0 | 61 | | { |
| | 0 | 62 | | if (this.data.TryGetValue(index, out var value)) |
| | 0 | 63 | | { |
| | 0 | 64 | | return value; |
| | | 65 | | } |
| | | 66 | | else |
| | 0 | 67 | | { |
| | 0 | 68 | | return this.defaultValue; |
| | | 69 | | } |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | set |
| | 0 | 73 | | { |
| | 0 | 74 | | if (EqualityComparer<T>.Default.Equals(this.defaultValue, value)) |
| | 0 | 75 | | { |
| | 0 | 76 | | this.data.Remove(index); |
| | 0 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | this.data[index] = value; |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public IEnumerator<T> GetEnumerator() |
| | 0 | 86 | | { |
| | 0 | 87 | | for (long i = 0; i < this.length; i++) |
| | 0 | 88 | | { |
| | 0 | 89 | | if (this.data.TryGetValue(i, out var value)) |
| | 0 | 90 | | { |
| | 0 | 91 | | yield return value; |
| | 0 | 92 | | } |
| | | 93 | | else |
| | 0 | 94 | | { |
| | 0 | 95 | | yield return this.defaultValue; |
| | 0 | 96 | | } |
| | 0 | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| | 0 | 102 | | { |
| | 0 | 103 | | return this.GetEnumerator(); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | | 107 | | public ILineInfo<T> Resize(long length) |
| | 0 | 108 | | { |
| | 0 | 109 | | var newLineInfo = new DictionaryBasedLineInfo<T>(length, this.defaultValue); |
| | | 110 | | |
| | 0 | 111 | | foreach (var kv in this.data) |
| | 0 | 112 | | { |
| | 0 | 113 | | newLineInfo.data[kv.Key] = kv.Value; |
| | 0 | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return newLineInfo; |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public ILineInfo<T> Clone() |
| | 0 | 121 | | { |
| | 0 | 122 | | return this.Resize(this.LongLength); |
| | 0 | 123 | | } |
| | | 124 | | } |