generic

package
v0.0.0-...-93b7ddb Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: BSD-2-Clause Imports: 5 Imported by: 0

README

Useful generics

import "github.com/OpenPrinting/go-mfp/util/generic"

This package provides useful generic types and functions, such as generic sets.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcatSlices

func ConcatSlices[S ~[]T, T any](slices ...S) S

ConcatSlices returns the concatenation of some slices.

func CopySlice

func CopySlice[S ~[]T, T any](s S) (clone S)

CopySlice returns a shallow copy of the slice

func EqualSlices

func EqualSlices[S ~[]T, T comparable](s1, s2 S) bool

EqualSlices tells if two slices are equal (the same length and all elements are equal)

func LowerDivisibleBy

func LowerDivisibleBy[T Integer](v, d T) T

LowerDivisibleBy returns v rounded down to the nearest multiple of d.

func Max

func Max[T Ordered](a, b T) T

Max returns maximum of two values, a and b

func Min

func Min[T Ordered](a, b T) T

Min returns minimum of two values, a and b

func SortSlice

func SortSlice[S ~[]T, T Ordered](s S)

SortSlice sorts slice of Ordered elements.

func UpperDivisibleBy

func UpperDivisibleBy[T Integer](v, d T) T

UpperDivisibleBy returns v rounded up to the nearest multiple of d.

Types

type Bitset

type Bitset[T ~int | ~uint] uint32

Bitset represents a bitset of instances of some integer type T. Operations with Bitset are NOT goroutine-safe.

func MakeBitset

func MakeBitset[T ~int](list ...T) Bitset[T]

MakeBitset makes Bitset from the list of elements of type T.

func (*Bitset[T]) Add

func (set *Bitset[T]) Add(elem T) bool

Add adds element to the set. It returns true if element was actually added.

func (*Bitset[T]) Clear

func (set *Bitset[T]) Clear()

Clear purges the set.

func (Bitset[T]) Contains

func (set Bitset[T]) Contains(elem T) bool

Contains reports if element exists in the set.

func (Bitset[T]) Count

func (set Bitset[T]) Count() int

Count returns count of elements in the set.

func (*Bitset[T]) Del

func (set *Bitset[T]) Del(elem T) bool

Del deletes element from the set. It returns true if element was actually deleted.

func (Bitset[T]) Elements

func (set Bitset[T]) Elements() []T

Elements returns slice of the Bitset elements

func (Bitset[T]) Intersection

func (set Bitset[T]) Intersection(set2 Bitset[T]) Bitset[T]

Intersection returns intersection of two bitsets

func (Bitset[T]) IsEmpty

func (set Bitset[T]) IsEmpty() bool

IsEmpty reports if set is empty.

func (Bitset[T]) String

func (set Bitset[T]) String() string

String returns a string representation of the Bitset, for debugging.

func (Bitset[T]) Union

func (set Bitset[T]) Union(set2 Bitset[T]) Bitset[T]

Union returns union of two bitsets

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is a constraint that permits any integer type.

type LockedSet

type LockedSet[T comparable] struct {
	// contains filtered or unexported fields
}

LockedSet is the generic set of any comparable objects. It works like Set, but goroutine-safe.

func NewLockedSet

func NewLockedSet[T comparable]() *LockedSet[T]

NewLockedSet creates a new LockedSet

func (*LockedSet[T]) Add

func (ls *LockedSet[T]) Add(member T)

Add adds member to the set

func (*LockedSet[T]) Clear

func (ls *LockedSet[T]) Clear()

Clear purges the set

func (*LockedSet[T]) Contains

func (ls *LockedSet[T]) Contains(member T) bool

Contains reports if member already in the set

func (*LockedSet[T]) Del

func (ls *LockedSet[T]) Del(member T)

Del deletes member from the set

func (*LockedSet[T]) Empty

func (ls *LockedSet[T]) Empty() bool

Empty reports if set is empty

func (*LockedSet[T]) TestAndAdd

func (ls *LockedSet[T]) TestAndAdd(member T) (added bool)

TestAndAdd adds member to the set and returns true if it was actually added.

func (*LockedSet[T]) TestAndDel

func (ls *LockedSet[T]) TestAndDel(member T) (deleted bool)

TestAndDel deletes member from the set and returns true if it was actually deleted.

type Ordered

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~string
}

Ordered is a constraint that permits any ordered type, i.e., any type that supports <, <=, >= and > operations.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is the generic set of any comparable objects.

Set cannot be simultaneously accessed from multiple goroutines. If you need goroutine safety, use LockedSet.

func NewSet

func NewSet[T comparable]() Set[T]

NewSet creates a new Set

func NewSetOf

func NewSetOf[T comparable](elements ...T) Set[T]

NewSetOf creates and populates a new Set

func (Set[T]) Add

func (s Set[T]) Add(member T)

Add adds member to the set

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear purges the set

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone creates a shallow copy of the set.

func (Set[T]) Contains

func (s Set[T]) Contains(member T) bool

Contains reports if member already in the set

func (Set[T]) Count

func (s Set[T]) Count() int

Count returns count of elements in the set.

func (Set[T]) Del

func (s Set[T]) Del(member T)

Del deletes member from the set

func (Set[T]) Empty

func (s Set[T]) Empty() bool

Empty reports if set is empty

func (Set[T]) ForEach

func (s Set[T]) ForEach(f func(T))

ForEach applies function to the each member of the set

func (Set[T]) Merge

func (s Set[T]) Merge(s2 Set[T])

Merge adds into the set s all members of the set s2.

func (Set[T]) TestAndAdd

func (s Set[T]) TestAndAdd(member T) (added bool)

TestAndAdd adds member to the set and returns true if it was actually added.

func (Set[T]) TestAndDel

func (s Set[T]) TestAndDel(member T) (deleted bool)

TestAndDel deletes member from the set and returns true if it was actually deleted.

Jump to

Keyboard shortcuts

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