/*Package bitset implements bitsets, a mappingbetween non-negative integers and boolean values. It should be moreefficient than map[uint] bool.It provides methods for setting, clearing, flipping, and testingindividual integers.But it also provides set intersection, union, difference,complement, and symmetric operations, as well as tests tocheck whether any, all, or no bits are set, and querying abitset's current length and number of positive bits.BitSets are expanded to the size of the largest set bit; thememory allocation is approximately Max bits, where Max isthe largest set bit. BitSets are never shrunk. On creation,a hint can be given for the number of bits that will be used.Many of the methods, including Set,Clear, and Flip, returna BitSet pointer, which allows for chaining.Example use: import "bitset" var b BitSet b.Set(10).Set(11) if b.Test(1000) { b.Clear(1000) } if B.Intersection(bitset.New(100).Set(10)).Count() > 1 { fmt.Println("Intersection works.") }As an alternative to BitSets, one should check out the 'big' package,which provides a (less set-theoretical) view of bitsets.*/
package bitsetimport ()// the wordSize of a bit setconst wordSize = uint(64)// the wordSize of a bit set in bytesconst wordBytes = wordSize / 8// log2WordSize is lg(wordSize)const log2WordSize = uint(6)// allBits has every bit setconst allBits uint64 = 0xffffffffffffffff// default binary BigEndianvar binaryOrder binary.ByteOrder = binary.BigEndian// default json encoding base64.URLEncodingvar base64Encoding = base64.URLEncoding// Base64StdEncoding Marshal/Unmarshal BitSet with base64.StdEncoding(Default: base64.URLEncoding)func () { base64Encoding = base64.StdEncoding }// LittleEndian Marshal/Unmarshal Binary as Little Endian(Default: binary.BigEndian)func () { binaryOrder = binary.LittleEndian }// A BitSet is a set of bits. The zero value of a BitSet is an empty set of length 0.typeBitSetstruct { length uint set []uint64}// Error is used to distinguish errors (panics) generated in this package.typeErrorstring// safeSet will fixup b.set to be non-nil and return the field valuefunc ( *BitSet) () []uint64 {if .set == nil { .set = make([]uint64, wordsNeeded(0)) }return .set}// SetBitsetFrom fills the bitset with an array of integers without creating a new BitSet instancefunc ( *BitSet) ( []uint64) { .length = uint(len()) * 64 .set = }// From is a constructor used to create a BitSet from an array of wordsfunc ( []uint64) *BitSet {returnFromWithLength(uint(len())*64, )}// FromWithLength constructs from an array of words and length.func ( uint, []uint64) *BitSet {return &BitSet{, }}// Bytes returns the bitset as array of wordsfunc ( *BitSet) () []uint64 {return .set}// wordsNeeded calculates the number of words needed for i bitsfunc wordsNeeded( uint) int {if > (Cap() - wordSize + 1) {returnint(Cap() >> log2WordSize) }returnint(( + (wordSize - 1)) >> log2WordSize)}// wordsNeededUnbound calculates the number of words needed for i bits, possibly exceeding the capacity.// This function is useful if you know that the capacity cannot be exceeded (e.g., you have an existing bitmap).func wordsNeededUnbound( uint) int {returnint(( + (wordSize - 1)) >> log2WordSize)}// wordsIndex calculates the index of words in a `uint64`func wordsIndex( uint) uint {return & (wordSize - 1)}// New creates a new BitSet with a hint that length bits will be requiredfunc ( uint) ( *BitSet) {deferfunc() {if := recover(); != nil { = &BitSet{0,make([]uint64, 0), } } }() = &BitSet{ ,make([]uint64, wordsNeeded()), }return}// Cap returns the total possible capacity, or number of bitsfunc () uint {return ^uint(0)}// Len returns the number of bits in the BitSet.// Note the difference to method Count, see example.func ( *BitSet) () uint {return .length}// extendSet adds additional words to incorporate new bits if neededfunc ( *BitSet) ( uint) {if >= Cap() {panic("You are exceeding the capacity") } := wordsNeeded( + 1)if .set == nil { .set = make([]uint64, ) } elseifcap(.set) >= { .set = .set[:] // fast resize } elseiflen(.set) < { := make([]uint64, , 2*) // increase capacity 2xcopy(, .set) .set = } .length = + 1}// Test whether bit i is set.func ( *BitSet) ( uint) bool {if >= .length {returnfalse }return .set[>>log2WordSize]&(1<<wordsIndex()) != 0}// Set bit i to 1, the capacity of the bitset is automatically// increased accordingly.// If i>= Cap(), this function will panic.// Warning: using a very large value for 'i'// may lead to a memory shortage and a panic: the caller is responsible// for providing sensible parameters in line with their memory capacity.func ( *BitSet) ( uint) *BitSet {if >= .length { // if we need more bits, make 'em .extendSet() } .set[>>log2WordSize] |= 1 << wordsIndex()return}// Clear bit i to 0func ( *BitSet) ( uint) *BitSet {if >= .length {return } .set[>>log2WordSize] &^= 1 << wordsIndex()return}// SetTo sets bit i to value.// If i>= Cap(), this function will panic.// Warning: using a very large value for 'i'// may lead to a memory shortage and a panic: the caller is responsible// for providing sensible parameters in line with their memory capacity.func ( *BitSet) ( uint, bool) *BitSet {if {return .Set() }return .Clear()}// Flip bit at i.// If i>= Cap(), this function will panic.// Warning: using a very large value for 'i'// may lead to a memory shortage and a panic: the caller is responsible// for providing sensible parameters in line with their memory capacity.func ( *BitSet) ( uint) *BitSet {if >= .length {return .Set() } .set[>>log2WordSize] ^= 1 << wordsIndex()return}// FlipRange bit in [start, end).// If end>= Cap(), this function will panic.// Warning: using a very large value for 'end'// may lead to a memory shortage and a panic: the caller is responsible// for providing sensible parameters in line with their memory capacity.func ( *BitSet) (, uint) *BitSet {if >= {return }if -1 >= .length { // if we need more bits, make 'em .extendSet( - 1) }varuint = >> log2WordSizevaruint = >> log2WordSize .set[] ^= ^(^uint64(0) << wordsIndex())if > 0 {// bounds check elimination := .set _ = [-1]for := ; < ; ++ { [] = ^[] } }if &(wordSize-1) != 0 { .set[] ^= ^uint64(0) >> wordsIndex(-) }return}// Shrink shrinks BitSet so that the provided value is the last possible// set value. It clears all bits > the provided index and reduces the size// and length of the set.//// Note that the parameter value is not the new length in bits: it is the// maximal value that can be stored in the bitset after the function call.// The new length in bits is the parameter value + 1. Thus it is not possible// to use this function to set the length to 0, the minimal value of the length// after this function call is 1.//// A new slice is allocated to store the new bits, so you may see an increase in// memory usage until the GC runs. Normally this should not be a problem, but if you// have an extremely large BitSet its important to understand that the old BitSet will// remain in memory until the GC frees it.func ( *BitSet) ( uint) *BitSet { := + 1 := wordsNeeded()if > len(.set) {return } := make([]uint64, )copy(, .set[:]) .set = .length = := % 64if != 0 { .set[-1] &= allBits >> uint64(64-wordsIndex()) }return}// Compact shrinks BitSet to so that we preserve all set bits, while minimizing// memory usage. Compact calls Shrink.func ( *BitSet) () *BitSet { := len(.set) - 1for ; >= 0 && .set[] == 0; -- { } := uint(( + 1) << log2WordSize)if >= .length {return// nothing to do }if > 0 {return .Shrink( - 1) }// We preserve one wordreturn .Shrink(63)}// InsertAt takes an index which indicates where a bit should be// inserted. Then it shifts all the bits in the set to the left by 1, starting// from the given index position, and sets the index position to 0.//// Depending on the size of your BitSet, and where you are inserting the new entry,// this method could be extremely slow and in some cases might cause the entire BitSet// to be recopied.func ( *BitSet) ( uint) *BitSet { := >> log2WordSize// if length of set is a multiple of wordSize we need to allocate more space firstif .isLenExactMultiple() { .set = append(.set, uint64(0)) }varuintfor = uint(len(.set) - 1); > ; -- {// all elements above the position where we want to insert can simply by shifted .set[] <<= 1// we take the most significant bit of the previous element and set it as // the least significant bit of the current element .set[] |= (.set[-1] & 0x8000000000000000) >> 63 }// generate a mask to extract the data that we need to shift left // within the element where we insert a bit := uint64(1)<<uint64(wordsIndex()) - 1// extract that data that we'll shift := .set[] & (^)// set the positions of the data mask to 0 in the element where we insert .set[] &= // shift data mask to the left and insert its data to the slice element .set[] |= << 1// add 1 to length of BitSet .length++return}// String creates a string representation of the Bitmapfunc ( *BitSet) () string {// follows code from https://github.com/RoaringBitmap/roaringvarbytes.Buffer := []byte("{") .Write() := 0 , := .NextSet(0)for { = + 1// to avoid exhausting the memoryif > 0x40000 { .WriteString("...")break } .WriteString(strconv.FormatInt(int64(), 10)) , = .NextSet( + 1)if { .WriteString(",") } } .WriteString("}")return .String()}// DeleteAt deletes the bit at the given index position from// within the bitset// All the bits residing on the left of the deleted bit get// shifted right by 1// The running time of this operation may potentially be// relatively slow, O(length)func ( *BitSet) ( uint) *BitSet {// the index of the slice element where we'll delete a bit := >> log2WordSize// generate a mask for the data that needs to be shifted right // within that slice element that gets modified := ^((uint64(1) << wordsIndex()) - 1)// extract the data that we'll shift right from the slice element := .set[] & // set the masked area to 0 while leaving the rest as it is .set[] &= ^// shift the previously extracted data to the right and then // set it in the previously masked area .set[] |= ( >> 1) & // loop over all the consecutive slice elements to copy each // lowest bit into the highest position of the previous element, // then shift the entire content to the right by 1for := int() + 1; < len(.set); ++ { .set[-1] |= (.set[] & 1) << 63 .set[] >>= 1 } .length = .length - 1return}// NextSet returns the next bit set from the specified index,// including possibly the current index// along with an error code (true = valid, false = no set bit found)// for i,e := v.NextSet(0); e; i,e = v.NextSet(i + 1) {...}//// Users concerned with performance may want to use NextSetMany to// retrieve several values at once.func ( *BitSet) ( uint) (uint, bool) { := int( >> log2WordSize)if >= len(.set) {return0, false } := .set[] = >> wordsIndex()if != 0 {return + trailingZeroes64(), true } ++// bounds check elimination in the loopif < 0 {return0, false }for < len(.set) {if .set[] != 0 {returnuint()*wordSize + trailingZeroes64(.set[]), true } ++ }return0, false}// NextSetMany returns many next bit sets from the specified index,// including possibly the current index and up to cap(buffer).// If the returned slice has len zero, then no more set bits were found//// buffer := make([]uint, 256) // this should be reused// j := uint(0)// j, buffer = bitmap.NextSetMany(j, buffer)// for ; len(buffer) > 0; j, buffer = bitmap.NextSetMany(j,buffer) {// for k := range buffer {// do something with buffer[k]// }// j += 1// }//// It is possible to retrieve all set bits as follow://// indices := make([]uint, bitmap.Count())// bitmap.NextSetMany(0, indices)//// However if bitmap.Count() is large, it might be preferable to// use several calls to NextSetMany, for performance reasons.func ( *BitSet) ( uint, []uint) (uint, []uint) { := := cap() := int( >> log2WordSize)if >= len(.set) || == 0 {return0, [:0] } := wordsIndex() := .set[] >> = [:] := int(0)for != 0 { := trailingZeroes64() := & ((^) + 1) [] = + ++if == {goto } = ^ } ++for , := range .set[:] {for != 0 { := trailingZeroes64() := & ((^) + 1) [] = + (uint(+) << 6) ++if == {goto } = ^ } }:if > 0 {return [-1], [:] }return0, [:0]}// NextClear returns the next clear bit from the specified index,// including possibly the current index// along with an error code (true = valid, false = no bit found i.e. all bits are set)func ( *BitSet) ( uint) (uint, bool) { := int( >> log2WordSize)if >= len(.set) {return0, false } := .set[] = >> wordsIndex() := allBits >> wordsIndex() := + trailingZeroes64(^)if != && < .length {return , true } ++// bounds check elimination in the loopif < 0 {return0, false }for < len(.set) {if .set[] != allBits { = uint()*wordSize + trailingZeroes64(^.set[])if < .length {return , true } } ++ }return0, false}// ClearAll clears the entire BitSetfunc ( *BitSet) () *BitSet {if != nil && .set != nil {for := range .set { .set[] = 0 } }return}// SetAll sets the entire BitSetfunc ( *BitSet) () *BitSet {if != nil && .set != nil {for := range .set { .set[] = allBits } .cleanLastWord() }return}// wordCount returns the number of words used in a bit setfunc ( *BitSet) () int {returnwordsNeededUnbound(.length)}// Clone this BitSetfunc ( *BitSet) () *BitSet { := New(.length)if .set != nil { // Clone should not modify current objectcopy(.set, .set) }return}// Copy into a destination BitSet using the Go array copy semantics:// the number of bits copied is the minimum of the number of bits in the current// BitSet (Len()) and the destination Bitset.// We return the number of bits copied in the destination BitSet.func ( *BitSet) ( *BitSet) ( uint) {if == nil {return }if .set != nil { // Copy should not modify current objectcopy(.set, .set) } = .lengthif .length < .length { = .length }// Cleaning the last word is needed to keep the invariant that other functions, such as Count, require // that any bits in the last word that would exceed the length of the bitmask are set to 0. .cleanLastWord()return}// CopyFull copies into a destination BitSet such that the destination is// identical to the source after the operation, allocating memory if necessary.func ( *BitSet) ( *BitSet) {if == nil {return } .length = .lengthiflen(.set) == 0 {if .set != nil { .set = .set[:0] } } else {ifcap(.set) < len(.set) { .set = make([]uint64, len(.set)) } else { .set = .set[:len(.set)] }copy(.set, .set) }}// Count (number of set bits).// Also known as "popcount" or "population count".func ( *BitSet) () uint {if != nil && .set != nil {returnuint(popcntSlice(.set)) }return0}// Equal tests the equivalence of two BitSets.// False if they are of different sizes, otherwise true// only if all the same bits are setfunc ( *BitSet) ( *BitSet) bool {if == nil || == nil {return == }if .length != .length {returnfalse }if .length == 0 { // if they have both length == 0, then could have nil setreturntrue } := .wordCount()// bounds check eliminationif <= 0 {returntrue } _ = .set[-1] _ = .set[-1]for := 0; < ; ++ {if .set[] != .set[] {returnfalse } }returntrue}func panicIfNull( *BitSet) {if == nil {panic(Error("BitSet must not be null")) }}// Difference of base set and other set// This is the BitSet equivalent of &^ (and not)func ( *BitSet) ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() = .Clone() // clone b (in case b is bigger than compare) := .wordCount()if > .wordCount() { = .wordCount() }for := 0; < ; ++ { .set[] = .set[] &^ .set[] }return}// DifferenceCardinality computes the cardinality of the differncefunc ( *BitSet) ( *BitSet) uint {panicIfNull()panicIfNull() := .wordCount()if > .wordCount() { = .wordCount() } := uint64(0) += popcntMaskSlice(.set[:], .set[:]) += popcntSlice(.set[:])returnuint()}// InPlaceDifference computes the difference of base set and other set// This is the BitSet equivalent of &^ (and not)func ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() := .wordCount()if > .wordCount() { = .wordCount() }if <= 0 {return }// bounds check elimination , := .set, .set _ = [-1] _ = [-1]for := 0; < ; ++ { [] &^= [] }}// Convenience function: return two bitsets ordered by// increasing length. Note: neither can be nilfunc sortByLength( *BitSet, *BitSet) ( *BitSet, *BitSet) {if .length <= .length { , = , } else { , = , }return}// Intersection of base set and other set// This is the BitSet equivalent of & (and)func ( *BitSet) ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() , = sortByLength(, ) = New(.length)for , := range .set { .set[] = & .set[] }return}// IntersectionCardinality computes the cardinality of the unionfunc ( *BitSet) ( *BitSet) uint {panicIfNull()panicIfNull() , = sortByLength(, ) := popcntAndSlice(.set, .set)returnuint()}// InPlaceIntersection destructively computes the intersection of// base set and the compare set.// This is the BitSet equivalent of & (and)func ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() := .wordCount()if > .wordCount() { = .wordCount() }if > 0 {// bounds check elimination , := .set, .set _ = [-1] _ = [-1]for := 0; < ; ++ { [] &= [] } }if >= 0 {for := ; < len(.set); ++ { .set[] = 0 } }if .length > 0 {if .length-1 >= .length { .extendSet(.length - 1) } }}// Union of base set and other set// This is the BitSet equivalent of | (or)func ( *BitSet) ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() , = sortByLength(, ) = .Clone()for , := range .set { .set[] = | .set[] }return}// UnionCardinality computes the cardinality of the uniton of the base set// and the compare set.func ( *BitSet) ( *BitSet) uint {panicIfNull()panicIfNull() , = sortByLength(, ) := popcntOrSlice(.set, .set)iflen(.set) > len(.set) { += popcntSlice(.set[len(.set):]) }returnuint()}// InPlaceUnion creates the destructive union of base set and compare set.// This is the BitSet equivalent of | (or).func ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() := .wordCount()if > .wordCount() { = .wordCount() }if .length > 0 && .length-1 >= .length { .extendSet(.length - 1) }if > 0 {// bounds check elimination , := .set, .set _ = [-1] _ = [-1]for := 0; < ; ++ { [] |= [] } }iflen(.set) > {for := ; < len(.set); ++ { .set[] = .set[] } }}// SymmetricDifference of base set and other set// This is the BitSet equivalent of ^ (xor)func ( *BitSet) ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() , = sortByLength(, )// compare is bigger, so clone it = .Clone()for , := range .set { .set[] = ^ .set[] }return}// SymmetricDifferenceCardinality computes the cardinality of the symmetric differencefunc ( *BitSet) ( *BitSet) uint {panicIfNull()panicIfNull() , = sortByLength(, ) := popcntXorSlice(.set, .set)iflen(.set) > len(.set) { += popcntSlice(.set[len(.set):]) }returnuint()}// InPlaceSymmetricDifference creates the destructive SymmetricDifference of base set and other set// This is the BitSet equivalent of ^ (xor)func ( *BitSet) ( *BitSet) {panicIfNull()panicIfNull() := .wordCount()if > .wordCount() { = .wordCount() }if .length > 0 && .length-1 >= .length { .extendSet(.length - 1) }if > 0 {// bounds check elimination , := .set, .set _ = [-1] _ = [-1]for := 0; < ; ++ { [] ^= [] } }iflen(.set) > {for := ; < len(.set); ++ { .set[] = .set[] } }}// Is the length an exact multiple of word sizes?func ( *BitSet) () bool {returnwordsIndex(.length) == 0}// Clean last word by setting unused bits to 0func ( *BitSet) () {if !.isLenExactMultiple() { .set[len(.set)-1] &= allBits >> (wordSize - wordsIndex(.length)) }}// Complement computes the (local) complement of a bitset (up to length bits)func ( *BitSet) () ( *BitSet) {panicIfNull() = New(.length)for , := range .set { .set[] = ^ } .cleanLastWord()return}// All returns true if all bits are set, false otherwise. Returns true for// empty sets.func ( *BitSet) () bool {panicIfNull()return .Count() == .length}// None returns true if no bit is set, false otherwise. Returns true for// empty sets.func ( *BitSet) () bool {panicIfNull()if != nil && .set != nil {for , := range .set {if > 0 {returnfalse } } }returntrue}// Any returns true if any bit is set, false otherwisefunc ( *BitSet) () bool {panicIfNull()return !.None()}// IsSuperSet returns true if this is a superset of the other setfunc ( *BitSet) ( *BitSet) bool { := .wordCount()if .wordCount() < { = .wordCount() }for , := range .set[:] {if .set[]& != {returnfalse } }returnpopcntSlice(.set[:]) == 0}// IsStrictSuperSet returns true if this is a strict superset of the other setfunc ( *BitSet) ( *BitSet) bool {return .Count() > .Count() && .IsSuperSet()}// DumpAsBits dumps a bit set as a string of bits. Following the usual convention in Go,// the least significant bits are printed last (index 0 is at the end of the string).func ( *BitSet) () string {if .set == nil {return"." } := bytes.NewBufferString("") := len(.set) - 1for ; >= 0; -- {fmt.Fprintf(, "%064b.", .set[]) }return .String()}// BinaryStorageSize returns the binary storage requirements (see WriteTo) in bytes.func ( *BitSet) () int {returnint(wordBytes + wordBytes*uint(.wordCount()))}func readUint64Array( io.Reader, []uint64) error { := len() := 128 := make([]byte, *int(wordBytes))for := 0; < ; += { := + if > { = = [:wordBytes*uint(-)] } := [:]if , := io.ReadFull(, ); != nil {return }for := range { [] = uint64(binaryOrder.Uint64([8*:])) } }returnnil}func writeUint64Array( io.Writer, []uint64) error { := 128 := make([]byte, *int(wordBytes))for := 0; < len(); += { := + if > len() { = len() = [:wordBytes*uint(-)] } := [:]for , := range {binaryOrder.PutUint64([8*:], ) } , := .Write()if != nil {return } }returnnil}// WriteTo writes a BitSet to a stream. The format is:// 1. uint64 length// 2. []uint64 set// Upon success, the number of bytes written is returned.//// Performance: if this function is used to write to a disk or network// connection, it might be beneficial to wrap the stream in a bufio.Writer.// E.g.,//// f, err := os.Create("myfile")// w := bufio.NewWriter(f)func ( *BitSet) ( io.Writer) (int64, error) { := uint64(.length)// Write length := binary.Write(, binaryOrder, &)if != nil {// Upon failure, we do not guarantee that we // return the number of bytes written.returnint64(0), } = writeUint64Array(, .set[:.wordCount()])if != nil {// Upon failure, we do not guarantee that we // return the number of bytes written.returnint64(wordBytes), }returnint64(.BinaryStorageSize()), nil}// ReadFrom reads a BitSet from a stream written using WriteTo// The format is:// 1. uint64 length// 2. []uint64 set// Upon success, the number of bytes read is returned.// If the current BitSet is not large enough to hold the data,// it is extended. In case of error, the BitSet is either// left unchanged or made empty if the error occurs too late// to preserve the content.//// Performance: if this function is used to read from a disk or network// connection, it might be beneficial to wrap the stream in a bufio.Reader.// E.g.,//// f, err := os.Open("myfile")// r := bufio.NewReader(f)func ( *BitSet) ( io.Reader) (int64, error) {varuint64 := binary.Read(, binaryOrder, &)if != nil {if == io.EOF { = io.ErrUnexpectedEOF }return0, } := uint()ifuint64() != {return0, errors.New("unmarshalling error: type mismatch") } := wordsNeeded(uint())ifcap(.set) >= { .set = .set[:] } else { .set = make([]uint64, ) } .length = = readUint64Array(, .set)if != nil {if == io.EOF { = io.ErrUnexpectedEOF }// We do not want to leave the BitSet partially filled as // it is error prone. .set = .set[:0] .length = 0return0, }returnint64(.BinaryStorageSize()), nil}// MarshalBinary encodes a BitSet into a binary form and returns the result.func ( *BitSet) () ([]byte, error) {varbytes.Buffer , := .WriteTo(&)if != nil {return []byte{}, }return .Bytes(), }// UnmarshalBinary decodes the binary form generated by MarshalBinary.func ( *BitSet) ( []byte) error { := bytes.NewReader() , := .ReadFrom()return}// MarshalJSON marshals a BitSet as a JSON structurefunc ( BitSet) () ([]byte, error) { := bytes.NewBuffer(make([]byte, 0, .BinaryStorageSize())) , := .WriteTo()if != nil {returnnil, }// URLEncode all bytesreturnjson.Marshal(base64Encoding.EncodeToString(.Bytes()))}// UnmarshalJSON unmarshals a BitSet from JSON created using MarshalJSONfunc ( *BitSet) ( []byte) error {// Unmarshal as stringvarstring := json.Unmarshal(, &)if != nil {return }// URLDecode string , := base64Encoding.DecodeString()if != nil {return } _, = .ReadFrom(bytes.NewReader())return}// Rank returns the nunber of set bits up to and including the index// that are set in the bitset.// See https://en.wikipedia.org/wiki/Ranking#Ranking_in_statisticsfunc ( *BitSet) ( uint) uint {if >= .length {return .Count() } := ( + 1) & 63 := uint(popcntSlice(.set[:(+1)>>6]))if != 0 { += uint(popcount(.set[(+1)>>6] << (64 - ))) }return}// Select returns the index of the jth set bit, where j is the argument.// The caller is responsible to ensure that 0 <= j < Count(): when j is// out of range, the function returns the length of the bitset (b.length).//// Note that this function differs in convention from the Rank function which// returns 1 when ranking the smallest value. We follow the conventional// textbook definition of Select and Rank.func ( *BitSet) ( uint) uint { := for , := range .set { := uint(popcount())if > {returnuint()*64 + select64(, ) } -= }return .length}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.