class DefaultHTreeChartReducer implements HTreeChartReducer<N> {

  @Override
  public void reduce(@NotNull List<Rectangle2D.Float> rectangles, @NotNull List<N> nodes) {
    assert nodes.size() == rectangles.size();
    int n = nodes.size();
    int index = 0;
    int keepIndex = 0;
    while (index < n) {
      Rectangle2D.Float rect = rectangles.get(index);
      N node = nodes.get(index);
      if (Math.floor(rect.getMinX()) < Math.floor(rect.getMaxX())) {
        // Crossing several pixels on X axis
        rectangles.set(keepIndex, rect);
        nodes.set(keepIndex, node);
        ++keepIndex;
        ++index;
        continue;
      }

      // Whole rectangle inside a pixel on X axis, let's combine all rectangles inside the pixel
      int pixel = (int)Math.floor(rect.getMaxX());
      int curDepth = node.getDepth();

      N keepNode = node;
      Rectangle2D.Float keepRect = rect;

      while (index < n) {
        rect = rectangles.get(index);
        node = nodes.get(index);

        if (node.getDepth() != curDepth || Math.floor(rect.getMaxX()) != pixel) {
          break;
        }
        keepRect.width = (float)(rect.getMaxX() - keepRect.getMinX());
        ++index;
      }

      rectangles.set(keepIndex, keepRect);
      nodes.set(keepIndex, keepNode);
      ++keepIndex;
    }
    rectangles.subList(keepIndex, rectangles.size()).clear();
    nodes.subList(keepIndex, nodes.size()).clear();
  }

}