< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 42
Coverable lines: 42
Total lines: 92
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%440%
Clear()100%110%
IntersectWith(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ValueBitArray.cs

#LineLine coverage
 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
 4using System;
 5using System.Diagnostics;
 6
 7namespace 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
 019            {
 020                bool isEmpty = true;
 021                foreach (ulong word in _buffer)
 022                {
 023                    isEmpty &= word is 0;
 024                }
 25
 026                return isEmpty;
 027            }
 28        }
 29
 30        public bool this[int index]
 31        {
 32            readonly get
 033            {
 034                Debug.Assert((uint)index < (uint)_bitCount);
 35
 036                ulong mask = 1UL << (index % (sizeof(ulong) * 8));
 037                return (_buffer[index / (sizeof(ulong) * 8)] & mask) is not 0;
 038            }
 39            set
 040            {
 041                Debug.Assert((uint)index < (uint)_bitCount);
 42
 043                ulong mask = 1UL << (index % (sizeof(ulong) * 8));
 044                ref ulong word = ref _buffer[index / (sizeof(ulong) * 8)];
 045                word = value ? word | mask : word & ~mask;
 046            }
 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)
 059        {
 060            Debug.Assert(bitCount >= 0);
 061            Debug.Assert(stackBuffer.Length == ScratchBufferSize);
 062            _bitCount = bitCount;
 63
 064            int requiredUInt64Count =
 065                bitCount is 0 ? 0 : ((bitCount - 1) / (sizeof(ulong) * 8)) + 1;
 066            _buffer = requiredUInt64Count <= stackBuffer.Length
 067                ? stackBuffer.Slice(0, requiredUInt64Count)
 068                : new ulong[requiredUInt64Count];
 69
 070            _buffer.Fill(initialWordValue);
 071        }
 72
 073        public void Clear() => _buffer.Clear();
 74
 75        public void IntersectWith(scoped ValueBitArray other)
 076        {
 077            Debug.Assert(_bitCount == other._bitCount);
 78
 079            if (_buffer.Length is 1)
 080            {
 081                _buffer[0] &= other._buffer[0];
 082                return;
 83            }
 84
 085            for (int i = 0; i < _buffer.Length; i++)
 086            {
 087                _buffer[i] &= other._buffer[i];
 088            }
 089        }
 90    }
 91}
 92