mask

package module
v0.0.1-early-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 0 Imported by: 6

README

mask

The mask package provides utilities for managing bitmasks in Go, including functionality to mark bits, check if bits are set, and compare bitmasks for various conditions. The package supports multiple build configurations (e.g., 64-bit, 256-bit, 512-bit) to accommodate different mask sizes.

Features

  • Mark: Set specific bits in the mask.
  • Contains: Check if a specific bit is set.
  • ContainsAll: Check if all bits in one mask are contained in another.
  • ContainsAny: Check if any bits in one mask are contained in another.
  • ContainsNone: Check if none of the bits in one mask are set in another.

Build Configurations

The package supports different configurations using Go build tags:

  • Default (64-bits)
  • m256 (256-bits)
  • m512 (512 bits)
  • m1024 (1024 bits)

Mask 256

The package contains a fixed Mask256{} type, regardless of build tags.

go test -tags m256

Installation

import "github.com/TheBitDrifter/mask"

Usage

Mark a Bit

To set a specific bit in the mask:

var m mask.Mask
m.Mark(3) // Sets the 3rd bit

To check if a specific bit is set:

if m.Contains(3) {
    fmt.Println("Bit 3 is set!")
}
Compare Bitmasks

You can compare two Mask instances to see if they share certain bits.

Check if all bits in one mask are contained in another:

var other mask.Mask
other.Mark(5)

if m.ContainsAll(other) {
    fmt.Println("All bits in `other` are set in `m`.")
}

Check if any bits in one mask are contained in another:

if m.ContainsAny(other) {
    fmt.Println("At least one bit in `other` is set in `m`.")
}

Check if no bits in one mask are set in another:

if m.ContainsNone(other) {
    fmt.Println("No bits in `other` are set in `m`.")
}

Check for an Empty Mask To check if a mask is empty (i.e., no bits are set):

if mask.isEmpty(m) {
    fmt.Println("Mask is empty.")
}

Documentation

Overview

Package mask provides utilities for managing bitmasks in Go, supporting various mask sizes through build configurations.

The mask package implements functionality to manipulate and compare bitmasks, with support for configurable sizes (64, 256, 512, or 1024 bits) based on build tags. A fixed-size Mask256 is also available regardless of build configuration.

Mask Types

The package defines two primary types:

  • Mask: A variable-sized bitmask whose size depends on build tags
  • Mask256: A fixed 256-bit bitmask available in all configurations

Basic Usage

Creating and manipulating masks:

// Create a new mask
var m mask.Mask

// Set bits in the mask
m.Mark(3)    // Set bit 3
m.Mark(42)   // Set bit 42

// Check if a bit is set
if m.Contains(3) {
	// Bit 3 is set
}

// Clear a bit
m.Unmark(3)

Comparing Masks

Masks can be compared for various conditions:

var m1, m2 mask.Mask
m1.Mark(1)
m1.Mark(2)

m2.Mark(2)
m2.Mark(3)

// Check if m1 contains all bits in m2
if m1.ContainsAll(m2) {
	// m1 has all bits set that are set in m2
}

// Check if m1 contains any bits in m2
if m1.ContainsAny(m2) {
	// m1 and m2 share at least one bit
}

// Check if m1 contains none of the bits in m2
if m1.ContainsNone(m2) {
	// m1 and m2 have no bits in common
}

// Check if a mask is empty (no bits set)
if m1.IsEmpty() {
	// No bits are set in m1
}

Build Configurations

The package supports different bit sizes through build tags:

  • Default: 64-bit mask (no build tag required)
  • m256: 256-bit mask
  • m512: 512-bit mask
  • m1024: 1024-bit mask

Example of building with a specific configuration:

go build -tags m256

The Mask256 type is always available with a fixed 256-bit size, regardless of which build configuration is used.

Index

Constants

View Source
const (
	MaxBits = 64
)

MaxBits defines the maximum number of bits supported by this mask implementation

Variables

This section is empty.

Functions

This section is empty.

Types

type Mask

type Mask [maskSize]uint64

func (Mask) Contains

func (m Mask) Contains(bit uint32) bool

Contains checks if the specified bit is set in the mask

func (Mask) ContainsAll

func (m Mask) ContainsAll(other Mask) bool

ContainsAll checks if all bits set in the other mask are also set in this mask

func (Mask) ContainsAny

func (m Mask) ContainsAny(other Mask) bool

ContainsAny checks if any bit set in the other mask is also set in this mask

func (Mask) ContainsNone

func (m Mask) ContainsNone(other Mask) bool

ContainsNone checks if none of the bits set in the other mask are set in this mask Returns false if the other mask is empty

func (Mask) IsEmpty

func (m Mask) IsEmpty() bool

IsEmpty checks if the mask has no bits set

func (*Mask) Mark

func (m *Mask) Mark(bit uint32)

Mark sets the specified bit in the mask

func (*Mask) Unmark

func (m *Mask) Unmark(bit uint32)

Unmark clears the specified bit in the mask

type Mask256

type Mask256 [256]uint64

Mask256 represents a 256-element bit mask using an array of uint64

func (Mask256) Contains

func (m Mask256) Contains(bit uint32) bool

Contains checks if the specified bit is set in the mask

func (Mask256) ContainsAll

func (m Mask256) ContainsAll(other Mask256) bool

ContainsAll returns true if this mask contains all bits set in the other mask

func (Mask256) ContainsAny

func (m Mask256) ContainsAny(other Mask256) bool

ContainsAny returns true if this mask contains any bits set in the other mask

func (Mask256) ContainsNone

func (m Mask256) ContainsNone(other Mask256) bool

ContainsNone returns true if this mask contains none of the bits set in the other mask

func (Mask256) IsEmpty

func (m Mask256) IsEmpty() bool

IsEmpty returns true if no bits are set in this mask

func (*Mask256) Mark

func (m *Mask256) Mark(bit uint32)

Mark sets the specified bit in the mask

func (*Mask256) Unmark

func (m *Mask256) Unmark(bit uint32)

Unmark clears the specified bit in the mask

type Maskable

type Maskable interface {
	Mask() Mask
}

Maskable represents any type that can be converted to a Mask

Jump to

Keyboard shortcuts

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