github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekamath/flag.go (about) 1 // Copyright © 2020-2021. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekamath 7 8 type ( 9 // Flag8 is a type that allows you to represent a 8 bit set named "flagset". 10 // It's often used to optimized storing only true/false values of something. 11 Flag8 uint8 12 13 // Flag16 is a type that allows you to represent a 16 bit set named "flagset". 14 // It's often used to optimized storing only true/false values of something. 15 Flag16 uint16 16 17 // Flag32 is a type that allows you to represent a 32 bit set named "flagset". 18 // It's often used to optimized storing only true/false values of something. 19 Flag32 uint32 20 21 // Flag64 is a type that allows you to represent a 64 bit set named "flagset". 22 // It's often used to optimized storing only true/false values of something. 23 Flag64 uint64 24 25 // Flags8 is absolutely the same as Flag8 (8 bit set) but w/o trailing "s". 26 // It exists exists to make your code more clear. Using this type you can say: 27 // "This variable has exactly many flags, not only one!". 28 Flags8 = Flag8 29 30 // Flags16 is absolutely the same as Flag16 (16 bit set) but w/o trailing "s". 31 // It exists exists to make your code more clear. Using this type you can say: 32 // "This variable has exactly many flags, not only one!". 33 Flags16 = Flag16 34 35 // Flags32 is absolutely the same as Flag32 (32 bit set) but w/o trailing "s". 36 // It exists exists to make your code more clear. Using this type you can say: 37 // "This variable has exactly many flags, not only one!". 38 Flags32 = Flag32 39 40 // Flags64 is absolutely the same as Flag64 (64 bit set) but w/o trailing "s". 41 // It exists exists to make your code more clear. Using this type you can say: 42 // "This variable has exactly many flags, not only one!". 43 Flags64 = Flag64 44 ) 45 46 // --------------------------------- EXTENDERS -------------------------------- // 47 // ---------------------------------------------------------------------------- // 48 49 // To16 extends and returns current f (8 bit flagset) to 16 bit flagset. 50 func (f Flag8) To16() Flag16 { 51 return Flag16(f) 52 } 53 54 // To32 extends and returns current f (8 bit flagset) to 32 bit flagset. 55 func (f Flag8) To32() Flag32 { 56 return Flag32(f) 57 } 58 59 // To64 extends and returns current f (8 bit flagset) to 64 bit flagset. 60 func (f Flag8) To64() Flag64 { 61 return Flag64(f) 62 } 63 64 // To32 extends and returns current f (16 bit flagset) to 32 bit flagset. 65 func (f Flag16) To32() Flag32 { 66 return Flag32(f) 67 } 68 69 // To64 extends and returns current f (16 bit flagset) to 64 bit flagset. 70 func (f Flag16) To64() Flag64 { 71 return Flag64(f) 72 } 73 74 // To64 extends and returns current f (32 bit flagset) to 64 bit flagset. 75 func (f Flag32) To64() Flag64 { 76 return Flag64(f) 77 } 78 79 // ---------------------------- 8 BIT FLAG METHODS ---------------------------- // 80 // ---------------------------------------------------------------------------- // 81 82 // IsZero reports whether f is the empty flagset. Returns true if f == 0. 83 func (f Flag8) IsZero() bool { 84 return f == 0 85 } 86 87 // TestAny reports whether at least one flag from anotherFlags is set in f. 88 // Returns false if there is no flags in f from anotherFlags. 89 func (f Flag8) TestAny(anotherFlags Flag8) bool { 90 return f&anotherFlags != 0 91 } 92 93 // TestAll reports whether ALL flags from anotherFlags are set in f. 94 // Returns false if at least one flag from anotherFlags is NOT set in f. 95 func (f Flag8) TestAll(anotherFlags Flag8) bool { 96 return f&anotherFlags == anotherFlags 97 } 98 99 // SetAll sets all flags from anotherFlags in f. Returns changed f. Keeps flags 100 // already been set in f. If you want overwrite them use method ReplaceBy() instead. 101 func (f *Flag8) SetAll(anotherFlags Flag8) *Flag8 { 102 *f |= anotherFlags 103 return f 104 } 105 106 // ReplaceBy replaces all being set flags in f by newFlags. It's like assign op. 107 // Returns changed f. 108 func (f *Flag8) ReplaceBy(newFlags Flag8) *Flag8 { 109 *f = newFlags 110 return f 111 } 112 113 // Clear downs all flags from cleanedFlags in f. Returns changed f. 114 func (f *Flag8) Clear(cleanedFlags Flag8) *Flag8 { 115 *f &^= cleanedFlags 116 return f 117 } 118 119 // Zero downs all set flags in f. After call f.IsZero() == true. Returns clean f. 120 func (f *Flag8) Zero() *Flag8 { 121 *f = 0 122 return f 123 } 124 125 // --------------------------- 16 BIT FLAG METHODS ---------------------------- // 126 // ---------------------------------------------------------------------------- // 127 128 // IsZero reports whether f is the empty flagset. Returns true if f == 0. 129 func (f Flag16) IsZero() bool { 130 return f == 0 131 } 132 133 // TestAny reports whether at least one flag from anotherFlags is set in f. 134 // Returns false if there is no flags in f from anotherFlags. 135 func (f Flag16) TestAny(anotherFlags Flag16) bool { 136 return f&anotherFlags != 0 137 } 138 139 // TestAll reports whether ALL flags from anotherFlags are set in f. 140 // Returns false if at least one flag from anotherFlags is NOT set in f. 141 func (f Flag16) TestAll(anotherFlags Flag16) bool { 142 return f&anotherFlags == anotherFlags 143 } 144 145 // SetAll sets all flags from anotherFlags in f. Returns changed f. Keeps flags 146 // already been set in f. If you want overwrite them use method ReplaceBy() instead. 147 func (f *Flag16) SetAll(anotherFlags Flag16) *Flag16 { 148 *f |= anotherFlags 149 return f 150 } 151 152 // ReplaceBy replaces all being set flags in f by newFlags. It's like assign op. 153 // Returns changed f. 154 func (f *Flag16) ReplaceBy(newFlags Flag16) *Flag16 { 155 *f = newFlags 156 return f 157 } 158 159 // Clear downs all flags from cleanedFlags in f. Returns changed f. 160 func (f *Flag16) Clear(cleanedFlags Flag16) *Flag16 { 161 *f &^= cleanedFlags 162 return f 163 } 164 165 // Zero downs all set flags in f. After call f.IsZero() == true. Returns clean f. 166 func (f *Flag16) Zero() *Flag16 { 167 *f = 0 168 return f 169 } 170 171 // --------------------------- 32 BIT FLAG METHODS ---------------------------- // 172 // ---------------------------------------------------------------------------- // 173 174 // IsZero reports whether f is the empty flagset. Returns true if f == 0. 175 func (f Flag32) IsZero() bool { 176 return f == 0 177 } 178 179 // TestAny reports whether at least one flag from anotherFlags is set in f. 180 // Returns false if there is no flags in f from anotherFlags. 181 func (f Flag32) TestAny(anotherFlags Flag32) bool { 182 return f&anotherFlags != 0 183 } 184 185 // TestAll reports whether ALL flags from anotherFlags are set in f. 186 // Returns false if at least one flag from anotherFlags is NOT set in f. 187 func (f Flag32) TestAll(anotherFlags Flag32) bool { 188 return f&anotherFlags == anotherFlags 189 } 190 191 // SetAll sets all flags from anotherFlags in f. Returns changed f. Keeps flags 192 // already been set in f. If you want overwrite them use method ReplaceBy() instead. 193 func (f *Flag32) SetAll(anotherFlags Flag32) *Flag32 { 194 *f |= anotherFlags 195 return f 196 } 197 198 // ReplaceBy replaces all being set flags in f by newFlags. It's like assign op. 199 // Returns changed f. 200 func (f *Flag32) ReplaceBy(newFlags Flag32) *Flag32 { 201 *f = newFlags 202 return f 203 } 204 205 // Clear downs all flags from cleanedFlags in f. Returns changed f. 206 func (f *Flag32) Clear(cleanedFlags Flag32) *Flag32 { 207 *f &^= cleanedFlags 208 return f 209 } 210 211 // Zero downs all set flags in f. After call f.IsZero() == true. Returns clean f. 212 func (f *Flag32) Zero() *Flag32 { 213 *f = 0 214 return f 215 } 216 217 // --------------------------- 64 BIT FLAG METHODS ---------------------------- // 218 // ---------------------------------------------------------------------------- // 219 220 // IsZero reports whether f is the empty flagset. Returns true if f == 0. 221 func (f Flag64) IsZero() bool { 222 return f == 0 223 } 224 225 // TestAny reports whether at least one flag from anotherFlags is set in f. 226 // Returns false if there is no flags in f from anotherFlags. 227 func (f Flag64) TestAny(anotherFlags Flag64) bool { 228 return f&anotherFlags != 0 229 } 230 231 // TestAll reports whether ALL flags from anotherFlags are set in f. 232 // Returns false if at least one flag from anotherFlags is NOT set in f. 233 func (f Flag64) TestAll(anotherFlags Flag64) bool { 234 return f&anotherFlags == anotherFlags 235 } 236 237 // SetAll sets all flags from anotherFlags in f. Returns changed f. Keeps flags 238 // already been set in f. If you want overwrite them use method ReplaceBy() instead. 239 func (f *Flag64) SetAll(anotherFlags Flag64) *Flag64 { 240 *f |= anotherFlags 241 return f 242 } 243 244 // ReplaceBy replaces all being set flags in f by newFlags. It's like assign op. 245 // Returns changed f. 246 func (f *Flag64) ReplaceBy(newFlags Flag64) *Flag64 { 247 *f = newFlags 248 return f 249 } 250 251 // Clear downs all flags from cleanedFlags in f. Returns changed f. 252 func (f *Flag64) Clear(cleanedFlags Flag64) *Flag64 { 253 *f &^= cleanedFlags 254 return f 255 } 256 257 // Zero downs all set flags in f. After call f.IsZero() == true. Returns clean f. 258 func (f *Flag64) Zero() *Flag64 { 259 *f = 0 260 return f 261 }