bitset

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2015 License: BSD-3-Clause, Apache-2.0 Imports: 7 Imported by: 0

README

Package bitset implements bitsets, a mapping between non-negative integers and boolean values. It should be more efficient than map[uint] bool.

It provides methods for setting, clearing, flipping, and testing individual integers.

But it also provides set intersection, union, difference, complement, and symmetric operations, as well as tests to check whether any, all, or no bits are set, and querying a bitset's current length and number of postive bits.

BitSets are expanded to the size of the largest set bit; the memory allocation is approximately Max bits, where Max is the 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, return a 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)
}
for i,e := v.NextSet(0); e; i,e = v.NextSet(i + 1) {
   frmt.Println("The following bit is set:",i);
}
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.

Discussions golang-nuts Google Group:

Godoc documentation is at: https://godoc.org/github.com/willf/bitset

Documentation

Overview

	Package bitset implements bitsets, a mapping
	between non-negative integers and boolean values. It should be more
	efficient than map[uint] bool.

	It provides methods for setting, clearing, flipping, and testing
	individual integers.

	But it also provides set intersection, union, difference,
	complement, and symmetric operations, as well as tests to
	check whether any, all, or no bits are set, and querying a
	bitset's current length and number of postive bits.

	BitSets are expanded to the size of the largest set bit; the
	memory allocation is approximately Max bits, where Max is
	the 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, return
	a 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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cap

func Cap() uint

Types

type BitSet

type BitSet struct {
	// contains filtered or unexported fields
}

The zero value of a BitSet is an empty set of length 0.

func New

func New(length uint) *BitSet

func (*BitSet) All

func (b *BitSet) All() bool

Returns true if all bits are set, false otherwise

func (*BitSet) Any

func (b *BitSet) Any() bool

Return true if any bit is set, false otherwise

func (*BitSet) BinaryStorageSize

func (b *BitSet) BinaryStorageSize() int

func (*BitSet) Clear

func (b *BitSet) Clear(i uint) *BitSet

Clear bit i to 0

func (*BitSet) ClearAll

func (b *BitSet) ClearAll() *BitSet

Clear entire BitSet

func (*BitSet) Clone

func (b *BitSet) Clone() *BitSet

Clone this BitSet

func (*BitSet) Complement

func (b *BitSet) Complement() (result *BitSet)

Return the (local) Complement of a biset (up to length bits)

func (*BitSet) Copy

func (b *BitSet) Copy(c *BitSet) (count uint)

Copy this BitSet into a destination BitSet Returning the size of the destination BitSet like array copy

func (*BitSet) Count

func (b *BitSet) Count() uint

Count (number of set bits)

func (*BitSet) Difference

func (b *BitSet) Difference(compare *BitSet) (result *BitSet)

Difference of base set and other set This is the BitSet equivalent of &^ (and not)

func (*BitSet) DifferenceCardinality

func (b *BitSet) DifferenceCardinality(compare *BitSet) uint

computes the cardinality of the differnce

func (*BitSet) DumpAsBits

func (b *BitSet) DumpAsBits() string

Dump as bits

func (*BitSet) Equal

func (b *BitSet) Equal(c *BitSet) bool

Test the equvalence of two BitSets. False if they are of different sizes, otherwise true only if all the same bits are set

func (*BitSet) Flip

func (b *BitSet) Flip(i uint) *BitSet

Flip bit at i

func (*BitSet) InPlaceDifference

func (b *BitSet) InPlaceDifference(compare *BitSet)

Difference of base set and other set This is the BitSet equivalent of &^ (and not)

func (*BitSet) InPlaceIntersection

func (b *BitSet) InPlaceIntersection(compare *BitSet)

Intersection of base set and other set This is the BitSet equivalent of & (and)

func (*BitSet) InPlaceSymmetricDifference

func (b *BitSet) InPlaceSymmetricDifference(compare *BitSet)

SymmetricDifference of base set and other set This is the BitSet equivalent of ^ (xor)

func (*BitSet) InPlaceUnion

func (b *BitSet) InPlaceUnion(compare *BitSet)

Union of base set and other set This is the BitSet equivalent of | (or)

func (*BitSet) Intersection

func (b *BitSet) Intersection(compare *BitSet) (result *BitSet)

Intersection of base set and other set This is the BitSet equivalent of & (and)

func (*BitSet) IntersectionCardinality

func (b *BitSet) IntersectionCardinality(compare *BitSet) uint

Computes the cardinality of the union

func (*BitSet) Len

func (b *BitSet) Len() uint

func (*BitSet) MarshalJSON

func (b *BitSet) MarshalJSON() ([]byte, error)

func (*BitSet) NextClear

func (b *BitSet) NextClear(i uint) (uint, bool)

return 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) NextSet

func (b *BitSet) NextSet(i uint) (uint, bool)

return 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) {...}

func (*BitSet) None

func (b *BitSet) None() bool

Return true if no bit is set, false otherwise

func (*BitSet) ReadFrom

func (b *BitSet) ReadFrom(stream io.Reader) (int64, error)

func (*BitSet) Set

func (b *BitSet) Set(i uint) *BitSet

Set bit i to 1

func (*BitSet) SetTo

func (b *BitSet) SetTo(i uint, value bool) *BitSet

Set bit i to value

func (*BitSet) SymmetricDifference

func (b *BitSet) SymmetricDifference(compare *BitSet) (result *BitSet)

SymmetricDifference of base set and other set This is the BitSet equivalent of ^ (xor)

func (*BitSet) SymmetricDifferenceCardinality

func (b *BitSet) SymmetricDifferenceCardinality(compare *BitSet) uint

computes the cardinality of the symmetric difference

func (*BitSet) Test

func (b *BitSet) Test(i uint) bool

/ Test whether bit i is set.

func (*BitSet) Union

func (b *BitSet) Union(compare *BitSet) (result *BitSet)

Union of base set and other set This is the BitSet equivalent of | (or)

func (*BitSet) UnionCardinality

func (b *BitSet) UnionCardinality(compare *BitSet) uint

func (*BitSet) UnmarshalJSON

func (b *BitSet) UnmarshalJSON(data []byte) error

func (*BitSet) WriteTo

func (b *BitSet) WriteTo(stream io.Writer) (int64, error)

type BitSetError

type BitSetError string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL