Set<String> dimensionSubset = new HashSet<String>();
dimensionSubset.addAll(dimSlices.keySet());
dimensionSubset.addAll(groupDimensions);
Cuboid cuboid = null;
// find a suitable cuboid per above with fewest extra dimensions.
for (Cuboid c : cube.getCuboids()) {
List<String> cPath = c.getCuboidPath();
// filter out smaller cubes
if (cPath.size() < dimensionSubset.size())
continue;
// filter out those that don't contain all the dimensions we need
if (!cPath.containsAll(dimensionSubset))
continue;
// now check group dimensions that must be stacked on the left.
int cnt = groupDimensions.size();
for (String dimName : cPath) {
if (groupDimensions.contains(dimName)) {
cnt--;
} else {
/*
* easy but still quite effective optimization is that if
* slices are degenerate, their dimension could be pushed
* left in the cuboid without breaking inline grouping
* prerequisites. This is surprisingly much more often the
* case as degenerate slicing is quite common.
*/
List<Slice> slices = dimSlices.get(dimName);
if (slices == null || slices.size() != 1)
break; // clearly not degenerate
Slice slice = slices.get(0);
if (slice.isLeftOpen() || slice.isRightOpen()
|| !slice.getLeftBound().equals(slice.getRightBound()))
break; // not degenerate.
}
}
if (cnt > 0)
continue;
// found qualifying cuboid. good.
if (cuboid == null || cPath.size() < cuboid.getCuboidPath().size())
cuboid = c;
}
return cuboid;