// Port of google/brotli v1.2.1 c/enc/block_splitter{,_inc.h}.c.
// Copyright 2013 Google Inc.; MIT, see LICENSE-encoder.

///|
fn hq_split_blocks(
  data : FixedArray[Int32],
  ids : FixedArray[Int32],
  blocks : Int32,
  alphabet : Int32,
  memory? : HqMemory,
) -> RefSplit {
  let mark = hq_memory_mark(memory)
  defer hq_memory_restore(memory, mark)
  let lengths = FixedArray::make(blocks, 0)
  let mut block = 0
  for i in 0.. HqHistogram::new(alphabet))
    let sizes = FixedArray::make(count, 1)
    let assignment = FixedArray::makei(count, i => i)
    let clusters = Array::makei(count, i => i)
    let batch_mark = hq_memory_mark(memory)
    hq_memory_add(
      memory,
      hq_histograms_memory(hist) +
      4L *
      (sizes.length() + assignment.length() + clusters.capacity()).to_int64(),
    )
    for j in 0.. n + hist[c].memory_usage()),
    )
  }
  let sizes_capacity = sizes_all.capacity()
  let sizes = FixedArray::from_array(sizes_all)
  hq_memory_handoff(memory, sizes_capacity, sizes.length())
  let clusters = Array::makei(all.length(), i => i)
  hq_memory_add(memory, 4L * clusters.capacity().to_int64())
  if memory is Some(m) {
    m.peak.clusters = m.peak.clusters.max(all.length())
  }
  hq_combine(
    all,
    tmp,
    sizes,
    symbols.mut_view(),
    clusters,
    256,
    hq_max_pairs(all.length()),
    memory?,
  )
  let index = FixedArray::make(all.length(), -1)
  let mut next = 0
  pos = 0
  let other = HqHistogram::new(alphabet)
  hq_memory_add(memory, 4L * index.length().to_int64() + other.memory_usage())
  for i in 0.. RefSplit {
  let mark = hq_memory_mark(memory)
  defer hq_memory_restore(memory, mark)
  let length = data.length()
  if length < 128 {
    let split = RefSplit::new(alphabet, 1, 0, 0.0)
    split.types.push(0)
    split.lengths.push(length)
    split.hist.push(FixedArray::make(alphabet, 0))
    hq_memory_add(memory, split.hq_memory_usage())
    return split
  }
  let mut count = (length / per_hist + 1).min(max_hist)
  let hist = Array::makei(count, _ => HqHistogram::new(alphabet))
  hq_memory_add(memory, hq_histograms_memory(hist))
  let block_length = length / count
  let mut seed = 7U
  for i in 0..= length {
      pos = length - stride - 1
    }
    for j in pos..<(pos + stride) {
      hist[i].add(data[j])
    }
  }
  seed = 7U
  let iters = (2 * length / stride + 100 + count - 1) / count * count
  for iter in 0..> 3), b'\x00')
  let new_id = FixedArray::make(count, -1)
  hq_memory_add(
    memory,
    4L * (ids.length() + new_id.length()).to_int64() +
    8L * (insert.length() + cost.length()).to_int64() +
    signals.length().to_int64(),
  )
  let mut blocks = 1
  for _ in 0..<(if quality < 11 { 3 } else { 10 }) {
    blocks = 1
    if count > 1 {
      for i in 0..> 3
      for at in 0..= switch {
            cost[k] = switch
            let ix = at * bitmap + (k >> 3)
            signals[ix] = (signals[ix].to_int() | (1 << (k & 7))).to_byte()
          }
        }
      }
      let mut current = ids[length - 1]
      for at = length - 2; at >= 0; at = at - 1 {
        if (
            signals[at * bitmap + (current >> 3)].to_int() &
            (1 << (current & 7))
          ) !=
          0 &&
          current != ids[at] {
          current = ids[at]
          blocks += 1
        }
        ids[at] = current
      }
    } else {
      ids.fill(0)
    }
    new_id.fill(-1)
    let mut next = 0
    for id in ids {
      if new_id[id] < 0 {
        new_id[id] = next
        next += 1
      }
    }
    for i in 0..