/
sequence.go
91 lines (76 loc) · 2.18 KB
/
sequence.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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package types
import (
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/hash"
)
type sequenceItem interface{}
type compareFn func(x int, y int) bool
type sequence interface {
asValueImpl() valueImpl
cumulativeNumberOfLeaves(idx int) uint64
Empty() bool
Equals(other Value) bool
getChildSequence(idx int) sequence
getCompareFn(other sequence) compareFn
getCompositeChildSequence(start uint64, length uint64) sequence
getItem(idx int) sequenceItem
Hash() hash.Hash
isLeaf() bool
Kind() NomsKind
Len() uint64
Less(other Value) bool
numLeaves() uint64
seqLen() int
treeLevel() uint64
typeOf() *Type
valueBytes() []byte
valueReadWriter() ValueReadWriter
valuesSlice(from, to uint64) []Value
WalkRefs(cb RefCallback)
writeTo(nomsWriter)
}
const (
sequencePartKind = 0
sequencePartLevel = 1
sequencePartCount = 2
sequencePartValues = 3
)
type sequenceImpl struct {
valueImpl
len uint64
}
func newSequenceImpl(vrw ValueReadWriter, buff []byte, offsets []uint32, len uint64) sequenceImpl {
return sequenceImpl{valueImpl{vrw, buff, offsets}, len}
}
func (seq sequenceImpl) decoderSkipToValues() (valueDecoder, uint64) {
dec := seq.decoderAtPart(sequencePartCount)
count := dec.readCount()
return dec, count
}
func (seq sequenceImpl) decoderAtPart(part uint32) valueDecoder {
offset := seq.offsets[part] - seq.offsets[sequencePartKind]
return newValueDecoder(seq.buff[offset:], seq.vrw)
}
func (seq sequenceImpl) Empty() bool {
return seq.Len() == 0
}
func (seq sequenceImpl) Len() uint64 {
return seq.len
}
func (seq sequenceImpl) seqLen() int {
_, count := seq.decoderSkipToValues()
return int(count)
}
func (seq sequenceImpl) getItemOffset(idx int) int {
// kind, level, count, elements...
// 0 1 2 3 n+1
d.PanicIfTrue(idx+sequencePartValues+1 > len(seq.offsets))
return int(seq.offsets[idx+sequencePartValues] - seq.offsets[sequencePartKind])
}
func (seq sequenceImpl) decoderSkipToIndex(idx int) valueDecoder {
offset := seq.getItemOffset(idx)
return seq.decoderAtOffset(offset)
}