typeutil

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterSlice

func FilterSlice[T any](in []T, fn func(T) bool) []T

func LimitSlice

func LimitSlice[T any](slice []T, limit int) ([]T, int)

func Pointer

func Pointer[T any](v T) *T

Pointer creates a pointer from the given value. This is useful, when creating a pointer from a loop variable 1 or when creating a pointer from a return value of another function, since both would require assigning the value to an intermediate variable first.

func Value

func Value[T any](p *T) T

Value returns the value from a pointer. If the pointer is nil, it will return the zero value instead.

Types

type Set

type Set[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

Set implements a set data structure based on built-in maps. This is not optimized for large data, but to be convient.

func NewSet

func NewSet[T constraints.Ordered](values ...T) *Set[T]

NewSet initializes a new Set with the given values. If there are no values provides this is equivalent to new(Set[T]).

func SetIntersect

func SetIntersect[T constraints.Ordered](sets ...*Set[T]) *Set[T]

SetIntersect returns a set that only contains elements which exist in all sets.

func SetUnion

func SetUnion[T constraints.Ordered](sets ...*Set[T]) *Set[T]

func (*Set[T]) Add

func (s *Set[T]) Add(value T)

Add puts a single value to the set. The set will be the same, if it already contains the value.

func (*Set[T]) AddSet

func (s *Set[T]) AddSet(other *Set[T])

AddSet adds each value from the given set to the set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(value T) bool

Contains returns true, if the given value is part of the set.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of all values in the set.

func (Set[T]) MarshalJSON

func (s Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON adds support for mashaling the set into a JSON list.

func (*Set[T]) Remove

func (s *Set[T]) Remove(value T)

Remove removes the given value from the set. The set will be the same, if the value is not part of it.

func (*Set[T]) Subtract

func (s *Set[T]) Subtract(other *Set[T])

Substract removes every element from the given set from the set.

func (*Set[T]) ToList

func (s *Set[T]) ToList() []T

ToList converts the set into a slice. Since the set uses a map as an underlying data structure, this will copy each value. So it might be memory intensive. Also it sorts the slice to ensure a cosistent result.

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) error

MarshalJSON adds support for unmashaling the set from a JSON list.

Jump to

Keyboard shortcuts

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