-
Notifications
You must be signed in to change notification settings - Fork 6
/
group.go
108 lines (90 loc) · 2.4 KB
/
group.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Go IPP - IPP core protocol implementation in pure Go
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Groups of attributes
*/
package goipp
import "sort"
// Group represents a group of attributes.
//
// Since 1.1.0
type Group struct {
Tag Tag // Group tag
Attrs Attributes // Group attributes
}
// Groups represents a sequence of groups
//
// The primary purpose of this type is to represent
// messages with repeated groups with the same group tag
//
// # See Message type documentation for more details
//
// Since 1.1.0
type Groups []Group
// Add Attribute to the Group
func (g *Group) Add(attr Attribute) {
g.Attrs.Add(attr)
}
// Equal checks that groups g and g2 are equal
func (g Group) Equal(g2 Group) bool {
return g.Tag == g2.Tag && g.Attrs.Equal(g2.Attrs)
}
// Similar checks that groups g and g2 are **logically** equal.
func (g Group) Similar(g2 Group) bool {
return g.Tag == g2.Tag && g.Attrs.Similar(g2.Attrs)
}
// Add Group to Groups
func (groups *Groups) Add(g Group) {
*groups = append(*groups, g)
}
// Clone creates a copy of Groups
func (groups Groups) Clone() Groups {
groups2 := make(Groups, len(groups))
copy(groups2, groups)
return groups2
}
// Equal checks that groups and groups2 are equal
func (groups Groups) Equal(groups2 Groups) bool {
if len(groups) != len(groups2) {
return false
}
for i, g := range groups {
g2 := groups2[i]
if !g.Equal(g2) {
return false
}
}
return true
}
// Similar checks that groups and groups2 are **logically** equal,
// which means the following:
// - groups and groups2 contain the same set of
// groups, but groups with different tags may
// be reordered between each other.
// - groups with the same tag cannot be reordered.
// - attributes of corresponding groups are similar.
func (groups Groups) Similar(groups2 Groups) bool {
// Fast check: if lengths are not the same, groups
// are definitely not equal
if len(groups) != len(groups2) {
return false
}
// Sort groups by tag
groups = groups.Clone()
groups2 = groups2.Clone()
sort.SliceStable(groups, func(i, j int) bool {
return groups[i].Tag < groups[j].Tag
})
sort.SliceStable(groups2, func(i, j int) bool {
return groups2[i].Tag < groups2[j].Tag
})
// Now compare, group by group
for i := range groups {
if !groups[i].Similar(groups2[i]) {
return false
}
}
return true
}