Exemple #1
0
        private void UpdateModel()
        {
            //System.out.println("ModelPPM.updateModel()");
            // STATE fs = *FoundState, *p = NULL;
            StateRef fs = _tempStateRef1;

            fs.Values = FoundState;
            State p         = _tempState3.Initialize(Heap);
            State tempState = _tempState4.Initialize(Heap);

            PpmContext pc        = _tempPpmContext3.Initialize(Heap);
            PpmContext successor = _tempPpmContext4.Initialize(Heap);

            int ns1, ns, cf, sf, s0;

            pc.Address = _minContext.GetSuffix();
            if (fs.Freq < MAX_FREQ / 4 && pc.Address != 0)
            {
                if (pc.NumStats != 1)
                {
                    p.Address = pc.FreqData.GetStats();
                    if (p.Symbol != fs.Symbol)
                    {
                        do
                        {
                            p.IncrementAddress();
                        }while (p.Symbol != fs.Symbol);
                        tempState.Address = p.Address - State.SIZE;
                        if (p.Freq >= tempState.Freq)
                        {
                            State.PpmdSwap(p, tempState);
                            p.DecrementAddress();
                        }
                    }
                    if (p.Freq < MAX_FREQ - 9)
                    {
                        p.IncrementFreq(2);
                        pc.FreqData.IncrementSummFreq(2);
                    }
                }
                else
                {
                    p.Address = pc.GetOneState().Address;
                    if (p.Freq < 32)
                    {
                        p.IncrementFreq(1);
                    }
                }
            }
            if (_orderFall == 0)
            {
                FoundState.SetSuccessor(CreateSuccessors(true, p));
                _minContext.Address = FoundState.GetSuccessor();
                _maxContext.Address = FoundState.GetSuccessor();
                if (_minContext.Address == 0)
                {
                    UpdateModelRestart();
                    return;
                }
                return;
            }
            SubAlloc.Heap[SubAlloc.PText] = (byte)fs.Symbol;
            SubAlloc.IncPText();
            successor.Address = SubAlloc.PText;
            if (SubAlloc.PText >= SubAlloc.FakeUnitsStart)
            {
                UpdateModelRestart();
                return;
            }

            //        // Debug
            //        subAlloc.dumpHeap();
            if (fs.GetSuccessor() != 0)
            {
                if (fs.GetSuccessor() <= SubAlloc.PText)
                {
                    fs.SetSuccessor(CreateSuccessors(false, p));
                    if (fs.GetSuccessor() == 0)
                    {
                        UpdateModelRestart();
                        return;
                    }
                }
                if (--_orderFall == 0)
                {
                    successor.Address = fs.GetSuccessor();
                    if (_maxContext.Address != _minContext.Address)
                    {
                        SubAlloc.DecPText(1);
                    }
                }
            }
            else
            {
                FoundState.SetSuccessor(successor.Address);
                fs.SetSuccessor(_minContext);
            }

            //        // Debug
            //        subAlloc.dumpHeap();
            ns = _minContext.NumStats;
            s0 = _minContext.FreqData.SummFreq - (ns) - (fs.Freq - 1);
            for (pc.Address = _maxContext.Address; pc.Address != _minContext.Address; pc.Address = pc.GetSuffix())
            {
                if ((ns1 = pc.NumStats) != 1)
                {
                    if ((ns1 & 1) == 0)
                    {
                        //System.out.println(ns1);
                        pc.FreqData.SetStats(SubAlloc.ExpandUnits(pc.FreqData.GetStats(), Utility.URShift(ns1, 1)));
                        if (pc.FreqData.GetStats() == 0)
                        {
                            UpdateModelRestart();
                            return;
                        }
                    }

                    // bug fixed
                    //				int sum = ((2 * ns1 < ns) ? 1 : 0) +
                    //                        2 * ((4 * ((ns1 <= ns) ? 1 : 0)) & ((pc.getFreqData()
                    //								.getSummFreq() <= 8 * ns1) ? 1 : 0));
                    int sum = ((2 * ns1 < ns) ? 1 : 0) +
                              2 * (((4 * ns1 <= ns) ? 1 : 0) & ((pc.FreqData.SummFreq <= 8 * ns1) ? 1 : 0));
                    pc.FreqData.IncrementSummFreq(sum);
                }
                else
                {
                    p.Address = SubAlloc.AllocUnits(1);
                    if (p.Address == 0)
                    {
                        UpdateModelRestart();
                        return;
                    }
                    p.SetValues(pc.GetOneState());
                    pc.FreqData.SetStats(p);
                    if (p.Freq < MAX_FREQ / 4 - 1)
                    {
                        p.IncrementFreq(p.Freq);
                    }
                    else
                    {
                        p.Freq = MAX_FREQ - 4;
                    }
                    pc.FreqData.SummFreq = (p.Freq + _initEsc + (ns > 3 ? 1 : 0));
                }
                cf = 2 * fs.Freq * (pc.FreqData.SummFreq + 6);
                sf = s0 + pc.FreqData.SummFreq;
                if (cf < 6 * sf)
                {
                    cf = 1 + (cf > sf ? 1 : 0) + (cf >= 4 * sf ? 1 : 0);
                    pc.FreqData.IncrementSummFreq(3);
                }
                else
                {
                    cf = 4 + (cf >= 9 * sf ? 1 : 0) + (cf >= 12 * sf ? 1 : 0) + (cf >= 15 * sf ? 1 : 0);
                    pc.FreqData.IncrementSummFreq(cf);
                }
                p.Address = pc.FreqData.GetStats() + ns1 * State.SIZE;
                p.SetSuccessor(successor);
                p.Symbol    = fs.Symbol;
                p.Freq      = cf;
                pc.NumStats = ++ns1;
            }

            int address = fs.GetSuccessor();

            _maxContext.Address = address;
            _minContext.Address = address;

            //TODO-----debug
            //		int pos = minContext.getFreqData().getStats();
            //		State a = new State(getHeap());
            //		a.Address=pos);
            //		pos+=State.size;
            //		a.Address=pos);
            //--dbg end
        }
Exemple #2
0
        public int DecodeChar(Decoder decoder)
        {
            if (_minContext.NumStats != 1)
            {
                State s = _tempState1.Initialize(Heap);
                s.Address = _minContext.FreqData.GetStats();
                int i;
                int count, hiCnt;
                if ((count = (int)decoder.GetThreshold((uint)_minContext.FreqData.SummFreq)) < (hiCnt = s.Freq))
                {
                    byte symbol;
                    decoder.Decode(0, (uint)s.Freq);
                    symbol = (byte)s.Symbol;
                    _minContext.update1_0(this, s.Address);
                    NextContext();
                    return(symbol);
                }
                _prevSuccess = 0;
                i            = _minContext.NumStats - 1;
                do
                {
                    s.IncrementAddress();
                    if ((hiCnt += s.Freq) > count)
                    {
                        byte symbol;
                        decoder.Decode((uint)(hiCnt - s.Freq), (uint)s.Freq);
                        symbol = (byte)s.Symbol;
                        _minContext.Update1(this, s.Address);
                        NextContext();
                        return(symbol);
                    }
                }while (--i > 0);
                if (count >= _minContext.FreqData.SummFreq)
                {
                    return(-2);
                }
                _hiBitsFlag = _hb2Flag[FoundState.Symbol];
                decoder.Decode((uint)hiCnt, (uint)(_minContext.FreqData.SummFreq - hiCnt));
                for (i = 0; i < 256; i++)
                {
                    _charMask[i] = -1;
                }
                _charMask[s.Symbol] = 0;
                i = _minContext.NumStats - 1;
                do
                {
                    s.DecrementAddress();
                    _charMask[s.Symbol] = 0;
                }while (--i > 0);
            }
            else
            {
                State rs = _tempState1.Initialize(Heap);
                rs.Address  = _minContext.GetOneState().Address;
                _hiBitsFlag = GetHb2Flag()[FoundState.Symbol];
                int off1 = rs.Freq - 1;
                int off2 = _minContext.GetArrayIndex(this, rs);
                int bs   = _binSumm[off1][off2];
                if (decoder.DecodeBit((uint)bs, 14) == 0)
                {
                    byte symbol;
                    _binSumm[off1][off2] = (bs + INTERVAL - _minContext.GetMean(bs, PERIOD_BITS, 2)) & 0xFFFF;
                    FoundState.Address   = rs.Address;
                    symbol = (byte)rs.Symbol;
                    rs.IncrementFreq((rs.Freq < 128) ? 1 : 0);
                    _prevSuccess = 1;
                    IncRunLength(1);
                    NextContext();
                    return(symbol);
                }
                bs = (bs - _minContext.GetMean(bs, PERIOD_BITS, 2)) & 0xFFFF;
                _binSumm[off1][off2] = bs;
                _initEsc             = PpmContext.EXP_ESCAPE[Utility.URShift(bs, 10)];
                int i;
                for (i = 0; i < 256; i++)
                {
                    _charMask[i] = -1;
                }
                _charMask[rs.Symbol] = 0;
                _prevSuccess         = 0;
            }
            for (;;)
            {
                State       s = _tempState1.Initialize(Heap);
                int         i;
                int         freqSum, count, hiCnt;
                See2Context see;
                int         num, numMasked = _minContext.NumStats;
                do
                {
                    _orderFall++;
                    _minContext.Address = _minContext.GetSuffix();
                    if (_minContext.Address <= SubAlloc.PText || _minContext.Address > SubAlloc.HeapEnd)
                    {
                        return(-1);
                    }
                }while (_minContext.NumStats == numMasked);
                hiCnt     = 0;
                s.Address = _minContext.FreqData.GetStats();
                i         = 0;
                num       = _minContext.NumStats - numMasked;
                do
                {
                    int k = _charMask[s.Symbol];
                    hiCnt += s.Freq & k;
                    _minContext._ps[i] = s.Address;
                    s.IncrementAddress();
                    i -= k;
                }while (i != num);

                see      = _minContext.MakeEscFreq(this, numMasked, out freqSum);
                freqSum += hiCnt;
                count    = (int)decoder.GetThreshold((uint)freqSum);

                if (count < hiCnt)
                {
                    byte  symbol;
                    State ps = _tempState2.Initialize(Heap);
                    for (hiCnt = 0, i = 0, ps.Address = _minContext._ps[i];
                         (hiCnt += ps.Freq) <= count;
                         i++, ps.Address = _minContext._ps[i])
                    {
                        ;
                    }
                    s.Address = ps.Address;
                    decoder.Decode((uint)(hiCnt - s.Freq), (uint)s.Freq);
                    see.Update();
                    symbol = (byte)s.Symbol;
                    _minContext.Update2(this, s.Address);
                    UpdateModel();
                    return(symbol);
                }
                if (count >= freqSum)
                {
                    return(-2);
                }
                decoder.Decode((uint)hiCnt, (uint)(freqSum - hiCnt));
                see.Summ = see.Summ + freqSum;
                do
                {
                    s.Address           = _minContext._ps[--i];
                    _charMask[s.Symbol] = 0;
                }while (i != 0);
            }
        }
Exemple #3
0
        private int CreateSuccessors(bool skip, State p1)
        {
            //State upState = tempState1.Initialize(null);
            StateRef upState   = _tempStateRef2;
            State    tempState = _tempState1.Initialize(Heap);

            // PPM_CONTEXT* pc=MinContext, * UpBranch=FoundState->Successor;
            PpmContext pc = _tempPpmContext1.Initialize(Heap);

            pc.Address = _minContext.Address;
            PpmContext upBranch = _tempPpmContext2.Initialize(Heap);

            upBranch.Address = FoundState.GetSuccessor();

            // STATE * p, * ps[MAX_O], ** pps=ps;
            State p   = _tempState2.Initialize(Heap);
            int   pps = 0;

            bool noLoop = false;

            if (!skip)
            {
                _ps[pps++] = FoundState.Address; // *pps++ = FoundState;
                if (pc.GetSuffix() == 0)
                {
                    noLoop = true;
                }
            }
            if (!noLoop)
            {
                bool loopEntry = false;
                if (p1.Address != 0)
                {
                    p.Address  = p1.Address;
                    pc.Address = pc.GetSuffix(); // =pc->Suffix;
                    loopEntry  = true;
                }
                do
                {
                    if (!loopEntry)
                    {
                        pc.Address = pc.GetSuffix(); // pc=pc->Suffix;
                        if (pc.NumStats != 1)
                        {
                            p.Address = pc.FreqData.GetStats(); // p=pc->U.Stats
                            if (p.Symbol != FoundState.Symbol)
                            {
                                do
                                {
                                    p.IncrementAddress();
                                }while (p.Symbol != FoundState.Symbol);
                            }
                        }
                        else
                        {
                            p.Address = pc.GetOneState().Address; // p=&(pc->OneState);
                        }
                    } // LOOP_ENTRY:
                    loopEntry = false;
                    if (p.GetSuccessor() != upBranch.Address)
                    {
                        pc.Address = p.GetSuccessor(); // =p->Successor;
                        break;
                    }
                    _ps[pps++] = p.Address;
                }while (pc.GetSuffix() != 0);
            } // NO_LOOP:
            if (pps == 0)
            {
                return(pc.Address);
            }
            upState.Symbol = Heap[upBranch.Address]; // UpState.Symbol=*(byte*)

            // UpBranch;
            // UpState.Successor=(PPM_CONTEXT*) (((byte*) UpBranch)+1);
            upState.SetSuccessor(upBranch.Address + 1); //TODO check if +1 necessary
            if (pc.NumStats != 1)
            {
                if (pc.Address <= SubAlloc.PText)
                {
                    return(0);
                }
                p.Address = pc.FreqData.GetStats();
                if (p.Symbol != upState.Symbol)
                {
                    do
                    {
                        p.IncrementAddress();
                    }while (p.Symbol != upState.Symbol);
                }
                int cf = p.Freq - 1;
                int s0 = pc.FreqData.SummFreq - pc.NumStats - cf;

                // UpState.Freq=1+((2*cf <= s0)?(5*cf > s0):((2*cf+3*s0-1)/(2*s0)));
                upState.Freq = 1 + ((2 * cf <= s0) ? (5 * cf > s0 ? 1 : 0) : ((2 * cf + 3 * s0 - 1) / (2 * s0)));
            }
            else
            {
                upState.Freq = pc.GetOneState().Freq; // UpState.Freq=pc->OneState.Freq;
            }
            do
            {
                // pc = pc->createChild(this,*--pps,UpState);
                tempState.Address = _ps[--pps];
                pc.Address        = pc.CreateChild(this, tempState, upState);
                if (pc.Address == 0)
                {
                    return(0);
                }
            }while (pps != 0);
            return(pc.Address);
        }