Exemplo n.º 1
0
        public void UInt64Array()
        {
            var n    = 1000;
            var rand = new MersenneTwisterFast(0L);
            var r    = new ulong[n];

            for (var i = 0; i < n; i++)
            {
                r[i] = (ulong)rand.NextLong();
            }
            var a = (ulong[])r.Clone();

            Array.Sort(r);      // Reference
            QuickSort.QSort(a); // Test

            // First we just ensure that values are sorted.
            var last = r[0];

            for (var i = 1; i < r.Length; i++)
            {
                Assert.IsTrue(r[i] >= last);
            }
            // Now we make sure both arrays are in the same order.
            for (var i = 0; i < a.Length; i++)
            {
                Assert.AreEqual(r[i], a[i]);
            }
        }
Exemplo n.º 2
0
        private ulong Run(int iterations)
        {
            ulong[] fish = new ulong[9];
            ulong[] next = (ulong[])fish.Clone();

            foreach (int start in _input)
            {
                fish[start]++;
            }

            for (int i = 0; i < iterations; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    next[j] = fish[j + 1];
                }

                next[6] += fish[0];
                next[8]  = fish[0];

                (fish, next) = (next, fish);
            }

            ulong total = 0;

            for (int i = 0; i < fish.Length; i++)
            {
                total += fish[i];
            }

            return(total);
        }
Exemplo n.º 3
0
        public EnumInfo(Type underlyingType, object[] rawValues, string[] names, bool isFlags)
        {
            Debug.Assert(rawValues.Length == names.Length);

            UnderlyingType = underlyingType;

            int numValues = rawValues.Length;

            ulong[] values = new ulong[numValues];
            for (int i = 0; i < numValues; i++)
            {
                object rawValue = rawValues[i];

                ulong rawUnboxedValue;
                if (rawValue is ulong)
                {
                    rawUnboxedValue = (ulong)rawValue;
                }
                else
                {
                    // This conversion is this way for compatibility: do a value-preseving cast to long - then store (and compare) as ulong. This affects
                    // the order in which the Enum apis return names and values.
                    rawUnboxedValue = (ulong)(((IConvertible)rawValue).ToInt64(null));
                }
                values[i] = rawUnboxedValue;
            }

            // Need to sort the `names` and `rawValues` arrays according to the `values` array
            ulong[] valuesCopy = (ulong[])values.Clone();
            Array.Sort(keys: valuesCopy, items: rawValues, comparer: Comparer <ulong> .Default);
            Array.Sort(keys: values, items: names, comparer: Comparer <ulong> .Default);

            Names  = names;
            Values = values;

            // Create the unboxed version of values for the Values property to return. (We didn't do this earlier because
            // declaring "rawValues" as "Array" would prevent us from using the generic overload of Array.Sort()).
            //
            // The array element type is the underlying type, not the enum type. (The enum type could be an open generic.)
            ValuesAsUnderlyingType = Type.GetTypeCode(UnderlyingType) switch
            {
                TypeCode.Byte => new byte[numValues],
                TypeCode.SByte => new sbyte[numValues],
                TypeCode.UInt16 => new ushort[numValues],
                TypeCode.Int16 => new short[numValues],
                TypeCode.UInt32 => new uint[numValues],
                TypeCode.Int32 => new int[numValues],
                TypeCode.UInt64 => new ulong[numValues],
                TypeCode.Int64 => new long[numValues],
                _ => throw new NotSupportedException(),
            };
            Array.Copy(rawValues, ValuesAsUnderlyingType, numValues);

            HasFlagsAttribute = isFlags;
        }
Exemplo n.º 4
0
        static ulong GetFibo(ulong n)
        {
            if (n + 1 == 0)
            {
                return(0);
            }
            if (n + 1 == 1)
            {
                return(1);
            }

            var root = new ulong[2][];

            for (ulong i = 0; i < 2; i++)
            {
                root[i] = new ulong[2];
            }
            root[0][0] = 1;
            root[0][1] = 1;
            root[1][0] = 1;
            root[1][1] = 0;

            var mul = new ulong[2][];

            for (ulong i = 0; i < 2; i++)
            {
                mul[i]    = new ulong[1];
                mul[i][0] = 1 - i;
            }

            var ret = (ulong[][])root.Clone();

            while (n > 0)
            {
                if ((n & 1) == 1)
                {
                    ret = Multi(ret, root, 2, 2, 2);
                }
                root = Multi(root, root, 2, 2, 2);
                n   /= 2;
            }

            ret = Multi(ret, mul, 2, 2, 1);
            //Show(ret, 2, 1);
            return(ret[1][0]);
        }