sets

package
v0.4.188 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: ISC Imports: 9 Imported by: 0

Documentation

Overview

Set provides a collection of unique elements of a particular type T that are printable, type convertible and have verifiable validity. Element represents an element of a set that has a unique value and is printable. SetElements provides an iterator for all elements of a set intended for SetFactory.NewSet.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicSet added in v0.4.135

type BasicSet[E comparable] struct {
	// contains filtered or unexported fields
}

BasicSet elements are basic-type comparable

func (*BasicSet[E]) Description added in v0.4.135

func (s *BasicSet[E]) Description(value E) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*BasicSet[E]) IsValid added in v0.4.135

func (s *BasicSet[E]) IsValid(value E) (isValid bool)

IsValid returns whether value is part of the set

func (*BasicSet[E]) Iterator added in v0.4.135

func (s *BasicSet[E]) Iterator() (iterator iters.Iterator[E])

Iterator allows iteration over all set elements

func (*BasicSet[E]) String added in v0.4.135

func (s *BasicSet[E]) String() (s2 string)

func (*BasicSet[E]) StringT added in v0.4.135

func (s *BasicSet[E]) StringT(value E) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

type Element

type Element[T comparable] interface {
	Value() (value T)
	fmt.Stringer
}

Element represents an element of a set that has a unique value and is printable. set element values are unique but not necessarily ordered. set.SetElement is an implementation.

type ElementDescription added in v0.4.135

type ElementDescription interface {
	Description() (full string)
}

type FuncSet added in v0.4.135

type FuncSet[T comparable, E any] struct {
	// contains filtered or unexported fields
}

FuncSet is a set where a function eIDFunc extracts element ID from elements

func (*FuncSet[T, E]) Description added in v0.4.135

func (s *FuncSet[T, E]) Description(value T) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*FuncSet[T, E]) EIterator added in v0.4.135

func (s *FuncSet[T, E]) EIterator() (iterator iters.Iterator[*E])

Iterator allows iteration over all set elements

func (*FuncSet[T, E]) Element added in v0.4.135

func (s *FuncSet[T, E]) Element(value T) (elementType *E)

Element returns the element representation for value or nil if value is not an element of the set

func (*FuncSet[T, E]) IsValid added in v0.4.135

func (s *FuncSet[T, E]) IsValid(value T) (isValid bool)

IsValid returns whether value is part of the set

func (*FuncSet[T, E]) Iterator added in v0.4.135

func (s *FuncSet[T, E]) Iterator() (iterator iters.Iterator[T])

Iterator allows iteration over all set elements

func (*FuncSet[T, E]) String added in v0.4.135

func (s *FuncSet[T, E]) String() (s2 string)

func (*FuncSet[T, E]) StringT added in v0.4.135

func (s *FuncSet[T, E]) StringT(value T) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

type MapSet added in v0.4.135

type MapSet[T constraints.Ordered, E any] struct {
	// contains filtered or unexported fields
}

func (*MapSet[T, E]) Description added in v0.4.135

func (s *MapSet[T, E]) Description(value T) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*MapSet[T, E]) EIterator added in v0.4.135

func (s *MapSet[T, E]) EIterator() (iterator iters.Iterator[*E])

Iterator allows iteration over all set elements

func (*MapSet[T, E]) Element added in v0.4.135

func (s *MapSet[T, E]) Element(value T) (elementType *E)

Element returns the element representation for value or nil if value is not an element of the set

func (*MapSet[T, E]) IsValid added in v0.4.135

func (s *MapSet[T, E]) IsValid(value T) (isValid bool)

IsValid returns whether value is part of the set

func (*MapSet[T, E]) Iterator added in v0.4.135

func (s *MapSet[T, E]) Iterator() (iterator iters.Iterator[T])

Iterator allows iteration over all set elements

func (*MapSet[T, E]) String added in v0.4.135

func (s *MapSet[T, E]) String() (s2 string)

func (*MapSet[T, E]) StringT added in v0.4.135

func (s *MapSet[T, E]) StringT(value T) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

type PointerSet added in v0.4.135

type PointerSet[E any] struct {
	// contains filtered or unexported fields
}

PointerSet is a set where pointer-value provides comparable identity

func (*PointerSet[E]) Description added in v0.4.135

func (s *PointerSet[E]) Description(value *E) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*PointerSet[E]) IsValid added in v0.4.135

func (s *PointerSet[E]) IsValid(value *E) (isValid bool)

IsValid returns whether value is part of the set

func (*PointerSet[E]) Iterator added in v0.4.135

func (s *PointerSet[E]) Iterator() (iterator iters.Iterator[*E])

Iterator allows iteration over all set elements

func (*PointerSet[E]) String added in v0.4.135

func (s *PointerSet[E]) String() (s2 string)

func (*PointerSet[E]) StringT added in v0.4.135

func (s *PointerSet[E]) StringT(value *E) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

type Set

type Set[E comparable] interface {
	// IsValid returns whether value is part of the set
	IsValid(value E) (isValid bool)
	// Iterator allows iteration over all set elements
	Iterator() (iterator iters.Iterator[E])
	// StringT returns a string representation for an element of this set.
	// If value is not a valid element, a fmt.Printf value is output like ?'%v'
	StringT(value E) (s string)
	// Description returns a more elaborate string representation
	// for an element. Description and StringT may return the same value
	Description(value E) (s string)
	fmt.Stringer
}

Set is a collection of elements.

A set has:

  • a unique named type for elements that is comparable
  • element values and literals providing access to set functions:
  • — iterator over all elements
  • — IsValid method determining if the element is valid or zero-value
  • — String method making the element printable
  • — for elements with element ID, access to the element type

A set element has:

  • element or element ID literals, typically typed constants
  • series of element or element ID literals are often typed iota-based constants
  • comparable mechanic:
  • — an incomparable element must have a comparable element ID
  • — element ID may also otherwise be used to refer to an element
  • — elements or element IDs may also be ordered

benefits of using sets include:

  • Iterator method iterating over all elements
  • IsValid method verifying whether value is valid
  • String method ensuring elements to be printable
  • make incomparable elements comparable and iterable
  • referring to a complex element any-type using a basic-type comparable element ID
  • type safety by:
  • — distinguishing the set from all other types and values
  • — using elements or element IDs as typed function arguments

composite or incomparable elements

  • If the element type is incomparable, the element should have a basic-type ID for which literals exist:
  • — Integer, Complex string float boolean
  • — slice index or pointers can be used as identity and element iD, but this complicates element ID literals or storing of element IDs
  • incomparables are slice, map, function, and structs or interface dynamic types of those. In particular, a byte slice is incomparable
  • an element must at least have a derived comparable ID type
  • a method can be used to obtain a comparable ID from a composite or incomparable type

complex elements

  • similarly, if element type is large, it may benefit from being referred by an element ID

Usage:

const IsSame NextAction = 0
type NextAction uint8
func (na NextAction) String() (s string) {return nextActionSet.StringT(na)}
var nextActionSet = set.NewSet(yslices.ConvertSliceToInterface[
  set.Element[NextAction],
  parli.Element[NextAction],
]([]set.Element[NextAction]{{ValueV: IsSame, Name: "IsSame"}}))

func NewBasicSet added in v0.4.135

func NewBasicSet[E comparable](elements []E) (set Set[E])

NewBasicSet returns a set of basic-type comparable elements

func NewPointerSet added in v0.4.135

func NewPointerSet[E any](elements []*E) (set Set[*E])

NewPointerSet returns a set where pointer-value provides comparable identity

  • on every process launch, element identity changes

func NewSet

func NewSet[T comparable, E any](elements []E) (set Set[T])

NewSet returns an enumeration of printable semantic elements

  • T is a comparable type for indexing the set, ie. the type of the constants used as set values [SetElement.ValueV]
  • E is type of declarative elements assigning additional properties to T. Typically: sets.SetElement. E is typically inferred and does not have to pbe provided.
  • elements: a slice whose elements implement the sets.Element[E] interface:
  • SetElement.Value method returns the T comparable
  • SetElement.String method returns string representation
  • elements: a simple concrete type is sets.SetElement:
  • — ValueV field is T comparable
  • — Name field is the string value: typically single word
  • another concerete type is sets.ElementFull[E]: Value, String, Description
  • — Description method returns a sentence-length description

usage:

sets.NewSet([]sets.SetElement[int]{
	{ValueV: unix.AF_INET, Name: "IPv4"},
	{ValueV: unix.AF_INET6, Name: "IPv6"},
})

func NewSetFieldp added in v0.4.135

func NewSetFieldp[T comparable, E any](
	elements []E,
	fieldp *Set0[T, E],
	fTEp *func(comparable T) (ep *E),
	epIteratorp *func() (iterator iters.Iterator[*E]),
) (set Set[T])

type Set0 added in v0.4.135

type Set0[T comparable, E any] struct {
	// contains filtered or unexported fields
}

Set0 holds a multiple-selection function argument that has printable representation. PrintableEnum allows finite selection function arguments to have meaningful names and makes those names printable.

func (*Set0[T, E]) Description added in v0.4.135

func (s *Set0[T, E]) Description(value T) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*Set0[T, E]) IsValid added in v0.4.135

func (s *Set0[T, E]) IsValid(value T) (isValid bool)

IsValid returns whether value is part of the set

func (*Set0[T, E]) Iterator added in v0.4.135

func (s *Set0[T, E]) Iterator() (iterator iters.Iterator[T])

Iterator allows iteration over all set elements

func (*Set0[T, E]) String added in v0.4.135

func (s *Set0[T, E]) String() (s2 string)

func (*Set0[T, E]) StringT added in v0.4.135

func (s *Set0[T, E]) StringT(value T) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

type SetElement

type SetElement[T comparable] struct {
	ValueV T
	Name   string
}

func (*SetElement[T]) String

func (pv *SetElement[T]) String() (s string)

func (*SetElement[T]) Value

func (pv *SetElement[T]) Value() (value T)

type SetElementFull

type SetElementFull[T comparable] struct {
	ValueV T
	Name   string // key that maps to this enumeration value
	Full   string // sentence describing this flag
}

SetElementFull is an value or flag-value item of an enumeration container.

  • K is the type of a unique key mapping one-to-one to an enumeration value
  • V is the type of the internally used value representation

func (*SetElementFull[T]) Description

func (item *SetElementFull[T]) Description() (desc string)

func (*SetElementFull[T]) String

func (item *SetElementFull[T]) String() (s string)

func (*SetElementFull[T]) Value

func (item *SetElementFull[T]) Value() (value T)

type SetID added in v0.4.135

type SetID[T comparable, E any] interface {
	Set[T]
	// Element returns the element representation for value or
	// nil if value is not an element of the set
	Element(value T) (elementType *E)
	// Iterator allows iteration over all set elements
	EIterator() (iterator iters.Iterator[*E])
}

func NewFunctionSet added in v0.4.135

func NewFunctionSet[T comparable, E any](elements []E, eIDFunc func(elementType *E) (elementID T)) (set SetID[T, E])

NewFunctionSet returns a set where a function eIDFunc extracts element ID from elements

func NewMapSet added in v0.4.135

func NewMapSet[T constraints.Ordered, E any](m map[T]E) (set SetID[T, E])

func NewSetID added in v0.4.135

func NewSetID[T comparable, E any](elements []E) (set SetID[T, E])

NewSetID returns a set of elements set.Element[T] where element type E is accessible

func NewSliceSet added in v0.4.135

func NewSliceSet[E any](elements []E) (set SetID[int, E])

NewSliceSet is a slice-based set using slice index as comparable

  • element type is therefore any
  • if the slice changes, IDs change, too

type SetID0 added in v0.4.135

type SetID0[T comparable, E any] struct {
	Set0[T, E]
	// contains filtered or unexported fields
}

SetID0 is a set of elements set.Element[T] where element type E is accessible

func (*SetID0[T, E]) EIterator added in v0.4.135

func (s *SetID0[T, E]) EIterator() (iterator iters.Iterator[*E])

Iterator allows iteration over all set elements

func (*SetID0[T, E]) Element added in v0.4.135

func (s *SetID0[T, E]) Element(value T) (element *E)

Element returns the element representation for value or nil if value is not an element of the set

type SliceSet added in v0.4.135

type SliceSet[E any] struct {
	// contains filtered or unexported fields
}

SliceSet is a set of elements that may not be comparable

  • slice index int is used as untyped identity
  • elements may be a struct with slice, map or function fields, or an interface type whose dynamic type is one of those
  • if elements have String method, this is description. Otherwise the “IDn” is description
  • SliceSet does:
  • — ID range checking
  • — provides pointer-iterator
  • a set should have:
  • — its own unique named type
  • — element ID literal, usually a typed const value
  • — an iterator
  • — a way to determine if a value is valid

func (*SliceSet[E]) Description added in v0.4.135

func (s *SliceSet[E]) Description(anID int) (full string)

Description returns a more elaborate string representation for an element. Description and StringT may return the same value

func (*SliceSet[E]) EIterator added in v0.4.135

func (s *SliceSet[E]) EIterator() (iterator iters.Iterator[*E])

Iterator allows iteration over all set elements

func (*SliceSet[E]) Element added in v0.4.135

func (s *SliceSet[E]) Element(anID int) (elementType *E)

Element returns the element representation for value or nil if value is not an element of the set

func (*SliceSet[E]) IsValid added in v0.4.135

func (s *SliceSet[E]) IsValid(elementID int) (isValid bool)

IsValid returns whether value is part of the set

func (*SliceSet[E]) Iterator added in v0.4.135

func (s *SliceSet[E]) Iterator() (iterator iters.Iterator[int])

Iterator allows iteration over all set elements

func (*SliceSet[E]) String added in v0.4.135

func (s *SliceSet[E]) String() (s2 string)

func (*SliceSet[E]) StringT added in v0.4.135

func (s *SliceSet[E]) StringT(anID int) (s2 string)

StringT returns a string representation for an element of this set. If value is not a valid element, a fmt.Printf value is output like ?'%v'

Jump to

Keyboard shortcuts

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