cluster_command.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package brotli
  2. /* Copyright 2013 Google Inc. All Rights Reserved.
  3. Distributed under MIT license.
  4. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. */
  6. /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if
  7. it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */
  8. func compareAndPushToQueueCommand(out []histogramCommand, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) {
  9. var is_good_pair bool = false
  10. var p histogramPair
  11. p.idx2 = 0
  12. p.idx1 = p.idx2
  13. p.cost_combo = 0
  14. p.cost_diff = p.cost_combo
  15. if idx1 == idx2 {
  16. return
  17. }
  18. if idx2 < idx1 {
  19. var t uint32 = idx2
  20. idx2 = idx1
  21. idx1 = t
  22. }
  23. p.idx1 = idx1
  24. p.idx2 = idx2
  25. p.cost_diff = 0.5 * clusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2]))
  26. p.cost_diff -= out[idx1].bit_cost_
  27. p.cost_diff -= out[idx2].bit_cost_
  28. if out[idx1].total_count_ == 0 {
  29. p.cost_combo = out[idx2].bit_cost_
  30. is_good_pair = true
  31. } else if out[idx2].total_count_ == 0 {
  32. p.cost_combo = out[idx1].bit_cost_
  33. is_good_pair = true
  34. } else {
  35. var threshold float64
  36. if *num_pairs == 0 {
  37. threshold = 1e99
  38. } else {
  39. threshold = brotli_max_double(0.0, pairs[0].cost_diff)
  40. }
  41. var combo histogramCommand = out[idx1]
  42. var cost_combo float64
  43. histogramAddHistogramCommand(&combo, &out[idx2])
  44. cost_combo = populationCostCommand(&combo)
  45. if cost_combo < threshold-p.cost_diff {
  46. p.cost_combo = cost_combo
  47. is_good_pair = true
  48. }
  49. }
  50. if is_good_pair {
  51. p.cost_diff += p.cost_combo
  52. if *num_pairs > 0 && histogramPairIsLess(&pairs[0], &p) {
  53. /* Replace the top of the queue if needed. */
  54. if *num_pairs < max_num_pairs {
  55. pairs[*num_pairs] = pairs[0]
  56. (*num_pairs)++
  57. }
  58. pairs[0] = p
  59. } else if *num_pairs < max_num_pairs {
  60. pairs[*num_pairs] = p
  61. (*num_pairs)++
  62. }
  63. }
  64. }
  65. func histogramCombineCommand(out []histogramCommand, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint {
  66. var cost_diff_threshold float64 = 0.0
  67. var min_cluster_size uint = 1
  68. var num_pairs uint = 0
  69. {
  70. /* We maintain a vector of histogram pairs, with the property that the pair
  71. with the maximum bit cost reduction is the first. */
  72. var idx1 uint
  73. for idx1 = 0; idx1 < num_clusters; idx1++ {
  74. var idx2 uint
  75. for idx2 = idx1 + 1; idx2 < num_clusters; idx2++ {
  76. compareAndPushToQueueCommand(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs)
  77. }
  78. }
  79. }
  80. for num_clusters > min_cluster_size {
  81. var best_idx1 uint32
  82. var best_idx2 uint32
  83. var i uint
  84. if pairs[0].cost_diff >= cost_diff_threshold {
  85. cost_diff_threshold = 1e99
  86. min_cluster_size = max_clusters
  87. continue
  88. }
  89. /* Take the best pair from the top of heap. */
  90. best_idx1 = pairs[0].idx1
  91. best_idx2 = pairs[0].idx2
  92. histogramAddHistogramCommand(&out[best_idx1], &out[best_idx2])
  93. out[best_idx1].bit_cost_ = pairs[0].cost_combo
  94. cluster_size[best_idx1] += cluster_size[best_idx2]
  95. for i = 0; i < symbols_size; i++ {
  96. if symbols[i] == best_idx2 {
  97. symbols[i] = best_idx1
  98. }
  99. }
  100. for i = 0; i < num_clusters; i++ {
  101. if clusters[i] == best_idx2 {
  102. copy(clusters[i:], clusters[i+1:][:num_clusters-i-1])
  103. break
  104. }
  105. }
  106. num_clusters--
  107. {
  108. /* Remove pairs intersecting the just combined best pair. */
  109. var copy_to_idx uint = 0
  110. for i = 0; i < num_pairs; i++ {
  111. var p *histogramPair = &pairs[i]
  112. if p.idx1 == best_idx1 || p.idx2 == best_idx1 || p.idx1 == best_idx2 || p.idx2 == best_idx2 {
  113. /* Remove invalid pair from the queue. */
  114. continue
  115. }
  116. if histogramPairIsLess(&pairs[0], p) {
  117. /* Replace the top of the queue if needed. */
  118. var front histogramPair = pairs[0]
  119. pairs[0] = *p
  120. pairs[copy_to_idx] = front
  121. } else {
  122. pairs[copy_to_idx] = *p
  123. }
  124. copy_to_idx++
  125. }
  126. num_pairs = copy_to_idx
  127. }
  128. /* Push new pairs formed with the combined histogram to the heap. */
  129. for i = 0; i < num_clusters; i++ {
  130. compareAndPushToQueueCommand(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs)
  131. }
  132. }
  133. return num_clusters
  134. }
  135. /* What is the bit cost of moving histogram from cur_symbol to candidate. */
  136. func histogramBitCostDistanceCommand(histogram *histogramCommand, candidate *histogramCommand) float64 {
  137. if histogram.total_count_ == 0 {
  138. return 0.0
  139. } else {
  140. var tmp histogramCommand = *histogram
  141. histogramAddHistogramCommand(&tmp, candidate)
  142. return populationCostCommand(&tmp) - candidate.bit_cost_
  143. }
  144. }