/// <summary> /// Find the best Bitfield and the best BitfieldSet, and choose whichever has the highest quality. /// </summary> /// <param name="maxBits">Maximum number of bits to select.</param> /// <returns></returns> private SwitchableNode?FindBestSwitchable(int maxBits) { Debug.Assert(maxBits >= 0 && maxBits < spec.NumBits); // find a single bitfield on which we can switch Bitfield?bitfield = analyser.FindBestBitfield(spec.Config.MinSwitchBits, maxBits); if (bitfield == null) { // if we can't find even a low-quality single bitfield, // we won't find a split one, so just fail return(null); } if (spec.Config.Verbose) { Console.WriteLine("Best single bitfield has quality {0}", bitfield.Quality); } SwitchableNode node = bitfield; // find a split bitfield BitfieldSet?bitfieldSet = analyser.FindBestBitfieldSet(spec.Config.MinSwitchBits, maxBits); if (bitfieldSet != null) { if (spec.Config.Verbose) { Console.WriteLine("Best bitfield set has quality {0}", bitfieldSet.Quality); } // select either the single or the split bitfield, depending on // which has the highest quality if (bitfieldSet.Quality > bitfield.Quality) { node = bitfieldSet; } } return(node); }
/// <summary> /// Actually build the switch node, given the supplied expression. /// </summary> /// <param name="expression">Either a bitfield or a bitfield set.</param> /// <returns>Bitfield node object.</returns> private SwitchNode BuildSwitch(SwitchableNode expression) { if (spec.Config.Verbose) { Console.WriteLine("Building switch node for {0}.", expression.ToString()); } var node = new SwitchNode(spec, expression); // iterate through every possible value of 'expression' for (int value = 0; value < expression.NumValues; value++) { // store child in switch node's table node[value] = BuildSwitchCase(node, value); } if (spec.Config.Verbose) { Console.WriteLine("Switch completed."); } return(node); }
/// <summary> /// Initializes a new instance of the <see cref="SwitchNode"/> class. /// </summary> public SwitchNode(Specification spec, SwitchableNode expression) : base(spec) { this.expression = expression; children = new Node[expression.NumValues]; }