| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace System.Buffers |
| | | 9 | | { |
| | | 10 | | internal struct BitVector256 |
| | | 11 | | { |
| | | 12 | | private InlineArray8<uint> _values; |
| | | 13 | | |
| | | 14 | | public readonly BitVector256 CreateInverse() |
| | | 15 | | { |
| | 1395 | 16 | | BitVector256 inverse = default; |
| | | 17 | | |
| | 25110 | 18 | | for (int i = 0; i < 8; i++) |
| | | 19 | | { |
| | 11160 | 20 | | inverse._values[i] = ~_values[i]; |
| | | 21 | | } |
| | | 22 | | |
| | 1395 | 23 | | return inverse; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public void Set(int c) |
| | | 27 | | { |
| | 604723 | 28 | | Debug.Assert(c < 256); |
| | 604723 | 29 | | uint offset = (uint)(c >> 5); |
| | 604723 | 30 | | uint significantBit = 1u << c; |
| | 604723 | 31 | | _values[(int)offset] |= significantBit; |
| | 604723 | 32 | | } |
| | | 33 | | |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public readonly bool Contains256(char c) => |
| | 231587 | 36 | | c < 256 && ContainsUnchecked(c); |
| | | 37 | | |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 39 | | public readonly bool Contains(byte b) => |
| | 1261417 | 40 | | ContainsUnchecked(b); |
| | | 41 | | |
| | | 42 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 43 | | private readonly bool ContainsUnchecked(int b) |
| | | 44 | | { |
| | 1321741 | 45 | | Debug.Assert(b < 256); |
| | 1321741 | 46 | | uint offset = (uint)(b >> 5); |
| | 1321741 | 47 | | uint significantBit = 1u << b; |
| | 1321741 | 48 | | return (_values[(int)offset] & significantBit) != 0; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public readonly unsafe char[] GetCharValues() |
| | | 52 | | { |
| | 0 | 53 | | Span<char> chars = stackalloc char[256]; |
| | 0 | 54 | | int size = 0; |
| | 0 | 55 | | for (int i = 0; i < 256; i++) |
| | | 56 | | { |
| | 0 | 57 | | if (ContainsUnchecked(i)) |
| | | 58 | | { |
| | 0 | 59 | | chars[size] = (char)i; |
| | 0 | 60 | | size++; |
| | | 61 | | } |
| | | 62 | | } |
| | 0 | 63 | | return chars.Slice(0, size).ToArray(); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public readonly unsafe byte[] GetByteValues() |
| | | 67 | | { |
| | 0 | 68 | | Span<byte> bytes = stackalloc byte[256]; |
| | 0 | 69 | | int size = 0; |
| | 0 | 70 | | for (int i = 0; i < 256; i++) |
| | | 71 | | { |
| | 0 | 72 | | if (ContainsUnchecked(i)) |
| | | 73 | | { |
| | 0 | 74 | | bytes[size] = (byte)i; |
| | 0 | 75 | | size++; |
| | | 76 | | } |
| | | 77 | | } |
| | 0 | 78 | | return bytes.Slice(0, size).ToArray(); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |