| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace DirectSight.Parser.Analysis.LineCoverage; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// An implementation of <see cref="ILineInfo{T}"/> that uses an array to store the line information. This implementatio |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="T">The type of the elements in the line information.</typeparam> |
| | | 10 | | internal class ArrayBasedLineInfo<T> : ILineInfo<T> |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The internal array that holds the line information. The length of this array is determined by the length specifi |
| | | 14 | | /// </summary> |
| | | 15 | | private readonly T[] data; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The default value used when initializing the array elements. |
| | | 19 | | /// </summary> |
| | | 20 | | private readonly T defaultValue; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="ArrayBasedLineInfo<T>"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="length">The number of elements to allocate for the internal data array. Must be non-negative.</para |
| | | 26 | | /// <param name="defaultValue">The default value to initialize each element with.</param> |
| | 7968 | 27 | | public ArrayBasedLineInfo(long length, T defaultValue) |
| | 7968 | 28 | | { |
| | 7968 | 29 | | this.defaultValue = defaultValue; |
| | | 30 | | |
| | 7968 | 31 | | this.data = new T[length]; |
| | | 32 | | |
| | 7968 | 33 | | if (EqualityComparer<T>.Default.Equals(defaultValue, default(T))) |
| | 4015 | 34 | | { |
| | 4015 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 124770 | 38 | | for (long i = 0; i < length; i++) |
| | 58432 | 39 | | { |
| | 58432 | 40 | | this.data[i] = defaultValue; |
| | 58432 | 41 | | } |
| | 7968 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public int Length |
| | | 46 | | { |
| | | 47 | | get |
| | 18648 | 48 | | { |
| | 18648 | 49 | | return this.data.Length; |
| | 18648 | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public long LongLength |
| | | 55 | | { |
| | | 56 | | get |
| | 29124 | 57 | | { |
| | 29124 | 58 | | return this.data.LongLength; |
| | 29124 | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public T this[long index] |
| | | 64 | | { |
| | 189591 | 65 | | get => this.data[index]; |
| | 76495 | 66 | | set => this.data[index] = value; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public IEnumerator<T> GetEnumerator() |
| | 4870 | 71 | | { |
| | 292780 | 72 | | foreach (var item in this.data) |
| | 139580 | 73 | | { |
| | 139580 | 74 | | yield return item; |
| | 138590 | 75 | | } |
| | 3880 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| | 0 | 80 | | { |
| | 0 | 81 | | return this.GetEnumerator(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public ILineInfo<T> Resize(long length) |
| | 910 | 86 | | { |
| | 910 | 87 | | var newLineInfo = new ArrayBasedLineInfo<T>(length, this.defaultValue); |
| | | 88 | | |
| | 910 | 89 | | Array.Copy(this.data, newLineInfo.data, Math.Min(this.data.LongLength, newLineInfo.data.LongLength)); |
| | | 90 | | |
| | 910 | 91 | | return newLineInfo; |
| | 910 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <inheritdoc /> |
| | | 95 | | public ILineInfo<T> Clone() |
| | 900 | 96 | | { |
| | 900 | 97 | | return this.Resize(this.LongLength); |
| | 900 | 98 | | } |
| | | 99 | | } |