| | | 1 | | using System; |
| | | 2 | | using System.Buffers; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 8 | | |
| | | 9 | | internal readonly struct ArrayBuilder<T> : IEquatable<ArrayBuilder<T>>, IDisposable |
| | | 10 | | { |
| | | 11 | | private readonly Writer writer; |
| | | 12 | | |
| | | 13 | | public ArrayBuilder() |
| | | 14 | | { |
| | 50 | 15 | | writer = new Writer(); |
| | 50 | 16 | | } |
| | | 17 | | |
| | | 18 | | public ArrayBuilder(int capacity) |
| | | 19 | | { |
| | 2077 | 20 | | writer = new Writer(capacity); |
| | 2077 | 21 | | } |
| | | 22 | | |
| | | 23 | | public ArrayBuilder(ReadOnlySpan<T> items) |
| | | 24 | | { |
| | 0 | 25 | | writer = new Writer(items.Length); |
| | 0 | 26 | | writer.AddRange(items); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void Add(T item) |
| | | 30 | | { |
| | 471 | 31 | | writer.Add(item); |
| | 471 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void AddRange(ReadOnlySpan<T> items) |
| | | 35 | | { |
| | 4372 | 36 | | writer.AddRange(items); |
| | 4372 | 37 | | } |
| | | 38 | | |
| | | 39 | | public void Clear() |
| | | 40 | | { |
| | 48 | 41 | | writer.Clear(); |
| | 48 | 42 | | } |
| | | 43 | | |
| | | 44 | | public void Dispose() |
| | | 45 | | { |
| | 2102 | 46 | | writer.Dispose(); |
| | 2102 | 47 | | } |
| | | 48 | | |
| | | 49 | | public ImmutableArray<T> ToImmutable() |
| | | 50 | | { |
| | 61 | 51 | | ReadOnlySpan<T> span = writer.AsSpan(); |
| | 61 | 52 | | if (span.Length == 0) |
| | | 53 | | { |
| | 0 | 54 | | return ImmutableArray<T>.Empty; |
| | | 55 | | } |
| | | 56 | | |
| | 61 | 57 | | T[]? array = span.ToArray(); |
| | | 58 | | |
| | 61 | 59 | | return Unsafe.As<T[], ImmutableArray<T>>(ref array); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public ReadOnlySpan<T> AsSpan() |
| | | 63 | | { |
| | 96 | 64 | | return writer.AsSpan(); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public override string ToString() |
| | | 69 | | { |
| | 1969 | 70 | | return writer.AsSpan().ToString(); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private sealed class Writer : IDisposable |
| | | 74 | | { |
| | | 75 | | private T[] array; |
| | | 76 | | private int index = 0; |
| | | 77 | | |
| | 2127 | 78 | | public Writer(int capacity = 0) |
| | | 79 | | { |
| | 2127 | 80 | | array = capacity == 0 ? Array.Empty<T>() : ArrayPool<T>.Shared.Rent(capacity); |
| | 2127 | 81 | | } |
| | | 82 | | |
| | | 83 | | public void Add(T value) |
| | | 84 | | { |
| | 471 | 85 | | EnsureCapacity(1); |
| | | 86 | | |
| | 471 | 87 | | array[index++] = value; |
| | 471 | 88 | | } |
| | | 89 | | |
| | | 90 | | public void AddRange(ReadOnlySpan<T> items) |
| | | 91 | | { |
| | 4372 | 92 | | EnsureCapacity(items.Length); |
| | | 93 | | |
| | 4372 | 94 | | items.CopyTo(array.AsSpan(index)); |
| | 4372 | 95 | | index += items.Length; |
| | 4372 | 96 | | } |
| | | 97 | | |
| | | 98 | | public void Clear() |
| | | 99 | | { |
| | 48 | 100 | | Array.Clear(array, 0, index); |
| | 48 | 101 | | index = 0; |
| | 48 | 102 | | } |
| | | 103 | | |
| | | 104 | | private void EnsureCapacity(int requestedSize) |
| | | 105 | | { |
| | 4843 | 106 | | if (requestedSize > array.Length - index) |
| | | 107 | | { |
| | 135 | 108 | | ResizeBuffer(requestedSize); |
| | | 109 | | } |
| | 4843 | 110 | | } |
| | | 111 | | |
| | | 112 | | private void ResizeBuffer(int capacity) |
| | | 113 | | { |
| | 135 | 114 | | int minimumSize = index + capacity; |
| | | 115 | | |
| | 135 | 116 | | T[] oldBuffer = array; |
| | 135 | 117 | | T[]? newBuffer = ArrayPool<T>.Shared.Rent(minimumSize); |
| | | 118 | | |
| | 135 | 119 | | Array.Copy(oldBuffer, newBuffer, oldBuffer.Length); |
| | | 120 | | |
| | 135 | 121 | | array = newBuffer; |
| | | 122 | | |
| | 135 | 123 | | if (oldBuffer.Length > 0) |
| | | 124 | | { |
| | 85 | 125 | | ArrayPool<T>.Shared.Return(oldBuffer, typeof(T) != typeof(char)); |
| | | 126 | | } |
| | 135 | 127 | | } |
| | | 128 | | |
| | | 129 | | public void Dispose() |
| | | 130 | | { |
| | 2102 | 131 | | if (array.Length > 0) |
| | | 132 | | { |
| | 2102 | 133 | | ArrayPool<T>.Shared.Return(array, typeof(T) != typeof(char)); |
| | | 134 | | } |
| | 2102 | 135 | | } |
| | | 136 | | |
| | | 137 | | public ReadOnlySpan<T> AsSpan() |
| | | 138 | | { |
| | 2126 | 139 | | return new ReadOnlySpan<T>(array, 0, index); |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <inheritdoc /> |
| | | 144 | | public bool Equals(ArrayBuilder<T> other) |
| | | 145 | | { |
| | 0 | 146 | | ReadOnlySpan<T> otherSpan = other.AsSpan(); |
| | 0 | 147 | | ReadOnlySpan<T> thisSpan = AsSpan(); |
| | | 148 | | |
| | 0 | 149 | | if (thisSpan.Length != otherSpan.Length) |
| | | 150 | | { |
| | 0 | 151 | | return false; |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default; |
| | | 155 | | |
| | 0 | 156 | | for (int i = 0; i < thisSpan.Length; i++) |
| | | 157 | | { |
| | 0 | 158 | | if (!equalityComparer.Equals(thisSpan[i], otherSpan[i])) |
| | | 159 | | { |
| | 0 | 160 | | return false; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return true; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc /> |
| | | 168 | | public override bool Equals(object? obj) |
| | | 169 | | { |
| | 0 | 170 | | return obj is ArrayBuilder<T> other && Equals(other); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <inheritdoc /> |
| | | 174 | | public override int GetHashCode() |
| | | 175 | | { |
| | 0 | 176 | | ReadOnlySpan<T> thisSpan = AsSpan(); |
| | | 177 | | |
| | 0 | 178 | | if (thisSpan.Length == 0) |
| | | 179 | | { |
| | 0 | 180 | | return 0; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default; |
| | 0 | 184 | | int hash = 17; |
| | | 185 | | unchecked |
| | | 186 | | { |
| | 0 | 187 | | foreach (T item in thisSpan) |
| | | 188 | | { |
| | 0 | 189 | | hash = hash * 23 * equalityComparer.GetHashCode(item); |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return hash; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | public static bool operator ==(ArrayBuilder<T> left, ArrayBuilder<T> right) |
| | | 197 | | { |
| | 0 | 198 | | return left.Equals(right); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | public static bool operator !=(ArrayBuilder<T> left, ArrayBuilder<T> right) |
| | | 202 | | { |
| | 0 | 203 | | return !left.Equals(right); |
| | | 204 | | } |
| | | 205 | | } |