trim method
Shorten the DartBlock program based on the given percentage value, by starting from the end of the program!
trimPercentage
: value in range (0.0, 1.0)
This is a deep trim, i.e., the maximum depth of each function is calculated based on its tree representation. Then, the trimmed length is calculated based on maximum_depth*trimPercentage.
Implementation
DartBlockProgram trim({
required double trimPercentage,
bool trimMainFunction = true,
bool trimCustomFunctions = true,
}) {
trimPercentage = 1.0 - max(0.0, min(1.0, trimPercentage));
DartBlockFunction trimFunction(DartBlockFunction function) {
/// Get the max depth of the function based on its tree-based representation.
final int? functionDepth = buildTree()
.findNodeByKey(function.hashCode)
?.getMaxDepth();
if (functionDepth != null && functionDepth >= 0) {
int trimToLength = (functionDepth * trimPercentage).floor();
return function.trim(trimToLength);
} else {
return function;
}
}
/// Trim the main function.
DartBlockFunction trimmedMainFunction = trimMainFunction
? trimFunction(mainFunction)
: mainFunction;
/// Trim each custom function
List<DartBlockFunction> trimmedCustomFunctions = [];
if (trimCustomFunctions) {
for (final customFunction in customFunctions) {
DartBlockFunction trimmedCustomFunction = trimFunction(customFunction);
trimmedCustomFunctions.add(trimmedCustomFunction);
}
} else {
trimmedCustomFunctions = customFunctions;
}
return DartBlockProgram(
mainLanguage,
trimmedMainFunction,
trimmedCustomFunctions,
version,
);
}