コード例 #1
0
 public BinaryExpressionNode(BinaryOperations op, ExpressionNode left, ExpressionNode right, SourceSpan sourceSpan)
     : base(sourceSpan)
 {
     this.op = op;
     this.left = left;
     this.right = right;
 }
コード例 #2
0
        public void SelfReferenceSubstituteTest()
        {
            var ast1 = new ArithmExprNode(1,
                                          new IdNode(1, "x"),
                                          ArithmOpNode.FromSymbol(1, "+"),
                                          new ArithmExprNode(1,
                                                             new IdNode(1, "y"),
                                                             ArithmOpNode.FromSymbol(1, "+"),
                                                             new IdNode(1, "x")
                                                             )
                                          );

            var repl = new ArithmExprNode(1,
                                          new IdNode(1, "y"),
                                          new ArithmOpNode(1, "-", BinaryOperations.ArithmeticFromSymbol("-")),
                                          new IdNode(1, "x")
                                          );
            var ast2 = new ArithmExprNode(1,
                                          new IdNode(1, "x"),
                                          ArithmOpNode.FromSymbol(1, "+"),
                                          new ArithmExprNode(1,
                                                             repl,
                                                             ArithmOpNode.FromSymbol(1, "+"),
                                                             new IdNode(1, "x")
                                                             )
                                          );

            Assert.That(ast1.Substitute(new IdNode(2, "y"), repl), Is.EqualTo(ast2));
        }
コード例 #3
0
        public void ReverseBits_0x8000000000000000_Is_0x1()
        {
            ulong input  = 0x8000000000000000;
            ulong result = 0x01;

            Assert.Equal(result, BinaryOperations.ReverseBits(input));
        }
コード例 #4
0
        public void SetBitsFromToRight_1_7()
        {
            // 0000 0000 0000//0000 0000.0000 0/000.0000 0000.00/00 0000.0000 000/0.0000 0111.1111
            ulong result = 0x000000000000007F;

            Assert.Equal(result, BinaryOperations.SetBitsFromToRight(1, 7));
        }
コード例 #5
0
        public void SetBitsFromToLeft_52_7()
        {
            // 00000000 0000//1111 1110.0000 0/000.0000 0000.00/00 0000.0000 000/0.0000 0000.0000
            ulong result = 0xFE00000000000;

            Assert.Equal(result, BinaryOperations.SetBitsFromToLeft(52, 7));
        }
コード例 #6
0
        public void SetBitsFromToLeft_52_1()
        {
            // 00000000 0000//1000 0000.0000 0/000.0000 0000.00/00 0000.0000 000/0.0000 0000.0000
            ulong result = 0x8000000000000;

            Assert.Equal(result, BinaryOperations.SetBitsFromToLeft(52, 1));
        }
コード例 #7
0
        private static bool IsStraightFoundation(long hand)
        {
            CardsCount = new Dictionary <long, int>();
            var        isStraight            = false;
            long       straightMask          = 0x1F00; // 1 1111 0000 0000
            const long exceptionStraightMask = 0x100F; // 1 0000 0000 1111

            Kickers = new long[1];
            var clubs = new Club(hand);

            for (var i = 0; i < (clubs.MaxSuitCards - 4); i++)
            {
                var total = hand & straightMask;
                if (straightMask == total)
                {
                    isStraight = true;
                    Kickers[0] = BinaryOperations.GetTheMostRightSetBit(straightMask);    // Set the highest card of the straight as the kicker
                    break;
                }
                straightMask >>= 1;
            }

            // Special Straight (ace is the bit of the highest weight and could match a straight with the four lowest bits
            if (!isStraight && exceptionStraightMask == (hand & exceptionStraightMask))
            {
                isStraight = true;
                Kickers[0] = 0x1;
            }

            return(isStraight);
        }
コード例 #8
0
        public int PerformCalculation(int firstNumber, int secondNumber, string operatorSymbol)
        {
            BinaryOperations execute = new BinaryOperations();

            if (operatorSymbol == OperatorConstants.AddSymbol.ToString())
            {
                secondNumber = execute.Addition(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.SubtractSymbol.ToString())
            {
                secondNumber = execute.Subtraction(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.DivideSymbol.ToString())
            {
                secondNumber = execute.Division(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.MultiplySymbol.ToString())
            {
                secondNumber = execute.Multiplication(firstNumber, secondNumber);

                return(secondNumber);
            }
            else
            {
                return(0);
            }
        }
コード例 #9
0
 public void AddBinaryOperation(IBinaryOperation operation)
 {
     if (!BinaryOperations.Contains(operation))
     {
         BinaryOperations.Add(operation);
     }
 }
コード例 #10
0
        public void SetBitsFromToRight_53_12()
        {
            // 1111 1111 1111//0000 0000.0000 0/000.0000 0000.00/00 0000.0000 000/0.0000 0000.0000
            ulong result = 0xFFF0000000000000;

            Assert.Equal(result, BinaryOperations.SetBitsFromToRight(53, 12));
        }
コード例 #11
0
        public void Insert_ArgumentOutOfRangeException()
        {
            var destination = Convert.ToInt32(TestContext.DataRow["Destination"]);
            var source      = Convert.ToInt32(TestContext.DataRow["Source"]);
            var startIndex  = Convert.ToInt32(TestContext.DataRow["StartIndex"]);
            var endIndex    = Convert.ToInt32(TestContext.DataRow["EndIndex"]);

            BinaryOperations.Insert(destination, source, startIndex, endIndex);
        }
コード例 #12
0
        private static void RegiterOperations()
        {
            BinaryOperations.Add(OperatorTypes.Add, Add);
            BinaryOperations.Add(OperatorTypes.Subtract, Subtract);
            BinaryOperations.Add(OperatorTypes.Divide, Divide);
            BinaryOperations.Add(OperatorTypes.Multiply, Multiply);
            BinaryOperations.Add(OperatorTypes.Power, Pow);

            UnaryOperations.Add(OperatorTypes.Sign, Sign);
        }
コード例 #13
0
        public void BinaryNumbersOperations_LeftMoreRight_Exception()
        {
            int first, second, leftPosition, rightPosition;

            first         = 8;
            second        = 15;
            leftPosition  = 7;
            rightPosition = 6;

            int actual = BinaryOperations.BinaryNumbersOperation(first, second, leftPosition, rightPosition);
        }
コード例 #14
0
        public void Insert_Positive()
        {
            var destination = Convert.ToInt32(TestContext.DataRow["Destination"]);
            var source      = Convert.ToInt32(TestContext.DataRow["Source"]);
            var startIndex  = Convert.ToInt32(TestContext.DataRow["StartIndex"]);
            var endIndex    = Convert.ToInt32(TestContext.DataRow["EndIndex"]);

            var expected = Convert.ToInt32(TestContext.DataRow["ExpectedResult"]);
            var actual   = BinaryOperations.Insert(destination, source, startIndex, endIndex);

            Assert.AreEqual(expected, actual);
        }
コード例 #15
0
        private static bool IsFormatV1(ReadOnlySpan <byte> buffer, out Endianness endianness)
        {
            endianness = Endianness.Little;

            // Need at least 256 bytes
            if (buffer.Length < 256)
            {
                return(false);
            }

            if (buffer[0] == 0)
            {
                // First byte is zero.
                // V1 not an option, as it would make for an invalid entry name
                // V2-3 are not an option either, as it would mean the entry count is 0
                // Either invalid, or big endian
                return(false);
            }

            bool nameTerminated = false;

            for (int i = 0; i < 252; i++)
            {
                if (buffer[i] == 0)
                {
                    nameTerminated = true;
                }

                // If the name has already been terminated but there's still data in the reserved space,
                // fail the test
                if (nameTerminated && buffer[i] != 0)
                {
                    return(false);
                }
            }

            var length = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(252));

            if (length < 0 || length >= MAX_LENGTH_SANITY_VALUE)
            {
                BinaryOperations <int> .Reverse(ref length);

                if (length < 0 || length >= MAX_LENGTH_SANITY_VALUE)
                {
                    return(false);
                }

                endianness = Endianness.Big;
            }

            return(true);
        }
コード例 #16
0
        public void BinaryNumbersOperations_PositiveTest()
        {
            int first, second, leftPosition, rightPosition;

            first         = 15;
            second        = int.MaxValue;
            leftPosition  = 3;
            rightPosition = 5;

            int expected = 63;
            int actual   = BinaryOperations.BinaryNumbersOperation(first, second, leftPosition, rightPosition);

            Assert.AreEqual(expected, actual);
        }
コード例 #17
0
        private void BtnDivide_Click(object sender, RoutedEventArgs e)
        {
            string trigger = "BtnDivide";

            sb.Clear();

            if ((subInput.HasValue == true &&
                 mainInput.HasValue == true))
            {
                this.Execute(subInput.Value, mainInput.Value);
                BinaryOperations bin = new BinaryOperations();
                SubInput.Text = bin.OperatorReplacer(SubInput.Text, trigger);
            }
            else if (subInput == null)
            {
                subInput       = int.Parse(MainInput.Text);
                Operator.Text  = OperatorConstants.DivideSymbol.ToString(); //
                SubInput.Text += sb.Append(subInput).ToString() + Operator.Text;

                if (mainInput != null && subInput != null)
                {
                    this.Execute(subInput.Value, mainInput.Value);
                }
            }
            else if (subInput != null && mainInput == null)
            {
                mainInput      = int.Parse(MainInput.Text);
                SubInput.Text += sb.Append(mainInput).ToString() + Operator.Text;
                this.Execute(subInput.Value, mainInput.Value);

                Operator.Text = OperatorConstants.DivideSymbol.ToString();//
                sb.Clear();

                /* Replace operator symbol*/
                int subLen = SubInput.Text.Length - 1;

                if (SubInput.Text != String.Empty &&
                    SubInput.Text.Substring(subLen, 1) != OperatorConstants.DivideSymbol.ToString()) //
                {
                    BinaryOperations bin = new BinaryOperations();
                    SubInput.Text = bin.OperatorReplacer(SubInput.Text, trigger);
                }
            }
            else
            {
                MessageBox.Show("An Error occured", "Warning");
            }
            isClicked    = true;
            equalTrigger = false;
        }
コード例 #18
0
        /// <summary>
        /// Generate the operation expression
        /// </summary>
        /// <typeparam name="T">Type of the values to apply operation on</typeparam>
        /// <param name="member">Member to apply operation on</param>
        /// <param name="targetValues">Values to apply operation on</param>
        /// <returns>Operation expression</returns>
        public Expression GetExpression <T>(Expression member, List <T> targetValues)
        {
            var type = member.Type;

            // check if the member is a method call
            if (IsMethodCall)
            {
                // get method by name
                var method = type.GetMethod(MethodName);
                if (method == null)
                {
                    throw new Exception($"There is no method named {this} in the type {type.Name}");
                }

                // get parameters list
                var parameters = method.GetParameters().ToList();
                // check if values match parameters count
                if (parameters.Count != targetValues.Count)
                {
                    throw new Exception($"There is no method named {this} in the type {type.Name} taking the same count of input");
                }

                // convert values to a list of ConstantExpressions
                var constants = parameters.Select(p => Expression.Constant(Convert.ChangeType(targetValues[parameters.IndexOf(p)], p.ParameterType))).ToList();
                // generate a MethodCallExpression
                return(Expression.Call(member, method, constants));
            }

            // if not a method call
            // then validate that the member type is one of the predefined operations
            if (Enum.TryParse(Type.ToString(), out ExpressionType expressionType))
            {
                // check if it is in the binary operations
                if (BinaryOperations.Contains(Type))
                {
                    // convert value to ConstantExpression
                    var constant = Expression.Constant(Convert.ChangeType(targetValues[0], type));
                    // generate a BinaryExpression
                    return(Expression.MakeBinary(expressionType, member, constant));
                }
                else if (UnaryOperations.Contains(Type))
                {
                    // generate a UnaryExpression
                    return(Expression.MakeUnary(expressionType, member, typeof(bool)));
                }
            }
            // throw an exception if operation not found
            throw new Exception("Unknown Operation");
        }
コード例 #19
0
        public void SubstractionInputArgumentReturnedValue(int leftValue, int rightValue, int expectedValue)
        {
            var leftNumber = new Number()
            {
                Value = leftValue
            };
            var rightNumber = new Number()
            {
                Value = rightValue
            };

            var result = BinaryOperations.Subsctraction(leftNumber, rightNumber);

            Assert.AreEqual(expectedValue, result.Value);
        }
コード例 #20
0
        public void DivisionReaminderInputArgumentReturnedValue(int leftValue, int rightValue, int expectedValue)
        {
            var leftNumber = new Number()
            {
                Value = leftValue
            };
            var rightNumber = new Number()
            {
                Value = rightValue
            };

            var result = BinaryOperations.DivisionReaminder(leftNumber, rightNumber);

            Assert.AreEqual(expectedValue, result.Value);
        }
コード例 #21
0
        public override string ToString()
        {
            string op = "";
            switch (this.op)
            {
                case BinaryOperations.Add:
                    op = "+";
                    break;

                case BinaryOperations.Subtract:
                    op = "-";
                    break;

                case BinaryOperations.Divide:
                    op = "/";
                    break;

                case BinaryOperations.Multiply:
                    op = "*";
                    break;

                case BinaryOperations.Modulo:
                    op = "%";
                    break;

                case BinaryOperations.LessThan:
                    op = "<";
                    break;

                case BinaryOperations.LessThanOrEqual:
                    op = "<=";
                    break;

                case BinaryOperations.GreaterThan:
                    op = ">";
                    break;

                case BinaryOperations.GreaterThanOrEqual:
                    op = ">=";
                    break;

                default:
                    throw Assert.Unreachable;
            }

            return left.ToString() + " " + op + " " + right.ToString() + ";";
        }
コード例 #22
0
ファイル: Program.cs プロジェクト: RandyLedbetter/PokerBot
        public static int GenerateAllCombinations(int numCards, int combinations)
        {
            var mask = new MaskBits(numCards, combinations);
            var totalCombinations = BinaryOperations.GetNumberOfCombinations(52ul, 7ul);
            var numCombinations   = 0;

            using (var file = new StreamWriter(@"poker-all-" + combinations + "-hands.txt"))
            {
                while (!mask.IsParsingComplete)
                {
                    file.WriteLine(mask.ToUint64());
                    mask.Decrement();
                    numCombinations++;
                    ConsoleHelper.DrawProgressBar(numCombinations, totalCombinations);
                }
            }
            return(numCombinations);
        }
 public override void PressBinaryOperationButton(BinaryOperations binaryOperation, CalculatorCore core)
 {
     SetState(core, new BinaryOperationIntroductionCalculatorCoreState());
     try
     {
         ApplyOtherBinaryOperations(binaryOperation, core);
     }
     catch (DivideByZeroException e)
     {
         AssignEnteredValueToTextBox(e.Message, core);
         SetState(core, new ExceptionCalculatorCoreState());
     }
     catch (OverflowException e)
     {
         AssignEnteredValueToTextBox(e.Message, core);
         SetState(core, new ExceptionCalculatorCoreState());
     }
 }
コード例 #24
0
        /// <summary>
        /// Математические операции с двумя векторами
        /// </summary>
        /// <param name="vector1">Первый вектор</param>
        /// <param name="vector2">Второй вектор</param>
        /// <param name="operation">Тип операции</param>
        /// <returns></returns>
        public static Vector4 vectorBinaryOperations(Vector4 vector1, Vector4 vector2, BinaryOperations operation)
        {
            Vector4 resultVector = new Vector4();

            switch (operation)
            {
            case BinaryOperations.ADDITION:
            {
                resultVector.x = vector1.x + vector2.x;
                resultVector.y = vector1.y + vector2.y;
                resultVector.z = vector1.z + vector2.z;
                resultVector.w = (vector1.w + vector2.w) > 1 ? 1 : 0;
                break;
            }

            case BinaryOperations.SUBSTRACTION:
            {
                resultVector.x = vector1.x - vector2.x;
                resultVector.y = vector1.y - vector2.y;
                resultVector.z = vector1.z - vector2.z;
                resultVector.w = (vector1.w - vector2.w) <= 0 ? 0 : 1;
                break;
            }

            case BinaryOperations.MULTIPLICATION:
            {
                resultVector.x = vector1.x * vector2.x;
                resultVector.y = vector1.y * vector2.y;
                resultVector.z = vector1.z * vector2.z;
                resultVector.w = vector1.w * vector2.w;
                break;
            }

            case BinaryOperations.DIVISION:
            {
                resultVector.x = vector1.x / vector2.x;
                resultVector.y = vector1.y / vector2.y;
                resultVector.z = vector1.z / vector2.z;
                resultVector.w = vector2.w == 0 ? vector1.w : vector1.w / vector2.w;
                break;
            }
            }
            return(resultVector);
        }
コード例 #25
0
 public void GenerateAllCombinations_8bits_1combination()
 {
     Assert.Equal(8, BinaryOperations.GenerateAllCombinations(8, 1));
 }
コード例 #26
0
        public void SetBitsFromToRight_8_1()
        {
            ulong result = 0x80;

            Assert.Equal(result, BinaryOperations.SetBitsFromToRight(8, 1));
        }
コード例 #27
0
        public void GetNumberOfCombinations_8_8()
        {
            int result = 1;

            Assert.Equal(result, BinaryOperations.GetNumberOfCombinations(8, 8));
        }
コード例 #28
0
        private static ExpressionType GetExpressionType(BinaryOperations op)
        {
            switch (op)
            {
                case BinaryOperations.Add:
                    return ExpressionType.Add;

                case BinaryOperations.Subtract:
                    return ExpressionType.Subtract;

                case BinaryOperations.Divide:
                    return ExpressionType.Divide;

                case BinaryOperations.Multiply:
                    return ExpressionType.Multiply;

                case BinaryOperations.Modulo:
                    return ExpressionType.Modulo;

                case BinaryOperations.LessThan:
                    return ExpressionType.LessThan;

                case BinaryOperations.LessThanOrEqual:
                    return ExpressionType.LessThanOrEqual;

                case BinaryOperations.GreaterThan:
                    return ExpressionType.GreaterThan;

                case BinaryOperations.GreaterThanOrEqual:
                    return ExpressionType.GreaterThanOrEqual;
                
                default:
                    throw Assert.Unreachable;
            }
        }
コード例 #29
0
        public void SetBitsFromToLeft_8_8()
        {
            ulong result = 0xFF;

            Assert.Equal(result, BinaryOperations.SetBitsFromToLeft(8, 8));
        }
コード例 #30
0
 public void GenerateAllCombinations_52bits_7combinations()
 {
     Assert.Equal(BinaryOperations.GetNumberOfCombinations(52, 7), BinaryOperations.GenerateAllCombinations(52, 7));
 }
コード例 #31
0
        public void GetNumberOfCombinations_52_7()
        {
            int result = 133784560;

            Assert.Equal(result, BinaryOperations.GetNumberOfCombinations(52, 7));
        }
コード例 #32
0
 public void GenerateAllCombinations_8bits_3combinations()
 {
     Assert.Equal(BinaryOperations.GetNumberOfCombinations(8, 3), BinaryOperations.GenerateAllCombinations(8, 3));
 }
コード例 #33
0
        public void GetNumberOfCombinations_0_0()
        {
            int result = 1;

            Assert.Equal(result, BinaryOperations.GetNumberOfCombinations(0, 0));
        }