Class: RBzip2::Ruby::Compressor
- Inherits:
-
Object
- Object
- RBzip2::Ruby::Compressor
- Includes:
- Constants
- Defined in:
- lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb
Overview
This code is free software; you can redistribute it and/or modify it under the terms of the new BSD License.
Copyright © 2011-2017, Sebastian Staudt
Constant Summary
Constants included from Constants
RBzip2::Ruby::Constants::BASEBLOCKSIZE, RBzip2::Ruby::Constants::CLEARMASK, RBzip2::Ruby::Constants::DEPTH_THRESH, RBzip2::Ruby::Constants::EOF, RBzip2::Ruby::Constants::GREATER_ICOST, RBzip2::Ruby::Constants::G_SIZE, RBzip2::Ruby::Constants::INCS, RBzip2::Ruby::Constants::LESSER_ICOST, RBzip2::Ruby::Constants::MAX_ALPHA_SIZE, RBzip2::Ruby::Constants::MAX_BLOCK_SIZE, RBzip2::Ruby::Constants::MAX_CODE_LEN, RBzip2::Ruby::Constants::MAX_SELECTORS, RBzip2::Ruby::Constants::MIN_BLOCK_SIZE, RBzip2::Ruby::Constants::NO_RAND_PART_A_STATE, RBzip2::Ruby::Constants::NO_RAND_PART_B_STATE, RBzip2::Ruby::Constants::NO_RAND_PART_C_STATE, RBzip2::Ruby::Constants::NUM_OVERSHOOT_BYTES, RBzip2::Ruby::Constants::N_GROUPS, RBzip2::Ruby::Constants::N_ITERS, RBzip2::Ruby::Constants::QSORT_STACK_SIZE, RBzip2::Ruby::Constants::RAND_PART_A_STATE, RBzip2::Ruby::Constants::RAND_PART_B_STATE, RBzip2::Ruby::Constants::RAND_PART_C_STATE, RBzip2::Ruby::Constants::RNUMS, RBzip2::Ruby::Constants::RUNA, RBzip2::Ruby::Constants::RUNB, RBzip2::Ruby::Constants::SETMASK, RBzip2::Ruby::Constants::SMALL_THRESH, RBzip2::Ruby::Constants::START_BLOCK_STATE, RBzip2::Ruby::Constants::WORK_FACTOR
Instance Attribute Summary collapse
-
#block_size ⇒ Object
readonly
Returns the value of attribute block_size.
Class Method Summary collapse
- .assign_codes(code, length, min_len, max_len, alpha_size) ⇒ Object
- .choose_block_size(input_length) ⇒ Object
- .make_code_lengths(len, freq, data, alpha_size, max_len) ⇒ Object
- .med3(a, b, c) ⇒ Object
- .vswap(fmap, p1, p2, n) ⇒ Object
Instance Method Summary collapse
- #block_sort ⇒ Object
- #close ⇒ Object
- #end_block ⇒ Object
- #end_compression ⇒ Object
- #finish ⇒ Object
- #finished_with_stream ⇒ Object
- #flush ⇒ Object
- #generate_mtf_values ⇒ Object
- #init ⇒ Object
- #init_block ⇒ Object
-
#initialize(io, block_size = MAX_BLOCK_SIZE) ⇒ Compressor
constructor
A new instance of Compressor.
- #main_qsort3(data_shadow, lo_st, hi_st, d_st) ⇒ Object
- #main_simple_sort(data_shadow, lo, hi, d) ⇒ Object
- #main_sort ⇒ Object
- #move_to_front_code_and_send ⇒ Object
- #put_byte(c) ⇒ Object
- #put_int(u) ⇒ Object
- #putc(int) ⇒ Object
- #puts(line) ⇒ Object
- #randomize_block ⇒ Object
- #send_mtf_values ⇒ Object
- #send_mtf_values0(n_groups, alpha_size) ⇒ Object
- #send_mtf_values1(n_groups, alpha_size) ⇒ Object
- #send_mtf_values2(n_groups, n_selectors) ⇒ Object
- #send_mtf_values3(n_groups, alpha_size) ⇒ Object
- #send_mtf_values4 ⇒ Object
- #send_mtf_values5(n_groups, n_selectors) ⇒ Object
- #send_mtf_values6(n_groups, alpha_size) ⇒ Object
- #send_mtf_values7 ⇒ Object
- #w(n, v) ⇒ Object
- #write(bytes) ⇒ Object
- #write0(b) ⇒ Object
- #write_run ⇒ Object
Constructor Details
#initialize(io, block_size = MAX_BLOCK_SIZE) ⇒ Compressor
Returns a new instance of Compressor.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 168 def initialize(io, block_size = MAX_BLOCK_SIZE) @allowable_block_size = 0 @block_size = block_size @buff = 0 @combined_crc = 0 @crc = RBzip2::Ruby::CRC.new @current_char = -1 @io = io @last = 0 @live = 0 @run_length = 0 init end |
Instance Attribute Details
#block_size ⇒ Object (readonly)
Returns the value of attribute block_size.
166 167 168 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 166 def block_size @block_size end |
Class Method Details
.assign_codes(code, length, min_len, max_len, alpha_size) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 10 def self.assign_codes(code, length, min_len, max_len, alpha_size) vec = 0 min_len.upto(max_len) do |n| alpha_size.times do |i| if (length[i] & 0xff) == n code[i] = vec vec += 1 end end vec <<= 1 end end |
.choose_block_size(input_length) ⇒ Object
23 24 25 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 23 def self.choose_block_size(input_length) input_length > 0 ? [(input_length / 132000) + 1, 9].min : MAX_BLOCK_SIZE end |
.make_code_lengths(len, freq, data, alpha_size, max_len) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 27 def self.make_code_lengths(len, freq, data, alpha_size, max_len) heap = data.heap weight = data.weight parent = data.parent weight[0] = 0 (alpha_size - 1).downto(0) do |i| weight[i + 1] = (freq[i] == 0 ? 1 : freq[i]) << 8 end too_long = true while too_long too_long = false n_nodes = alpha_size n_heap = 0 heap[0] = 0 weight[0] = 0 parent[0] = -2 1.upto(alpha_size) do |i| parent[i] = -1 n_heap += 1 heap[n_heap] = i zz = n_heap tmp = heap[zz] while weight[tmp] < weight[heap[zz >> 1]] heap[zz] = heap[zz >> 1] zz >>= 1 end heap[zz] = tmp end while n_heap > 1 n1 = heap[1] heap[1] = heap[n_heap] n_heap -= 1 zz = 1 tmp = heap[1] while true do yy = zz << 1 break if yy > n_heap yy += 1 if (yy < n_heap) && (weight[heap[yy + 1]] < weight[heap[yy]]) break if weight[tmp] < weight[heap[yy]] heap[zz] = heap[yy] zz = yy end heap[zz] = tmp n2 = heap[1] heap[1] = heap[n_heap] n_heap -= 1 zz = 1 tmp = heap[1] while true do yy = zz << 1 break if yy > n_heap yy += 1 if (yy < n_heap) && (weight[heap[yy + 1]] < weight[heap[yy]]) break if weight[tmp] < weight[heap[yy]] heap[zz] = heap[yy] zz = yy end heap[zz] = tmp n_nodes += 1 parent[n1] = parent[n2] = n_nodes weight_n1 = weight[n1] weight_n2 = weight[n2] weight[n_nodes] = ((weight_n1 & 0xffffff00) + (weight_n2 & 0xffffff00)) | (1 + (((weight_n1 & 0x000000ff) > (weight_n2 & 0x000000ff)) ? (weight_n1 & 0x000000ff) : (weight_n2 & 0x000000ff))) parent[n_nodes] = -1 n_heap += 1 heap[n_heap] = n_nodes zz = n_heap tmp = heap[zz] weight_tmp = weight[tmp] while weight_tmp < weight[heap[zz >> 1]] heap[zz] = heap[zz >> 1] zz >>= 1 end heap[zz] = tmp end 1.upto(alpha_size) do |i| j = 0 k = i while (parent_k = parent[k]) >= 0 k = parent_k j += 1 end len[i - 1] = j too_long = true if j > max_len end if too_long 1.upto(alpha_size) do |i| j = weight[i] >> 8 j = 1 + (j >> 1) weight[i] = j << 8 end end end end |
.med3(a, b, c) ⇒ Object
153 154 155 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 153 def self.med3(a, b, c) (a < b) ? (b < c ? b : a < c ? c : a) : (b > c ? b : a > c ? c : a) end |
.vswap(fmap, p1, p2, n) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 157 def self.vswap(fmap, p1, p2, n) n += p1 while p1 < n fmap[p1], fmap[p2] = fmap[p2], fmap[p1] p1 += 1 p2 += 1 end end |
Instance Method Details
#block_sort ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 183 def block_sort @work_limit = WORK_FACTOR * @last @work_done = 0 @block_randomized = false @first_attempt = true main_sort if @first_attempt && @work_done > @work_limit randomize_block @work_limit = @work_done = 0 @first_attempt = false main_sort end fmap = @data.fmap @orig_ptr = -1 (@last + 1).times do |i| if fmap[i] == 0 @orig_ptr = i break end end end |
#close ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 207 def close unless @io.nil? io_shadow = @io finish io_shadow.close end end |
#end_block ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 215 def end_block @block_crc = @crc.final_crc @combined_crc = (@combined_crc << 1) | (@combined_crc >> 31) @combined_crc ^= @block_crc return if @last == -1 block_sort put_byte 0x31 put_byte 0x41 put_byte 0x59 put_byte 0x26 put_byte 0x53 put_byte 0x59 put_int @block_crc @block_randomized ? w(1, 1) : w(1, 0) move_to_front_code_and_send end |
#end_compression ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 238 def end_compression put_byte 0x17 put_byte 0x72 put_byte 0x45 put_byte 0x38 put_byte 0x50 put_byte 0x90 put_int @combined_crc finished_with_stream end |
#finish ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 251 def finish unless @io.nil? begin write_run if @run_length > 0 @current_char = -1 end_block end_compression ensure @io = nil @data = nil end end end |
#finished_with_stream ⇒ Object
265 266 267 268 269 270 271 272 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 265 def finished_with_stream while @live > 0 @io.write((@buff >> 24).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end end |
#flush ⇒ Object
274 275 276 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 274 def flush @io.flush unless @io.nil? end |
#generate_mtf_values ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 278 def generate_mtf_values last_shadow = @last data_shadow = @data in_use = data_shadow.in_use block = data_shadow.block fmap = data_shadow.fmap sfmap = data_shadow.sfmap mtf_freq = data_shadow.mtf_freq unseq_to_seq = data_shadow.unseq_to_seq yy = data_shadow.generate_mtf_values_yy n_in_use_shadow = 0 256.times do |i| if in_use[i] unseq_to_seq[i] = n_in_use_shadow n_in_use_shadow += 1 end end @n_in_use = n_in_use_shadow eob = n_in_use_shadow + 1 eob.times { |i| mtf_freq[i] } n_in_use_shadow.times { |i| yy[i] = i } wr = 0 z_pend = 0 0.upto(last_shadow) do |i| ll_i = unseq_to_seq[block[fmap[i]]] tmp = yy[0] j = 0 while ll_i != tmp j += 1 tmp, yy[j] = yy[j], tmp end yy[0] = tmp if j == 0 z_pend += 1 else if z_pend > 0 z_pend -= 1 while true do if (z_pend & 1) == 0 sfmap[wr] = RUNA mtf_freq[RUNA] += 1 else sfmap[wr] = RUNB mtf_freq[RUNB] += 1 end wr += 1 break if z_pend < 2 z_pend = (z_pend - 2) >> 1 end z_pend = 0 end sfmap[wr] = j + 1 wr += 1 mtf_freq[j + 1] += 1 end end if z_pend > 0 z_pend -= 1 while true do if (z_pend & 1) == 0 sfmap[wr] = RUNA mtf_freq[RUNA] += 1 else sfmap[wr] = RUNB mtf_freq[RUNB] += 1 end wr += 1 break if z_pend < 2 z_pend = (z_pend - 2) >> 1 end end sfmap[wr] = eob mtf_freq[eob] += 1 @n_mtf = wr + 1 end |
#init ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 367 def init put_byte 0x42 put_byte 0x5a @data = RBzip2::Ruby::OutputData.new @block_size put_byte 0x68 put_byte 0x30 + @block_size @combined_crc = 0 init_block end |
#init_block ⇒ Object
380 381 382 383 384 385 386 387 388 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 380 def init_block @crc.initialize_crc @last = -1 in_use = @data.in_use in_use[0, 256] = [false] * 256 @allowable_block_size = (@block_size * BASEBLOCKSIZE) - 20 end |
#main_qsort3(data_shadow, lo_st, hi_st, d_st) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 506 def main_qsort3(data_shadow, lo_st, hi_st, d_st) stack_ll = data_shadow.stack_ll stack_hh = data_shadow.stack_hh stack_dd = data_shadow.stack_dd fmap = data_shadow.fmap block = data_shadow.block stack_ll[0] = lo_st stack_hh[0] = hi_st stack_dd[0] = d_st sp = 1 while (sp -= 1) >= 0 lo = stack_ll[sp] hi = stack_hh[sp] d = stack_dd[sp] if (hi - lo < SMALL_THRESH) || (d > DEPTH_THRESH) return if main_simple_sort data_shadow, lo, hi, d else d1 = d + 1 med = self.class.med3 block[fmap[lo] + d1], block[fmap[hi] + d1], block[fmap[(lo + hi) >> 1] + d1] un_lo = lo un_hi = hi lt_lo = lo gt_hi = hi while true while un_lo <= un_hi n = block[fmap[un_lo] + d1] - med if n == 0 fmap[un_lo], fmap[lt_lo] = fmap[lt_lo], fmap[un_lo] un_lo += 1 lt_lo += 1 elsif n < 0 un_lo += 1 else break end end while un_lo <= un_hi n = block[fmap[un_hi] + d1] - med if n == 0 fmap[un_hi], fmap[gt_hi] = fmap[gt_hi], fmap[un_hi] un_hi -= 1 gt_hi -= 1 elsif n > 0 un_hi -= 1 else break end end if un_lo <= un_hi fmap[un_lo], fmap[un_hi] = fmap[un_hi], fmap[un_lo] un_lo += 1 un_hi -= 1 else break end end if gt_hi < lt_lo stack_ll[sp] = lo stack_hh[sp] = hi stack_dd[sp] = d1 sp += 1 else n = ((lt_lo - lo) < (un_lo - lt_lo)) ? (lt_lo - lo) : (un_lo - lt_lo) self.class.vswap fmap, lo, un_lo - n, n m = ((hi - gt_hi) < (gt_hi - un_hi)) ? (hi - gt_hi) : (gt_hi - un_hi) self.class.vswap fmap, un_lo, hi - m + 1, m n = lo + un_lo - lt_lo - 1 m = hi - (gt_hi - un_hi) + 1 stack_ll[sp, 3] = lo, n + 1, m stack_hh[sp, 3] = n, m - 1, hi stack_dd[sp, 3] = d, d1, d sp += 3 end end end end |
#main_simple_sort(data_shadow, lo, hi, d) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 593 def main_simple_sort(data_shadow, lo, hi, d) big_n = hi - lo + 1 return @first_attempt && (@work_done > @work_limit) if big_n < 2 hp = 0 while INCS[hp] < big_n hp += 1 end fmap = data_shadow.fmap quadrant = data_shadow.quadrant block = data_shadow.block last_plus_1 = @last + 1 h = nil i1 = nil i2 = nil j = nil mj = nil once_runned = nil vd = nil x = nil x_loop = lambda do while x > 0 x -= 4 if block[i1 + 1] == block[i2 + 1] if quadrant[i1] == quadrant[i2] if block[i1 + 2] == block[i2 + 2] if quadrant[i1 + 1] == quadrant[i2 + 1] if block[i1 + 3] == block[i2 + 3] if quadrant[i1 + 2] == quadrant[i2 + 2] if block[i1 + 4] == block[i2 + 4] if quadrant[i1 + 3] == quadrant[i2 + 3] i1 -= last_plus_1 if (i1 += 4) >= last_plus_1 i2 -= last_plus_1 if (i2 += 4) >= last_plus_1 @work_done += 1 next elsif quadrant[i1 + 3] > quadrant[i2 + 3] return true else return false end elsif block[i1 + 4] > block[i2 + 4] return true else return false end elsif quadrant[i1 + 2] > quadrant[i2 + 2] return true else return false end elsif block[i1 + 3] > block[i2 + 3] return true else return false end elsif quadrant[i1 + 1] > quadrant[i2 + 1] return true else return false end elsif block[i1 + 2] > block[i2 + 2] return true else return false end elsif quadrant[i1] > quadrant[i2] return true else return false end elsif block[i1 + 1] > block[i2 + 1] return true else return false end end true end hammer_loop = lambda do a = 0 while true do if once_runned fmap[j] = a break if (j -= h) <= mj else once_runned = true end a = fmap[j - h] i1 = a + d i2 = vd if block[i1 + 1] == block[i2 + 1] if block[i1 + 2] == block[i2 + 2] if block[i1 + 3] == block[i2 + 3] if block[i1 + 4] == block[i2 + 4] if block[i1 + 5] == block[i2 + 5] if block[i1 += 6] == block[i2 += 6] x = @last break unless x_loop.call else if block[i1] > block[i2] next else break end end elsif block[i1 + 5] > block[i2 + 5] next else break end elsif block[i1 + 4] > block[i2 + 4] next else break end elsif block[i1 + 3] > block[i2 + 3] next else break end elsif block[i1 + 2] > block[i2 + 2] next else break end elsif block[i1 + 1] > block[i2 + 1] next else break end end end while (hp -= 1) >= 0 h = INCS[hp] mj = lo + h - 1 i = lo + h while i <= hi k = 3 while i <= hi && (k -= 1) >= 0 v = fmap[i] vd = v + d j = i once_runned = false hammer_loop.call fmap[j] = v i += 1 end end break if @first_attempt && i <= hi && @work_done > @work_limit end @first_attempt && @work_done > @work_limit end |
#main_sort ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 390 def main_sort data_shadow = @data running_order = data_shadow.main_sort_running_order copy = data_shadow.main_sort_copy big_done = data_shadow.main_sort_big_done ftab = data_shadow.ftab block = data_shadow.block fmap = data_shadow.fmap quadrant = data_shadow.quadrant 65537.times { |i| ftab[i] = 0 } NUM_OVERSHOOT_BYTES.times do |i| block[@last + i + 2] = block[(i % (@last + 1)) + 1] end (@last + NUM_OVERSHOOT_BYTES).times do |i| quadrant[i] = 0 end block[0] = block[@last + 1] c1 = block[0] (@last + 1).times do |i| c2 = block[i + 1] ftab[(c1 << 8) + c2] += 1 c1 = c2 end 1.upto(65536) { |i| ftab[i] += ftab[i - 1] } c1 = block[1] @last.times do |i| c2 = block[i + 2] fmap[ftab[(c1 << 8) + c2] -= 1] = i c1 = c2 end fmap[ftab[((block[@last + 1]) << 8) + block[1]] -= 1] = @last big_done.replace Array.new(256, false) 256.times { |i| running_order[i] = i } h = 364 while h != 1 h /= 3 h.upto(255) do |i| vv = running_order[i] a = ftab[(vv + 1) << 8] - ftab[vv << 8] b = h - 1 j = i ro = running_order[j - h] while ftab[(ro + 1) << 8] - ftab[ro << 8] > a running_order[j] = ro j -= h break if j <= b ro = running_order[j - h] end running_order[j] = vv end end 256.times do |i| ss = running_order[i] 256.times do |j| sb = (ss << 8) + j ftab_sb = ftab[sb] if (ftab_sb & SETMASK) != SETMASK lo = ftab_sb & CLEARMASK hi = (ftab[sb + 1] & CLEARMASK) - 1 if hi > lo main_qsort3 data_shadow, lo, hi, 2 return if @first_attempt && (@work_done > @work_limit) end ftab[sb] = ftab_sb | SETMASK end end 256.times { |j| copy[j] = ftab[(j << 8) + ss] & CLEARMASK } hj = ftab[(ss + 1) << 8] & CLEARMASK (ftab[ss << 8] & CLEARMASK).upto(hj - 1) do |j| fmap_j = fmap[j] c1 = block[fmap_j] unless big_done[c1] fmap[copy[c1]] = (fmap_j == 0) ? @last : (fmap_j - 1) copy[c1] += 1 end end 255.downto(0) { |j| ftab[(j << 8) + ss] |= SETMASK } big_done[ss] = true if i < 255 bb_start = ftab[ss << 8] & CLEARMASK bb_size = (ftab[(ss + 1) << 8] & CLEARMASK) - bb_start shifts = 0 while (bb_size >> shifts) > 65534 shifts += 1 end bb_size.times do |j| a2update = fmap[bb_start + j] q_val = j >> shifts quadrant[a2update] = q_val if a2update < NUM_OVERSHOOT_BYTES quadrant[a2update + @last + 1] = q_val end end end end end |
#move_to_front_code_and_send ⇒ Object
764 765 766 767 768 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 764 def move_to_front_code_and_send w 24, @orig_ptr generate_mtf_values send_mtf_values end |
#put_byte(c) ⇒ Object
782 783 784 785 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 782 def put_byte(c) c = c[0].to_i if c.is_a? String w 8, c end |
#put_int(u) ⇒ Object
787 788 789 790 791 792 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 787 def put_int(u) w 8, (u >> 24) & 0xff w 8, (u >> 16) & 0xff w 8, (u >> 8) & 0xff w 8, u & 0xff end |
#putc(int) ⇒ Object
770 771 772 773 774 775 776 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 770 def putc(int) if int.is_a? Numeric put_byte int & 0xff else write int.to_s[0].chr end end |
#puts(line) ⇒ Object
778 779 780 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 778 def puts(line) write line + $/ end |
#randomize_block ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 794 def randomize_block in_use = @data.in_use block = @data.block 256.times { |i| in_use[i] = false } r_n_to_go = 0 r_t_pos = 0 j = 1 i = 0 while i <= @last i = j if r_n_to_go == 0 r_n_to_go = RNUMS[r_t_pos] r_t_pos = 0 if (r_t_pos += 1) == 512 end r_n_to_go -= 1 block[j] ^= r_n_to_go == 1 ? 1 : 0 in_use[block[j]] = true j += 1 end @block_randomized = true end |
#send_mtf_values ⇒ Object
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 823 def send_mtf_values len = @data.send_mtf_values_len alpha_size = @n_in_use + 2 N_GROUPS.times do |t| len_t = len[t] alpha_size.times { |v| len_t[v] = GREATER_ICOST } end n_groups = (@n_mtf < 200) ? 2 : (@n_mtf < 600) ? 3 : (@n_mtf < 1200) ? 4 : (@n_mtf < 2400) ? 5 : 6 send_mtf_values0 n_groups, alpha_size n_selectors = send_mtf_values1 n_groups, alpha_size send_mtf_values2 n_groups, n_selectors send_mtf_values3 n_groups, alpha_size send_mtf_values4 send_mtf_values5 n_groups, n_selectors send_mtf_values6 n_groups, alpha_size send_mtf_values7 end |
#send_mtf_values0(n_groups, alpha_size) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 845 def send_mtf_values0(n_groups, alpha_size) len = @data.send_mtf_values_len mtf_freq = @data.mtf_freq rem_f = @n_mtf gs = 0 n_groups.downto(1) do |n_part| t_freq = rem_f / n_part ge = gs - 1 a_freq = 0 a = alpha_size - 1 while a_freq < t_freq && ge < a ge += 1 a_freq += mtf_freq[ge] end if ge > gs && n_part != n_groups && n_part != 1 && ((n_groups - n_part) & 1) != 0 ge -= 1 a_freq -= mtf_freq[ge] end len_np = len[n_part - 1] (alpha_size - 1).downto(0) do |v| if v >= gs && v <= ge len_np[v] = LESSER_ICOST else len_np[v] = GREATER_ICOST end end gs = ge + 1 rem_f -= a_freq end end |
#send_mtf_values1(n_groups, alpha_size) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 883 def send_mtf_values1(n_groups, alpha_size) data_shadow = @data rfreq = data_shadow.send_mtf_values_rfreq fave = data_shadow.send_mtf_values_fave cost = data_shadow.send_mtf_values_cost sfmap = data_shadow.sfmap selector = data_shadow.selector len = data_shadow.send_mtf_values_len len_0 = len[0] len_1 = len[1] len_2 = len[2] len_3 = len[3] len_4 = len[4] len_5 = len[5] n_selectors = 0 N_ITERS.times do (n_groups - 1).downto(0) do |t| fave[t] = 0 rfreqt = rfreq[t] (alpha_size - 1).downto(0) { |i| rfreqt[i] = 0 } end n_selectors = 0 gs = 0 while gs < @n_mtf ge = [gs + G_SIZE - 1, @n_mtf - 1].min if n_groups == N_GROUPS cost0 = 0 cost1 = 0 cost2 = 0 cost3 = 0 cost4 = 0 cost5 = 0 gs.upto(ge) do |i| icv = sfmap[i] cost0 += len_0[icv] & 0xff cost1 += len_1[icv] & 0xff cost2 += len_2[icv] & 0xff cost3 += len_3[icv] & 0xff cost4 += len_4[icv] & 0xff cost5 += len_5[icv] & 0xff end cost[0] = cost0 cost[1] = cost1 cost[2] = cost2 cost[3] = cost3 cost[4] = cost4 cost[5] = cost5 else (n_groups - 1).downto(0) { |t| cost[t] = 0 } gs.upto(ge) do |i| icv = sfmap[i] (n_groups - 1).downto(0) { |t| cost[t] += len[t][icv] & 0xff } end end bt = -1 bc = 999999999 (n_groups - 1).downto(0) do |t| cost_t = cost[t] if cost_t < bc bc = cost_t bt = t end end fave[bt] += 1 selector[n_selectors] = bt n_selectors += 1 rfreq_bt = rfreq[bt] gs.upto(ge) { |i| rfreq_bt[sfmap[i]] += 1 } gs = ge + 1 end n_groups.times do |t| self.class.make_code_lengths len[t], rfreq[t], @data, alpha_size, 20 end end n_selectors end |
#send_mtf_values2(n_groups, n_selectors) ⇒ Object
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 973 def send_mtf_values2(n_groups, n_selectors) data_shadow = @data pos = data_shadow.send_mtf_values2_pos n_groups.times { |i| pos[i] = i } n_selectors.times do |i| ll_i = data_shadow.selector[i] tmp = pos[0] j = 0 while ll_i != tmp j += 1 tmp, pos[j] = pos[j], tmp end pos[0] = tmp data_shadow.selector_mtf[i] = j end end |
#send_mtf_values3(n_groups, alpha_size) ⇒ Object
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 994 def send_mtf_values3(n_groups, alpha_size) code = @data.send_mtf_values_code len = @data.send_mtf_values_len n_groups.times do |t| min_len = 32 max_len = 0 len_t = len[t] (alpha_size - 1).downto(0) do |i| l = len_t[i] & 0xff max_len = l if l > max_len min_len = l if l < min_len end self.class.assign_codes code[t], len[t], min_len, max_len, alpha_size end end |
#send_mtf_values4 ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1012 def send_mtf_values4 in_use = @data.in_use in_use_16 = @data.send_mtf_values4_in_use_16 15.downto(0) do |i| in_use_16[i] = false i16 = i * 16 15.downto(0) do |j| in_use_16[i] = true if in_use[i16 + j] end end 16.times { |i| w 1, in_use_16[i] ? 1 : 0 } io_shadow = @io live_shadow = @live buff_shadow = @buff 16.times do |i| if in_use_16[i] i16 = i * 16 16.times do |j| while live_shadow >= 8 io_shadow.write(((buff_shadow >> 24) & 0xffffffff).chr) buff_shadow <<= 8 buff_shadow &= 0xffffffff live_shadow -= 8 end buff_shadow |= 1 << (32 - live_shadow - 1) if in_use[i16 + j] live_shadow += 1 end end end @buff = buff_shadow @live = live_shadow end |
#send_mtf_values5(n_groups, n_selectors) ⇒ Object
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 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1050 def send_mtf_values5(n_groups, n_selectors) w 3, n_groups w 15, n_selectors io_shadow = @io selector_mtf = @data.selector_mtf live_shadow = @live buff_shadow = @buff n_selectors.times do |i| hj = selector_mtf[i] & 0xff hj.times do while live_shadow >= 8 io_shadow.write(((buff_shadow >> 24) & 0xffffffff).chr) buff_shadow <<= 8 buff_shadow &= 0xffffffff live_shadow -= 8 end buff_shadow |= 1 << (32 - live_shadow - 1) live_shadow += 1 end while live_shadow >= 8 io_shadow.write(((buff_shadow >> 24) & 0xffffffff).chr) buff_shadow <<= 8 buff_shadow &= 0xffffffff live_shadow -= 8 end live_shadow += 1 end @buff = buff_shadow @live = live_shadow end |
#send_mtf_values6(n_groups, alpha_size) ⇒ Object
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1086 def send_mtf_values6(n_groups, alpha_size) len = @data.send_mtf_values_len n_groups.times do |t| len_t = len[t] curr = len_t[0] & 0xff while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end @buff |= curr << (32 - @live - 5) @live += 5 alpha_size.times do |i| lti = len_t[i] & 0xff while curr < lti while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end @buff |= 2 << (32 - @live - 2) @live += 2 curr += 1 end while curr > lti while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end @buff |= 3 << (32 - @live - 2) @live += 2 curr -= 1 end while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end @live += 1 end end end |
#send_mtf_values7 ⇒ Object
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1141 def send_mtf_values7 data_shadow = @data len = data_shadow.send_mtf_values_len code = data_shadow.send_mtf_values_code selector = data_shadow.selector sfmap = data_shadow.sfmap sel_ctr = 0 gs = 0 while gs < @n_mtf ge = [gs + G_SIZE - 1, @n_mtf - 1].min selector_sel_ctr = selector[sel_ctr] & 0xff code_sel_ctr = code[selector_sel_ctr] len_sel_ctr = len[selector_sel_ctr] while gs <= ge sfmap_i = sfmap[gs] while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end n = len_sel_ctr[sfmap_i] & 0xff @buff |= code_sel_ctr[sfmap_i] << (32 - @live - n) @live += n gs += 1 end gs = ge + 1 sel_ctr += 1 end end |
#w(n, v) ⇒ Object
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1178 def w(n, v) while @live >= 8 @io.write(((@buff >> 24) & 0xffffffff).chr) @buff <<= 8 @buff &= 0xffffffff @live -= 8 end @buff = @buff | (v << (32 - @live - n)) @live += n end |
#write(bytes) ⇒ Object
1190 1191 1192 1193 1194 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1190 def write(bytes) raise 'stream closed' if @io.nil? bytes.each_byte { |b| write0 b } end |
#write0(b) ⇒ Object
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1196 def write0(b) b &= 0xff if @current_char != -1 if @current_char == b @run_length += 1 if @run_length > 254 write_run @current_char = -1 @run_length = 0 end else write_run @run_length = 1 @current_char = b end else @current_char = b @run_length += 1 end end |
#write_run ⇒ Object
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/compressor.rb', line 1217 def write_run if @last < @allowable_block_size ch = @current_char data_shadow = @data data_shadow.in_use[ch] = true block = data_shadow.block run_length_shadow = @run_length run_length_shadow.times { @crc.update_crc ch } case run_length_shadow when 1 block[@last + 2] = ch @last += 1 when 2 block[@last + 2] = ch block[@last + 3] = ch @last += 2 when 3 block[@last + 2] = ch block[@last + 3] = ch block[@last + 4] = ch @last += 3 else run_length_shadow -= 4 data_shadow.in_use[run_length_shadow] = true block[@last + 2] = ch block[@last + 3] = ch block[@last + 4] = ch block[@last + 5] = ch block[@last + 6] = run_length_shadow @last += 5 end else end_block init_block write_run end end |