FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-java/zip/TrieKeep.java at master · ccking1981/JSON-java · GitHub
ccking1981
/
JSON-java
Public
forked from
stleary/JSON-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-java
/
zip
/
TrieKeep.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
396 lines (366 loc) · 12.2 KB
Breadcrumbs
JSON-java
/
zip
/
TrieKeep.java
Copy path
File metadata and controls
396 lines (366 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package
org
.
json
.
zip
;
import
org
.
json
.
Kim
;
/*
Copyright (c) 2013 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* A TrieKeep is a Keep that implements a Trie.
*/
class
TrieKeep
extends
Keep
{
/**
* The trie is made of nodes.
*/
class
Node
implements
PostMortem
{
private
int
integer
;
private
Node
[]
next
;
/**
* Each non-leaf node contains links to up to 256 next nodes. Each node
* has an integer value.
*/
public
Node
() {
this
.
integer
=
none
;
this
.
next
=
null
;
}
/**
* Get one of a node's 256 links. If it is a leaf node, it returns
* null.
*
* @param cell
* A integer between 0 and 255.
* @return
*/
public
Node
get
(
int
cell
) {
return
this
.
next
==
null
?
null
:
this
.
next
[
cell
];
}
/**
* Get one of a node's 256 links. If it is a leap node, it returns
* null. The argument is treated as an unsigned integer.
*
* @param cell
* A byte.
* @return
*/
public
Node
get
(
byte
cell
) {
return
get
(((
int
)
cell
) &
0xFF
);
}
/**
* Compare two nodes. Their lengths must be equal. Their links must
* also compare.
*/
public
boolean
postMortem
(
PostMortem
pm
) {
Node
that
= (
Node
)
pm
;
if
(
that
==
null
) {
JSONzip
.
log
(
"
\n
Misalign"
);
return
false
;
}
if
(
this
.
integer
!=
that
.
integer
) {
JSONzip
.
log
(
"
\n
Integer "
+
this
.
integer
+
" <> "
+
that
.
integer
);
return
false
;
}
if
(
this
.
next
==
null
) {
if
(
that
.
next
==
null
) {
return
true
;
}
JSONzip
.
log
(
"
\n
Next is null "
+
this
.
integer
);
return
false
;
}
for
(
int
i
=
0
;
i
<
256
;
i
+=
1
) {
Node
node
=
this
.
next
[
i
];
if
(
node
!=
null
) {
if
(!
node
.
postMortem
(
that
.
next
[
i
])) {
return
false
;
}
}
else
if
(
that
.
next
[
i
] !=
null
) {
JSONzip
.
log
(
"
\n
Misalign "
+
i
);
return
false
;
}
}
return
true
;
}
/**
* Set a node's link to another node.
*
* @param cell
* An integer between 0 and 255.
* @param node
* The new value for the cell.
*/
public
void
set
(
int
cell
,
Node
node
) {
if
(
this
.
next
==
null
) {
this
.
next
=
new
Node
[
256
];
}
if
(
JSONzip
.
probe
) {
if
(
node
==
null
||
this
.
next
[
cell
] !=
null
) {
JSONzip
.
log
(
"
\n
Unexpected set.
\n
"
);
}
}
this
.
next
[
cell
] =
node
;
}
/**
* Set a node's link to another node.
*
* @param cell
* A byte.
* @param node
* The new value for the cell.
*/
public
void
set
(
byte
cell
,
Node
node
) {
set
(((
int
)
cell
) &
0xFF
,
node
);
}
/**
* Get one of a node's 256 links. It will not return null. If there is
* no link, then a link is manufactured.
*
* @param cell
* A integer between 0 and 255.
* @return
*/
public
Node
vet
(
int
cell
) {
Node
node
=
get
(
cell
);
if
(
node
==
null
) {
node
=
new
Node
();
set
(
cell
,
node
);
}
return
node
;
}
/**
* Get one of a node's 256 links. It will not return null. If there is
* no link, then a link is manufactured.
*
* @param cell
* A byte.
* @return
*/
public
Node
vet
(
byte
cell
) {
return
vet
(((
int
)
cell
) &
0xFF
);
}
}
private
int
[]
froms
;
private
int
[]
thrus
;
private
Node
root
;
private
Kim
[]
kims
;
/**
* Create a new Keep of kims.
*
* @param bits
* The log2 of the capacity of the Keep. For example, if bits is
* 12, then the keep's capacity will be 4096.
*/
public
TrieKeep
(
int
bits
) {
super
(
bits
);
this
.
froms
=
new
int
[
this
.
capacity
];
this
.
thrus
=
new
int
[
this
.
capacity
];
this
.
kims
=
new
Kim
[
this
.
capacity
];
this
.
root
=
new
Node
();
}
/**
* Get the kim associated with an integer.
*
* @param integer
* @return
*/
public
Kim
kim
(
int
integer
) {
Kim
kim
=
this
.
kims
[
integer
];
int
from
=
this
.
froms
[
integer
];
int
thru
=
this
.
thrus
[
integer
];
if
(
from
!=
0
||
thru
!=
kim
.
length
) {
kim
=
new
Kim
(
kim
,
from
,
thru
);
this
.
froms
[
integer
] =
0
;
this
.
thrus
[
integer
] =
kim
.
length
;
this
.
kims
[
integer
] =
kim
;
}
return
kim
;
}
/**
* Get the length of the Kim associated with an integer. This is sometimes
* much faster than get(integer).length.
*
* @param integer
* @return
*/
public
int
length
(
int
integer
) {
return
this
.
thrus
[
integer
] -
this
.
froms
[
integer
];
}
/**
* Find the integer value associated with this key, or nothing if this key
* is not in the keep.
*
* @param key
* An object.
* @return An integer
*/
public
int
match
(
Kim
kim
,
int
from
,
int
thru
) {
Node
node
=
this
.
root
;
int
best
=
none
;
for
(
int
at
=
from
;
at
<
thru
;
at
+=
1
) {
node
=
node
.
get
(
kim
.
get
(
at
));
if
(
node
==
null
) {
break
;
}
if
(
node
.
integer
!=
none
) {
best
=
node
.
integer
;
}
from
+=
1
;
}
return
best
;
}
public
boolean
postMortem
(
PostMortem
pm
) {
boolean
result
=
true
;
TrieKeep
that
= (
TrieKeep
)
pm
;
if
(
this
.
length
!=
that
.
length
) {
JSONzip
.
log
(
"
\n
Length "
+
this
.
length
+
" <> "
+
that
.
length
);
return
false
;
}
if
(
this
.
capacity
!=
that
.
capacity
) {
JSONzip
.
log
(
"
\n
Capacity "
+
this
.
capacity
+
" <> "
+
that
.
capacity
);
return
false
;
}
for
(
int
i
=
0
;
i
<
this
.
length
;
i
+=
1
) {
Kim
thiskim
=
this
.
kim
(
i
);
Kim
thatkim
=
that
.
kim
(
i
);
if
(!
thiskim
.
equals
(
thatkim
)) {
JSONzip
.
log
(
"
\n
["
+
i
+
"] "
+
thiskim
+
" <> "
+
thatkim
);
result
=
false
;
}
}
return
result
&&
this
.
root
.
postMortem
(
that
.
root
);
}
public
void
registerMany
(
Kim
kim
) {
int
length
=
kim
.
length
;
int
limit
=
this
.
capacity
-
this
.
length
;
if
(
limit
>
JSONzip
.
substringLimit
) {
limit
=
JSONzip
.
substringLimit
;
}
int
until
=
length
- (
JSONzip
.
minSubstringLength
-
1
);
for
(
int
from
=
0
;
from
<
until
;
from
+=
1
) {
int
len
=
length
-
from
;
if
(
len
>
JSONzip
.
maxSubstringLength
) {
len
=
JSONzip
.
maxSubstringLength
;
}
len
+=
from
;
Node
node
=
this
.
root
;
for
(
int
at
=
from
;
at
<
len
;
at
+=
1
) {
Node
next
=
node
.
vet
(
kim
.
get
(
at
));
if
(
next
.
integer
==
none
&&
at
-
from
>= (
JSONzip
.
minSubstringLength
-
1
)) {
next
.
integer
=
this
.
length
;
this
.
uses
[
this
.
length
] =
1
;
this
.
kims
[
this
.
length
] =
kim
;
this
.
froms
[
this
.
length
] =
from
;
this
.
thrus
[
this
.
length
] =
at
+
1
;
if
(
JSONzip
.
probe
) {
try
{
JSONzip
.
log
(
"<<"
+
this
.
length
+
" "
+
new
Kim
(
kim
,
from
,
at
+
1
) +
">> "
);
}
catch
(
Throwable
ignore
) {
}
}
this
.
length
+=
1
;
limit
-=
1
;
if
(
limit
<=
0
) {
return
;
}
}
node
=
next
;
}
}
}
public
void
registerOne
(
Kim
kim
) {
int
integer
=
registerOne
(
kim
,
0
,
kim
.
length
);
if
(
integer
!=
none
) {
this
.
kims
[
integer
] =
kim
;
}
}
public
int
registerOne
(
Kim
kim
,
int
from
,
int
thru
) {
if
(
this
.
length
<
this
.
capacity
) {
Node
node
=
this
.
root
;
for
(
int
at
=
from
;
at
<
thru
;
at
+=
1
) {
node
=
node
.
vet
(
kim
.
get
(
at
));
}
if
(
node
.
integer
==
none
) {
int
integer
=
this
.
length
;
node
.
integer
=
integer
;
this
.
uses
[
integer
] =
1
;
this
.
kims
[
integer
] =
kim
;
this
.
froms
[
integer
] =
from
;
this
.
thrus
[
integer
] =
thru
;
if
(
JSONzip
.
probe
) {
try
{
JSONzip
.
log
(
"<<"
+
integer
+
" "
+
new
Kim
(
kim
,
from
,
thru
) +
">> "
);
}
catch
(
Throwable
ignore
) {
}
}
this
.
length
+=
1
;
return
integer
;
}
}
return
none
;
}
/**
* Reserve space in the keep, compacting if necessary. A keep may contain
* at most -capacity- elements. The keep contents can be reduced by
* deleting all elements with low use counts, rebuilding the trie with the
* survivors.
*/
public
void
reserve
() {
if
(
this
.
capacity
-
this
.
length
<
JSONzip
.
substringLimit
) {
int
from
=
0
;
int
to
=
0
;
this
.
root
=
new
Node
();
while
(
from
<
this
.
capacity
) {
if
(
this
.
uses
[
from
] >
1
) {
Kim
kim
=
this
.
kims
[
from
];
int
thru
=
this
.
thrus
[
from
];
Node
node
=
this
.
root
;
for
(
int
at
=
this
.
froms
[
from
];
at
<
thru
;
at
+=
1
) {
Node
next
=
node
.
vet
(
kim
.
get
(
at
));
node
=
next
;
}
node
.
integer
=
to
;
this
.
uses
[
to
] =
age
(
this
.
uses
[
from
]);
this
.
froms
[
to
] =
this
.
froms
[
from
];
this
.
thrus
[
to
] =
thru
;
this
.
kims
[
to
] =
kim
;
to
+=
1
;
}
from
+=
1
;
}
// It is possible, but highly unlikely, that too many items survive.
// If that happens, clear the keep.
if
(
this
.
capacity
-
to
<
JSONzip
.
substringLimit
) {
this
.
power
=
0
;
this
.
root
=
new
Node
();
to
=
0
;
}
this
.
length
=
to
;
while
(
to
<
this
.
capacity
) {
this
.
uses
[
to
] =
0
;
this
.
kims
[
to
] =
null
;
this
.
froms
[
to
] =
0
;
this
.
thrus
[
to
] =
0
;
to
+=
1
;
}
}
}
public
Object
value
(
int
integer
) {
return
kim
(
integer
);
}
}
Back
|
FazBrowse Home
|
New Git URL