-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathsortedset.go
More file actions
177 lines (155 loc) · 3.72 KB
/
sortedset.go
File metadata and controls
177 lines (155 loc) · 3.72 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package set
import (
crand "crypto/rand"
"encoding/binary"
"log"
mrand "math/rand"
)
import (
"github.com/timtadh/data-structures/errors"
"github.com/timtadh/data-structures/list"
trand "github.com/timtadh/data-structures/rand"
"github.com/timtadh/data-structures/types"
)
var rand *mrand.Rand
func init() {
seed := make([]byte, 8)
if _, err := crand.Read(seed); err == nil {
rand = trand.ThreadSafeRand(int64(binary.BigEndian.Uint64(seed)))
} else {
panic(err)
}
}
type MSortedSet struct {
list.MSorted
}
func NewMSortedSet(s *SortedSet, marshal types.ItemMarshal, unmarshal types.ItemUnmarshal) *MSortedSet {
return &MSortedSet{
MSorted: *list.NewMSorted(&s.Sorted, marshal, unmarshal),
}
}
func (m *MSortedSet) SortedSet() *SortedSet {
return &SortedSet{*m.MSorted.Sorted()}
}
// SortedSet is a list.Sorted and therefore has all of the methods
// that list.Sorted has. So although they do not show up in the generated
// docs you can just do this:
//
// s := NewSortedSet(10)
// s.Add(types.Int(5))
// s2 = s.Union(FromSlice([]types.Hashable{types.Int(7)}))
// fmt.Println(s2.Has(types.Int(7)))
// fmt.Println(s.Has(types.Int(7)))
//
type SortedSet struct {
list.Sorted
}
func NewSortedSet(initialSize int) *SortedSet {
return &SortedSet{*list.NewSorted(initialSize, false)}
}
func FromSlice(items []types.Hashable) *SortedSet {
s := NewSortedSet(len(items))
for _, item := range items {
err := s.Add(item)
if err != nil {
log.Panic(err)
}
}
return s
}
func SortedFromSet(s types.Set) *SortedSet {
if s == nil || s.Size() == 0 {
return NewSortedSet(0)
}
n := NewSortedSet(s.Size())
for i, next := s.Items()(); next != nil; i, next = next() {
n.Add(i)
}
return n
}
func (s *SortedSet) Copy() *SortedSet {
return &SortedSet{*s.Sorted.Copy()}
}
func (s *SortedSet) Random() (item types.Hashable, err error) {
if s.Size() <= 0 {
return nil, errors.Errorf("Set is empty")
} else if s.Size() <= 1 {
return s.Get(0)
}
i := rand.Intn(s.Size())
return s.Get(i)
}
// Unions s with o and returns a new Sorted Set
func (s *SortedSet) Union(other types.Set) (types.Set, error) {
if o, ok := other.(*SortedSet); ok {
return s.union(o)
} else {
return Union(s, other)
}
}
func (s *SortedSet) union(o *SortedSet) (n *SortedSet, err error) {
n = NewSortedSet(s.Size() + o.Size() + 10)
cs, si := s.Items()()
co, oi := o.Items()()
for si != nil || oi != nil {
var err error
if si == nil {
err = n.Add(co)
co, oi = oi()
} else if oi == nil {
err = n.Add(cs)
cs, si = si()
} else if cs.Less(co) {
err = n.Add(cs)
cs, si = si()
} else {
err = n.Add(co)
co, oi = oi()
}
if err != nil {
return nil, err
}
}
return n, nil
}
// Unions s with o and returns a new Sorted Set
func (s *SortedSet) Intersect(other types.Set) (types.Set, error) {
return Intersect(s, other)
}
// Unions s with o and returns a new Sorted Set
func (s *SortedSet) Subtract(other types.Set) (types.Set, error) {
return Subtract(s, other)
}
// Are there any overlapping elements?
func (s *SortedSet) Overlap(o *SortedSet) bool {
cs, si := s.Items()()
co, oi := o.Items()()
for si != nil && oi != nil {
s := cs.(types.Hashable)
o := co.(types.Hashable)
if s.Equals(o) {
return true
} else if s.Less(o) {
cs, si = si()
} else {
co, oi = oi()
}
}
return false
}
// Is s a subset of o?
func (s *SortedSet) Subset(o types.Set) bool {
return Subset(s, o)
}
// Is s a proper subset of o?
func (s *SortedSet) ProperSubset(o types.Set) bool {
return ProperSubset(s, o)
}
// Is s a superset of o?
func (s *SortedSet) Superset(o types.Set) bool {
return Superset(s, o)
}
// Is s a proper superset of o?
func (s *SortedSet) ProperSuperset(o types.Set) bool {
return ProperSuperset(s, o)
}