-
Notifications
You must be signed in to change notification settings - Fork 416
/
string_table.go
51 lines (42 loc) · 1.2 KB
/
string_table.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
package fastdelta
import (
"hash"
)
type stringTable struct {
// Passing a byte slice to hash.Hash causes it to escape to the heap, so
// we keep around a single Hash to reuse to avoid a new allocation every
// time we add an element to the string table
reuse Hash
h []Hash
hash hash.Hash
}
func newStringTable(h hash.Hash) *stringTable {
return &stringTable{hash: h}
}
func (s *stringTable) Reset() {
s.h = s.h[:0]
}
func (s *stringTable) GetBytes(i int) []byte {
return s.h[i][:]
}
// Contains returns whether i is a valid index for the string table
func (s *stringTable) Contains(i uint64) bool {
return i < uint64(len(s.h))
}
func (s *stringTable) Add(b []byte) {
s.hash.Reset()
s.hash.Write(b)
s.hash.Sum(s.reuse[:0])
s.h = append(s.h, s.reuse)
}
// Equals returns whether the value at index i equals the byte string b
func (s *stringTable) Equals(i int, b []byte) bool {
s.hash.Reset()
s.hash.Write(b)
s.hash.Sum(s.reuse[:0])
return s.reuse == s.h[i]
}