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
|
//start of HashAndBinaryTreeSearch.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* HashAndBinaryTreeSearch.java
*
* Copyright (C) 2002 Michel Ishizuka All rights reserved.
*
* ȉ̏ɓӂȂ\[XƃoCi`̍ĔzzƎgp
* ύX̗Lɂ炸B
*
* ȉ̏ɓӂȂ\[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 jp.gr.java_conf.dangan.lang.reflect.Factory;
import jp.gr.java_conf.dangan.util.lha.HashShort;
import jp.gr.java_conf.dangan.util.lha.HashMethod;
import jp.gr.java_conf.dangan.util.lha.LzssOutputStream;
import jp.gr.java_conf.dangan.util.lha.LzssSearchMethod;
//import exceptions
import java.io.IOException;
import java.lang.NoSuchMethodException;
import java.lang.ClassNotFoundException;
import java.lang.InstantiationException;
import java.lang.reflect.InvocationTargetException;
import java.lang.Error;
import java.lang.NoSuchMethodError;
import java.lang.InstantiationError;
import java.lang.NoClassDefFoundError;
/**
* nbVƓg LzssSearchMethod ̎B<br>
* <pre>
* f[^knhubN[]
* M.l\/J.-L.QB[
* uERp
* ISBN4-8101-8605-9
* 5728~(Ŕ,̍w̉i)
* </pre>
* QlɂB<br>
* ł́AŒv邱Ƃ͂ł邪A
* ł߂vƂ͌Ȃ߁A
* LZSS vʒu߂ꏊɕ鎖
* p悤 -lh5- ̂悤Ȉk@ł́A
* k͂炩ቺB
*
* <pre>
* -- revision history --
* $Log: HashAndBinaryTreeSearch.java,v $
* Revision 1.0 2002/08/05 00:00:00 dangan
* add to version control
* [change]
* LzssSearchMethod ̃C^tFCXύXɂ킹ăC^tFCXύX
* [maintenance]
* \[X
* ^up~
* CZX̏C
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.0 $
*/
public class HashAndBinaryTreeSearch implements LzssSearchMethod{
//------------------------------------------------------------------
// class field
//------------------------------------------------------------------
// private static final int UNUSED
// private static final int ROOT_NODE
//------------------------------------------------------------------
/**
* gpĂȂlB
* parent[node] UNUSED ꍇ node ͖gpnodełB
* small[node], large[node] UNUSED ꍇ
* node 瑤̎qm[hȂB
*/
private static final int UNUSED = -1;
/**
* ̍lB
* parent[node] ROOT_NODE ꍇ node ͓̍łB
*/
private static final int ROOT_NODE = -2;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// LZSS parameter
//------------------------------------------------------------------
// private int DictionarySize
// private int MaxMatch
// private int Threshold
//------------------------------------------------------------------
/**
* LZSSTCYB
*/
private int DictionarySize;
/**
* LZSSkɎgplB
* ővB
*/
private int MaxMatch;
/**
* LZSSkɎgp臒lB
* v ̒lȏłAkR[ho͂B
*/
private int Threshold;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// text buffer
//------------------------------------------------------------------
// private byte[] TextBuffer
// private int DictionaryLimit
//------------------------------------------------------------------
/**
* LZSSk{߂̃obt@B
* Ö͎A
* 㔼͈k{߂̃f[^̓obt@B
* LzssSearchMethod̎ł͓ǂݍ݂̂B
*/
private byte[] TextBuffer;
/**
* ̌EʒuB
* TextBufferO̎̈Ƀf[^ꍇ
* ̈ɂs̃f[^(Javał0)gp
* Ĉkŝ}~B
*/
private int DictionaryLimit;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// hash
//------------------------------------------------------------------
// private HashMethod hashMethod
// private int[] hashTable
//------------------------------------------------------------------
/**
* nbV
*/
private HashMethod hashMethod;
/**
* nbVe[u
* Y̓nbVlAe͌X̃nbVl
* ̍̃f[^p^̊JnʒuB
*/
private int[] hashTable;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// binary tree
//------------------------------------------------------------------
// private int[] parent
// private int[] small
// private int[] large
// private int[] dummy
//------------------------------------------------------------------
/**
* ẽf[^p^̊JnʒuB
* Y̓m[hԍAe͐em[h̃f[^p^̊Jnʒu
*/
private int[] parent;
/**
* q̃f[^p^̊JnʒuB
* Y̓m[hԍAe͏qm[hf[^p^̊Jnʒu
*/
private int[] small;
/**
* 傫q̃f[^p^̊JnʒuB
* Y̓m[hԍAe͑傫qm[hf[^p^̊Jnʒu
*/
private int[] large;
/**
* slide p̃obt@
*/
private int[] dummy;
//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
// private HashAndBinaryTreeSearch()
// public HashAndBinaryTreeSearch( int DictionarySize, int MaxMatch,
// int Threshold, byte[] TextBuffer )
// public HashAndBinaryTreeSearch( int DictionarySize, int MaxMatch,
// int Threshold, byte[] TextBuffer,
// String HashMethodClassName )
//------------------------------------------------------------------
/**
* ftHgRXgN^B
* gps
*/
private HashAndBinaryTreeSearch(){ }
/**
* nbVƓgp@\\zB<br>
* nbV̓ftHĝ̂gpB
*
* @param DictionarySize TCY
* @param MaxMatch Œv
* @param Threshold kAk臒l
* @param TextBuffer LZSSk{߂̃obt@
*/
public HashAndBinaryTreeSearch( int DictionarySize,
int MaxMatch,
int Threshold,
byte[] TextBuffer ){
this( DictionarySize,
MaxMatch,
Threshold,
TextBuffer,
HashShort.class.getName() );
}
/**
* nbVƓgp LzssSearchMethod \zB
*
* @param DictionarySize TCY
* @param MaxMatch Œv
* @param Threshold kAk臒l
* @param TextBuffer LZSSk{߂̃obt@
* @param HashMethodClassName HashNX
*
* @exception NoClassDefFoundError
* HashMethodClassName ŗ^ꂽNX
* ȂꍇB
* @exception InstantiationError
* HashMethodClassName ŗ^ꂽNX
* abstract class ł邽߃CX^XłȂꍇB
* @exception NoSuchMethodError
* HashMethodClassName ŗ^ꂽNX
* RXgN^ HashMethod( byte[] )
* Ȃꍇ
*/
public HashAndBinaryTreeSearch( int DictionarySize,
int MaxMatch,
int Threshold,
byte[] TextBuffer,
String HashMethodClassName ){
this.DictionarySize = DictionarySize;
this.MaxMatch = MaxMatch;
this.Threshold = Threshold;
this.TextBuffer = TextBuffer;
this.DictionaryLimit = this.DictionarySize;
try{
this.hashMethod = (HashMethod)Factory.createInstance(
HashMethodClassName,
new Object[]{ TextBuffer } );
}catch( ClassNotFoundException exception ){
throw new NoClassDefFoundError( exception.getMessage() );
}catch( InvocationTargetException exception ){
throw new Error( exception.getTargetException().getMessage() );
}catch( NoSuchMethodException exception ){
throw new NoSuchMethodError( exception.getMessage() );
}catch( InstantiationException exception ){
throw new InstantiationError( exception.getMessage() );
}
// nbVe[ȕ
this.hashTable = new int[ this.hashMethod.tableSize() ];
for( int i = 0 ; i < this.hashTable.length ; i++ ){
this.hashTable[i] = HashAndBinaryTreeSearch.UNUSED;
}
// ̏
this.parent = new int[ DictionarySize ];
this.large = new int[ DictionarySize ];
this.small = new int[ DictionarySize ];
for( int i = 0 ; i < this.parent.length ; i++ ){
this.parent[i] = HashAndBinaryTreeSearch.UNUSED;
}
}
//------------------------------------------------------------------
// 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^
* nbVƓgp@\ɓo^B<br>
*
* @param position TextBuffer̃f[^p^̊Jnʒu
*/
public void put( int position ){
//------------------------------------------------------------------
// łÂf[^p^폜
this.deleteNode( position - this.DictionarySize );
//------------------------------------------------------------------
// position }ʒu
int hash = this.hashMethod.hash( position );
int parentpos = this.hashTable[ hash ];
int scanpos = this.hashTable[ hash ];
byte[] buf = this.TextBuffer;
int max = position + this.MaxMatch;
int p = 0;
int s = 0;
while( scanpos != HashAndBinaryTreeSearch.UNUSED ){
s = scanpos;
p = position;
while( buf[ s ] == buf[ p ] ){
s++;
p++;
if( max <= p ){
//Sv
this.replaceNode( scanpos, position );
return;
}
}
parentpos = scanpos;
scanpos = ( buf[ s ] < buf[ p ]
? this.large[ scanpos & ( this.DictionarySize - 1 ) ]
: this.small[ scanpos & ( this.DictionarySize - 1 ) ] );
}
//------------------------------------------------------------------
// position n܂f[^p^ ɓo^
if( this.hashTable[ hash ] != HashAndBinaryTreeSearch.UNUSED ){
this.addNode( parentpos, position, p - position );
}else{
this.hashTable[ hash ] = position;
int node = position & ( this.DictionarySize - 1 );
this.parent[ node ] = HashAndBinaryTreeSearch.ROOT_NODE;
this.small[ node ] = HashAndBinaryTreeSearch.UNUSED;
this.large[ node ] = HashAndBinaryTreeSearch.UNUSED;
}
}
/**
* nbVƓgp@\ɓo^ꂽ
* f[^p^ position n܂f[^p^
* Œ̈v̂A
* position n܂f[^p^
* nbVƓgp@\ɓ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 ){
//------------------------------------------------------------------
// łÂf[^p^폜
this.deleteNode( position - this.DictionarySize );
//------------------------------------------------------------------
// Œv
int hash = this.hashMethod.hash( position );
int matchlen = -1;
int matchpos = this.hashTable[ hash ];
int parentpos = this.hashTable[ hash ];
int scanpos = this.hashTable[ hash ];
byte[] buf = this.TextBuffer;
int max = position + this.MaxMatch;
int p = 0;
int s = 0;
int len = 0;
while( scanpos != HashAndBinaryTreeSearch.UNUSED ){
s = scanpos;
p = position;
while( buf[ s ] == buf[ p ] ){
s++;
p++;
if( max <= p ){
//Sv
this.replaceNode( matchpos, position );
return LzssOutputStream.createSearchReturn( matchlen, matchpos );
}
}
len = p - position;
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
}else if( matchlen == len && matchpos < scanpos ){
matchpos = scanpos;
}
parentpos = scanpos;
scanpos = ( buf[ s ] < buf[ p ]
? this.large[ scanpos & ( this.DictionarySize - 1 ) ]
: this.small[ scanpos & ( this.DictionarySize - 1 ) ] );
}
//------------------------------------------------------------------
// position n܂f[^p^ ɓo^
if( this.hashTable[ hash ] != HashAndBinaryTreeSearch.UNUSED ){
this.addNode( parentpos, position, len );
}else{
this.hashTable[ hash ] = position;
int node = position & ( this.DictionarySize - 1 );
this.parent[ node ] = HashAndBinaryTreeSearch.ROOT_NODE;
this.small[ node ] = HashAndBinaryTreeSearch.UNUSED;
this.large[ node ] = HashAndBinaryTreeSearch.UNUSED;
}
//------------------------------------------------------------------
// \bh̐擪ō폜ꂽ
// łf[^p^Ɣr
scanpos = position - this.DictionarySize;
if( this.DictionaryLimit <= scanpos ){
len = 0;
while( this.TextBuffer[ scanpos + len ]
== this.TextBuffer[ position + len ] ){
if( this.MaxMatch <= ++len ) break;
}
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
}
}
//------------------------------------------------------------------
// ŒvĂяoɕԂB
if( this.Threshold <= matchlen ){
return LzssOutputStream.createSearchReturn( matchlen, matchpos );
}else{
return LzssOutputStream.NOMATCH;
}
}
/**
* nbVƓgp@\ɓo^ꂽf[^p^
* position n܂f[^p^ƍŒ̈v̂B<br>
* TextBuffer.length < position + MaxMatch ƂȂ悤 position ł́A
* SɑȂߍŒ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 ){
//------------------------------------------------------------------
// ɓo^ĂȂf[^p^
// PȒŌB
int matchlen = this.Threshold - 1;
int matchpos = position;
int scanpos = position - 1;
int scanlimit = Math.max( this.DictionaryLimit, lastPutPos );
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;
}
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
}
scanpos--;
}
//------------------------------------------------------------------
// T
if( this.hashMethod.hashRequires() <= this.TextBuffer.length - position ){
int hash = this.hashMethod.hash( position );
scanpos = this.hashTable[ hash ];
scanlimit = Math.max( this.DictionaryLimit,
position - this.DictionarySize );
while( scanpos != HashAndBinaryTreeSearch.UNUSED ){
s = scanpos;
p = position;
while( buf[ s ] == buf[ p ] ){
s++;
p++;
if( max <= p ) break;
}
if( p < max ){
len = p - position;
if( scanlimit <= scanpos ){
if( matchlen < len ){
matchpos = scanpos;
matchlen = len;
}else if( matchlen == len && matchpos < scanpos ){
matchpos = scanpos;
}
}
scanpos = ( buf[ s ] < buf[ p ]
? this.large[ scanpos & ( this.DictionarySize - 1 ) ]
: this.small[ scanpos & ( this.DictionarySize - 1 ) ] );
}else{
break;
}
}
}
//------------------------------------------------------------------
// ŒvĂяoɕԂB
if( this.Threshold <= matchlen ){
return LzssOutputStream.createSearchReturn( matchlen, matchpos );
}else{
return LzssOutputStream.NOMATCH;
}
}
/**
* TextBuffer position ܂ł̃f[^OֈړہA
* ɉ nbVƓgp@\\f[^
* TextBuffer̃f[^ƖȂ悤ɑOֈړ鏈sB
*/
public void slide(){
this.DictionaryLimit = Math.max( 0, this.DictionaryLimit - this.DictionarySize );
this.slideTree( this.hashTable );
this.slideTree( this.parent );
this.slideTree( this.small );
this.slideTree( this.large );
}
/**
* put() ܂ searchAndPut() gp
* f[^p^ɓo^ۂ
* KvƂf[^ʂB<br>
* ͓o^̍ۂɃf[^p^\
* S(MaxMatchoCg)̃f[^KvƂB
*
* @return RXgN^ŗ^ MaxMatch
*/
public int putRequires(){
return this.MaxMatch;
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// node operation
//------------------------------------------------------------------
// private void addNode( int addpos, int position, int len )
// private void deleteNode( int position )
// private void contractNode( int oldpos, int newpos )
// private void replaceNode( int oldpos, int newpos )
//------------------------------------------------------------------
/**
* parentpos ̃f[^p^̎qƂ
* position n܂f[^p^ɓo^B<br>
* parentpos position ̃f[^p^ len oCgvB
* position ̈ʒũm[h͂炩 deleteNode
* UNUSED ̏ԂɂĂƁB
*
* @param parentpos ẽf[^p^TextBuffer̊Jnʒu
* @param position VKljf[^p^TextBuffer̊Jnʒu
* @param len ẽf[^p^ƐVKljf[^p^̈v
*/
private void addNode( int parentpos, int position, int len ){
int parentnode = parentpos & ( this.DictionarySize - 1 );
int node = position & ( this.DictionarySize - 1 );
if( this.TextBuffer[ parentpos + len ] < this.TextBuffer[ position + len ] ){
this.large[ parentnode ] = position;
}else{
this.small[ parentnode ] = position;
}
this.parent[ node ] = parentpos;
this.small[ node ] = HashAndBinaryTreeSearch.UNUSED;
this.large[ node ] = HashAndBinaryTreeSearch.UNUSED;
}
/**
* position n܂f[^p^폜B<br>
*
* @param position 폜f[^p^̊Jnʒu
*/
private void deleteNode( int position ){
int node = position & ( this.DictionarySize - 1 );
if( this.parent[ node ] != HashAndBinaryTreeSearch.UNUSED ){
if( this.small[ node ] == HashAndBinaryTreeSearch.UNUSED
&& this.large[ node ] == HashAndBinaryTreeSearch.UNUSED ){
this.contractNode( position, HashAndBinaryTreeSearch.UNUSED );
}else if( this.small[ node ] == HashAndBinaryTreeSearch.UNUSED ){
this.contractNode( position, this.large[ node ] );
}else if( this.large[ node ] == HashAndBinaryTreeSearch.UNUSED ){
this.contractNode( position, this.small[ node ] );
}else{
int replace = this.findNext( position );
this.deleteNode( replace );
this.replaceNode( position, replace );
}
}
}
/**
* q newpos Ȃ oldpos , newpos ŒuB
* oldpos ͓폜B
*
* @param oldpos 폜f[^p^̊Jnʒu
* @param newpos oldposɒuf[^p^̊Jnʒu
*/
private void contractNode( int oldpos, int newpos ){
int oldnode = oldpos & ( this.DictionarySize - 1 );
int newnode = newpos & ( this.DictionarySize - 1 );
int parentpos = this.parent[ oldnode ];
int parentnode = parentpos & ( this.DictionarySize - 1 );
if( parentpos != HashAndBinaryTreeSearch.ROOT_NODE ){
if( oldpos == this.small[ parentnode ] ){
this.small[ parentnode ] = newpos;
}else{
this.large[ parentnode ] = newpos;
}
}else{
this.hashTable[ this.hashMethod.hash( oldpos ) ] = newpos;
}
if( newpos != HashAndBinaryTreeSearch.UNUSED ){
this.parent[ newnode ] = parentpos;
}
this.parent[ oldnode ] = HashAndBinaryTreeSearch.UNUSED;
}
/**
* oldpos Ɋ܂܂ȂVf[^p^ newpos ŒuB
* newpos Ɋ܂܂Ă悤ȏꍇɂ́A
* deleteNode(newpos) ȂǂāA
* OKvB
* oldpos ͓폜B
*
* @param oldpos 폜f[^p^̊Jnʒu
* @param newpos oldposɒuf[^p^̊Jnʒu
*/
private void replaceNode( int oldpos, int newpos ){
int oldnode = oldpos & ( this.DictionarySize - 1 );
int newnode = newpos & ( this.DictionarySize - 1 );
int parentpos = this.parent[ oldnode ];
int parentnode = parentpos & ( this.DictionarySize - 1 );
if( parentpos != HashAndBinaryTreeSearch.ROOT_NODE ){
if( oldpos == this.small[ parentnode ] ){
this.small[ parentnode ] = newpos;
}else{
this.large[ parentnode ] = newpos;
}
}else{
this.hashTable[ this.hashMethod.hash( oldpos ) ] = newpos;
}
this.parent[ newnode ] = parentpos;
this.small[ newnode ] = this.small[ oldnode ];
this.large[ newnode ] = this.large[ oldnode ];
if( this.small[ newnode ] != HashAndBinaryTreeSearch.UNUSED ){
this.parent[ this.small[ newnode ] & ( this.DictionarySize - 1 ) ] = newpos;
}
if( this.large[ newnode ] != HashAndBinaryTreeSearch.UNUSED ){
this.parent[ this.large[ newnode ] & ( this.DictionarySize - 1 ) ] = newpos;
}
this.parent[ oldnode ] = HashAndBinaryTreeSearch.UNUSED;
this.large[ oldnode ] = HashAndBinaryTreeSearch.UNUSED;
this.small[ oldnode ] = HashAndBinaryTreeSearch.UNUSED;
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// private int findNext( int position )
// private void slideTree( int[] src, int[] dst, int start, int end, int width )
//------------------------------------------------------------------
/**
* deleteNode( position ) ƂɁA
* small large ̗̗tꍇA
* position ̂n܂f[^p^
* uׂ f[^p^̊JnʒuToB
*
* @param position uf[^p^̊Jnʒu
*
* @return position ̂n܂f[^p^
* uׂ f[^p^̊Jnʒu
*/
private int findNext( int position ){
int node = position & ( this.DictionarySize - 1 );
position = this.small[ node ];
node = position & ( this.DictionarySize - 1 );
while( HashAndBinaryTreeSearch.UNUSED != this.large[ node ] ){
position = this.large[ node ];
node = position & ( this.DictionarySize - 1 );
}
return position;
}
/**
* slide() ɁÅevfړ邽߂ɎgpB
*
* @param array z
*/
private void slideTree( int[] array ){
for( int i = 0 ; i < array.length ; i++ ){
array[ i ] = ( 0 <= array[i]
? array[i] - this.DictionarySize
: array[i] );
}
}
}
//end of HashAndBinaryTreeSearch.java
|