shrinkUnique function

Uint8List shrinkUnique(
  1. List<int> ids
)

Compresses a list of unique integers using the most efficient method.

This function automatically tries multiple compression algorithms and selects the one that produces the smallest result for the given input:

  • Delta-encoding with variable-length integers (good for sorted lists with small gaps)
  • Run-length encoding (good for lists with consecutive runs of integers)
  • Chunked encoding (good for lists with clustered values)
  • Bitmask encoding (good for dense sets of integers within a limited range)

The first byte of the returned Uint8List indicates the compression method used, followed by the compressed data.

Returns a compressed Uint8List representation of the integer list.

Implementation

Uint8List shrinkUnique(List<int> ids) {
  // Make a copy and ensure the list is sorted
  final sortedIds = [...ids]..sort();

  // Try each compression method
  final List<Uint8List> compressed = [
    _encodeDeltaVarint(sortedIds),
    _encodeRuns(sortedIds),
    _encodeChunked(sortedIds),
    _encodeBitmask(sortedIds),
  ];

  // Find the most efficient compression method
  int bestMethodIndex = 0;
  int bestSize = compressed[0].length;

  for (int i = 1; i < compressed.length; i++) {
    if (compressed[i].length < bestSize) {
      bestSize = compressed[i].length;
      bestMethodIndex = i;
    }
  }

  // Create result with method byte + compressed data
  final result = Uint8List(compressed[bestMethodIndex].length + 1);
  result[0] = bestMethodIndex; // First byte stores the method enum ordinal
  result.setRange(1, result.length, compressed[bestMethodIndex]);

  return result;
}