gorgonia.org/gorgonia@v0.9.17/internal/encoding/groups.go (about) 1 package encoding 2 3 var groupIndex int 4 5 // NewGroup creates a new group with a generated ID 6 func NewGroup(name string) Group { 7 g := Group{ 8 ID: groupIndex, 9 Name: name, 10 } 11 groupIndex++ 12 return g 13 } 14 15 // Group represent a cluster of elements 16 type Group struct { 17 ID int 18 IsPrimary bool 19 Name string 20 } 21 22 // Grouper is any object that can claim itself as being part of a group 23 type Grouper interface { 24 Groups() Groups 25 } 26 27 // Groups is a bag of groups 28 type Groups []Group 29 30 // Upsert the GroupID in the groups 31 func (g Groups) Upsert(grp Group) Groups { 32 for i := 0; i < len(g); i++ { 33 if (g)[i].ID == grp.ID { 34 return g 35 } 36 } 37 return append(g, grp) 38 } 39 40 // Have returns true if GroupID is in groups 41 func (g Groups) Have(grp Group) bool { 42 for i := 0; i < len(g); i++ { 43 if (g)[i].ID == grp.ID { 44 return true 45 } 46 } 47 return false 48 } 49 50 /* Groups by default sort by the group ID */ 51 52 // Len returns the length of a bag of groups 53 func (g Groups) Len() int { return len(g) } 54 55 // Less checks if an ID is less than or not 56 func (g Groups) Less(i, j int) bool { return g[i].ID < g[j].ID } 57 58 // Swap swaps the elements 59 func (g Groups) Swap(i, j int) { g[i], g[j] = g[j], g[i] } 60 61 // ByName is a sorting for a slice of groups, where the groups are sorted by name 62 type ByName []Group 63 64 func (g ByName) Len() int { return len(g) } 65 66 func (g ByName) Less(i, j int) bool { return g[i].Name < g[j].Name } 67 68 func (g ByName) Swap(i, j int) { g[i], g[j] = g[j], g[i] }