Skip to main content

getDownscaledCapacity

Short summary

The method takes the requested size and the current capacity as input and determines the next suitable capacity value based on the implemented resizing strategy. The resulting capacity is returned as newCapacity. It returns true when a downsizing is needed within the used strategy, false if no resizing is needed.

  • Return type: BOOL

Parameters

NameTypeCommentKind
requestedSizeUDINTthe new size that is neededinput
currentCapacityUDINTthe current actual size of the containerinput
newCapacityUDINTthe calculated size of the containeroutput

Code

Declaration

METHOD getDownscaledCapacity : BOOL
VAR_INPUT
(* the new size that is needed *)
requestedSize : UDINT;
(* the current actual size of the container *)
currentCapacity : UDINT;
END_VAR
VAR_OUTPUT
(* the calculated size of the container *)
newCapacity : UDINT;
END_VAR
VAR CONSTANT
SHRINKING_THRESHOLD_MULTIPLIER :UDINT := 3;
END_VAR

Implementation

IF (requestedSize < THIS^.initialSize) OR_ELSE (currentCapacity = 0) THEN 
newCapacity := THIS^.initialSize;
ELSE
newCapacity := currentCapacity;
// gradual shrink by cubic root of 0.5 with threshold
WHILE ( newCapacity > SHRINKING_THRESHOLD_MULTIPLIER*requestedSize ) DO
newCapacity := TO_UDINT(TO_REAL(newCapacity) / THIS^.GROW_FACTOR);
END_WHILE
END_IF;

getDownscaledCapacity := (newCapacity <> currentCapacity);