void (OperationChain newChain, List<OperationChain> result) { if (result.isEmpty()) { result.add(newChain); return; } boolean doAdd = true; IntStack indicesToRemove = new IntArrayList(); for (int i = 0; i < result.size(); i++) { OperationChain chain = result.get(i); OperationChain.CompareResult r = OperationChain.compare(chain, newChain); switch (r) { case LEFT_CONTAINS_RIGHT -> indicesToRemove.push(i); case RIGHT_CONTAINS_LEFT, EQUAL -> doAdd = false; case NOT_EQUAL -> {} } } while (!indicesToRemove.isEmpty()) { result.remove(indicesToRemove.popInt()); } if (doAdd) { result.add(newChain); } }