You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using map[type]struct{} is one way to represent a typed unordered set, although the profiling article (https://blog.golang.org/profiling-go-programs) from 2011 indicated that slices can be a more efficient representation. A typed unordered set type could be a useful reference type alongside map and slice and save some effort.
type PointSet set[Point]
func (p PointSet) ChessBoard() Board {
b := NewBoard()
for point := range p {
b[point.Address.Index()] = point
}
return b
}
var TestPointSet = PointSet{
{
Address: {0, 1},
Piece: &Piece{
Kind: Pawn,
Orientation: White,
},
},
{
Address: {4, 0},
Piece: &Piece{
Kind: King,
Orientation: White,
},
},
{
Address: {4, 7},
Piece: &Piece{
Kind: King,
Orientation: Black,
},
},
}
func addPawn() {
TestPointSet = combine(TestPointSet, PointSet{
{
Address: {0, 2},
Piece: &Piece{
Kind: Pawn,
Orientation: White,
},
},
})
}
func addSets() {
TestPointSet = combine(TestPointSet, set0, set1, set2, set3)
}
type Path []Point
type PathSet set[*Path] // slices cannot be compared for equality so use a pointer here
This proposal includes the type set representing an unordered set of same-typed items, a simplified range operator for sets (with only one assigned variable), and the related built-in functions add(set, element) set, combine(sets…) set, reduce(set) set, has(set, element) bool, equal(sets…) bool, diff(setA, setB) set, and set.String() string. Other functions may be useful such as count(set, item) uint, but these are the ones I’ve needed so far. append could be overloaded instead of having add, and delete(set, element) set is probably necessary.
For me it would be nice to not have messy statements like paths[&path] = struct{}{} and perhaps there could be some shared performance gain behind the implementation, but writing these set functions as methods on a type (either map or slice) is not a significant effort now. But almost from the start of working with Go I was reaching for an unordered set type.
The text was updated successfully, but these errors were encountered:
Using
map[type]struct{}
is one way to represent a typed unordered set, although the profiling article (https://blog.golang.org/profiling-go-programs) from 2011 indicated that slices can be a more efficient representation. A typed unorderedset
type could be a useful reference type alongsidemap
andslice
and save some effort.This proposal includes the type
set
representing an unordered set of same-typed items, a simplified range operator for sets (with only one assigned variable), and the related built-in functionsadd(set, element) set
,combine(sets…) set
,reduce(set) set
,has(set, element) bool
,equal(sets…) bool
,diff(setA, setB) set
, andset.String() string
. Other functions may be useful such ascount(set, item) uint
, but these are the ones I’ve needed so far.append
could be overloaded instead of havingadd
, anddelete(set, element) set
is probably necessary.For me it would be nice to not have messy statements like
paths[&path] = struct{}{}
and perhaps there could be some shared performance gain behind the implementation, but writing these set functions as methods on a type (either map or slice) is not a significant effort now. But almost from the start of working with Go I was reaching for an unordered set type.The text was updated successfully, but these errors were encountered: