-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorted_set.go
129 lines (108 loc) · 2.68 KB
/
sorted_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
125
126
127
128
129
/*
Copyright 2016 Phil Pennock
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sks_spider
// sorted set of strings:
//go:generate gengen -o ./internal/string_set github.com/joeshaw/gengen/examples/btree string struct{}
import (
"strings"
btree "github.com/philpennock/sks_spider/internal/string_set"
)
type sortedSet struct {
*btree.Tree
}
func newSortedSet() sortedSet {
return sortedSet{
Tree: btree.TreeNew(strings.Compare),
}
}
// eww, code smell XXX
func hostCompare(a, b string) int {
a1 := strings.Split(a, ".")
b1 := strings.Split(b, ".")
ReverseStringSlice(a1)
ReverseStringSlice(b1)
a2 := strings.Join(a1, ".")
b2 := strings.Join(b1, ".")
return strings.Compare(a2, b2)
}
func newHostReversedSet() sortedSet {
return sortedSet{
Tree: btree.TreeNew(hostCompare),
}
}
func (set sortedSet) Insert(key string) {
set.Tree.Set(key, struct{}{})
}
func (set sortedSet) Contains(key string) bool {
_, ok := set.Tree.Seek(key)
return ok
}
func (set sortedSet) Len() int {
return set.Tree.Len()
}
func (set sortedSet) Remove(key string) {
_ = set.Tree.Delete(key)
}
func (set sortedSet) Data() <-chan string {
ch := make(chan string, 100)
go func(c chan<- string) {
enum, err := set.Tree.SeekFirst()
if err != nil {
close(c)
return
}
for {
k, _, err := enum.Next()
if err != nil {
break
}
c <- k
}
close(c)
// enum.Close() -- in github.com/cznic/b docs but not in template
return
}(ch)
return ch
}
func (set sortedSet) AllData() []string {
if set.Tree.Len() == 0 {
return nil
}
dl := make([]string, 0, set.Tree.Len())
enum, err := set.Tree.SeekFirst()
if err != nil {
return dl
}
for {
k, _, err := enum.Next()
if err != nil {
break
}
dl = append(dl, k)
}
// enum.Close() -- in github.com/cznic/b docs but not in template
return dl
}
type collectionOfSortedSets map[string]sortedSet
func newCollectionOfSortedSets(size int) collectionOfSortedSets {
return make(collectionOfSortedSets, size)
}
func (css collectionOfSortedSets) Ensure(key string) {
if _, ok := css[key]; !ok {
css[key] = newSortedSet()
}
}
func (css collectionOfSortedSets) Insert(key, setKey string) {
css.Ensure(key)
css[key].Insert(setKey)
}