예제 #1
0
 public bool BufferEquals(BitBuffer other)
 {
     if (Length != other.Length)
     {
         return(false);
     }
     if (null == data && null == other.data)
     {
         return(true);
     }
     if (null == data ^ null == other.data)
     {
         return(false);
     }
     for (int i = 0; i < Length / 8; i++)
     {
         if (GetByteAt(i * 8) != other.GetByteAt(i * 8))
         {
             return(false);
         }
     }
     if (GetByteAt(~0x7 & Length, 0x7 & Length) != other.GetByteAt(~0x7 & Length, 0x7 & Length))
     {
         return(false);
     }
     return(true);
 }
예제 #2
0
        public void PutAt(int offset, BitBuffer src)
        {
            if (absOffset + offset + src.Length > absLength)
            {
                throw new IndexOutOfRangeException(MsgIOORE(true, absOffset + offset, src.Length));
            }

            if (simulateWrites)
            {
                return;
            }

            int srcByteAlignment = 0x7 & src.absOffset;
            int dstByteAlignment = 0x7 & (absOffset + offset);

            bool srcArrIsByteAligned = 0 == srcByteAlignment;
            bool dstArrIsByteAligned = 0 == dstByteAlignment;

            if (srcByteAlignment == dstByteAlignment && !srcArrIsByteAligned)
            {
                // Both have the same alineation but are not byte aligned. Copy first and last byte manually and the rest using blockcopy
                int bitsToByteAlign = 8 - srcByteAlignment;
                int bitsToWrite     = src.Length >= bitsToByteAlign ? bitsToByteAlign : src.Length;
                PutAt(offset, src.GetByteAt(0, bitsToWrite), bitsToWrite);
                if (src.Length > bitsToByteAlign)
                {
                    PutAt(offset + bitsToByteAlign, new BitBuffer(src.data, src.absOffset + bitsToByteAlign, src.Length - bitsToByteAlign));
                }
            }
            else
            {
                int writtenLength = ~0x7 & src.Length;
                if (writtenLength > 0)
                {
                    if (srcArrIsByteAligned && dstArrIsByteAligned)
                    {
                        Buffer.BlockCopy(src.data, src.absOffset / 8, data, (absOffset + offset) / 8, src.Length / 8);
                    }
                    else
                    {
                        for (int i = 0; i < src.Length / 8; i++)
                        {
                            PutAt(offset + i * 8, src.GetByteAt(i * 8));
                        }
                    }
                }
                int lastBitsCount = 0x7 & src.Length;
                if (0 != lastBitsCount)
                {
                    byte lastByte = src.GetByteAt(writtenLength, lastBitsCount);
                    PutAt(offset + writtenLength, lastByte, lastBitsCount);
                }
            }
        }