Beispiel #1
0
        public override MessageBundle PickPiece(PeerId id, BitField peerBitfield, List <PeerId> otherPeers, int count, int startIndex, int endIndex)
        {
            // Fast Path - the peer has nothing to offer
            if (peerBitfield.AllFalse)
            {
                return(null);
            }

            if (files.Count == 1)
            {
                if (files[0].File.Priority == Priority.DoNotDownload)
                {
                    return(null);
                }
                else
                {
                    return(base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
                }
            }

            files.Sort();

            // Fast Path - all the files have been set to DoNotDownload
            if (files[0].File.Priority == Priority.DoNotDownload)
            {
                return(null);
            }

            // Fast Path - If all the files are the same priority, call straight into the base picker
            if (files.TrueForAll(AllSamePriority))
            {
                return(base.PickPiece(id, peerBitfield, otherPeers, count, startIndex, endIndex));
            }

            temp.From(files[0].Selector);
            for (int i = 1; i < files.Count && files[i].File.Priority != Priority.DoNotDownload; i++)
            {
                if (files[i].File.Priority != files[i - 1].File.Priority)
                {
                    temp.And(peerBitfield);
                    if (!temp.AllFalse)
                    {
                        MessageBundle message = base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex);
                        if (message != null)
                        {
                            return(message);
                        }
                        temp.SetAll(false);
                    }
                }

                temp.Or(files[i].Selector);
            }

            if (temp.AllFalse || temp.And(peerBitfield).AllFalse)
            {
                return(null);
            }
            return(base.PickPiece(id, temp, otherPeers, count, startIndex, endIndex));
        }
Beispiel #2
0
        /// <summary>
        /// Performs a bitwise not operation on the (partially) known integer value.
        /// </summary>
        public virtual void Not()
        {
            Span <byte> bitsBuffer = stackalloc byte[Size];
            Span <byte> maskBuffer = stackalloc byte[Size];

            GetBits(bitsBuffer);
            GetMask(maskBuffer);

            var bits = new BitField(bitsBuffer);
            var mask = new BitField(maskBuffer);

            bits.Not();
            bits.And(mask);

            SetBits(bitsBuffer, maskBuffer);
        }
Beispiel #3
0
        public void And2()
        {
            var random = new Random();
            var a      = new byte [100];
            var b      = new byte [100];

            random.NextBytes(a);
            random.NextBytes(b);

            for (var i = 0; i < a.Length * 8; i++)
            {
                var first  = new BitField(a, i);
                var second = new BitField(b, i);

                first.And(second);
            }
        }
        public void And2()
        {
            var r = new Random();
            var a = new byte[100];
            var b = new byte[100];

            r.NextBytes(a);
            r.NextBytes(b);

            for (var i = 0; i < a.Length*8; i++)
            {
                var first = new BitField(a, i);
                var second = new BitField(b, i);

                first.And(second);
            }
        }
Beispiel #5
0
        public void And()
        {
            var bf2 = new BitField(_secondValues);

            _bf.And(bf2);

            Assert.AreEqual(new BitField(_secondValues), bf2, "#1: bf2 should be unmodified");
            for (var i = 0; i < _bf.Length; i++)
            {
                Assert.AreEqual(_initalValues[i] && _secondValues[i], _bf[i], "#2");
            }

            var count = _initalValues
                        .Where((x, i) => x && _secondValues[i])
                        .Count();

            Assert.AreEqual(count, _bf.TrueCount, "#3");
        }
        public void And2()
        {
            Random r = new Random();

            byte [] a = new byte [100];
            byte [] b = new byte [100];

            r.NextBytes(a);
            r.NextBytes(b);

            for (int i = 0; i < a.Length * 8; i++)
            {
                BitField first  = new BitField(a, i);
                BitField second = new BitField(b, i);

                first.And(second);
            }
        }
        public void And()
        {
            BitField bf2 = new BitField(secondValues);

            bf.And(bf2);

            Assert.AreEqual(new BitField(secondValues), bf2, "#1: bf2 should be unmodified");
            for (int i = 0; i < bf.Length; i++)
            {
                Assert.AreEqual(initalValues[i] && secondValues[i], bf[i], "#2");
            }

            int count = 0;

            for (int i = 0; i < initalValues.Length; i++)
            {
                if (initalValues[i] && secondValues[i])
                {
                    count++;
                }
            }

            Assert.AreEqual(count, bf.TrueCount, "#3");
        }
Beispiel #8
0
        public void And()
        {
            BitField bf2 = new BitField(secondValues);

            bf.And(bf2);

            Assert.Equal(new BitField(secondValues), bf2);
            for (int i = 0; i < bf.Length; i++)
            {
                Assert.Equal(initalValues[i] && secondValues[i], bf[i]);
            }

            int count = 0;

            for (int i = 0; i < initalValues.Length; i++)
            {
                if (initalValues[i] && secondValues[i])
                {
                    count++;
                }
            }

            Assert.Equal(count, bf.TrueCount);
        }
Beispiel #9
0
        public void And2 ()
        {
            Random r = new Random ();
            byte [] a = new byte [100];
            byte [] b = new byte [100];

            r.NextBytes (a);
            r.NextBytes (b);

            for (int i = 0; i < a.Length * 8; i++) {
                BitField first = new BitField (a, i);
                BitField second = new BitField (b, i);

                first.And (second);
            }
        }
        public void Test()
        {
            var bitField = new BitField(0, false);

            Assert.AreEqual(bitField.Length, 0);
            Assert.AreEqual(bitField[1], false);

            int size = Rand.Default.Range(1, 256);

            bitField = new BitField(size, false);
            BitArray bitArray = new BitArray(size, false);

            check(bitField, bitArray);

            size = Rand.Default.Range(1, 256);
            bool[] boolData = new bool[size];
            for (int i = 0; i < boolData.Length; i++)
            {
                boolData[i] = Rand.Default.Range(0, 2) > 0;
            }
            bitField = new BitField(boolData);
            bitArray = new BitArray(boolData);
            check(bitField, bitArray);

            size = Rand.Default.Range(1, 64);
            byte[] byteData = new byte[size];
            for (int i = 0; i < byteData.Length; i++)
            {
                byteData[i] = Rand.Default.RandByte();
            }
            bitField = new BitField(byteData);
            bitArray = new BitArray(byteData);
            check(bitField, bitArray);

            size = Rand.Default.Range(1, 32);
            int[] intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitField = new BitField(intData);
            bitArray = new BitArray(intData);
            check(bitField, bitArray);

            size    = Rand.Default.Range(1, 32);
            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitArray = new BitArray(intData);
            bitField = new BitField(bitArray);
            check(bitField, bitArray);

            size    = Rand.Default.Range(1, 32);
            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitField = new BitField(intData);
            bitField = new BitField(bitField);
            bitArray = new BitArray(intData);
            check(bitField, bitArray);

            for (int i = 0; i < bitField.Length; i++)
            {
                bool value = Rand.Default.Range(0, 2) > 0;
                bitField.Set(i, value);
                bitArray.Set(i, value);
            }
            check(bitField, bitArray);

            Assert.IsTrue(bitField.Any());

            bitField.SetAll(false);
            Assert.IsFalse(bitField.Any());

            bitField.SetAll(true);
            bitArray.SetAll(true);
            check(bitField, bitArray);

            size    = Rand.Default.Range(1, 32);
            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitField = new BitField(intData);
            bitArray = new BitArray(intData);
            check(bitField, bitArray);

            bitField = bitField.Not();
            bitArray = bitArray.Not();
            check(bitField, bitArray);

            size    = Rand.Default.Range(1, 32);
            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitField = new BitField(intData);
            bitArray = new BitArray(intData);
            check(bitField, bitArray);

            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            var bitFieldTwo = new BitField(intData);
            var bitArrayTwo = new BitArray(intData);

            check(bitFieldTwo, bitArrayTwo);

            bitField = bitField.Or(bitFieldTwo);
            bitArray = bitArray.Or(bitArrayTwo);
            check(bitField, bitArray);

            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitFieldTwo = new BitField(intData);
            bitArrayTwo = new BitArray(intData);
            check(bitFieldTwo, bitArrayTwo);

            bitField = bitField.Xor(bitFieldTwo);
            bitArray = bitArray.Xor(bitArrayTwo);
            check(bitField, bitArray);

            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }
            bitFieldTwo = new BitField(intData);
            bitArrayTwo = new BitArray(intData);
            check(bitFieldTwo, bitArrayTwo);

            bitField = bitField.And(bitFieldTwo);
            bitArray = bitArray.And(bitArrayTwo);
            check(bitField, bitArray);

            bitFieldTwo = bitField.Clone() as BitField;
            Assert.IsNotNull(bitFieldTwo);
            check(bitFieldTwo, bitArray);

            size     = Rand.Default.Range(1, 256);
            boolData = new bool[size];
            for (int i = 0; i < boolData.Length; i++)
            {
                boolData[i] = Rand.Default.Range(0, 2) > 0;
            }

            bitField = new BitField(boolData);
            var boolDataTwo = new bool[size];

            bitField.CopyTo(boolDataTwo, 0);
            for (int i = 0; i < boolData.Length; i++)
            {
                Assert.AreEqual(boolData[i], boolDataTwo[i]);
            }

            size     = Rand.Default.Range(1, 64);
            byteData = new byte[size];
            for (int i = 0; i < byteData.Length; i++)
            {
                byteData[i] = Rand.Default.RandByte();
            }

            bitField = new BitField(byteData);
            var byteDataTwo = new byte[size];

            bitField.CopyTo(byteDataTwo, 0);
            for (int i = 0; i < byteData.Length; i++)
            {
                Assert.AreEqual(byteData[i], byteDataTwo[i]);
            }

            size    = Rand.Default.Range(1, 32);
            intData = new int[size];
            for (int i = 0; i < intData.Length; i++)
            {
                intData[i] = Rand.Default.RandInt();
            }

            bitField = new BitField(intData);
            var intDataTwo = new int[size];

            bitField.CopyTo(intDataTwo, 0);
            for (int i = 0; i < intData.Length; i++)
            {
                Assert.AreEqual(intData[i], intDataTwo[i]);
            }
        }