private void flatten(HitGroup hits, int level, Result result) {
        int hitsLeft = hits.size(); // Iterate only through the initial size
        for (Iterator<Hit> i = hits.iterator(); i.hasNext() && hitsLeft-- > 0;) {
            Hit hit = i.next();

            // If we count the number of unique groups, use that as total hit count.
            if (level == 0 && (hit instanceof RootGroup)) {
                Object countField = hit.getField("count()");
                if (countField != null)
                    result.setTotalHitCount((long)countField);
            }

            if (hit instanceof HitGroup) {
                flatten((HitGroup)hit, level++, result);
                i.remove();
            } else {
                result.hits().add(hit);
            }
        }
    }
Flattens a grouping result into a flat list of hits on the top level in the returned result.
Useful when using grouping to create hits with diversity and similar.

@author bratseth
container-search/src/main/java/com/yahoo/search/grouping/result/FlatteningSearcher.java