aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src/BitField.cs
blob: 8bdac4fbdb1d543cfb5c5fb56d4f84307a0be190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Utils
* File: BitField.cs 
*
* BitField.cs is part of VNLib.Utils which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Utils is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.Utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with VNLib.Utils. If not, see http://www.gnu.org/licenses/.
*/

using System.Runtime.CompilerServices;

namespace VNLib.Utils
{
    /// <summary>
    /// Represents a field of 64 bits that can be set or cleared using unsigned or signed masks
    /// </summary>
    public class BitField
    {
        private ulong Field;

        /// <summary>
        /// The readonly value of the <see cref="BitField"/>
        /// </summary>
        public ulong Value => Field;

        /// <summary>
        /// Creates a new <see cref="BitField"/> initialized to the specified value
        /// </summary>
        /// <param name="initial">Initial value</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public BitField(ulong initial) => Field = initial;

        /// <summary>
        /// Creates a new <see cref="BitField"/> initialized to the specified value
        /// </summary>
        /// <param name="initial">Initial value</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public BitField(long initial) => Field = unchecked((ulong)initial);

        /// <summary>
        /// Determines if the specified flag is set
        /// </summary>
        /// <param name="mask">The mask to compare against the field value</param>
        /// <returns>True if the flag(s) is currently set, false if flag is not set</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsSet(ulong mask) => (Field & mask) != 0;

        /// <summary>
        /// Determines if the specified flag is set
        /// </summary>
        /// <param name="mask">The mask to compare against the field value</param>
        /// <returns>True if the flag(s) is currently set, false if flag is not set</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsSet(long mask) => (Field & unchecked((ulong)mask)) != 0;

        /// <summary>
        /// Determines if the specified flag is set
        /// </summary>
        /// <param name="mask">The mask to compare against the field value</param>
        /// <returns>True if the flag(s) is currently set, false if flag is not set</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Set(ulong mask) => Field |= mask;

        /// <summary>
        /// Determines if the specified flag is set
        /// </summary>
        /// <param name="mask">The mask to compare against the field value</param>
        /// <returns>True if the flag(s) is currently set, false if flag is not set</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Set(long mask) => Field |= unchecked((ulong)mask);

        /// <summary>
        /// Sets or clears a flag(s) indentified by a mask based on the value
        /// </summary>
        /// <param name="mask">Mask used to identify flags</param>
        /// <param name="value">True to set a flag, false to clear a flag</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Set(ulong mask, bool value)
        {
            if (value)
            {
                Set(mask);
            }
            else
            {
                Clear(mask);
            }
        }

        /// <summary>
        /// Clears the flag identified by the specified mask
        /// </summary>
        /// <param name="mask">The mask used to clear the given flag</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Clear(ulong mask) => Field &= ~mask;

        /// <summary>
        /// Clears the flag identified by the specified mask
        /// </summary>
        /// <param name="mask">The mask used to clear the given flag</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Clear(long mask) => Field &= ~unchecked((ulong)mask);

        /// <summary>
        /// Clears all flags by setting the <see cref="Field"/> property value to 0
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void ClearAll() => Field = 0;
    }
}