-
Notifications
You must be signed in to change notification settings - Fork 2
/
preflate_predictor_state.h
176 lines (153 loc) · 5.1 KB
/
preflate_predictor_state.h
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
/* Copyright 2018 Dirk Steinke
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#ifndef PREFLATE_PREDICTOR_STATE_H
#define PREFLATE_PREDICTOR_STATE_H
#include <vector>
#include "preflate_input.h"
#include "preflate_hash_chain.h"
#include "preflate_parser_config.h"
#include "preflate_seq_chain.h"
#include "preflate_token.h"
struct PreflatePreviousMatchInfo {
PreflateToken previousMatches[256];
};
struct PreflateNextMatchInfo {
unsigned short nextChainDepth;
unsigned short nextLen;
unsigned short nextDist;
};
struct PreflateRematchInfo {
unsigned short firstMatchDepth;
unsigned short firstMatchDist;
unsigned short requestedMatchDepth;
unsigned short condensedHops;
};
struct PreflatePredictorState {
const PreflateHashChainExt& hash;
const PreflateSeqChain& seq;
unsigned short windowBytes;
unsigned maxTokenCount;
const PreflateParserConfig& config;
PreflatePredictorState(const PreflateHashChainExt&,
const PreflateSeqChain&,
const PreflateParserConfig&,
const int wbits,
const int mbits);
unsigned currentInputPos() const {
return hash.input().pos();
}
const unsigned char* inputCursor() const {
return hash.input().curChars();
}
unsigned windowSize() const {
return windowBytes;
}
unsigned totalInputSize() const {
return hash.input().size();
}
unsigned availableInputSize() const {
return hash.input().remaining();
}
unsigned maxChainLength() const {
return config.max_chain;
}
unsigned niceMatchLength() const {
return config.nice_length;
}
unsigned goodMatchLength() const {
return config.good_length;
}
unsigned lazyMatchLength() const {
return config.max_lazy;
}
unsigned calculateHash() const {
return hash.curHash();
}
unsigned calculateHashNext() const {
return hash.curPlus1Hash();
}
unsigned getCurrentHashHead(const unsigned hashNext) const {
return hash.getHead(hashNext);
}
PreflateHashIterator iterateFromHead(const unsigned hash_, const unsigned refPos, const unsigned maxDist) const {
return hash.iterateFromHead(hash_, refPos, maxDist);
}
PreflateHashIterator iterateFromNode(const unsigned node_, const unsigned refPos, const unsigned maxDist) const {
return hash.iterateFromNode(node_, refPos, maxDist);
}
PreflateHashIterator iterateFromDist(const unsigned dist_, const unsigned refPos, const unsigned maxDist) const {
return hash.iterateFromPos(refPos - dist_, refPos, maxDist);
}
static unsigned prefixCompare(
const unsigned char* s1,
const unsigned char* s2,
const unsigned bestLen,
const unsigned maxLen);
static unsigned suffixCompare(
const unsigned char* s1,
const unsigned char* s2,
const unsigned bestLen,
const unsigned maxLen);
bool betterMatchPossible(
const unsigned prevLen,
const unsigned startPos);
PreflateToken matchHop0MaxDist(
const unsigned hashHead,
const unsigned prevLen,
const unsigned offset,
const bool veryFarMatches,
const bool matchesToStart);
PreflateToken match(
const unsigned hashHead,
const unsigned prevLen,
const unsigned offset,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth);
PreflateToken seqMatch(
const unsigned startPos,
const unsigned hashHead,
const unsigned prevLen,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth);
/*
unsigned short matchDepth(const unsigned hashHead, const PreflateToken& targetReference,
const PreflateHashChainExt&);*/
PreflateNextMatchInfo nextMatchInfo(const unsigned hashHead, const PreflateToken& targetReference,
const PreflateHashChainExt&);
PreflateRematchInfo rematchInfo(const unsigned hashHead, const PreflateToken& targetReference);
unsigned firstMatch(const unsigned len);
unsigned hopMatch(const PreflateToken& token, const unsigned hops);
private:
struct MatchHelper {
unsigned startPos;
unsigned maxLen;
unsigned curMaxDistHop0;
unsigned curMaxDistHop1Plus;
unsigned maxChain;
unsigned niceLen;
bool validHop0Dist(const unsigned d) const {
return d <= curMaxDistHop0;
}
bool validHop1PlusDist(const unsigned d) const {
return d <= curMaxDistHop1Plus;
}
};
bool createMatchHelper(
MatchHelper& helper,
const unsigned prevLen,
const unsigned startPos,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth);
};
#endif /* PREFLATE_PREDICTOR_STATE_H */