|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A helper type for avoiding allocations while building arrays. |
| 10 | +/// </summary> |
| 11 | +/// <typeparam name="T">The type of item contained in the array.</typeparam> |
| 12 | +internal struct ArrayBuilder<T> |
| 13 | + where T : struct |
| 14 | +{ |
| 15 | + private const int DefaultCapacity = 4; |
| 16 | + private const int MaxCoreClrArrayLength = 0x7FeFFFFF; |
| 17 | + |
| 18 | + // Starts out null, initialized on first Add. |
| 19 | + private T[]? data; |
| 20 | + private int size; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <see cref="ArrayBuilder{T}"/> struct. |
| 24 | + /// </summary> |
| 25 | + /// <param name="capacity">The initial capacity of the array.</param> |
| 26 | + public ArrayBuilder(int capacity) |
| 27 | + : this() |
| 28 | + { |
| 29 | + Guard.MustBeGreaterThanOrEqualTo(capacity, 0, nameof(capacity)); |
| 30 | + |
| 31 | + this.data = new T[capacity]; |
| 32 | + this.size = capacity; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets or sets the number of items in the array. |
| 37 | + /// </summary> |
| 38 | + public int Length |
| 39 | + { |
| 40 | + readonly get => this.size; |
| 41 | + |
| 42 | + set |
| 43 | + { |
| 44 | + if (value != this.size) |
| 45 | + { |
| 46 | + if (value > 0) |
| 47 | + { |
| 48 | + this.EnsureCapacity(value); |
| 49 | + this.size = value; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + this.size = 0; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Returns a reference to specified element of the array. |
| 61 | + /// </summary> |
| 62 | + /// <param name="index">The index of the element to return.</param> |
| 63 | + /// <returns>The <typeparamref name="T"/>.</returns> |
| 64 | + /// <exception cref="IndexOutOfRangeException"> |
| 65 | + /// Thrown when index less than 0 or index greater than or equal to <see cref="Length"/>. |
| 66 | + /// </exception> |
| 67 | + public readonly ref T this[int index] |
| 68 | + { |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | + get |
| 71 | + { |
| 72 | + DebugGuard.MustBeBetweenOrEqualTo(index, 0, this.size, nameof(index)); |
| 73 | + return ref this.data![index]; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Adds the given item to the array. |
| 79 | + /// </summary> |
| 80 | + /// <param name="item">The item to add.</param> |
| 81 | + public void Add(T item) |
| 82 | + { |
| 83 | + int position = this.size; |
| 84 | + |
| 85 | + // Expand the array. |
| 86 | + this.Length++; |
| 87 | + this.data![position] = item; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Remove the last item from the array. |
| 92 | + /// </summary> |
| 93 | + public void RemoveLast() |
| 94 | + { |
| 95 | + DebugGuard.MustBeGreaterThan(this.size, 0, nameof(this.size)); |
| 96 | + this.size--; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Clears the array. |
| 101 | + /// Allocated memory is left intact for future usage. |
| 102 | + /// </summary> |
| 103 | + public void Clear() => |
| 104 | + |
| 105 | + // No need to actually clear since we're not allowing reference types. |
| 106 | + this.size = 0; |
| 107 | + |
| 108 | + private void EnsureCapacity(int min) |
| 109 | + { |
| 110 | + int length = this.data?.Length ?? 0; |
| 111 | + if (length < min) |
| 112 | + { |
| 113 | + // Same expansion algorithm as List<T>. |
| 114 | + uint newCapacity = length == 0 ? DefaultCapacity : (uint)length * 2u; |
| 115 | + if (newCapacity > MaxCoreClrArrayLength) |
| 116 | + { |
| 117 | + newCapacity = MaxCoreClrArrayLength; |
| 118 | + } |
| 119 | + |
| 120 | + if (newCapacity < min) |
| 121 | + { |
| 122 | + newCapacity = (uint)min; |
| 123 | + } |
| 124 | + |
| 125 | + T[] array = new T[newCapacity]; |
| 126 | + |
| 127 | + if (this.size > 0) |
| 128 | + { |
| 129 | + Array.Copy(this.data!, array, this.size); |
| 130 | + } |
| 131 | + |
| 132 | + this.data = array; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments