public abstract class SwitchStrategy extends Object
getBestStrategy(double[], JavaConstant[], LabelRef[])
method can be used to get
strategy with the smallest average effort (average number of comparisons until a decision is
reached). The strategy returned by this method will have its averageEffort set, while a strategy
constructed directly will not.Modifier and Type | Class and Description |
---|---|
static class |
SwitchStrategy.BaseSwitchClosure
Backends can subclass this abstract class and generate code for switch strategies by
implementing the
SwitchStrategy.BaseSwitchClosure.conditionalJump(int, Condition, Label) method. |
static class |
SwitchStrategy.BinaryStrategy
This strategy recursively subdivides the list of keys to create a binary search based on
probabilities.
|
private class |
SwitchStrategy.EffortClosure
This closure is used internally to determine the average effort for a certain strategy on a
given switch instruction.
|
static class |
SwitchStrategy.RangesStrategy
This strategy divides the keys into ranges of successive keys with the same target and
creates comparisons for these ranges.
|
static class |
SwitchStrategy.SequentialStrategy
This strategy orders the keys according to their probability and creates one equality
comparison per key.
|
private static interface |
SwitchStrategy.SwitchClosure |
Modifier and Type | Field and Description |
---|---|
private double |
averageEffort |
private SwitchStrategy.EffortClosure |
effortClosure |
JavaConstant[] |
keyConstants |
double[] |
keyProbabilities |
Constructor and Description |
---|
SwitchStrategy(double[] keyProbabilities,
JavaConstant[] keyConstants) |
Modifier and Type | Method and Description |
---|---|
double |
getAverageEffort() |
static SwitchStrategy |
getBestStrategy(double[] keyProbabilities,
JavaConstant[] keyConstants,
LabelRef[] keyTargets)
Creates all switch strategies for the given switch, evaluates them (based on average effort)
and returns the best one.
|
protected int |
getSliceEnd(SwitchStrategy.SwitchClosure closure,
int pos)
Looks for the end of a stretch of key constants that are successive numbers and have the same
target.
|
private static SwitchStrategy[] |
getStrategies(double[] keyProbabilities,
JavaConstant[] keyConstants,
LabelRef[] keyTargets) |
protected void |
registerDefaultEffort(int depth)
Tells the system that the default successor is reached after depth number of comparisons,
which is used to calculate average effort.
|
protected void |
registerEffort(int rangeStart,
int rangeEnd,
int depth)
Tells the system that the given (inclusive) range of keys is reached after depth number of
comparisons, which is used to calculate the average effort.
|
abstract void |
run(SwitchStrategy.SwitchClosure closure) |
String |
toString() |
public final double[] keyProbabilities
public final JavaConstant[] keyConstants
private double averageEffort
private SwitchStrategy.EffortClosure effortClosure
public SwitchStrategy(double[] keyProbabilities, JavaConstant[] keyConstants)
public double getAverageEffort()
protected int getSliceEnd(SwitchStrategy.SwitchClosure closure, int pos)
protected void registerEffort(int rangeStart, int rangeEnd, int depth)
protected void registerDefaultEffort(int depth)
public abstract void run(SwitchStrategy.SwitchClosure closure)
private static SwitchStrategy[] getStrategies(double[] keyProbabilities, JavaConstant[] keyConstants, LabelRef[] keyTargets)
public static SwitchStrategy getBestStrategy(double[] keyProbabilities, JavaConstant[] keyConstants, LabelRef[] keyTargets)