< Summary

Information
Class: Hertzole.ScriptableValues.Generator.ArrayBuilder<T>
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Helpers/ArrayBuilder.cs
Tag: 52_28197466029
Line coverage
65%
Covered lines: 46
Uncovered lines: 24
Coverable lines: 70
Total lines: 205
Line coverage: 65.7%
Branch coverage
40%
Covered branches: 9
Total branches: 22
Branch coverage: 40.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%110%
Add(...)100%11100%
AddRange(...)100%11100%
Clear()100%11100%
Dispose()100%11100%
ToImmutable()50%2280%
AsSpan()100%11100%
ToString()100%11100%
.ctor(...)100%22100%
Add(...)100%11100%
AddRange(...)100%11100%
Clear()100%11100%
EnsureCapacity(...)100%22100%
ResizeBuffer(...)100%22100%
Dispose()100%22100%
AsSpan()100%11100%
Equals(...)0%660%
Equals(...)0%220%
GetHashCode()0%440%
op_Equality(...)100%110%
op_Inequality(...)100%110%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Helpers/ArrayBuilder.cs

#LineLine coverage
 1using System;
 2using System.Buffers;
 3using System.Collections.Generic;
 4using System.Collections.Immutable;
 5using System.Runtime.CompilerServices;
 6
 7namespace Hertzole.ScriptableValues.Generator;
 8
 9internal readonly struct ArrayBuilder<T> : IEquatable<ArrayBuilder<T>>, IDisposable
 10{
 11    private readonly Writer writer;
 12
 13    public ArrayBuilder()
 14    {
 5015        writer = new Writer();
 5016    }
 17
 18    public ArrayBuilder(int capacity)
 19    {
 207720        writer = new Writer(capacity);
 207721    }
 22
 23    public ArrayBuilder(ReadOnlySpan<T> items)
 24    {
 025        writer = new Writer(items.Length);
 026        writer.AddRange(items);
 027    }
 28
 29    public void Add(T item)
 30    {
 47131        writer.Add(item);
 47132    }
 33
 34    public void AddRange(ReadOnlySpan<T> items)
 35    {
 437236        writer.AddRange(items);
 437237    }
 38
 39    public void Clear()
 40    {
 4841        writer.Clear();
 4842    }
 43
 44    public void Dispose()
 45    {
 210246        writer.Dispose();
 210247    }
 48
 49    public ImmutableArray<T> ToImmutable()
 50    {
 6151        ReadOnlySpan<T> span = writer.AsSpan();
 6152        if (span.Length == 0)
 53        {
 054            return ImmutableArray<T>.Empty;
 55        }
 56
 6157        T[]? array = span.ToArray();
 58
 6159        return Unsafe.As<T[], ImmutableArray<T>>(ref array);
 60    }
 61
 62    public ReadOnlySpan<T> AsSpan()
 63    {
 9664        return writer.AsSpan();
 65    }
 66
 67    /// <inheritdoc />
 68    public override string ToString()
 69    {
 196970        return writer.AsSpan().ToString();
 71    }
 72
 73    private sealed class Writer : IDisposable
 74    {
 75        private T[] array;
 76        private int index = 0;
 77
 212778        public Writer(int capacity = 0)
 79        {
 212780            array = capacity == 0 ? Array.Empty<T>() : ArrayPool<T>.Shared.Rent(capacity);
 212781        }
 82
 83        public void Add(T value)
 84        {
 47185            EnsureCapacity(1);
 86
 47187            array[index++] = value;
 47188        }
 89
 90        public void AddRange(ReadOnlySpan<T> items)
 91        {
 437292            EnsureCapacity(items.Length);
 93
 437294            items.CopyTo(array.AsSpan(index));
 437295            index += items.Length;
 437296        }
 97
 98        public void Clear()
 99        {
 48100            Array.Clear(array, 0, index);
 48101            index = 0;
 48102        }
 103
 104        private void EnsureCapacity(int requestedSize)
 105        {
 4843106            if (requestedSize > array.Length - index)
 107            {
 135108                ResizeBuffer(requestedSize);
 109            }
 4843110        }
 111
 112        private void ResizeBuffer(int capacity)
 113        {
 135114            int minimumSize = index + capacity;
 115
 135116            T[] oldBuffer = array;
 135117            T[]? newBuffer = ArrayPool<T>.Shared.Rent(minimumSize);
 118
 135119            Array.Copy(oldBuffer, newBuffer, oldBuffer.Length);
 120
 135121            array = newBuffer;
 122
 135123            if (oldBuffer.Length > 0)
 124            {
 85125                ArrayPool<T>.Shared.Return(oldBuffer, typeof(T) != typeof(char));
 126            }
 135127        }
 128
 129        public void Dispose()
 130        {
 2102131            if (array.Length > 0)
 132            {
 2102133                ArrayPool<T>.Shared.Return(array, typeof(T) != typeof(char));
 134            }
 2102135        }
 136
 137        public ReadOnlySpan<T> AsSpan()
 138        {
 2126139            return new ReadOnlySpan<T>(array, 0, index);
 140        }
 141    }
 142
 143    /// <inheritdoc />
 144    public bool Equals(ArrayBuilder<T> other)
 145    {
 0146        ReadOnlySpan<T> otherSpan = other.AsSpan();
 0147        ReadOnlySpan<T> thisSpan = AsSpan();
 148
 0149        if (thisSpan.Length != otherSpan.Length)
 150        {
 0151            return false;
 152        }
 153
 0154        EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
 155
 0156        for (int i = 0; i < thisSpan.Length; i++)
 157        {
 0158            if (!equalityComparer.Equals(thisSpan[i], otherSpan[i]))
 159            {
 0160                return false;
 161            }
 162        }
 163
 0164        return true;
 165    }
 166
 167    /// <inheritdoc />
 168    public override bool Equals(object? obj)
 169    {
 0170        return obj is ArrayBuilder<T> other && Equals(other);
 171    }
 172
 173    /// <inheritdoc />
 174    public override int GetHashCode()
 175    {
 0176        ReadOnlySpan<T> thisSpan = AsSpan();
 177
 0178        if (thisSpan.Length == 0)
 179        {
 0180            return 0;
 181        }
 182
 0183        EqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
 0184        int hash = 17;
 185        unchecked
 186        {
 0187            foreach (T item in thisSpan)
 188            {
 0189                hash = hash * 23 * equalityComparer.GetHashCode(item);
 190            }
 191        }
 192
 0193        return hash;
 194    }
 195
 196    public static bool operator ==(ArrayBuilder<T> left, ArrayBuilder<T> right)
 197    {
 0198        return left.Equals(right);
 199    }
 200
 201    public static bool operator !=(ArrayBuilder<T> left, ArrayBuilder<T> right)
 202    {
 0203        return !left.Equals(right);
 204    }
 205}