Exemple #1
0
        /// <summary>
        /// Calculate the length of an encoded value in bits.
        /// </summary>
        /// <param name="allowZeros">(non-standard) Support zeros by automatically offsetting all values by one.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32?CalculateBitLength(UInt64 value)
        {
            // Offset for zero
            value++;

            return(BitOperation.CountUsed(value) * 2 - 1);
        }
        /// <summary>
        /// Append value to stream.
        /// </summary>
        /// <param name="value"></param>
        public void Write(UInt64 value)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            // Offset value to allow zeros
            value++;

            // #1 Separate X into the highest power of 2 it contains (2N) and the remaining N binary digits.
            Int32 n = 0;

            while (Math.Pow(2, n + 1) <= value)
            {
                n++;
            }
            var r = value - (UInt64)Math.Pow(2, n);

            // #2 Encode N+1 with Elias gamma coding.
            var np  = (UInt64)n + 1;
            var len = BitOperation.CountUsed(np);

            Output.Write(0, len - 1);
            Output.Write(np, len);

            // #3 Append the remaining N binary digits to this representation of N+1.
            Output.Write(r, n);
        }
        /// <summary>
        /// Calculate the length of an encoded value in bits.
        /// </summary>
        /// <param name="allowZeros">(non-standard) Support zeros by automatically offsetting all values by one.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32?CalculateBitLength(UInt64 value)
        {
            var result = 0;

            // Offset for zero
            value++;

            // #1 Separate X into the highest power of 2 it contains (2N) and the remaining N binary digits.
            Byte n = 0;

            while (Math.Pow(2, n + 1) <= value)
            {
                n++;
            }
            var r = value - (UInt64)Math.Pow(2, n);

            // #2 Encode N+1 with Elias gamma coding.
            var np  = (Byte)(n + 1);
            var len = BitOperation.CountUsed(np);

            result += len - 1;
            result += len;

            // #3 Append the remaining N binary digits to this representation of N+1.
            result += n;

            return(result);
        }
        /// <summary>
        /// Append value to stream.
        /// </summary>
        /// <param name="value"></param>
        public void Write(UInt64 value)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            // Offset value to allow zeros
            value++;

            // Count length
            var length = BitOperation.CountUsed(value);

            // Check not too large
            if (length > (LengthBits + 2) * 8)
            {
                throw new ArgumentOutOfRangeException("Value is greater than maximum of " + (UInt64.MaxValue >> 64 - LengthBits - 1) + ". Increase length bits to support larger numbers.");
            }

            // Clip MSB, it's redundant
            length--;

            // Write length
            Output.Write((UInt64)length, LengthBits);

            // Write number
            Output.Write(value, length);
        }
Exemple #5
0
        /// <summary>
        /// Append value to stream.
        /// </summary>
        /// <param name="value"></param>
        public void Write(UInt64 value)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            // Offset value to allow zeros
            value++;

            // Calculate length
            var length = BitOperation.CountUsed(value);

            // Write unary zeros
            Output.Write(0, length - 1);

            // Write value
            Output.Write(value, length);
        }
        /// <summary>
        /// Calculate the length of an encoded value in bits.
        /// </summary>
        /// <param name="lengthBits">Number of prefix bits used to store length.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32?CalculateBitLength(Int32 lengthBits, UInt64 value)
        {
            // Offset to allow for zero
            value++;

            // Calculate length
            var length = BitOperation.CountUsed(value);

            // Remove implied MSB
            length--;

            // Abort if it doesn't fit
            if (BitOperation.CountUsed((UInt64)length) > lengthBits)
            {
                return(null);
            }

            // Return size
            return(lengthBits + length);
        }
        /// <summary>
        /// Calculate the length of an encoded value in bits.
        /// </summary>
        /// <param name="allowZeros">(non-standard) Support zeros by automatically offsetting all values by one.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32?CalculateBitLength(UInt64 value)
        {
            var result = 1; // Termination bit

            // Offset value to allow for 0s
            value++;

            // #2 If N=1, stop; encoding is complete.
            while (value > 1)
            {
                // Calculate the length of value
                var length = BitOperation.CountUsed(value);

                // #3 Prepend the binary representation of N to the beginning of the code (this will be at least two bits, the first bit of which is a 1)
                result += length;

                // #4 Let N equal the number of bits just prepended, minus one.
                value = (UInt64)length - 1;
            }

            return(result);
        }
        /// <summary>
        /// Append value to stream.
        /// </summary>
        /// <param name="value"></param>
        public void Write(UInt64 value)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("this");
            }

            // Offset min value
            value++;

            // Prepare buffer
            var groups = new Stack <KeyValuePair <UInt64, Int32> >();

            // #1 Place a "0" at the end of the code.
            groups.Push(new KeyValuePair <UInt64, Int32>(0, 1));

            // #2 If N=1, stop; encoding is complete.
            while (value > 1)
            {
                // Calculate the length of value
                var length = BitOperation.CountUsed(value);

                // #3 Prepend the binary representation of N to the beginning of the code (this will be at least two bits, the first bit of which is a 1)
                groups.Push(new KeyValuePair <UInt64, Int32>(value, length));

                // #4 Let N equal the number of bits just prepended, minus one.
                value = (UInt64)length - 1;
            }

            // Write buffer
            foreach (var item in groups)
            {
                var bits  = item.Value;
                var group = item.Key;

                Output.Write(group, bits);
            }
        }
        public override Int32 CalculateBitLength(UInt64 symbol)
        {
            var packets = (Int32)Math.Ceiling((Single)BitOperation.CountUsed(symbol) / (Single)PacketSize);

            return(packets * (PacketSize + 1));
        }
Exemple #10
0
        /// <summary>
        /// Calculate the length of an encoded value in bits.
        /// </summary>
        /// <param name="packetSize">The number of bits to include in each packet.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32 CalculateBitLength(Int32 packetSize, UInt64 value)
        {
            var packets = (Int32)Math.Ceiling((Single)BitOperation.CountUsed(value) / (Single)packetSize);

            return(packets * (packetSize + 1));
        }