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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
|
//start of PatriciaTrieSearch.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* PatriciaTrieSearch.java
*
* Copyright (C) 2001-2002 Michel Ishizuka All rights reserved.
*
* ȉ̏ɓӂȂ\[XƃoCi`̍ĔzzƎgp
* ύX̗Lɂ炸B
*
* PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐ێȂĂ͂ȂȂB
*
* QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐gp ̑̔zz
* ܂ގɋLqȂȂȂB
*
* ̃\tgEFA͐Β˔ڂɂĖۏŒA̖
* IBłƂۏAilLƂۏɂƂǂ܂炸A
* Ȃ閾IшÎIȕۏȂB
* Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
* IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
* AƖ̒f〈܂Ăv̈⎸Ai
* T[rX̓l邪AĂꂾɌ肳Ȃ
* Q)ɑāAȂ鎖Ԃ̌ƂȂƂĂA_̐
* C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
* ŝׂ߂łƂĂA܂͂̂悤ȑQ̉\
* ĂƂĂ̐ӔCȂ̂ƂB
*/
package jp.gr.java_conf.dangan.util.lha;
//import classes and interfaces
import java.math.BigInteger;
import jp.gr.java_conf.dangan.io.Bits;
import jp.gr.java_conf.dangan.util.lha.LzssOutputStream;
import jp.gr.java_conf.dangan.util.lha.LzssSearchMethod;
//import exceptions
/**
* PATRICIA Trie gp LzssSearchMethod ̎B
*
* <pre>
* -- revision history --
* $Log: PatriciaTrieSearch.java,v $
* Revision 1.2 2002/12/10 22:28:55 dangan
* [bug fix]
* put( DictionarySize * 2 )
* searchAndPut( DictionarySize * 2 ) ɑΉĂȂ̂CB
*
* Revision 1.1 2002/12/04 00:00:00 dangan
* [change]
* LzssSearchMethod ̃C^tFCXύXɍ킹ăC^tFCXύXB
* [maintenance]
* \[X
*
* Revision 1.0 2002/08/15 00:00:00 dangan
* add to version control
* [bug fix]
* contractNode hashtable ̘AXgɌq̂YĂCB
* z PatriciaTrieSearch.ROOT_NODE(-1) ŃANZXĂ̂CB
* [maintenance]
* \[X
* ^up~
* CZX̏C
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.2 $
*/
public class PatriciaTrieSearch implements LzssSearchMethod{
//------------------------------------------------------------------
// class field
//------------------------------------------------------------------
// private static final int UNUSED
//------------------------------------------------------------------
/**
* gpĂȂlB
* parent[node] UNUSED ꍇ node ͖gp node łB
* prev[node], next[node] UNUSED ꍇ́A
* 瑤ɌZm[hƂB
*/
private static final int UNUSED = 0;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// LZSS parameter
//------------------------------------------------------------------
// private int DictionarySize
// private int MaxMatch
// private int Threshold
//------------------------------------------------------------------
/**
* LZSS TCY
*/
private int DictionarySize;
/**
* LZSS kɎgplB
* ővB
*/
private int MaxMatch;
/**
* LZSS k/k臒lB
* v ̒lȏłAkR[ho͂B
*/
private int Threshold;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// Text Buffer
//------------------------------------------------------------------
// private byte[] TextBuffer
// private int DictionaryLimit
//------------------------------------------------------------------
/**
* LZSS k{߂̃obt@B
* LzssSearchMethod ̎ł͓ǂݍ݂̂B
*/
private byte[] TextBuffer;
/**
* ̌EʒuB
* TextBufferO̎̈ɖɃf[^ꍇ
* ̈ɂs̃f[^(Java ł0)gp
* ksȂ悤ɂB
*/
private int DictionaryLimit;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// PATRICIA TRIE
//------------------------------------------------------------------
// private int[] parent
// private int[] hashTable
// private int[] prev
// private int[] next
// private int[] position
// private int[] level
// private int[] childnum
// private int avail
// private int shift
//------------------------------------------------------------------
/**
* ẽm[hԍB
* parent[node] node ̐em[h̔ԍB
*/
private int[] parent;
/**
* q̃nbVlB
* hashTable[ hash( node, ch ) ]
* node ̕ ch ̎qm[h̃nbVlB
*/
private int[] hashTable;
/**
* hashTable AȂoAXg̈ꕔB
* nbVl Õm[h̃m[hԍB
* prev[ node ] node ƓnbVlA
* AXg node ̈OɈʒum[h node ԍB
* prev[ node ] l̏ꍇ͑Srbg]nbVlB
*/
private int[] prev;
/**
* hashTable AȂoAXg̈ꕔB
* nbVl ̃m[h̃m[hԍB
* next[ node ] node ƓnbVlA
* AXg node ̈Ɉʒum[h node ԍB
*
* ܂AtłȂm[hɊւĂ next avail gpȃm[h
* X^bN(AXg)\B
*
* ɁASvߍ폜ꂽtm[hŁA
* PATRICIA Trie ɑ݂Ătm[hւ̈AXg\B
*/
private int[] next;
/**
* m[h TextBuffer ̃f[^p^̊JnʒuB
* position[ node ] node ̃f[^p^̊JnʒuB
*/
private int[] position;
/**
* m[h ʒuB
* level[ node ] node ̎qm[hʒuB
*/
private int[] level;
/**
* m[h̎qm[h̐B
* childnum[ node ] node ̎qm[h̐B
*/
private int[] childnum;
/**
* next \関gpm[h̃X^bÑX^bN|C^B
*/
private int avail;
/**
* nbVɎgpVtgl
*/
private int shift;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// private int lastMatchPos
// private int lastMatchLen
//------------------------------------------------------------------
/**
* Ō searchAndPut() ܂ put() œꂽ
* ꂽ PatriciaTrie̍Œvʒu
*/
private int lastMatchPos;
/**
* Ō searchAndPut() ܂ put()
* ꂽ PatriciaTrie̍Œv
*/
private int lastMatchLen;
//------------------------------------------------------------------
// constructer
//------------------------------------------------------------------
// private PatriciaTreeSearch()
// public PatriciaTreeSearch( int DictionarySize, int MaxMatch,
// int Threshold, byte[] TextBuffer )
//------------------------------------------------------------------
/**
* ftHgRXgN^B
* gpsB
*/
private PatriciaTrieSearch(){ }
/**
* RXgN^B
* PATRICIA Trie gp@\\zB
*
* @param DictionarySize TCY
* @param MaxMatch Œv
* @param Threshold kAk臒l
* @param TextBuffer LZSSk{߂̃obt@
*/
public PatriciaTrieSearch( int DictionarySize,
int MaxMatch,
int Threshold,
byte[] TextBuffer ){
this.DictionarySize = DictionarySize;
this.MaxMatch = MaxMatch;
this.Threshold = Threshold;
this.TextBuffer = TextBuffer;
this.DictionaryLimit = this.DictionarySize;
this.parent = new int[ this.DictionarySize * 2 ];
this.prev = new int[ this.DictionarySize * 2 ];
this.next = new int[ this.DictionarySize * 2 ];
this.position = new int[ this.DictionarySize ];
this.level = new int[ this.DictionarySize ];
this.childnum = new int[ this.DictionarySize ];
this.hashTable = new int[
PatriciaTrieSearch.generateProbablePrime(
this.DictionarySize + ( this.DictionarySize >> 2 ) ) ];
for( int i = 2 ; i < this.DictionarySize ; i++ ){
this.next[i] = i - 1;
}
this.avail = this.DictionarySize - 1;
for( int i = 0 ; i < this.DictionarySize * 2 ; i++ ){
this.parent[ i ] = PatriciaTrieSearch.UNUSED;
}
for( int i = 0 ; i < this.hashTable.length ; i++ ){
this.hashTable[ i ] = PatriciaTrieSearch.UNUSED;
}
this.shift = Bits.len( this.DictionarySize ) - 8;
this.lastMatchLen = 0;
this.lastMatchPos = 0;
}
//------------------------------------------------------------------
// method of jp.gr.java_conf.dangan.util.lha.LzssSearchMethod
//------------------------------------------------------------------
// public void put( int position )
// public int searchAndPut( int position )
// public int search( int position, int lastPutPos )
// public void slide( int slideWidth, int slideEnd )
// public int putRequires()
//------------------------------------------------------------------
/**
* position n܂f[^p^
* PATRICIA Trie ɓo^B<br>
*
* @param position TextBuffer̃f[^p^̊Jnʒu
*/
public void put( int position ){
//------------------------------------------------------------------
// PATRICIA Trie łÂf[^p^폜
int posnode = ( position & ( this.DictionarySize - 1 ) ) + this.DictionarySize;
this.deleteNode( posnode );
//------------------------------------------------------------------
// PATRICIA Trie Œv
int matchnode = -1;
int matchpos = position;
int scannode;
int matchlen;
if( 3 < this.lastMatchLen ){
//Öv臒l傫A
//t lastMatchLen - 1 ̈vB
scannode = ( this.lastMatchPos + 1 ) | this.DictionarySize;
//Œv߂ scannode
//PATRICIA Trie 菜Ăꍇ̏
while( this.parent[ scannode ] == PatriciaTrieSearch.UNUSED ){
scannode = this.next[ scannode ];
}
//t ԂɐeւƒH
//lastMatchLen - 1 ȉ level m[hTB
int node = this.parent[ scannode ];
this.lastMatchLen--;
while( 0 < node
&& this.lastMatchLen <= this.level[ node ] ){
scannode = node;
node = this.parent[ node ];
}
//ɐeւƒH position XVĂB
while( 0 < node ){
this.position[ node ] = position;
node = this.parent[ node ];
}
matchlen = this.lastMatchLen;
}else{
//PATRICIA Trie HB
scannode = this.child( this.TextBuffer[ position ] - 128,
this.TextBuffer[ position + 1 ] & 0xFF );
matchlen = 2;
if( scannode == PatriciaTrieSearch.UNUSED ){
// position ljB
this.attachNode( this.TextBuffer[ position ] - 128, posnode,
this.TextBuffer[ position + 1 ] & 0xFF );
this.lastMatchLen = matchlen;
return;
}
}
while( true ){
int max;
if( scannode < this.DictionarySize ){
max = this.level[ scannode ];
matchnode = scannode;
matchpos = this.position[ scannode ];
}else{
max = this.MaxMatch;
matchnode = scannode;
matchpos = ( position <= scannode
? scannode - this.DictionarySize
: scannode );
}
while( matchlen < max
&& ( this.TextBuffer[ matchpos + matchlen ]
== this.TextBuffer[ position + matchlen ] ) ){
matchlen++;
}
if( matchlen == max && matchlen < this.MaxMatch ){
this.position[ scannode ] = position;
scannode = this.child( scannode,
this.TextBuffer[ position + matchlen ] & 0xFF );
if( scannode == PatriciaTrieSearch.UNUSED ){
this.attachNode( matchnode, posnode,
this.TextBuffer[ position + matchlen ] & 0xFF );
break;
}else{
matchlen++;
}
}else if( matchlen < max ){
//matchnode position B
this.splitNode( matchnode, matchpos, posnode, position, matchlen );
break;
}else{
//SvAm[huB
this.replaceNode( matchnode, posnode );
this.next[ matchnode ] = position;
break;
}
}
//ʂۑ
this.lastMatchLen = matchlen;
this.lastMatchPos = matchpos;
}
/**
* PATRICIA Trie ɓo^ꂽf[^p^
* position n܂f[^p^
* Œ̈v̂A
* position n܂f[^p^
* PATRICIA Trie ɓo^B<br>
*
* @param position TextBuffer̃f[^p^̊JnʒuB
*
* @return vꍇ
* LzssOutputStream.createSearchReturn
* ɂĐꂽvʒuƈv̏lA
* vȂꍇ
* LzssOutputStream.NOMATCHB
*
* @see LzssOutputStream#createSearchReturn(int,int)
* @see LzssOutputStream#NOMATCH
*/
public int searchAndPut( int position ){
//------------------------------------------------------------------
// PATRICIA Trie łÂf[^p^폜
int posnode = ( position & ( this.DictionarySize - 1 ) ) + this.DictionarySize;
this.deleteNode( posnode );
//------------------------------------------------------------------
// PATRICIA Trie Œv
int matchnode = -1;
int matchpos = position;
int scannode = 0;
int matchlen = 0;
if( 3 < this.lastMatchLen ){
//Öv臒l傫A
//t lastMatchLen - 1 ̈vB
scannode = ( this.lastMatchPos + 1 ) | this.DictionarySize;
//Œv߂ scannode
//PATRICIA Trie 菜Ăꍇ̏
while( this.parent[ scannode ] == PatriciaTrieSearch.UNUSED ){
scannode = this.next[ scannode ];
}
//t ԂɐeւƒH
//lastMatchLen - 1 ȉ level m[hTB
int node = this.parent[ scannode ];
this.lastMatchLen--;
while( 0 < node
&& this.lastMatchLen <= this.level[ node ] ){
scannode = node;
node = this.parent[ node ];
}
//ɐeւƒH position XVĂB
while( 0 < node ){
this.position[ node ] = position;
node = this.parent[ node ];
}
matchlen = this.lastMatchLen;
}else{
//PATRICIA Trie HB
scannode = this.child( this.TextBuffer[ position ] - 128,
this.TextBuffer[ position + 1 ] & 0xFF );
matchlen = 2;
}
// scannode == UNUSED ƂȂ̂ lastMatchLen 臒l菬Ƃ̂݁B
if( scannode != PatriciaTrieSearch.UNUSED ){
while( true ){
int max;
if( scannode < this.DictionarySize ){
max = this.level[ scannode ];
matchnode = scannode;
matchpos = this.position[ scannode ];
}else{
max = this.MaxMatch;
matchnode = scannode;
matchpos = ( position <= scannode
? scannode - this.DictionarySize
: scannode );
}
while( matchlen < max
&& ( this.TextBuffer[ matchpos + matchlen ]
== this.TextBuffer[ position + matchlen ] ) ){
matchlen++;
}
if( matchlen == max && matchlen < this.MaxMatch ){
this.position[ scannode ] = position;
scannode = this.child( scannode,
this.TextBuffer[ position + matchlen ] & 0xFF );
if( scannode == PatriciaTrieSearch.UNUSED ){
//matchnode position ljB
this.attachNode( matchnode, posnode,
this.TextBuffer[ position + matchlen ] & 0xFF );
break;
}else{
matchlen++;
}
}else if( matchlen < max ){
//matchnode position B
this.splitNode( matchnode, matchpos, posnode, position, matchlen );
break;
}else{
//SvAm[huB
this.replaceNode( matchnode, posnode );
this.next[ matchnode ] = position;
break;
}
}
}else{ //if( scannode != PatriciaTrieSearch.UNUSED )
// position ljB
this.attachNode( this.TextBuffer[ position ] - 128, posnode,
this.TextBuffer[ position + 1 ] & 0xFF );
matchlen = 0;
}
//ʂۑ
this.lastMatchLen = matchlen;
this.lastMatchPos = matchpos;
//------------------------------------------------------------------
// \bh擪 PATRICIA Trie 폜f[^p^`FbNB
scannode = position - this.DictionarySize;
if( this.DictionaryLimit <= scannode ){
int len = 0;
while( this.TextBuffer[ scannode + len ]
== this.TextBuffer[ position + len ] )
if( this.MaxMatch <= ++len ) break;
if( matchlen < len ){
matchpos = scannode;
matchlen = len;
}
}
//------------------------------------------------------------------
// ŒvĂяoɕԂB
if( this.Threshold <= matchlen ){
return LzssOutputStream.createSearchReturn( matchlen, matchpos );
}else{
return LzssOutputStream.NOMATCH;
}
}
/**
* PATRICIA Trie ɓo^ꂽf[^p^
* position n܂f[^p^
* Œ̈v̂B<br>
*
* @param position TextBuffer̃f[^p^̊JnʒuB
* @param lastPutPos Ōɓo^f[^p^̊JnʒuB
*
* @return vꍇ
* LzssOutputStream.createSearchReturn
* ɂĐꂽvʒuƈv̏lA
* vȂꍇ
* LzssOutputStream.NOMATCHB
*
* @see LzssOutputStream#createSearchReturn(int,int)
* @see LzssOutputStream#NOMATCH
*/
public int search( int position, int lastPutPos ){
//------------------------------------------------------------------
// PATRICIA Trie ɓo^ĂȂf[^p^
// PȒŌB
int scanlimit = Math.max( this.DictionaryLimit, lastPutPos );
int scanpos = position - 1;
int matchlen = 0;
int matchpos = 0;
byte[] buf = this.TextBuffer;
int max = Math.min( this.TextBuffer.length,
position + this.MaxMatch );
int s = 0;
int p = 0;
int len = 0;
while( scanlimit < scanpos ){
s = scanpos;
p = position;
while( buf[ s ] == buf[ p ] ){
s++;
p++;
if( max <= p ) break;
}
len = p - position;
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
if( max <= p ) break;
}
scanpos--;
}
//------------------------------------------------------------------
// PATRICIA Trie T
if( 2 < this.TextBuffer.length - position ){
int matchnode = this.child( this.TextBuffer[ position ] - 128,
this.TextBuffer[ position + 1 ] & 0xFF );
scanlimit = Math.max( this.DictionaryLimit,
position - this.DictionarySize );
len = 2;
while( matchnode != PatriciaTrieSearch.UNUSED ){
int maxlen;
if( matchnode < this.DictionarySize ){
maxlen = this.level[ matchnode ];
scanpos = this.position[ matchnode ];
}else{
maxlen = this.MaxMatch;
scanpos = ( lastPutPos < matchnode
? matchnode - this.DictionarySize
: matchnode );
}
if( scanlimit <= scanpos ){
max = Math.min( this.TextBuffer.length,
position + maxlen );
s = scanpos + len;
p = position + len;
if( p < max ){
while( buf[ s ] == buf[ p ] ){
s++;
p++;
if( max <= p ) break;
}
}
len = p - position;
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
}
if( len == maxlen && matchlen < this.MaxMatch ){
if( position + len < this.TextBuffer.length ){
matchnode = this.child( matchnode,
this.TextBuffer[ position + len ] & 0xFF );
if( matchnode != PatriciaTrieSearch.UNUSED ){
len++;
}
}else{
break;
}
}else{ //maxlen ɖȂv Sv
break;
}
}else{ //if( scanlimit <= scanpos ) vp^͌EĂB
break;
}
} //while( matchnode != PatriciaTrieSearch.UNUSED )
} //if( 2 <= this.TextBuffer.length - position )
//------------------------------------------------------------------
// ŒvĂяoɕԂB
if( this.Threshold <= matchlen ){
return LzssOutputStream.createSearchReturn( matchlen, matchpos );
}else{
return LzssOutputStream.NOMATCH;
}
}
/**
* TextBufferposition܂ł̃f[^
* OֈړہAɉ LzssSearchMethod
* ̃f[^ TextBuffer̃f[^ƖȂ
* ɑOֈړ鏈sB
*/
public void slide(){
this.DictionaryLimit = Math.max( 0, this.DictionaryLimit - this.DictionarySize );
this.lastMatchPos -= this.DictionarySize;
for( int i = 0 ; i < this.position.length ; i++ ){
int pos = this.position[i] - this.DictionarySize;
if( 0 < pos ){
this.position[i] = pos;
}else{
this.position[i] = 0;
}
}
}
/**
* put() LzssSearchMethodɃf[^
* o^ƂɎgpf[^ʂB
* PatriciaTrieSearch ł́A MaxMatch ԂB
*
* @return MaxMatch
*/
public int putRequires(){
return this.MaxMatch;
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// manipulate node
//------------------------------------------------------------------
// private void splitNode( int oldnode, int oldpos, int position, int splitLen )
// private void deleteNode( int node )
// private void attatchNode( int parentnode, int childnode, int pos )
// private void replaceNode( int oldnode, int newnode )
// private void contractNode( int node )
//------------------------------------------------------------------
/**
* oldnode splitLen ŕB
* oldnode ̂ʒuɂ͐Vm[hV݂A
* Vm[h oldnode position qɎB
*
* @param oldnode m[h
* @param oldpos oldnode wf[^p^̊Jnʒu
* @param posnode position pm[h
* @param position TextBuffer ̃f[^p^̊Jnʒu
* @param splitLen f[^p^̕ʒu
*/
private void splitNode( int oldnode, int oldpos, int posnode, int position, int splitLen ){
//X^bN Vm[h擾B
int newnode = this.avail;
this.avail = this.next[ newnode ];
this.replaceNode( oldnode, newnode );
this.level[ newnode ] = splitLen;
this.position[ newnode ] = position;
this.childnum[ newnode ] = 0;
this.attachNode( newnode, oldnode,
this.TextBuffer[ oldpos + splitLen ] & 0xFF );
this.attachNode( newnode, posnode,
this.TextBuffer[ position + splitLen ] & 0xFF );
}
/**
* PATRICIA Trie tł node 폜B
* Kvł node ̐em[ȟJグsB
*
* @param node 폜tm[h
*/
private void deleteNode( int node ){
if( this.parent[ node ] != PatriciaTrieSearch.UNUSED ){
int parent = this.parent[ node ];
int prev = this.prev[ node ];
int next = this.next[ node ];
this.parent[ node ] = PatriciaTrieSearch.UNUSED;
this.prev[ node ] = PatriciaTrieSearch.UNUSED;
this.next[ node ] = PatriciaTrieSearch.UNUSED;
if( 0 <= prev ){
this.next[ prev ] = next;
}else{
this.hashTable[ ~prev ] = next;
}
this.prev[ next ] = prev;
if( 0 < parent ){ //parent PATRICIA Trie ̍Ŗꍇ true ƂȂ
this.childnum[ parent ]--;
if( this.childnum[ parent ] <= 1 ){
this.contractNode( this.child( parent,
this.TextBuffer[ this.position[ parent ]
+ this.level[ parent ] ]
& 0xFF ) );
}
}
}
}
/**
* parentnode childnode ljB
*
* @param parentnode childnode ljΏۂ̐em[h
* @param childnode parentnode ɒljm[h
* @param pos TextBufferݏʒuB
* t position m肷邽߂ɎgpB
*/
private void attachNode( int parentnode, int childnode, int ch ){
int hash = this.hash( parentnode, ch );
int node = this.hashTable[ hash ];
this.hashTable[ hash ] = childnode;
this.parent[ childnode ] = parentnode;
this.prev[ childnode ] = ~hash;
this.next[ childnode ] = node;
this.prev[ node ] = childnode;
if( 0 < parentnode ){
this.childnum[ parentnode ]++;
}
}
/**
* oldnode newnode ւB
* newnode ͎qm[hƂ̊WێB
* oldnode ͒u PATRICIA Trie 菜B
*
* @param oldnode ւ Trie 폜m[h
* @param newnode oldnode ̂ʒuڑm[h
*/
private void replaceNode( int oldnode, int newnode ){
this.parent[ newnode ] = this.parent[ oldnode ];
this.prev[ newnode ] = this.prev[ oldnode ];
this.next[ newnode ] = this.next[ oldnode ];
this.prev[ this.next[ newnode ] ] = newnode;
if( this.prev[ newnode ] < 0 ){
this.hashTable[ ~this.prev[ newnode ] ] = newnode;
}else{
this.next[ this.prev[ newnode ] ] = newnode;
}
this.parent[ oldnode ] = PatriciaTrieSearch.UNUSED;
this.prev[ oldnode ] = PatriciaTrieSearch.UNUSED;
this.next[ oldnode ] = PatriciaTrieSearch.UNUSED;
}
/**
* Z̖Ȃ node グB
* node ̐em[h PATRICIA Trie 폜A
* node ̈ʒuɐڑB
* Z킪ǂ ͌ĂяosB
*
* @param node グm[h
*/
private void contractNode( int node ){
int parentnode = this.parent[ node ];
this.prev[ this.next[ node ] ] = this.prev[ node ];
if( 0 <= this.prev[ node ] ){
this.next[ this.prev[ node ] ] = this.next[ node ];
}else{
this.hashTable[ ~ this.prev[ node ] ] = this.next[ node ];
}
this.replaceNode( parentnode, node );
//gpȂȂ parentnode X^bNɕԊ҂B
this.next[ parentnode ] = this.avail;
this.avail = parentnode;
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// public void slideTree( int[] src, int[] dst, int width )
// private int child( int parent, int ch )
// private int hash( int node, int ch )
//------------------------------------------------------------------
/**
* slide Trie ̊evfړ鏈sB
*
* @param src ړ
* @param dst ړ
* @param width ړ
*/
private void slideTree( int[] src, int[] dst, int width ){
for( int i = 0 ; i < this.DictionarySize ; i++ )
dst[i] = ( src[ i ] < this.DictionarySize
? src[ i ]
: ( ( src[ i ] - width ) & ( this.DictionarySize - 1 ) )
+ this.DictionarySize );
for( int i = this.DictionarySize ; i < src.length ; i++ )
dst[ ( ( i - width ) & ( this.DictionarySize - 1 ) )
+ this.DictionarySize ] = ( src[ i ] < this.DictionarySize
? src[ i ]
: ( ( src[ i ] - width )
& ( this.DictionarySize - 1 ) )
+ this.DictionarySize );
}
/**
* parent ch ŕqB
* m[hꍇ UNUSED ԂB
*
* @param parent em[h
* @param ch
*
* @return qm[h
*/
private int child( int parent, int ch ){
int node = this.hashTable[ this.hash( parent, ch ) ];
//this.parent[ PatriciaTrieSearch.UNUSED ] = parent;
while( node != PatriciaTrieSearch.UNUSED
&& this.parent[ node ] != parent ){
node = this.next[ node ];
}
return node;
}
/**
* node ch nbVl
*
* @param node m[h
* @param ch
*
* @return nbVl
*/
private int hash( int node, int ch ){
return ( node + ( ch << this.shift ) + 256 ) % this.hashTable.length;
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// generate prime
//------------------------------------------------------------------
// private static int generateProbablePrime( int num )
//------------------------------------------------------------------
/**
* num ȏ̍ł f(͋[f)B
* ߂l fłȂm 1/256 ȉłB
*
* @param num ̒lȏ̑fB
*
* @return ꂽf(͋[f)
*/
private static int generateProbablePrime( int num ){
num = num + ( ( num & 1 ) == 0 ? 1 : 0 );
while( !(new BigInteger(Integer.toString(num))).isProbablePrime( 8 ) ){
num += 2;
num = num + ( ( num % 3 ) == 0 ? 2 : 0 );
num = num + ( ( num % 5 ) == 0 ? 2 : 0 );
num = num + ( ( num % 7 ) == 0 ? 2 : 0 );
}
return num;
}
}
//
//
// //------------------------------------------------------------------
// // local method
// //------------------------------------------------------------------
// // check
// //------------------------------------------------------------------
// // private void checkTrie( int pos )
// // private void checkNode( int node, int pos )
// // private void writeNode( int node )
// //------------------------------------------------------------------
// /**
// * TrieŜ̃`FbNsB
// *
// * @param pos ݏʒuB
// *
// * @exception RuntimeException Trie ĂꍇB
// */
// private void checkTrie( int pos ){
// for( int i = -256 ; i < 0 ; i++ ){
// this.checkNode( i, pos );
// }
//
// for( int i = 1 ; i < this.DictionarySize ; i++ ){
// if( this.parent[ i ] != PatriciaTrieSearch.UNUSED ){
// this.checkNode( i, pos );
// }
// }
// }
//
// /**
// * tłȂ Node ̃`FbNsB
// *
// * `FbNڂ
// * (1) eqW
// * (2) position ɖB
// * (3) level ɖB
// * (4) node this.childnum[node] ̎qĂ鎖B
// * 4ځB
// *
// * @param node `FbNm[h
// * @param pos ݏʒu
// *
// * @exception RuntimeException L̃`FbN̉ꂩsꍇB
// */
// private void checkNode( int node, int pos ){
//
// int nlevel;
// int npos;
// if( node < 0 ){
// nlevel = 0;
// npos = this.TextBuffer.length;
// }else{
// nlevel = this.level[ node ];
// npos = this.position[ node ];
// }
//
// int childcount = 0;
// for( int i = 0 ; i < 256 ; i++ ){
// int child = this.child( node, i );
//
// if( child != PatriciaTrieSearch.UNUSED ){
// childcount++;
//
// if( this.parent[ child ] != node ){
// System.out.println( "unlink::parent<->child" );
// this.writeNode( node );
// this.writeNode( child );
// throw new RuntimeException( "Trie Broken" );
// }
//
// if( child < this.DictionarySize ){
// if( this.level[ child ] <= nlevel ){
// System.out.println( "broken hierarchy::level" );
// this.writeNode( node );
// this.writeNode( child );
// throw new RuntimeException( "Trie Broken" );
// }
//
// if( npos < this.position[ child ] ){
// System.out.println( "broken hierarchy::position" );
// this.writeNode( node );
// this.writeNode( child );
// throw new RuntimeException( "Trie Broken" );
// }
// //this.checkTrie( child, pos );
// }else{
// int childpos = ( pos <= child ? child - this.DictionarySize : child );
// if( npos < childpos ){
// System.out.println( "broken hierarchy::position" );
// this.writeNode( node );
// this.writeNode( child );
// throw new RuntimeException( "Trie Broken" );
// }
// }
// }
// }
//
// if( 0 < node && node < this.DictionarySize ){
// if( this.childnum[ node ] != childcount ){
// System.out.println( "broken hierarchy::childnum" );
// this.writeNode( node );
// throw new RuntimeException( "Trie Broken" );
// }
// }
// }
//
// /**
// * m[h̏o͂B
// *
// * @param node o͂m[h
// */
// private void writeNode( int node ){
// if( 0 < node ){
// System.out.println( "this.parent[" + node + "] ::" + this.parent[ node ] );
// System.out.println( "this.prev[" + node + "] ::" + this.prev[ node ] );
// System.out.println( "this.next[" + node + "] ::" + this.next[ node ] );
// if( node < this.DictionarySize ){
// System.out.println( "this.childnum[" + node + "]::" + this.childnum[ node ] );
// System.out.println( "this.position[" + node + "]::" + this.position[ node ] );
// System.out.println( "this.level[" + node + "] ::" + this.level[ node ] );
// }
// }else if( node < 0 ){
// System.out.println( "ROOT_NODE ::" + node );
// }else{
// System.out.println( "UNUSED ::" + node );
// }
//
// }
//end of PatriciaTrieSearch.java
|