-
Notifications
You must be signed in to change notification settings - Fork 1
/
if-set.go
67 lines (61 loc) · 2.16 KB
/
if-set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"fmt"
)
// Set contains unique set elements of a particular type T that are printable.
// 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],
// parly.Element[NextAction],
// ]([]set.Element[NextAction]{{ValueV: IsSame, Name: "IsSame"}}))
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])
// 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
}
// 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.Element is an implementation.
type Element[T comparable] interface {
Value() (value T)
fmt.Stringer
}
type ElementFull[T comparable] interface {
Element[T]
Description() (full string)
}
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],
// parly.Element[NextAction],
// ]([]set.Element[NextAction]{{ValueV: IsSame, Name: "IsSame"}}))
NewSet(elements []Element[T]) (interfaceSet Set[T])
}