sixb

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

README

sixb go.dev ref report card coverage OpenSSF badge

  • Various utility functions
  • string/slice conversions to avoid redundant memory allocations & copies
  • Containers: Set, CircleQ

with a clean api and comprehensive tests. Use with:

import "github.com/jfcg/sixb/v3"

sixb adheres to semantic versioning.

Support

See Contributing, Security and Support guides. Also if you use sixb and like it, please support via Github Sponsors or:

  • BTC:bc1qr8m7n0w3xes6ckmau02s47a23e84umujej822e
  • ETH:0x3a844321042D8f7c5BB2f7AB17e20273CA6277f6

Documentation

Overview

Package sixb provides string, slice & integer utility functions. string/slice functions help avoid redundant memory allocations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnumToSixb

func AnumToSixb(alphanum byte) byte

AnumToSixb is a bijection (without fixed points, single cycle and inverse of SixbToAnum) that maps 0-9: @A-Z a-z onto 6-bits.

func Bytes

func Bytes[T ~string](str T) []byte

Bytes converts string to byte slice.

func Copy

func Copy[S ~[]T, T any](slc S) S

Copy creates a copy of a slice.

func Dec

func Dec(ctr *uint32) (new uint32)

Dec attempts to decrement a counter with:

if ctr == nil || *ctr == 0 {
	return ^0 // -1
}
atomic {
	*ctr--
	return *ctr
}

func Inc

func Inc(ctr *uint32, max uint32) (new uint32)

Inc attempts to increment a counter with:

if ctr == nil || *ctr >= max {
	return 0
}
atomic {
	*ctr++
	return *ctr
}

func InsideTest

func InsideTest() bool

InsideTest returns true inside a Go test.

func Integers

func Integers[U Integer, T ~string](str T) []U

Integers converts string to integer slice (including []byte).

func Mean

func Mean[T Integer](x, y T) T

Mean returns average of integers x and y, mathematically equivalent to floor((x+y)/2). This function works on the whole domain of (x,y) pairs, unlike (x+y)/2.

func MeanS

func MeanS[T ~string](s1, s2 T) T

MeanS returns lexicographic average of s1 and s2. It treats ascii specially. The result is (len(s1)+len(s2)+1)/2 bytes and may contain unprintable characters or may not be valid utf8.

func Median3

func Median3[T cmp.Ordered](a, b, c T) T

Median3 returns median of three values

func Median4

func Median4[T Integer](a, b, c, d T) T

Median4 returns median of four integers

func PtrToInt

func PtrToInt[P ~*T, T any](ptr P) uint

PtrToInt converts a pointer to an integer.

func SamePtr

func SamePtr[P1 ~*T, P2 ~*U, T, U any](a P1, b P2) bool

SamePtr returns true if pointers a and b are same addresses in memory.

func SixbToAnum

func SixbToAnum(sixb byte) byte

SixbToAnum is a bijection (without fixed points, single cycle and inverse of AnumToSixb) that maps 6-bits onto 0-9: @A-Z a-z.

func Slice

func Slice[U any, S ~[]T, T any](slc S) []U

Slice converts a slice to another slice type, considering element type sizes. Be careful with types that contain pointers.

func String

func String[S ~[]T, T Integer](slc S) string

String converts integer slice (including []byte) to string.

Types

type CircleQ

type CircleQ[T any] struct {
	// contains filtered or unexported fields
}

CircleQ is a circular, fifo queue of items of type T with a maximum capacity. Create a new one like:

// It has four distinct states:
var q CircleQ[int]  // null   : Push & Pop wont work
q.Reset(2)          // empty  : Only Push will work
q.Push(45)          // partial: Push & Pop will work
q.Push(46)          // full   : Only Pop will work
item, ok := q.Pop() // returns 45, true
r := NewCircleQ[int](2) // same as Reset

func NewCircleQ

func NewCircleQ[T any](capacity uint32) (r CircleQ[T])

NewCircleQ creates a circular, fifo queue of given maximum capacity.

func (*CircleQ[T]) Capacity

func (q *CircleQ[T]) Capacity() int

Capacity is the maximum number of items the queue can store.

func (*CircleQ[T]) Copy

func (q *CircleQ[T]) Copy() CircleQ[T]

Copy returns a deep copy of the queue.

func (*CircleQ[T]) Pop

func (q *CircleQ[T]) Pop() (item T, ok bool)

Pop item from the queue if available.

func (*CircleQ[T]) Push

func (q *CircleQ[T]) Push(item T) (ok bool)

Push item to the queue if space is available.

func (*CircleQ[T]) Reset

func (q *CircleQ[T]) Reset(capacity uint32)

Reset the queue to empty with new maximum capacity.

func (*CircleQ[T]) Size

func (q *CircleQ[T]) Size() int

Size is the number of current items in the queue.

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is the set of integer types.

type None

type None struct{}

None represents empty struct with zero size.

type Set

type Set[K comparable] map[K]None

Set of elements of type K. Create empty set with

s := Set[K]{}

func NewSet

func NewSet[K comparable](els ...K) Set[K]

NewSet creates a set from given elements.

func (Set[K]) Add

func (s Set[K]) Add(els ...K)

Add elements to set.

func (Set[K]) HasAll

func (s Set[K]) HasAll(els ...K) bool

HasAll returns true only if set contains all of given elements. If els is empty, it returns true by definition.

func (Set[K]) HasAny

func (s Set[K]) HasAny(els ...K) bool

HasAny returns true only if set contains any of given elements. If els is empty, it returns false by definition.

func (Set[K]) Remove

func (s Set[K]) Remove(els ...K)

Remove elements from set.

func (Set[K]) Size

func (s Set[K]) Size() int

Size returns number of elements in set.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is the set of signed integer types.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned is the set of unsigned integer types.

Jump to

Keyboard shortcuts

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