-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_set.go
82 lines (69 loc) · 1.96 KB
/
output_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sortnet
type OutputSet interface {
Derive(Network) OutputSet
Add(sequence BinarySequence)
Size() int
IsSubset(OutputSet, PermutationMap) bool
Contains(BinarySequence) bool
ContainsInPartition(seq BinarySequence, partition int) bool
Metadata() *SetMetadata
}
func PopulateOutputSet(set OutputSet, channels int) OutputSet {
mask := SequenceMask(channels)
for seq := BinarySequence(1); seq < mask; seq++ {
set.Add(seq)
}
return set
}
type NetworkID int64
type SetMetadata struct {
NetworkID
Size int
PartitionSizes []int
OnesMasks []BinarySequence
ZerosMasks []BinarySequence
}
func (md *SetMetadata) Add(seq BinarySequence, partition int) {
if partition >= len(md.PartitionSizes) {
for i := len(md.PartitionSizes); i <= partition; i++ {
md.OnesMasks = append(md.OnesMasks, 0)
md.ZerosMasks = append(md.ZerosMasks, 0)
md.PartitionSizes = append(md.PartitionSizes, 0)
}
}
md.Size++
md.PartitionSizes[partition]++
md.OnesMasks[partition] |= seq
md.ZerosMasks[partition] |= ^seq
}
// ST1 check total size of the output
func (md *SetMetadata) ST1(other *SetMetadata) bool {
return md.Size <= other.Size
}
// ST2 check the size of corresponding clusters/partitions
func (md *SetMetadata) ST2(other *SetMetadata) bool {
for pi := 0; pi < len(md.PartitionSizes); pi++ {
if md.PartitionSizes[pi] > other.PartitionSizes[pi] {
return false
}
}
return true
}
// ST3 check the ones and the zeros
func (md *SetMetadata) ST3(other *SetMetadata) bool {
for pi := 0; pi < len(md.PartitionSizes); pi++ {
if md.OnesMasks[pi].OnesCount() > other.OnesMasks[pi].OnesCount() {
return false
}
}
for pi := 0; pi < len(md.PartitionSizes); pi++ {
if md.ZerosMasks[pi].OnesCount() > other.ZerosMasks[pi].OnesCount() {
return false
}
}
return true
}
// ST4 check all permutations
func (md *SetMetadata) ST4(other *SetMetadata) bool {
panic("ST4 is instead implemented as a permutation generator - see sortnet/permutation.go")
}