| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization |
| | | 8 | | { |
| | | 9 | | internal readonly ref struct ValueBitArray |
| | | 10 | | { |
| | | 11 | | public const int ScratchBufferSize = 4; |
| | | 12 | | |
| | | 13 | | private readonly int _bitCount; |
| | | 14 | | private readonly Span<ulong> _buffer; |
| | | 15 | | |
| | | 16 | | public readonly bool IsEmpty |
| | | 17 | | { |
| | | 18 | | get |
| | 0 | 19 | | { |
| | 0 | 20 | | bool isEmpty = true; |
| | 0 | 21 | | foreach (ulong word in _buffer) |
| | 0 | 22 | | { |
| | 0 | 23 | | isEmpty &= word is 0; |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | return isEmpty; |
| | 0 | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public bool this[int index] |
| | | 31 | | { |
| | | 32 | | readonly get |
| | 0 | 33 | | { |
| | 0 | 34 | | Debug.Assert((uint)index < (uint)_bitCount); |
| | | 35 | | |
| | 0 | 36 | | ulong mask = 1UL << (index % (sizeof(ulong) * 8)); |
| | 0 | 37 | | return (_buffer[index / (sizeof(ulong) * 8)] & mask) is not 0; |
| | 0 | 38 | | } |
| | | 39 | | set |
| | 0 | 40 | | { |
| | 0 | 41 | | Debug.Assert((uint)index < (uint)_bitCount); |
| | | 42 | | |
| | 0 | 43 | | ulong mask = 1UL << (index % (sizeof(ulong) * 8)); |
| | 0 | 44 | | ref ulong word = ref _buffer[index / (sizeof(ulong) * 8)]; |
| | 0 | 45 | | word = value ? word | mask : word & ~mask; |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary>Initializes a bit array using the supplied scratch buffer when possible.</summary> |
| | | 50 | | /// <param name="bitCount">The number of addressable bits.</param> |
| | | 51 | | /// <param name="stackBuffer"> |
| | | 52 | | /// A scratch buffer whose length must equal <see cref="ScratchBufferSize"/>. |
| | | 53 | | /// </param> |
| | | 54 | | /// <param name="initialWordValue">The initial value assigned to each backing word.</param> |
| | | 55 | | public ValueBitArray( |
| | | 56 | | int bitCount, |
| | | 57 | | Span<ulong> stackBuffer, |
| | | 58 | | ulong initialWordValue = 0) |
| | 0 | 59 | | { |
| | 0 | 60 | | Debug.Assert(bitCount >= 0); |
| | 0 | 61 | | Debug.Assert(stackBuffer.Length == ScratchBufferSize); |
| | 0 | 62 | | _bitCount = bitCount; |
| | | 63 | | |
| | 0 | 64 | | int requiredUInt64Count = |
| | 0 | 65 | | bitCount is 0 ? 0 : ((bitCount - 1) / (sizeof(ulong) * 8)) + 1; |
| | 0 | 66 | | _buffer = requiredUInt64Count <= stackBuffer.Length |
| | 0 | 67 | | ? stackBuffer.Slice(0, requiredUInt64Count) |
| | 0 | 68 | | : new ulong[requiredUInt64Count]; |
| | | 69 | | |
| | 0 | 70 | | _buffer.Fill(initialWordValue); |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | public void Clear() => _buffer.Clear(); |
| | | 74 | | |
| | | 75 | | public void IntersectWith(scoped ValueBitArray other) |
| | 0 | 76 | | { |
| | 0 | 77 | | Debug.Assert(_bitCount == other._bitCount); |
| | | 78 | | |
| | 0 | 79 | | if (_buffer.Length is 1) |
| | 0 | 80 | | { |
| | 0 | 81 | | _buffer[0] &= other._buffer[0]; |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | for (int i = 0; i < _buffer.Length; i++) |
| | 0 | 86 | | { |
| | 0 | 87 | | _buffer[i] &= other._buffer[i]; |
| | 0 | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |