AdGuardHome/internal/dhcpd/bitset.go
Eugene Burkov f40ef76c79 Pull request 1861: 4923-gopacket-dhcp vol.1
Merge in DNS/adguard-home from 4923-gopacket-dhcp to master

Updates #4923.

Squashed commit of the following:

commit edf36ce8b1873272c3daebe8cc8f8132793aac44
Merge: a17513d3e 123ca8738
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jun 22 14:14:39 2023 +0300

    Merge branch 'master' into 4923-gopacket-dhcp

commit a17513d3e0a9e596d56444dfa46478eee15631de
Merge: f04727c29 994906fbd
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Jun 21 17:49:09 2023 +0300

    Merge branch 'master' into 4923-gopacket-dhcp

commit f04727c29eaf22f9eb53f3aa33d42d00e177b224
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Jun 20 15:42:31 2023 +0300

    home: revert clients container

commit c58284ac6b5b2274da5eed2e853847d757709e5b
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Jun 19 21:10:36 2023 +0300

    all: imp code, names, docs

commit 4c4613c939e1325d11655822d9dbc3f05a6d203c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Jun 13 18:51:12 2023 +0300

    all: imp code

commit 0b4a6e0dd561d9b7bb78dea21dcc947bcd0bd583
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Jun 7 18:40:15 2023 +0300

    all: imp api

commit 0425edea03d6ca0859657df683bef6ec45bfc399
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Mon Jun 5 15:57:23 2023 +0300

    dhcpsvc: introduce package

commit 5628ebe6cccf91e2c48778966730bcbbe9e1d9f2
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jun 1 17:49:12 2023 +0300

    WIP
2023-06-22 14:18:43 +03:00

49 lines
812 B
Go

package dhcpd
const bitsPerWord = 64
// bitSet is a sparse bitSet. A nil *bitSet is an empty bitSet.
type bitSet struct {
words map[uint64]uint64
}
// newBitSet returns a new bitset.
func newBitSet() (s *bitSet) {
return &bitSet{
words: map[uint64]uint64{},
}
}
// isSet returns true if the bit n is set.
func (s *bitSet) isSet(n uint64) (ok bool) {
if s == nil {
return false
}
wordIdx := n / bitsPerWord
bitIdx := n % bitsPerWord
var word uint64
word, ok = s.words[wordIdx]
return ok && word&(1<<bitIdx) != 0
}
// set sets or unsets a bit.
func (s *bitSet) set(n uint64, ok bool) {
if s == nil {
return
}
wordIdx := n / bitsPerWord
bitIdx := n % bitsPerWord
word := s.words[wordIdx]
if ok {
word |= 1 << bitIdx
} else {
word &^= 1 << bitIdx
}
s.words[wordIdx] = word
}