-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.go
50 lines (40 loc) · 941 Bytes
/
sketch.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
package datasketch
import (
"fmt"
)
func NotImplementError(msg string) error {
return fmt.Errorf("method not implemented %s", msg)
}
func NoCompatibleTypeError(t1, t2 string, op string) error {
return fmt.Errorf("cannot call %s on %s of %s", op, t1, t2)
}
type Sketch interface {
Add(string)
Uniques() float64
Union(Sketch) (Sketch, error)
Sub(Sketch) (Sketch, error)
Intersection(Sketch) (Sketch, error)
Bytes() []byte
String() string
}
type SketchFactory interface {
NewSketch() Sketch
}
func Union(sketch, other Sketch) (float64, error) {
newSk, err := sketch.Union(other)
if err != nil {
return 0, err
}
return newSk.Uniques(), nil
}
func Intersect(sketch, other Sketch) (float64, error) {
newSk, err := sketch.Intersection(other)
if err == nil {
return newSk.Uniques(), nil
}
unions, err := Union(sketch, other)
if err != nil {
return 0, err
}
return sketch.Uniques()+other.Uniques()-unions, nil
}