sets

package
v0.4.119 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 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

func NewElements

func NewElements[T comparable, E any](elements []E) (iter iters.Iterator[Element[T]])

NewElements returns an iterator of interface-type sets.Element[T]

  • elements is a slice of a concrete type, named E, that should implement sets.Element
  • at compile time, elements is slice of any: []any
  • based on a slice of non-interface-type Elements[T comparable].

func PrintAsString

func PrintAsString(value any) (s string)

Types

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 ElementFull

type ElementFull[T comparable] interface {
	Element[T]
	Description() (full string)
}

type ElementIterator

type ElementIterator[T comparable] interface {
	Elements() iters.Iterator[*Element[T]]
}

ElementIterator provides an iterator for all elements of a set intended for SetFactory.NewSet.

type Elements added in v0.4.117

type Elements[T comparable, E any] struct {

	// Delegator implements the value methods required by the [Iterator] interface
	//   - Next HasNext NextValue
	//     Same Has SameValue
	//   - the delegate provides DelegateAction[T] function
	iters.Delegator[Element[T]]
	// contains filtered or unexported fields
}

Elements is an Iterator[Element[T]] that reads from concrete slice []E

  • E is input concrete type, likely *struct, that implements Element[T]
  • T is some comparable type that E produces, allowing for different instances of E to be distinguished from one another
  • Element[T] is a type used by sets, enumerations and bit-fields
  • the Elements iterator provides a sequence of interface-type elements based on a slice of concrete implementation-type values, without re-creating the slice. Go cannot do type assertions on a slice, only on individual values

func (Elements) Cancel added in v0.4.117

func (i Elements) Cancel(errp ...*error) (err error)

Cancel release resources for this iterator. Thread-safe

  • not every iterator requires a Cancel invocation

func (*Elements[T, E]) Cond added in v0.4.118

func (i *Elements[T, E]) Cond(iterationVariablep *Element[T], errp ...*error) (condition bool)

Cond implements the condition statement of a Go “for” clause

  • the iterationVariable is updated by being provided as a pointer. iterationVariable cannot be nil
  • errp is an optional error pointer receiving any errors during iterator execution
  • condition is true if iterationVariable was assigned a value and the iteration should continue

func (*Elements[T, E]) Init added in v0.4.118

func (i *Elements[T, E]) Init() (iterationVariable Element[T], iterator iters.Iterator[Element[T]])

Init implements the right-hand side of a short variable declaration in the init statement for a Go “for” clause

type Set

type Set[T comparable] interface {
	// IsValid returns whether value is part of this set
	IsValid(value T) (isValid bool)
	// Element returns the element representation for value or
	// nil if value is not an element of the set.
	Element(value T) (element Element[T])
	Iterator() (iterator iters.Iterator[T])
	Elements() (elements []T)
	// 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 T) (s string)
	Description(value T) (s string)
	fmt.Stringer
}

Set provides a collection of unique elements of a particular type T that are printable, type convertible and have verifiable validity. A set stores unique values without any particular order. The reasons for using a set over const are:

  • set memberhip enforcement
  • available string representation for elements
  • additional fields or methods assigned to elements
  • optional type safety

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 NewSet

func NewSet[T comparable](elements iters.Iterator[Element[T]]) (set Set[T])

NewSet returns an enumeration of printable semantic elements.

  • elements implement the sets.Element[E] interface
  • — ValueV type is the element type E
  • — Name is the string value displayed for ValueV
  • sets.SetElement or [set.SetElementFull] are sample element implementations

usage:

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

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 SetFactory

type SetFactory[T comparable] interface {
	// NewSet returns a set of a finite number of elements.
	// 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"}}))
	NewSet(elements []Element[T]) (interfaceSet Set[T])
}

type SetImpl

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

SetImpl 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 (*SetImpl[T]) Description

func (st *SetImpl[T]) Description(value T) (full string)

func (*SetImpl[T]) Element

func (st *SetImpl[T]) Element(value T) (element Element[T])

func (*SetImpl[T]) Elements added in v0.4.78

func (st *SetImpl[T]) Elements() (elements []T)

func (*SetImpl[T]) IsValid

func (st *SetImpl[T]) IsValid(value T) (isValid bool)

func (*SetImpl[T]) Iterator added in v0.4.78

func (st *SetImpl[T]) Iterator() (iterator iters.Iterator[T])

func (*SetImpl[T]) String

func (st *SetImpl[T]) String() (s string)

func (*SetImpl[T]) StringT

func (st *SetImpl[T]) StringT(value T) (s string)

StringT provides a String function to a named type implementing a set

Jump to

Keyboard shortcuts

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