-
Notifications
You must be signed in to change notification settings - Fork 49
/
skiplist_set.go
124 lines (101 loc) · 3.24 KB
/
skiplist_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
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
package stl4go
// SkipListSet is a SortedSet implemented with skiplist.
type SkipListSet[K any] SkipList[K, struct{}]
// NewSkipListSet creates a new SkipListSet object.
func NewSkipListSet[K Ordered]() *SkipListSet[K] {
return (*SkipListSet[K])(NewSkipList[K, struct{}]())
}
// NewSkipListSetFunc creates a new SkipListSet object with specified compare function.
func NewSkipListSetFunc[K any](cmp CompareFn[K]) *SkipListSet[K] {
return (*SkipListSet[K])(NewSkipListFunc[K, struct{}](cmp))
}
// NewSkipListSetOf creates a new SkipListSet object with specified elements.
func NewSkipListSetOf[K Ordered](elements ...K) *SkipListSet[K] {
s := NewSkipListSet[K]()
for i := range elements {
s.Insert(elements[i])
}
return s
}
// IsEmpty implements the SortedSet interface.
func (s *SkipListSet[K]) IsEmpty() bool {
return s.asMap().IsEmpty()
}
// Len implements the SortedSet interface.
func (s *SkipListSet[K]) Len() int {
return s.asMap().Len()
}
// Clear implements the SortedSet interface.
func (s *SkipListSet[K]) Clear() {
s.asMap().Clear()
}
// Has implements the SortedSet interface.
func (s *SkipListSet[K]) Has(key K) bool {
return s.asMap().Has(key)
}
// Insert implements the SortedSet interface.
func (s *SkipListSet[K]) Insert(key K) bool {
oldLen := s.Len()
s.asMap().Insert(key, struct{}{})
return s.Len() > oldLen
}
// InsertN implements the SortedSet interface.
func (s *SkipListSet[K]) InsertN(keys ...K) int {
oldLen := s.Len()
for i := range keys {
s.Insert(keys[i])
}
return s.Len() - oldLen
}
// Remove implements the SortedSet interface.
func (s *SkipListSet[K]) Remove(key K) bool {
return s.asMap().Remove(key)
}
// RemoveN implements the SortedSet interface.
func (s *SkipListSet[K]) RemoveN(keys ...K) int {
oldLen := s.Len()
for i := range keys {
s.Remove(keys[i])
}
return oldLen - s.Len()
}
// Keys return a copy of sorted keys as slice.
func (s *SkipListSet[K]) Keys() []K {
keys := make([]K, 0, s.Len())
s.ForEach(func(k K) { keys = append(keys, k) })
return keys
}
// ForEach implements the SortedSet interface.
func (s *SkipListSet[K]) ForEach(f func(K)) {
s.asMap().ForEach(func(k K, s struct{}) { f(k) })
}
// ForEachIf implements the SortedSet interface.
func (s *SkipListSet[K]) ForEachIf(f func(K) bool) {
s.asMap().ForEachIf(func(k K, s struct{}) bool { return f(k) })
}
// LowerBound implements the SortedSet interface.
func (s *SkipListSet[K]) LowerBound(key K) Iterator[K] {
return &skipListSetIterator[K]{s.asMap().impl.lowerBound(key), nil}
}
// UpperBound implements the SortedSet interface.
func (s *SkipListSet[K]) UpperBound(key K) Iterator[K] {
return &skipListSetIterator[K]{s.asMap().impl.upperBound(key), nil}
}
// FindRange implements the SortedSet interface.
func (s *SkipListSet[K]) FindRange(first, last K) Iterator[K] {
m := s.asMap().impl
return &skipListSetIterator[K]{m.lowerBound(first), m.upperBound(last)}
}
func (s *SkipListSet[K]) asMap() *SkipList[K, struct{}] {
return (*SkipList[K, struct{}])(s)
}
type skipListSetIterator[K any] skipListIterator[K, struct{}]
func (it *skipListSetIterator[K]) IsNotEnd() bool {
return it.node != it.end
}
func (it *skipListSetIterator[K]) MoveToNext() {
it.node = it.node.next[0]
}
func (it *skipListSetIterator[K]) Value() K {
return it.node.key
}