-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.go
More file actions
42 lines (36 loc) · 1.03 KB
/
element.go
File metadata and controls
42 lines (36 loc) · 1.03 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
package diffx
import "hash/fnv"
// Element represents a comparable unit (line, word, token).
// Implementations must provide equality comparison and hashing.
type Element interface {
// Equal reports whether this element is equal to another.
Equal(other Element) bool
// Hash returns a hash value for this element.
// Equal elements must have equal hashes.
Hash() uint64
}
// StringElement is the common case for line/word comparison.
type StringElement string
// Equal reports whether s equals other.
// Returns false if other is not a StringElement.
func (s StringElement) Equal(other Element) bool {
o, ok := other.(StringElement)
if !ok {
return false
}
return s == o
}
// Hash returns a FNV-1a hash of the string.
func (s StringElement) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte(s))
return h.Sum64()
}
// toElements converts a slice of strings to a slice of Elements.
func toElements(strs []string) []Element {
elems := make([]Element, len(strs))
for i, s := range strs {
elems[i] = StringElement(s)
}
return elems
}