Example #1
0
        public static bool Equal(BufBase b1, BufBase b2)
        {
            if (Object.ReferenceEquals(b1, null) && Object.ReferenceEquals(b2, null))
            {
                return(true);
            }
            if (Object.ReferenceEquals(b1, null) || Object.ReferenceEquals(b2, null))
            {
                return(false);
            }
            if (Object.ReferenceEquals(b1, b2))
            {
                return(true);
            }

            var b1len = b1.BaseArray.Length - b1.BaseArrayOffset;
            var b2len = b2.BaseArray.Length - b2.BaseArrayOffset;

            if (b1len != b2len)
            {
                return(false);
            }

            for (int i = 0; i < b1len; ++i)
            {
                if (b1.BaseArray[b1.BaseArrayOffset + i] != b2.BaseArray[b2.BaseArrayOffset + i])
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
 public static int operator -(BufRef left, BufLen right)
 {
     if (!BufBase.SameBuffer(left, right))
     {
         throw new InvalidOperationException("Can only subtract BufRef with the same underlying buffer!");
     }
     return(left.Position - right.BaseArrayOffset);
 }
Example #3
0
 bool IEquatable <BufBase> .Equals(BufBase other)
 {
     if (Object.ReferenceEquals(other, null))
     {
         return(false);
     }
     return(Equal(this, other));
 }
Example #4
0
 bool IEquatable <BufBase> .Equals(BufBase other)
 {
     if (other is null)
     {
         return(false);
     }
     return(Equal(this, other));
 }
Example #5
0
        public static int ComputeHash(BufBase data)
        {
            unchecked
            {
                const int p    = 16777619;
                int       hash = (int)2166136261;

                for (int i = data.BaseArrayOffset; i < data.BaseArray.Length; ++i)
                {
                    hash = (hash ^ data.BaseArray[i]) * p;
                }

                hash += hash << 13;
                hash ^= hash >> 7;
                hash += hash << 3;
                hash ^= hash >> 17;
                hash += hash << 5;
                return(hash);
            }
        }
Example #6
0
        public static int Compare(BufBase b1, BufBase b2)
        {
            if (Object.ReferenceEquals(b1, null) && Object.ReferenceEquals(b2, null))
            {
                return(0);
            }
            if (Object.ReferenceEquals(b1, null))
            {
                return(-1);
            }
            if (Object.ReferenceEquals(b2, null))
            {
                return(1);
            }
            if (Object.ReferenceEquals(b1, b2))
            {
                return(0);
            }

            var b1len = b1.BaseArray.Length - b1.BaseArrayOffset;
            var b2len = b2.BaseArray.Length - b2.BaseArrayOffset;

            for (int i = 0; i < b1len; ++i)
            {
                if (b2.BaseArrayOffset + i >= b2.BaseArray.Length)
                {
                    return(1);
                }
                var c = b1.BaseArray[b1.BaseArrayOffset + i] - b2.BaseArray[b2.BaseArrayOffset + i];
                if (c != 0)
                {
                    return(Math.Sign(c));
                }
            }

            if (b2len > b1len)
            {
                return(-1);
            }
            return(0);
        }
Example #7
0
 public BufLen(BufBase src, int offset, int len)
     : base(src, offset)
 {
     LengthDef = Math.Min(len, src.BaseArray.Length - src.BaseArrayOffset - offset);
 }
Example #8
0
 public static bool SameBuffer(BufBase left, BufBase right)
 {
     return(ReferenceEquals(left.Data, right.Data));
 }
Example #9
0
 public BufRef(BufBase src, int offset) : base(src, offset)
 {
 }
Example #10
0
 public BufRef(BufBase src) : base(src)
 {
 }
Example #11
0
 int IComparable <BufBase> .CompareTo(BufBase other)
 {
     return(Compare(this, other));
 }
Example #12
0
 public BufBase(BufBase src, int offset)
 {
     Data = src.Data; StartIx = Position = src.Position + offset;
 }
Example #13
0
 public BufBase(BufBase src)
 {
     Data = src.Data; StartIx = src.StartIx; Position = src.Position;
 }
Example #14
0
 public BufRefLen(BufBase src, int offset) : base(src, offset)
 {
     LengthDef = src.BaseArray.Length - src.BaseArrayOffset - offset;
 }
Example #15
0
 public BufRefLen(BufBase src) : base(src)
 {
     LengthDef = src.BaseArray.Length - src.BaseArrayOffset;
 }