Ejemplo n.º 1
0
            internal virtual void WriteHeader(bool reverse, int cleanLength, int dirtyLength)
            {
                int cleanLengthMinus2 = cleanLength - 2;

                Debug.Assert(cleanLengthMinus2 >= 0);
                Debug.Assert(dirtyLength >= 0);
                int token = ((cleanLengthMinus2 & 0x03) << 4) | (dirtyLength & 0x07);

                if (reverse)
                {
                    token |= 1 << 7;
                }
                if (cleanLengthMinus2 > 0x03)
                {
                    token |= 1 << 6;
                }
                if (dirtyLength > 0x07)
                {
                    token |= 1 << 3;
                }
                @out.WriteByte((byte)(sbyte)token);
                if (cleanLengthMinus2 > 0x03)
                {
                    @out.WriteVInt((int)((uint)cleanLengthMinus2 >> 2));
                }
                if (dirtyLength > 0x07)
                {
                    @out.WriteVInt((int)((uint)dirtyLength >> 3));
                }
            }
Ejemplo n.º 2
0
            internal virtual void WriteHeader(bool reverse, int cleanLength, int dirtyLength)
            {
                int cleanLengthMinus2 = cleanLength - 2;

                if (Debugging.AssertsEnabled)
                {
                    Debugging.Assert(cleanLengthMinus2 >= 0);
                    Debugging.Assert(dirtyLength >= 0);
                }
                int token = ((cleanLengthMinus2 & 0x03) << 4) | (dirtyLength & 0x07);

                if (reverse)
                {
                    token |= 1 << 7;
                }
                if (cleanLengthMinus2 > 0x03)
                {
                    token |= 1 << 6;
                }
                if (dirtyLength > 0x07)
                {
                    token |= 1 << 3;
                }
                @out.WriteByte((byte)token);
                if (cleanLengthMinus2 > 0x03)
                {
                    @out.WriteVInt32(cleanLengthMinus2.TripleShift(2));
                }
                if (dirtyLength > 0x07)
                {
                    @out.WriteVInt32(dirtyLength.TripleShift(3));
                }
            }
Ejemplo n.º 3
0
            internal virtual void PforEncode()
            {
                if (numExceptions > 0)
                {
                    int mask = (1 << bitsPerValue) - 1;
                    int ex   = 0;
                    for (int i = 0; i < bufferSize; ++i)
                    {
                        if (buffer[i] > mask)
                        {
                            exceptionIndices[ex] = i;
                            exceptions[ex++]     = (int)((uint)buffer[i] >> bitsPerValue);
                            buffer[i]           &= mask;
                        }
                    }
                    if (Debugging.AssertsEnabled)
                    {
                        Debugging.Assert(ex == numExceptions);
                    }
                    Arrays.Fill(exceptions, numExceptions, BLOCK_SIZE, 0);
                }

                if (bitsPerValue > 0)
                {
                    PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(PackedInt32s.Format.PACKED, PackedInt32s.VERSION_CURRENT, bitsPerValue);
                    int numIterations             = ITERATIONS[bitsPerValue];
                    encoder.Encode(buffer, 0, data.Bytes, data.Length, numIterations);
                    data.Length += encoder.ByteBlockCount * numIterations;
                }

                if (numExceptions > 0)
                {
                    if (Debugging.AssertsEnabled)
                    {
                        Debugging.Assert(bitsPerException > 0);
                    }
                    data.WriteByte((byte)(sbyte)numExceptions);
                    data.WriteByte((byte)(sbyte)bitsPerException);
                    PackedInt32s.IEncoder encoder = PackedInt32s.GetEncoder(PackedInt32s.Format.PACKED, PackedInt32s.VERSION_CURRENT, bitsPerException);
                    int numIterations             = (numExceptions + encoder.ByteValueCount - 1) / encoder.ByteValueCount;
                    encoder.Encode(exceptions, 0, data.Bytes, data.Length, numIterations);
                    data.Length += (int)PackedInt32s.Format.PACKED.ByteCount(PackedInt32s.VERSION_CURRENT, numExceptions, bitsPerException);
                    for (int i = 0; i < numExceptions; ++i)
                    {
                        data.WriteByte((byte)(sbyte)exceptionIndices[i]);
                    }
                }
            }
Ejemplo n.º 4
0
            internal virtual void PforEncode()
            {
                if (NumExceptions > 0)
                {
                    int mask = (1 << BitsPerValue) - 1;
                    int ex   = 0;
                    for (int i = 0; i < BufferSize; ++i)
                    {
                        if (Buffer[i] > mask)
                        {
                            ExceptionIndices[ex] = i;
                            Exceptions[ex++]     = (int)((uint)Buffer[i] >> BitsPerValue);
                            Buffer[i]           &= mask;
                        }
                    }
                    Debug.Assert(ex == NumExceptions);
                    Arrays.Fill(Exceptions, NumExceptions, BLOCK_SIZE, 0);
                }

                if (BitsPerValue > 0)
                {
                    PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerValue);
                    int numIterations          = ITERATIONS[BitsPerValue];
                    encoder.Encode(Buffer, 0, Data.Bytes, Data.Length, numIterations);
                    Data.Length += encoder.ByteBlockCount() * numIterations;
                }

                if (NumExceptions > 0)
                {
                    Debug.Assert(BitsPerException > 0);
                    Data.WriteByte((sbyte)NumExceptions);
                    Data.WriteByte((sbyte)BitsPerException);
                    PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerException);
                    int numIterations          = (NumExceptions + encoder.ByteValueCount() - 1) / encoder.ByteValueCount();
                    encoder.Encode(Exceptions, 0, Data.Bytes, Data.Length, numIterations);
                    Data.Length += (int)PackedInts.Format.PACKED.ByteCount(PackedInts.VERSION_CURRENT, NumExceptions, BitsPerException);
                    for (int i = 0; i < NumExceptions; ++i)
                    {
                        Data.WriteByte((sbyte)ExceptionIndices[i]);
                    }
                }
            }
Ejemplo n.º 5
0
            internal virtual void AddWord(int wordNum, byte word)
            {
                Debug.Assert(wordNum > LastWordNum);
                Debug.Assert(word != 0);

                if (!Reverse)
                {
                    if (LastWordNum == -1)
                    {
                        Clean = 2 + wordNum; // special case for the 1st sequence
                        DirtyWords.WriteByte(word);
                    }
                    else
                    {
                        switch (wordNum - LastWordNum)
                        {
                        case 1:
                            if (word == 0xFF && (byte)DirtyWords.Bytes[DirtyWords.Length - 1] == 0xFF)
                            {
                                --DirtyWords.Length;
                                WriteSequence();
                                Reverse = true;
                                Clean   = 2;
                            }
                            else
                            {
                                DirtyWords.WriteByte(word);
                            }
                            break;

                        case 2:
                            DirtyWords.WriteByte(0);
                            DirtyWords.WriteByte(word);
                            break;

                        default:
                            WriteSequence();
                            Clean = wordNum - LastWordNum - 1;
                            DirtyWords.WriteByte(word);
                            break;
                        }
                    }
                }
                else
                {
                    Debug.Assert(LastWordNum >= 0);
                    switch (wordNum - LastWordNum)
                    {
                    case 1:
                        if (word == 0xFF)
                        {
                            if (DirtyWords.Length == 0)
                            {
                                ++Clean;
                            }
                            else if ((byte)DirtyWords.Bytes[DirtyWords.Length - 1] == 0xFF)
                            {
                                --DirtyWords.Length;
                                WriteSequence();
                                Clean = 2;
                            }
                            else
                            {
                                DirtyWords.WriteByte(word);
                            }
                        }
                        else
                        {
                            DirtyWords.WriteByte(word);
                        }
                        break;

                    case 2:
                        DirtyWords.WriteByte(0);
                        DirtyWords.WriteByte(word);
                        break;

                    default:
                        WriteSequence();
                        Reverse = false;
                        Clean   = wordNum - LastWordNum - 1;
                        DirtyWords.WriteByte(word);
                        break;
                    }
                }
                LastWordNum  = wordNum;
                Cardinality += BitUtil.BitCount(word);
            }
Ejemplo n.º 6
0
            internal virtual void AddWord(int wordNum, byte word)
            {
                if (Debugging.AssertsEnabled)
                {
                    Debugging.Assert(wordNum > lastWordNum);
                    Debugging.Assert(word != 0);
                }

                if (!reverse)
                {
                    if (lastWordNum == -1)
                    {
                        clean = 2 + wordNum; // special case for the 1st sequence
                        dirtyWords.WriteByte(word);
                    }
                    else
                    {
                        switch (wordNum - lastWordNum)
                        {
                        case 1:
                            if (word == 0xFF && (byte)dirtyWords.Bytes[dirtyWords.Length - 1] == 0xFF)
                            {
                                --dirtyWords.Length;
                                WriteSequence();
                                reverse = true;
                                clean   = 2;
                            }
                            else
                            {
                                dirtyWords.WriteByte(word);
                            }
                            break;

                        case 2:
                            dirtyWords.WriteByte(0);
                            dirtyWords.WriteByte(word);
                            break;

                        default:
                            WriteSequence();
                            clean = wordNum - lastWordNum - 1;
                            dirtyWords.WriteByte(word);
                            break;
                        }
                    }
                }
                else
                {
                    if (Debugging.AssertsEnabled)
                    {
                        Debugging.Assert(lastWordNum >= 0);
                    }
                    switch (wordNum - lastWordNum)
                    {
                    case 1:
                        if (word == 0xFF)
                        {
                            if (dirtyWords.Length == 0)
                            {
                                ++clean;
                            }
                            else if ((byte)dirtyWords.Bytes[dirtyWords.Length - 1] == 0xFF)
                            {
                                --dirtyWords.Length;
                                WriteSequence();
                                clean = 2;
                            }
                            else
                            {
                                dirtyWords.WriteByte(word);
                            }
                        }
                        else
                        {
                            dirtyWords.WriteByte(word);
                        }
                        break;

                    case 2:
                        dirtyWords.WriteByte(0);
                        dirtyWords.WriteByte(word);
                        break;

                    default:
                        WriteSequence();
                        reverse = false;
                        clean   = wordNum - lastWordNum - 1;
                        dirtyWords.WriteByte(word);
                        break;
                    }
                }
                lastWordNum  = wordNum;
                cardinality += BitUtil.BitCount(word);
            }