Пример #1
0
        public static int GetHashCodeSimple(this byte[] buffer)
        {
            const int CONSTANT = 17;

            unchecked {
                int hashCode = 37;

                ReinterpretArray common = new ReinterpretArray();
                common.AsByteArray = buffer;
                int[] array = common.AsInt32Array;

                int length    = buffer.Length;
                int remainder = length & 3;
                int len       = length >> 2;

                int i = 0;

                while (i < len)
                {
                    hashCode = CONSTANT * hashCode + array[i];
                    i++;
                }

                if (remainder > 0)
                {
                    int shift = sizeof(uint) - remainder;
                    hashCode = CONSTANT * hashCode + ((array[i] << shift) >> shift);
                }

                return(hashCode);
            }
        }
Пример #2
0
        public int Compare(byte[] x, byte[] y, int length)
        {
            if (x == null && y == null && length == 0)
            {
                return(0);
            }

            ReinterpretArray common = new ReinterpretArray();

            common.AsByteArray = x;
            ulong[] array1 = common.AsUInt64Array;
            common.AsByteArray = y;
            ulong[] array2 = common.AsUInt64Array;

            int len       = length >> 3;
            int remainder = length & 7;

            int i = len;

            if (remainder > 0)
            {
                int shift = sizeof(ulong) - remainder;
                var v1    = (array1[i] << shift) >> shift;
                var v2    = (array2[i] << shift) >> shift;
                if (v1 < v2)
                {
                    return(-1);
                }
                if (v1 > v2)
                {
                    return(1);
                }
            }

            i--;

            while (i >= 0)
            {
                var v1 = array1[i];
                var v2 = array2[i];
                if (v1 < v2)
                {
                    return(-1);
                }
                if (v1 > v2)
                {
                    return(1);
                }

                i--;
            }

            return(0);
        }
Пример #3
0
        public bool Equals(byte[] x, byte[] y)
        {
            if (x == null && y == null)
            {
                return(true);
            }

            if (x.Length != y.Length)
            {
                return(false);
            }

            ReinterpretArray common = new ReinterpretArray();

            common.AsByteArray = x;
            ulong[] array1 = common.AsUInt64Array;
            common.AsByteArray = y;
            ulong[] array2 = common.AsUInt64Array;

            int length    = x.Length;
            int remainder = length & 7;
            int len       = length >> 3;

            int i = 0;

            while (i + 7 < len)
            {
                if (array1[i] != array2[i] ||
                    array1[i + 1] != array2[i + 1] ||
                    array1[i + 2] != array2[i + 2] ||
                    array1[i + 3] != array2[i + 3] ||
                    array1[i + 4] != array2[i + 4] ||
                    array1[i + 5] != array2[i + 5] ||
                    array1[i + 6] != array2[i + 6] ||
                    array1[i + 7] != array2[i + 7])
                {
                    return(false);
                }

                i += 8;
            }

            if (i + 3 < len)
            {
                if (array1[i] != array2[i] ||
                    array1[i + 1] != array2[i + 1] ||
                    array1[i + 2] != array2[i + 2] ||
                    array1[i + 3] != array2[i + 3])
                {
                    return(false);
                }

                i += 4;
            }

            if (i + 1 < len)
            {
                if (array1[i] != array2[i] ||
                    array1[i + 1] != array2[i + 1])
                {
                    return(false);
                }

                i += 2;
            }

            if (i < len)
            {
                if (array1[i] != array2[i])
                {
                    return(false);
                }

                i += 1;
            }

            if (remainder > 0)
            {
                int shift = sizeof(ulong) - remainder;
                if ((array1[i] << shift) >> shift != (array2[i] << shift) >> shift)
                {
                    return(false);
                }
            }

            return(true);
        }