/
vecstring.go
177 lines (145 loc) · 4.44 KB
/
vecstring.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// package vecstring provides a space-efficient representation of a vector of strings of variable length.
package vecstring
import (
"github.com/hillbig/partialsum"
"github.com/ugorji/go/codec"
)
// VecString represents a vector V[0...Num), and each value V[i] represents a string
// Internally, VecString are stored in space-efficient way.
// Strings are concatenated in a byte array, and each offset, length information is
// encoded in unary coding, and represented by rsdic to achieve further space reduction. // For strings of total lengths = TotalLen, VecString stores them in
// at most TotalLen + Num * (2 + log_2 TotalLen / Num) / 8 bytes.
type VecString interface {
// Get returns V[ind]
Get(ind uint64) string
// Find returns true if c is found in V[ind], and returns false otherwise.
Find(ind uint64, c byte) bool
// Num returns the number of strings
Num() uint64
// TotalLen returns the total length of strings
TotalLen() uint64
// ExactMatch returns true if V[ind] == str, and returns false otherwise
ExactMatch(ind uint64, str string) bool
// PrefixMatch returns true if V[ind] == Prefix of str, and returns false otherwise
PrefixMatch(ind uint64, str string) (uint64, bool)
// LenAndOffset returns the length of V[ind] and len(V[0]+...+len(V[ind-1])
LenAndOffset(ind uint64) (uint64, uint64)
// MarshalBinary encodes VecString into a binary form and returns the result.
MarshalBinary() ([]byte, error)
// UnmarshalBinary decodes the FixVec form a binary from generated MarshalBinary
UnmarshalBinary([]byte) error
// PushBack set V[Num] = v, and Num++
PushBack(v string)
}
// VecStringForWX represents a vector of strings, and provides extended interface
// for library WX.
type VecStringForWX interface {
VecString
// GetByte returns the (offset+1)-th byte in the concatenated strings.
GetByte(offset uint64) byte
// FindZeroRank finds c in V[ind], and returns (offset, true) if found,
// and returns (0, false) otherwise.
FindZeroRank(ind uint64, c byte) (uint64, bool)
// IthCharInd returns the index corresponding to i-th child.
// lens.Rank(lens.Select(i-1, false), true) - 1
IthCharInd(i uint64) uint64
}
// New returns VecString with 0 strings.
func New() VecString {
return &vecStringImpl{
ps: partialsum.New(),
bytes: make([]byte, 0),
}
}
// New returns VecStringForWX with 0 strings.
func NewForWX() VecStringForWX {
return &vecStringImpl{
ps: partialsum.New(),
bytes: make([]byte, 0),
}
}
type vecStringImpl struct {
ps partialsum.PartialSum
bytes []byte
}
func (vv vecStringImpl) LenAndOffset(ind uint64) (uint64, uint64) {
return vv.ps.LookupAndSum(ind)
}
func (vv vecStringImpl) Get(ind uint64) string {
l, beg := vv.LenAndOffset(ind)
return string(vv.bytes[beg : beg+l])
}
func (vv vecStringImpl) GetByte(offset uint64) byte {
return vv.bytes[offset]
}
func (vv vecStringImpl) Num() uint64 {
return vv.ps.Num()
}
func (vv *vecStringImpl) TotalLen() uint64 {
return vv.ps.AllSum()
}
func (vv vecStringImpl) IthCharInd(i uint64) uint64 {
ind, _ := vv.ps.Find(i)
return ind
}
func (vv vecStringImpl) Find(ind uint64, c byte) bool {
l, beg := vv.LenAndOffset(ind)
for i := uint64(0); i < l; i++ {
if c == vv.bytes[beg+i] {
return true
}
}
return false
}
func (vv vecStringImpl) FindZeroRank(ind uint64, c byte) (uint64, bool) {
l, beg := vv.LenAndOffset(ind)
for i := uint64(0); i < l; i++ {
if c == vv.bytes[beg+i] {
return beg + i, true
}
}
return 0, false
}
func (vv vecStringImpl) ExactMatch(ind uint64, str string) bool {
l, beg := vv.LenAndOffset(ind)
return string(vv.bytes[beg:beg+l]) == str
}
func (vv vecStringImpl) PrefixMatch(ind uint64, str string) (uint64, bool) {
l, beg := vv.LenAndOffset(ind)
for i := uint64(0); i < l; i++ {
if int(i) >= len(str) || vv.bytes[beg+i] != str[i] {
return i, false
}
}
return l, true
}
func (vb *vecStringImpl) PushBack(str string) {
vb.ps.IncTail(vb.ps.Num(), uint64(len(str)))
vb.bytes = append(vb.bytes, []byte(str)...)
}
func (vs vecStringImpl) MarshalBinary() (out []byte, err error) {
var bh codec.MsgpackHandle
enc := codec.NewEncoderBytes(&out, &bh)
err = enc.Encode(vs.ps)
if err != nil {
return
}
err = enc.Encode(vs.bytes)
if err != nil {
return
}
return
}
func (vs *vecStringImpl) UnmarshalBinary(in []byte) (err error) {
var bh codec.MsgpackHandle
dec := codec.NewDecoderBytes(in, &bh)
err = dec.Decode(&vs.ps)
if err != nil {
return
}
err = dec.Decode(&vs.bytes)
if err != nil {
return
}
return nil
}