/// <summary> /// Creates a new <see cref="DataValue" /> that has a value equal to all of the specified values merged together, using the /// specified /// pull behavior. /// To merge values, each bit is compared. /// If there is more than one non-<see cref="BitValue.Floating" /> value for that bit, the resulting bit is /// <see cref="BitValue.Error" />. /// If there is only one non-<see cref="BitValue.Floating" /> value for that bit, the resulting bit is equal to that value. /// If all values for that bit are <see cref="BitValue.Floating" />, the resulting bit is determined by the given /// <see cref="PullBehavior" />. /// </summary> /// <param name="values"> The values to merge. </param> /// <param name="pullBehavior"> /// The pull behavior to use when a bit for all input values is /// <see cref="BitValue.Floating" />. /// </param> /// <returns> A newly created <see cref="DataValue" />. </returns> public static DataValue Merge(ICollection <DataValue> values, PullBehavior pullBehavior) { Assert.Validate.Compare(values.Count, CompareOperation.Greater, 0, "No values to merge."); BitValue[] result = null; var bitWidth = 0; foreach (DataValue value in values) { if (result == null) { Assert.Validate.IsNotNull(value, "Cannot merge null value."); bitWidth = value.BitWidth; result = value.CopyBitValues(); continue; } Assert.Validate.IsNotNull(value, "Cannot merge null value."); Assert.Validate.AreEqual(value.BitWidth, bitWidth, "BitWidth of merged value does not equal bitWidth."); for (var i = 0; i < bitWidth; i++) { if (value.bitValues[i] != BitValue.Floating) { result[i] = result[i] == BitValue.Floating ? value.bitValues[i] : BitValue.Error; } } } result.Pull(pullBehavior, ref result); return(new DataValue(result)); }
/// <summary> /// Pulls the specified value using the given <see cref="PullBehavior" /> and stores the result in the array specified by output. Output can be equal to value. /// </summary> /// <param name="value">The value to pull.</param> /// <param name="pullBehavior">The pull behavior.</param> /// <param name="output">The output array.</param> public static void Pull(this BitValue[] value, PullBehavior pullBehavior, ref BitValue[] output) { Assert.Validate.IsNotNull(output, "Cannot perform operation. Output array is null"); Assert.Validate.AreEqual( value.Length, output.Length, "Cannot perform operation. Output is not the correct length."); if (pullBehavior == PullBehavior.Unchanged) { if (!object.ReferenceEquals(value, output)) { Array.Copy(value, output, value.Length); } return; } BitValue pulledValue = pullBehavior == PullBehavior.PullUp ? BitValue.One : BitValue.Zero; for (var i = 0; i < value.Length; i++) { output[i] = value[i] == BitValue.Floating ? pulledValue : value[i]; } }