// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name bflag_16.go // Created by DEV // Modified 15/02/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package bits import ( "math/bits" ) type BFlags16 uint16 func (f BFlags16) Has ( flag BFlags16) bool { return f&flag != 0 } func (f BFlags16) Add ( flag BFlags16) BFlags16 { return f|flag } func (f BFlags16) AddWithMask ( flag BFlags16, mask BFlags16) BFlags16 { return (f&mask) | flag } func (f BFlags16) Del ( flag BFlags16) BFlags16 { return f&^flag } func (f BFlags16) Invert () BFlags16 { return ^f } func (f BFlags16) Count() int { return bits.OnesCount16(uint16(f)) } func (f BFlags16) Extract() []BFlags16 { if f == 0 { return []BFlags16{} } l := make([]BFlags16,0,16) c := BFlags16(1) for i:=1; i<= 7; i++ { if c&f == c { l = append(l,c) } c <<= 1 } return l } func (f BFlags16) FlagsToNames (namer func(flags BFlags16) string) []string { if f == 0 { return []string{} } l := make([]string,0,16) c := BFlags16(1) for i:=1; i<= 7; i++ { if c&f == c { l = append(l,namer(c)) } c <<= 1 } return l } func (f BFlags16) NamesToFlags (names []string, flagger func(name string) BFlags16) BFlags16 { for _,s := range names { f|=flagger(s) } return f }