Exemple #1
0
        public static RentedBuffer RentBuffer(int size)
        {
            if (__isBufferRented)
            {
                throw new InvalidOperationException("Thread static buffer is already in use.");
            }

            if (size <= 0 || size > MaxAllocationSize)
            {
                throw new ArgumentOutOfRangeException(nameof(size), size, $"Valid buffer size range is [1..{MaxAllocationSize}] bytes.");
            }

            __isBufferRented = true;

            if (__threadId == default)
            {
                __threadId = Thread.CurrentThread.ManagedThreadId;
            }

            if (size > MaxSize)
            {
                return(new RentedBuffer(__threadId, new byte[size]));
            }

            if (__buffer == null || __buffer.Length < size)
            {
                var newSize = size <= MinSize ? MinSize : PowerOf2.RoundUpToPowerOf2(size);
                __buffer = new byte[newSize];
            }

            return(new RentedBuffer(__threadId, __buffer));
        }
Exemple #2
0
        public void RoundUpToPowerOf2_should_throw_when_n_is_invalid(
            [Values(-1, 0x40000001)]
            int n)
        {
            Action action = () => PowerOf2.RoundUpToPowerOf2(n);

            action.ShouldThrow <ArgumentOutOfRangeException>().And.ParamName.Should().Be("n");
        }
Exemple #3
0
        public void IsPowerOf2_should_return_true_when_a_power_of_2(
            [Values(0, 1, 2, 4, 8, 128, 0x40000000)]
            int n)
        {
            var result = PowerOf2.IsPowerOf2(n);

            result.Should().BeTrue();
        }
Exemple #4
0
        public void IsPowerOf2_should_return_false_when_not_a_power_of_2(
            [Values(3, 5, 6, 7, 9, 127, 129, 0x37ffffff)]
            int n)
        {
            var result = PowerOf2.IsPowerOf2(n);

            result.Should().BeFalse();
        }
Exemple #5
0
        public void IsNumberPowerOf2_Book(int number, bool expected)
        {
            //Arrange

            //Act
            var isPowerOf2 = PowerOf2.IsNumberPowerOf2_Book(number);

            //Assert
            Assert.True(expected == isPowerOf2, $"IsNumberPowerOf2_Book should return true for {number}");
        }
Exemple #6
0
        /// <inheritdoc/>
        public void EnsureCapacity(int minimumCapacity)
        {
            if (minimumCapacity < 0)
            {
                throw new ArgumentOutOfRangeException("minimumCapacity");
            }
            ThrowIfDisposed();
            EnsureIsWritable();

            if (minimumCapacity > _bytes.Length)
            {
                var powerOf2 = Math.Max(32, PowerOf2.RoundUpToPowerOf2(minimumCapacity));
                SetCapacity(powerOf2);
            }
        }
Exemple #7
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="OutputBufferChunkSource"/> class.
        /// </summary>
        /// <param name="baseSource">The chunk source.</param>
        /// <param name="initialUnpooledChunkSize">The size of the initial unpooled chunk.</param>
        /// <param name="minChunkSize">The minimum size of a chunk.</param>
        /// <param name="maxChunkSize">The maximum size of a chunk.</param>
        public OutputBufferChunkSource(
            IBsonChunkSource baseSource,
            int initialUnpooledChunkSize = DefaultInitialUnpooledChunkSize,
            int minChunkSize             = DefaultMinChunkSize,
            int maxChunkSize             = DefaultMaxChunkSize)
        {
            if (baseSource == null)
            {
                throw new ArgumentNullException("baseSource");
            }
            if (initialUnpooledChunkSize < 0)
            {
                throw new ArgumentOutOfRangeException("initialUnpooledChunkSize");
            }
            if (minChunkSize <= 0)
            {
                throw new ArgumentOutOfRangeException("minChunkSize");
            }
            if (maxChunkSize <= 0)
            {
                throw new ArgumentOutOfRangeException("maxChunkSize");
            }
            if (!PowerOf2.IsPowerOf2(minChunkSize))
            {
                throw new ArgumentException("minChunkSize is not a power of 2.", "minChunkSize");
            }
            if (!PowerOf2.IsPowerOf2(maxChunkSize))
            {
                throw new ArgumentException("maxChunkSize is not a power of 2.", "maxChunkSize");
            }
            if (maxChunkSize < minChunkSize)
            {
                throw new ArgumentException("maxChunkSize is less than minChunkSize", "maxChunkSize");
            }

            _baseSource = baseSource;
            _initialUnpooledChunkSize = initialUnpooledChunkSize;
            _minChunkSize             = minChunkSize;
            _maxChunkSize             = maxChunkSize;
        }
        /// <inheritdoc/>
        public IBsonChunk GetChunk(int requestedSize)
        {
            if (requestedSize <= 0)
            {
                throw new ArgumentOutOfRangeException("requestedSize");
            }
            ThrowIfDisposed();

            if (requestedSize <= _maxUnpooledChunkSize)
            {
                return(new ByteArrayChunk(requestedSize));
            }

            var powerOf2Size = PowerOf2.RoundUpToPowerOf2(requestedSize);

            if (powerOf2Size - requestedSize > _minChunkSize)
            {
                powerOf2Size = powerOf2Size / 2;
            }
            var chunkSize = Math.Max(Math.Min(powerOf2Size, _maxChunkSize), _minChunkSize);

            return(_baseSource.GetChunk(chunkSize));
        }
Exemple #9
0
        /// <inheritdoc/>
        public IBsonChunk GetChunk(int requestedSize)
        {
            if (requestedSize <= 0)
            {
                throw new ArgumentOutOfRangeException("requestedSize");
            }
            ThrowIfDisposed();

            IBsonChunk chunk;

            if (_previousChunkSize == 0 && _initialUnpooledChunkSize != 0)
            {
                chunk = new ByteArrayChunk(_initialUnpooledChunkSize);
            }
            else
            {
                var powerOf2Size = PowerOf2.RoundUpToPowerOf2(_previousChunkSize + 1);
                var chunkSize    = Math.Max(Math.Min(powerOf2Size, _maxChunkSize), _minChunkSize);
                chunk = _baseSource.GetChunk(chunkSize);
            }

            _previousChunkSize = chunk.Bytes.Count;
            return(chunk);
        }
Exemple #10
0
        public void RoundUpToPowerOf2_should_return_expected_result(int n, int expectedResult)
        {
            var result = PowerOf2.RoundUpToPowerOf2(n);

            result.Should().Be(expectedResult);
        }
Exemple #11
0
        public static void Main(string[] args)
        {
            Console.WriteLine(" 1. Replace String \n 2. FlipCoin \n 3. LeapYear \n 4. PowerOf2 \n 5. HarmonicNo." +
                              " \n 6. PrimeFactor \n 7. WindChill Temperature \n 8. Euclidean distance \n 9. QuadraticEquation " +
                              "\n 10. FindTriplets \n 11. Gambler \n 12. 2DArray \n 13. CouponNumber \n 14. StopWatch \n 15. TicTaeToe" +
                              "\n 16. Vending Machine \n 17. Day of week \n 18. TemperatureConversion \n 19. MonthlyPayment \n 20. SquareRoot" +
                              "\n 21. BinaryConversion \n 22. SwapNibbles \n 23. ResultantNo is Power of two \n 24. BinarySearch \n 25. BubbleSort" +
                              "\n 26. Insertion Sort \n 27. Merge Sort \n 28. Anagram \n 29. Prime btw 1-1000 \n 30. Permutation Recursive \n 31. Permutation Iterartion ");
            int option = Utility.readInt();

            switch (option)
            {
            case 1:
                ReplaceString.Replace();
                break;

            case 2:
                FlipCoin.flipCoin();
                break;

            case 3:
                LeapYear.LeapYears();
                break;

            case 4:
                PowerOf2.PrintTable();
                break;

            case 5:
                Harmonic.HarmonicNo();
                break;

            case 6:
                PrimeFactor.Primefactor();
                break;

            case 7:
                WindChill.Chill();
                break;

            case 8:
                Distance.EuclideanDistance();
                break;

            case 9:
                QuadraticEquation.Quadratic();
                break;

            case 10:
                Sumof3No.SumZero();
                break;

            case 11:
                Gambler.Gambling();
                break;

            case 12:
                Array2D.IntegerArray();
                break;

            case 13:
                CouponNumber.couponNumber();
                break;

            case 14:
                StopWatch.Watch();
                break;

            case 15:
                TicTacToe.TictaeToeGame();
                break;

            case 16:
                VendingMachine.Atm();
                break;

            case 17:
                DayOfWeek.checkday();
                break;

            case 18:
                TemperatureConversion.Conversion();
                break;

            case 19:
                MonthlyPayment.PaymentMonthly();
                break;

            case 20:
                Sqrt.Root();
                break;

            case 21:
                Binary.DecToBinary();
                break;

            case 22:
                BinarySwaapNibble.SwapNibble();
                break;

            case 23:
                ResultantPower2.Resultant();
                break;

            case 24:
                BinarySearch.Search();
                break;

            case 25:
                BubbleSort.Bubblesort();
                break;

            case 26:
                Insertion.InsertionSorting();
                break;

            case 27:
                MergeSort.Mergesorting();
                break;

            case 28:
                Anagram.AnagramString();
                break;

            case 29:
                PrimeNoRange.Range();
                break;

            case 30:
                Permutation.Recursion();
                break;

            case 31:
                permutationIterative.Iterative();
                break;

            case 32:
                PlaindromePrimeNo.AnagramPrime();
                //PlaindromePrimeNo.Palindrome();
                break;
            }
        }
Exemple #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Class1 c1 = new Class1();

            Console.WriteLine($"Hello,World {c1.ReturnMessage()} \n");

            Sample sample = new Sample();

            Console.WriteLine($"Search Result : {sample.Search()}");

            Fibonacci fib = new Fibonacci();

            fib.generateFibonacci();

            AccessModifierPublic classPublic = new AccessModifierPublic(45);

            // public method
            classPublic.AreaOfCircle();

            // internal method
            classPublic.perimeterOfCircleInternal();

            // public method bypass for protected method and private method
            // classPublic.perimeterOfCircleBypass();

            // private method cannot be accessed
            // classPublic.perimeterOfCirclePrivate();

            // protectected method cannot be accessed
            // classPublic.perimeterOfCircleProtected

            // call main method of another class
            AccessModifierPublic.main(null);

            // static class method cannot be accessed by instance but by classname itself
            StaticClass.StaticMessage();

            ContactStaticClass.main(null);

            // from namespace iterators ,accessing static method
            ForeachExamples.ExampleOne();

            // calling main
            PowerOf2 power = new PowerOf2();

            PowerOf2.main(null);

            // calling main of static class
            GalaxyClass.main(null);

            Console.Clear();

            foreach (var item in IteratorMethods.GetSingleDigitNumbers())
            {
                Console.WriteLine(item);
            }

            foreach (var item in IteratorMethods.GetSingleDigitNumbersV2())
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\nGeneric Array ");

            GenericArray <int> genericArray = new GenericArray <int>(5);

            genericArray.setItem(0, 1);

            Console.WriteLine(genericArray.getItem(0));

            // will produce assertion error by making assertion false message displays
            Console.WriteLine(genericArray.getItem(6));

            Console.Clear();
        }