* NEWS: Mention this. * COPYING: Replace by GPL v3. * src/sanity/style.test: Check files with the wrong license, in case we forgot to update it during a merge. * Makefile.am, bench/Makefile.am, bench/emptchk/Makefile.am, bench/emptchk/defs.in, bench/emptchk/ltl-human.sh, bench/emptchk/ltl-random.sh, bench/emptchk/pml-clserv.sh, bench/emptchk/pml-eeaean.sh, bench/emptchk/pml2tgba.pl, bench/ltl2tgba/big, bench/ltl2tgba/defs.in, bench/ltl2tgba/known, bench/ltl2tgba/lbtt2csv.pl, bench/ltl2tgba/ltl2baw.in, bench/ltl2tgba/parseout.pl, bench/ltl2tgba/small, bench/ltlclasses/Makefile.am, bench/ltlclasses/defs.in, bench/ltlclasses/run, bench/ltlcounter/Makefile.am, bench/ltlcounter/defs.in, bench/ltlcounter/run, bench/scc-stats/Makefile.am, bench/scc-stats/stats.cc, bench/split-product/Makefile.am, bench/split-product/cutscc.cc, bench/split-product/pml2tgba.pl, bench/wdba/Makefile.am, bench/wdba/defs.in, bench/wdba/run, configure.ac, doc/Makefile.am, doc/dot.in, doc/tl/Makefile.am, iface/Makefile.am, iface/dve2/Makefile.am, iface/dve2/defs.in, iface/dve2/dve2.cc, iface/dve2/dve2.hh, iface/dve2/dve2check.cc, iface/dve2/dve2check.test, iface/dve2/finite.test, iface/dve2/kripke.test, iface/gspn/Makefile.am, iface/gspn/common.cc, iface/gspn/common.hh, iface/gspn/dcswave.test, iface/gspn/dcswaveeltl.test, iface/gspn/dcswavefm.test, iface/gspn/dcswaveltl.test, iface/gspn/dottygspn.cc, iface/gspn/dottyssp.cc, iface/gspn/gspn.cc, iface/gspn/gspn.hh, iface/gspn/ltlgspn.cc, iface/gspn/simple.test, iface/gspn/ssp.cc, iface/gspn/ssp.hh, iface/gspn/udcsefm.test, iface/gspn/udcseltl.test, iface/gspn/udcsfm.test, iface/gspn/udcsltl.test, src/Makefile.am, src/bin/Makefile.am, src/bin/common_cout.cc, src/bin/common_cout.hh, src/bin/common_finput.cc, src/bin/common_finput.hh, src/bin/common_output.cc, src/bin/common_output.hh, src/bin/common_post.cc, src/bin/common_post.hh, src/bin/common_r.cc, src/bin/common_r.hh, src/bin/common_range.cc, src/bin/common_range.hh, src/bin/common_setup.cc, src/bin/common_setup.hh, src/bin/common_sys.hh, src/bin/genltl.cc, src/bin/ltl2tgba.cc, src/bin/ltl2tgta.cc, src/bin/ltlfilt.cc, src/bin/man/Makefile.am, src/bin/randltl.cc, src/eltlparse/Makefile.am, src/eltlparse/eltlparse.yy, src/eltlparse/eltlscan.ll, src/eltlparse/fmterror.cc, src/eltlparse/parsedecl.hh, src/eltlparse/public.hh, src/eltltest/Makefile.am, src/eltltest/acc.cc, src/eltltest/acc.test, src/eltltest/defs.in, src/eltltest/nfa.cc, src/eltltest/nfa.test, src/evtgba/Makefile.am, src/evtgba/evtgba.cc, src/evtgba/evtgba.hh, src/evtgba/evtgbaiter.hh, src/evtgba/explicit.cc, src/evtgba/explicit.hh, src/evtgba/product.cc, src/evtgba/product.hh, src/evtgba/symbol.cc, src/evtgba/symbol.hh, src/evtgbaalgos/Makefile.am, src/evtgbaalgos/dotty.cc, src/evtgbaalgos/dotty.hh, src/evtgbaalgos/reachiter.cc, src/evtgbaalgos/reachiter.hh, src/evtgbaalgos/save.cc, src/evtgbaalgos/save.hh, src/evtgbaalgos/tgba2evtgba.cc, src/evtgbaalgos/tgba2evtgba.hh, src/evtgbaparse/Makefile.am, src/evtgbaparse/evtgbaparse.yy, src/evtgbaparse/evtgbascan.ll, src/evtgbaparse/fmterror.cc, src/evtgbaparse/parsedecl.hh, src/evtgbaparse/public.hh, src/evtgbatest/Makefile.am, src/evtgbatest/defs.in, src/evtgbatest/explicit.cc, src/evtgbatest/explicit.test, src/evtgbatest/ltl2evtgba.cc, src/evtgbatest/ltl2evtgba.test, src/evtgbatest/product.cc, src/evtgbatest/product.test, src/evtgbatest/readsave.cc, src/evtgbatest/readsave.test, src/kripke/Makefile.am, src/kripke/fairkripke.cc, src/kripke/fairkripke.hh, src/kripke/kripke.cc, src/kripke/kripke.hh, src/kripke/kripkeexplicit.cc, src/kripke/kripkeexplicit.hh, src/kripke/kripkeprint.cc, src/kripke/kripkeprint.hh, src/kripkeparse/Makefile.am, src/kripkeparse/fmterror.cc, src/kripkeparse/kripkeparse.yy, src/kripkeparse/kripkescan.ll, src/kripkeparse/parsedecl.hh, src/kripkeparse/public.hh, src/kripkeparse/scankripke.ll, src/kripketest/Makefile.am, src/kripketest/bad_parsing.test, src/kripketest/defs.in, src/kripketest/kripke.test, src/kripketest/parse_print_test.cc, src/ltlast/Makefile.am, src/ltlast/allnodes.hh, src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh, src/ltlast/automatop.cc, src/ltlast/automatop.hh, src/ltlast/binop.cc, src/ltlast/binop.hh, src/ltlast/bunop.cc, src/ltlast/bunop.hh, src/ltlast/constant.cc, src/ltlast/constant.hh, src/ltlast/formula.cc, src/ltlast/formula.hh, src/ltlast/formula_tree.cc, src/ltlast/formula_tree.hh, src/ltlast/multop.cc, src/ltlast/multop.hh, src/ltlast/nfa.cc, src/ltlast/nfa.hh, src/ltlast/predecl.hh, src/ltlast/refformula.cc, src/ltlast/refformula.hh, src/ltlast/unop.cc, src/ltlast/unop.hh, src/ltlast/visitor.hh, src/ltlenv/Makefile.am, src/ltlenv/declenv.cc, src/ltlenv/declenv.hh, src/ltlenv/defaultenv.cc, src/ltlenv/defaultenv.hh, src/ltlenv/environment.hh, src/ltlparse/Makefile.am, src/ltlparse/fmterror.cc, src/ltlparse/ltlfile.cc, src/ltlparse/ltlfile.hh, src/ltlparse/ltlparse.yy, src/ltlparse/ltlscan.ll, src/ltlparse/parsedecl.hh, src/ltlparse/public.hh, src/ltltest/Makefile.am, src/ltltest/consterm.cc, src/ltltest/consterm.test, src/ltltest/defs.in, src/ltltest/equals.cc, src/ltltest/equals.test, src/ltltest/kind.cc, src/ltltest/kind.test, src/ltltest/length.cc, src/ltltest/length.test, src/ltltest/lunabbrev.test, src/ltltest/nenoform.test, src/ltltest/parse.test, src/ltltest/parseerr.test, src/ltltest/readltl.cc, src/ltltest/reduc.cc, src/ltltest/reduc.test, src/ltltest/reduccmp.test, src/ltltest/reducpsl.test, src/ltltest/syntimpl.cc, src/ltltest/syntimpl.test, src/ltltest/tostring.cc, src/ltltest/tostring.test, src/ltltest/tunabbrev.test, src/ltltest/tunenoform.test, src/ltltest/utf8.test, src/ltltest/uwrm.test, src/ltlvisit/Makefile.am, src/ltlvisit/apcollect.cc, src/ltlvisit/apcollect.hh, src/ltlvisit/clone.cc, src/ltlvisit/clone.hh, src/ltlvisit/contain.cc, src/ltlvisit/contain.hh, src/ltlvisit/destroy.cc, src/ltlvisit/destroy.hh, src/ltlvisit/dotty.cc, src/ltlvisit/dotty.hh, src/ltlvisit/dump.cc, src/ltlvisit/dump.hh, src/ltlvisit/lbt.cc, src/ltlvisit/lbt.hh, src/ltlvisit/length.cc, src/ltlvisit/length.hh, src/ltlvisit/lunabbrev.cc, src/ltlvisit/lunabbrev.hh, src/ltlvisit/mark.cc, src/ltlvisit/mark.hh, src/ltlvisit/nenoform.cc, src/ltlvisit/nenoform.hh, src/ltlvisit/postfix.cc, src/ltlvisit/postfix.hh, src/ltlvisit/randomltl.cc, src/ltlvisit/randomltl.hh, src/ltlvisit/reduce.cc, src/ltlvisit/reduce.hh, src/ltlvisit/relabel.cc, src/ltlvisit/relabel.hh, src/ltlvisit/simpfg.cc, src/ltlvisit/simpfg.hh, src/ltlvisit/simplify.cc, src/ltlvisit/simplify.hh, src/ltlvisit/snf.cc, src/ltlvisit/snf.hh, src/ltlvisit/tostring.cc, src/ltlvisit/tostring.hh, src/ltlvisit/tunabbrev.cc, src/ltlvisit/tunabbrev.hh, src/ltlvisit/wmunabbrev.cc, src/ltlvisit/wmunabbrev.hh, src/misc/Makefile.am, src/misc/acccompl.cc, src/misc/acccompl.hh, src/misc/accconv.cc, src/misc/accconv.hh, src/misc/bareword.cc, src/misc/bareword.hh, src/misc/bddalloc.cc, src/misc/bddalloc.hh, src/misc/bddlt.hh, src/misc/bddop.cc, src/misc/bddop.hh, src/misc/casts.hh, src/misc/escape.cc, src/misc/escape.hh, src/misc/fixpool.hh, src/misc/freelist.cc, src/misc/freelist.hh, src/misc/hash.hh, src/misc/hashfunc.hh, src/misc/intvcmp2.cc, src/misc/intvcmp2.hh, src/misc/intvcomp.cc, src/misc/intvcomp.hh, src/misc/ltstr.hh, src/misc/memusage.cc, src/misc/memusage.hh, src/misc/minato.cc, src/misc/minato.hh, src/misc/modgray.cc, src/misc/modgray.hh, src/misc/mspool.hh, src/misc/optionmap.cc, src/misc/optionmap.hh, src/misc/random.cc, src/misc/random.hh, src/misc/timer.cc, src/misc/timer.hh, src/misc/unique_ptr.hh, src/misc/version.cc, src/misc/version.hh, src/neverparse/Makefile.am, src/neverparse/fmterror.cc, src/neverparse/neverclaimparse.yy, src/neverparse/neverclaimscan.ll, src/neverparse/parsedecl.hh, src/neverparse/public.hh, src/saba/Makefile.am, src/saba/explicitstateconjunction.cc, src/saba/explicitstateconjunction.hh, src/saba/saba.cc, src/saba/saba.hh, src/saba/sabacomplementtgba.cc, src/saba/sabacomplementtgba.hh, src/saba/sabastate.hh, src/saba/sabasucciter.hh, src/sabaalgos/Makefile.am, src/sabaalgos/sabadotty.cc, src/sabaalgos/sabadotty.hh, src/sabaalgos/sabareachiter.cc, src/sabaalgos/sabareachiter.hh, src/sabatest/Makefile.am, src/sabatest/defs.in, src/sabatest/sabacomplementtgba.cc, src/sanity/Makefile.am, src/sanity/readme.test, src/sanity/style.test, src/ta/Makefile.am, src/ta/ta.cc, src/ta/ta.hh, src/ta/taexplicit.cc, src/ta/taexplicit.hh, src/ta/taproduct.cc, src/ta/taproduct.hh, src/ta/tgta.cc, src/ta/tgta.hh, src/ta/tgtaexplicit.cc, src/ta/tgtaexplicit.hh, src/ta/tgtaproduct.cc, src/ta/tgtaproduct.hh, src/taalgos/Makefile.am, src/taalgos/dotty.cc, src/taalgos/dotty.hh, src/taalgos/emptinessta.cc, src/taalgos/emptinessta.hh, src/taalgos/minimize.cc, src/taalgos/minimize.hh, src/taalgos/reachiter.cc, src/taalgos/reachiter.hh, src/taalgos/statessetbuilder.cc, src/taalgos/statessetbuilder.hh, src/taalgos/stats.cc, src/taalgos/stats.hh, src/taalgos/tgba2ta.cc, src/taalgos/tgba2ta.hh, src/tgba/Makefile.am, src/tgba/bdddict.cc, src/tgba/bdddict.hh, src/tgba/bddprint.cc, src/tgba/bddprint.hh, src/tgba/formula2bdd.cc, src/tgba/formula2bdd.hh, src/tgba/futurecondcol.cc, src/tgba/futurecondcol.hh, src/tgba/public.hh, src/tgba/sba.hh, src/tgba/state.hh, src/tgba/statebdd.cc, src/tgba/statebdd.hh, src/tgba/succiter.hh, src/tgba/succiterconcrete.cc, src/tgba/succiterconcrete.hh, src/tgba/taatgba.cc, src/tgba/taatgba.hh, src/tgba/tgba.cc, src/tgba/tgba.hh, src/tgba/tgbabddconcrete.cc, src/tgba/tgbabddconcrete.hh, src/tgba/tgbabddconcretefactory.cc, src/tgba/tgbabddconcretefactory.hh, src/tgba/tgbabddconcreteproduct.cc, src/tgba/tgbabddconcreteproduct.hh, src/tgba/tgbabddcoredata.cc, src/tgba/tgbabddcoredata.hh, src/tgba/tgbabddfactory.hh, src/tgba/tgbaexplicit.cc, src/tgba/tgbaexplicit.hh, src/tgba/tgbakvcomplement.cc, src/tgba/tgbakvcomplement.hh, src/tgba/tgbaproduct.cc, src/tgba/tgbaproduct.hh, src/tgba/tgbasafracomplement.cc, src/tgba/tgbasafracomplement.hh, src/tgba/tgbascc.cc, src/tgba/tgbascc.hh, src/tgba/tgbasgba.cc, src/tgba/tgbasgba.hh, src/tgba/tgbatba.cc, src/tgba/tgbatba.hh, src/tgba/tgbaunion.cc, src/tgba/tgbaunion.hh, src/tgba/wdbacomp.cc, src/tgba/wdbacomp.hh, src/tgbaalgos/Makefile.am, src/tgbaalgos/bfssteps.cc, src/tgbaalgos/bfssteps.hh, src/tgbaalgos/cutscc.cc, src/tgbaalgos/cutscc.hh, src/tgbaalgos/cycles.cc, src/tgbaalgos/cycles.hh, src/tgbaalgos/degen.cc, src/tgbaalgos/degen.hh, src/tgbaalgos/dotty.cc, src/tgbaalgos/dotty.hh, src/tgbaalgos/dottydec.cc, src/tgbaalgos/dottydec.hh, src/tgbaalgos/dupexp.cc, src/tgbaalgos/dupexp.hh, src/tgbaalgos/eltl2tgba_lacim.cc, src/tgbaalgos/eltl2tgba_lacim.hh, src/tgbaalgos/emptiness.cc, src/tgbaalgos/emptiness.hh, src/tgbaalgos/emptiness_stats.hh, src/tgbaalgos/gtec/Makefile.am, src/tgbaalgos/gtec/ce.cc, src/tgbaalgos/gtec/ce.hh, src/tgbaalgos/gtec/explscc.cc, src/tgbaalgos/gtec/explscc.hh, src/tgbaalgos/gtec/gtec.cc, src/tgbaalgos/gtec/gtec.hh, src/tgbaalgos/gtec/nsheap.cc, src/tgbaalgos/gtec/nsheap.hh, src/tgbaalgos/gtec/sccstack.cc, src/tgbaalgos/gtec/sccstack.hh, src/tgbaalgos/gtec/status.cc, src/tgbaalgos/gtec/status.hh, src/tgbaalgos/gv04.cc, src/tgbaalgos/gv04.hh, src/tgbaalgos/isdet.cc, src/tgbaalgos/isdet.hh, src/tgbaalgos/isweakscc.cc, src/tgbaalgos/isweakscc.hh, src/tgbaalgos/lbtt.cc, src/tgbaalgos/lbtt.hh, src/tgbaalgos/ltl2taa.cc, src/tgbaalgos/ltl2taa.hh, src/tgbaalgos/ltl2tgba_fm.cc, src/tgbaalgos/ltl2tgba_fm.hh, src/tgbaalgos/ltl2tgba_lacim.cc, src/tgbaalgos/ltl2tgba_lacim.hh, src/tgbaalgos/magic.cc, src/tgbaalgos/magic.hh, src/tgbaalgos/minimize.cc, src/tgbaalgos/minimize.hh, src/tgbaalgos/ndfs_result.hxx, src/tgbaalgos/neverclaim.cc, src/tgbaalgos/neverclaim.hh, src/tgbaalgos/postproc.cc, src/tgbaalgos/postproc.hh, src/tgbaalgos/powerset.cc, src/tgbaalgos/powerset.hh, src/tgbaalgos/projrun.cc, src/tgbaalgos/projrun.hh, src/tgbaalgos/randomgraph.cc, src/tgbaalgos/randomgraph.hh, src/tgbaalgos/reachiter.cc, src/tgbaalgos/reachiter.hh, src/tgbaalgos/reducerun.cc, src/tgbaalgos/reducerun.hh, src/tgbaalgos/reductgba_sim.cc, src/tgbaalgos/reductgba_sim.hh, src/tgbaalgos/replayrun.cc, src/tgbaalgos/replayrun.hh, src/tgbaalgos/rundotdec.cc, src/tgbaalgos/rundotdec.hh, src/tgbaalgos/safety.cc, src/tgbaalgos/safety.hh, src/tgbaalgos/save.cc, src/tgbaalgos/save.hh, src/tgbaalgos/scc.cc, src/tgbaalgos/scc.hh, src/tgbaalgos/sccfilter.cc, src/tgbaalgos/sccfilter.hh, src/tgbaalgos/se05.cc, src/tgbaalgos/se05.hh, src/tgbaalgos/simulation.cc, src/tgbaalgos/simulation.hh, src/tgbaalgos/stats.cc, src/tgbaalgos/stats.hh, src/tgbaalgos/tau03.cc, src/tgbaalgos/tau03.hh, src/tgbaalgos/tau03opt.cc, src/tgbaalgos/tau03opt.hh, src/tgbaalgos/weight.cc, src/tgbaalgos/weight.hh, src/tgbaparse/Makefile.am, src/tgbaparse/fmterror.cc, src/tgbaparse/parsedecl.hh, src/tgbaparse/public.hh, src/tgbaparse/tgbaparse.yy, src/tgbaparse/tgbascan.ll, src/tgbatest/Makefile.am, src/tgbatest/babiak.test, src/tgbatest/bddprod.test, src/tgbatest/complementation.cc, src/tgbatest/complementation.test, src/tgbatest/cycles.test, src/tgbatest/defs.in, src/tgbatest/degendet.test, src/tgbatest/degenid.test, src/tgbatest/dfs.test, src/tgbatest/dupexp.test, src/tgbatest/eltl2tgba.test, src/tgbatest/emptchk.test, src/tgbatest/emptchke.test, src/tgbatest/emptchkr.test, src/tgbatest/explicit.cc, src/tgbatest/explicit.test, src/tgbatest/explicit2.cc, src/tgbatest/explicit2.test, src/tgbatest/explpro2.test, src/tgbatest/explpro3.test, src/tgbatest/explpro4.test, src/tgbatest/explprod.cc, src/tgbatest/explprod.test, src/tgbatest/intvcmp2.cc, src/tgbatest/intvcomp.cc, src/tgbatest/intvcomp.test, src/tgbatest/kv.test, src/tgbatest/ltl2neverclaim.test, src/tgbatest/ltl2ta.test, src/tgbatest/ltl2tgba.cc, src/tgbatest/ltl2tgba.test, src/tgbatest/ltlcounter.test, src/tgbatest/ltlprod.cc, src/tgbatest/ltlprod.test, src/tgbatest/mixprod.cc, src/tgbatest/mixprod.test, src/tgbatest/neverclaimread.test, src/tgbatest/nondet.test, src/tgbatest/obligation.test, src/tgbatest/powerset.cc, src/tgbatest/randpsl.test, src/tgbatest/randtgba.cc, src/tgbatest/randtgba.test, src/tgbatest/readsave.test, src/tgbatest/renault.test, src/tgbatest/scc.test, src/tgbatest/sccsimpl.test, src/tgbatest/spotlbtt.test, src/tgbatest/spotlbtt2.test, src/tgbatest/taatgba.cc, src/tgbatest/taatgba.test, src/tgbatest/tgbaread.cc, src/tgbatest/tgbaread.test, src/tgbatest/tripprod.cc, src/tgbatest/tripprod.test, src/tgbatest/wdba.test, src/tgbatest/wdba2.test, wrap/Makefile.am, wrap/python/Makefile.am, wrap/python/ajax/Makefile.am, wrap/python/ajax/spot.in, wrap/python/buddy.i, wrap/python/spot.i, wrap/python/tests/Makefile.am, wrap/python/tests/alarm.py, wrap/python/tests/bddnqueen.py, wrap/python/tests/implies.py, wrap/python/tests/interdep.py, wrap/python/tests/ltl2tgba.py, wrap/python/tests/ltl2tgba.test, wrap/python/tests/ltlparse.py, wrap/python/tests/ltlsimple.py, wrap/python/tests/minato.py, wrap/python/tests/modgray.py, wrap/python/tests/optionmap.py, wrap/python/tests/parsetgba.py, wrap/python/tests/run.in, wrap/python/tests/setxor.py: Update licence version, and replace the FSF address by a URL.
709 lines
14 KiB
C++
709 lines
14 KiB
C++
// Copyright (C) 2011 Laboratoire de Recherche et Developpement de
|
|
// l'Epita (LRDE).
|
|
//
|
|
// This file is part of Spot, a model checking library.
|
|
//
|
|
// Spot is free software; you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Spot is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
// License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#include <cstddef>
|
|
#include <cassert>
|
|
#include "intvcomp.hh"
|
|
|
|
#if __GNUC__ >= 3
|
|
# define likely(expr) __builtin_expect(!!(expr), 1)
|
|
# define unlikely(expr) __builtin_expect(!!(expr), 0)
|
|
#else
|
|
# define likely(expr) (expr)
|
|
# define unlikely(expr) (expr)
|
|
#endif
|
|
|
|
|
|
namespace spot
|
|
{
|
|
|
|
// Compression scheme
|
|
// ------------------
|
|
//
|
|
// Assumptions:
|
|
// - small and positive values are more frequent than negative
|
|
// and large values.
|
|
// - 0 is the most frequent value
|
|
// - repeated values (esp. repeated 0s occur often).
|
|
//
|
|
// 00 encodes "value 0"
|
|
// 010 encodes "value 1"
|
|
// 011 encodes "a value in [2..5]" followed by 2 bits
|
|
// 100 encodes "a value in [6..22]" followed by 4 bits
|
|
// 101 encodes "repeat prev. value [1..8] times" followed by 3 bits count
|
|
// 110 encodes "repeat prev. value [9..40] times" followed by 5 bits count
|
|
// 111 encodes "an int value" followed by 32 bits
|
|
//
|
|
// If 101 or 110 occur at the start, the value to repeat is 0.
|
|
|
|
namespace
|
|
{
|
|
|
|
template <class Self>
|
|
class stream_compression_base
|
|
{
|
|
static const unsigned int max_bits = sizeof(unsigned int) * 8;
|
|
|
|
public:
|
|
stream_compression_base()
|
|
: cur_(0), bits_left_(max_bits)
|
|
{
|
|
}
|
|
|
|
void emit(unsigned int val)
|
|
{
|
|
if (val == 0)
|
|
{
|
|
self().push_bits(0x0, 2, 0x3);
|
|
}
|
|
else if (val == 1)
|
|
{
|
|
self().push_bits(0x2, 3, 0x7);
|
|
}
|
|
else if (val >= 2 && val <= 5)
|
|
{
|
|
self().push_bits(0x3, 3, 0x7);
|
|
self().push_bits(val - 2, 2, 0x3);
|
|
}
|
|
else if (val >= 6 && val <= 22)
|
|
{
|
|
self().push_bits(0x4, 3, 0x7);
|
|
self().push_bits(val - 6, 4, 0xf);
|
|
}
|
|
else
|
|
{
|
|
assert(val > 22);
|
|
self().push_bits(0x7, 3, 0x7);
|
|
self().push_bits(val, 32, -1U);
|
|
}
|
|
}
|
|
|
|
void run()
|
|
{
|
|
unsigned int last_val = 0;
|
|
|
|
while (likely(self().have_data()))
|
|
{
|
|
unsigned int val = self().next_data();
|
|
// Repeated value? Try to find more.
|
|
if (val == last_val)
|
|
{
|
|
unsigned int count = 1;
|
|
while (count < 40 && self().skip_if(val))
|
|
++count;
|
|
|
|
if ((val == 0 && count < 3) || (val == 1 && count == 1))
|
|
{
|
|
// it is more efficient to emit 0 once or twice directly
|
|
// (e.g., 00 00 vs. 011 11)
|
|
// for value 1, repetition is worthwhile for count > 1
|
|
// (e.g., 010 010 vs. 011 00)
|
|
while (count--)
|
|
emit(val);
|
|
}
|
|
else if (count < 9)
|
|
{
|
|
self().push_bits(0x5, 3, 0x7);
|
|
self().push_bits(count - 1, 3, 0x7);
|
|
}
|
|
else
|
|
{
|
|
self().push_bits(0x6, 3, 0x7);
|
|
self().push_bits(count - 9, 5, 0x1f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
emit(val);
|
|
last_val = val;
|
|
}
|
|
}
|
|
flush();
|
|
}
|
|
|
|
// This version assumes there is at least n bits free in cur_.
|
|
void
|
|
push_bits_unchecked(unsigned int bits, unsigned int n, unsigned int mask)
|
|
{
|
|
cur_ <<= n;
|
|
cur_ |= (bits & mask);
|
|
if (likely(bits_left_ -= n))
|
|
return;
|
|
|
|
self().push_data(cur_);
|
|
cur_ = 0;
|
|
bits_left_ = max_bits;
|
|
}
|
|
|
|
void
|
|
push_bits(unsigned int bits, unsigned int n, unsigned int mask)
|
|
{
|
|
if (likely(n <= bits_left_))
|
|
{
|
|
push_bits_unchecked(bits, n, mask);
|
|
return;
|
|
}
|
|
|
|
// bits_left_ < n
|
|
|
|
unsigned int right_bit_count = n - bits_left_;
|
|
unsigned int left = bits >> right_bit_count;
|
|
push_bits_unchecked(left, bits_left_, (1 << bits_left_) - 1);
|
|
push_bits_unchecked(bits, right_bit_count, (1 << right_bit_count) - 1);
|
|
}
|
|
|
|
void flush()
|
|
{
|
|
if (bits_left_ == max_bits)
|
|
return;
|
|
cur_ <<= bits_left_;
|
|
self().push_data(cur_);
|
|
}
|
|
|
|
protected:
|
|
Self& self()
|
|
{
|
|
return static_cast<Self&>(*this);
|
|
}
|
|
|
|
const Self& self() const
|
|
{
|
|
return static_cast<const Self&>(*this);
|
|
}
|
|
|
|
unsigned int cur_;
|
|
unsigned int bits_left_;
|
|
};
|
|
|
|
class int_array_vector_compression:
|
|
public stream_compression_base<int_array_vector_compression>
|
|
{
|
|
public:
|
|
int_array_vector_compression(const int* array, size_t n)
|
|
: array_(array), n_(n), pos_(0), result_(new std::vector<unsigned int>)
|
|
{
|
|
}
|
|
|
|
void push_data(unsigned int i)
|
|
{
|
|
result_->push_back(i);
|
|
}
|
|
|
|
const std::vector<unsigned int>*
|
|
result() const
|
|
{
|
|
return result_;
|
|
}
|
|
|
|
bool have_data() const
|
|
{
|
|
return pos_ < n_;
|
|
}
|
|
|
|
unsigned int next_data()
|
|
{
|
|
return static_cast<unsigned int>(array_[pos_++]);
|
|
}
|
|
|
|
bool skip_if(unsigned int val)
|
|
{
|
|
if (unlikely(!have_data()))
|
|
return false;
|
|
|
|
if (static_cast<unsigned int>(array_[pos_]) != val)
|
|
return false;
|
|
|
|
++pos_;
|
|
return true;
|
|
}
|
|
|
|
protected:
|
|
const int* array_;
|
|
size_t n_;
|
|
size_t pos_;
|
|
std::vector<unsigned int>* result_;
|
|
};
|
|
|
|
class int_vector_vector_compression:
|
|
public stream_compression_base<int_vector_vector_compression>
|
|
{
|
|
public:
|
|
int_vector_vector_compression(const std::vector<int>& input,
|
|
std::vector<unsigned int>& output)
|
|
: input_(input), pos_(input.begin()), end_(input.end()), output_(output)
|
|
{
|
|
}
|
|
|
|
void push_data(unsigned int i)
|
|
{
|
|
output_.push_back(i);
|
|
}
|
|
|
|
bool have_data() const
|
|
{
|
|
return pos_ < end_;
|
|
}
|
|
|
|
unsigned int next_data()
|
|
{
|
|
return static_cast<unsigned int>(*pos_++);
|
|
}
|
|
|
|
bool skip_if(unsigned int val)
|
|
{
|
|
if (unlikely(!have_data()))
|
|
return false;
|
|
|
|
if (static_cast<unsigned int>(*pos_) != val)
|
|
return false;
|
|
|
|
++pos_;
|
|
return true;
|
|
}
|
|
|
|
protected:
|
|
const std::vector<int>& input_;
|
|
std::vector<int>::const_iterator pos_;
|
|
std::vector<int>::const_iterator end_;
|
|
std::vector<unsigned int>& output_;
|
|
};
|
|
|
|
class int_array_array_compression:
|
|
public stream_compression_base<int_array_array_compression>
|
|
{
|
|
public:
|
|
int_array_array_compression(const int* array, size_t n,
|
|
int* dest, size_t& dest_n)
|
|
: array_(array), n_(n), pos_(0),
|
|
result_size_(dest_n), result_(dest), result_end_(dest + dest_n)
|
|
{
|
|
result_size_ = 0; // this resets dest_n.
|
|
}
|
|
|
|
void push_data(unsigned int i)
|
|
{
|
|
assert(result_ < result_end_);
|
|
++result_size_;
|
|
*result_++ = static_cast<int>(i);
|
|
}
|
|
|
|
bool have_data() const
|
|
{
|
|
return pos_ < n_;
|
|
}
|
|
|
|
unsigned int next_data()
|
|
{
|
|
return static_cast<unsigned int>(array_[pos_++]);
|
|
}
|
|
|
|
bool skip_if(unsigned int val)
|
|
{
|
|
if (unlikely(!have_data()))
|
|
return false;
|
|
|
|
if (static_cast<unsigned int>(array_[pos_]) != val)
|
|
return false;
|
|
|
|
++pos_;
|
|
return true;
|
|
}
|
|
|
|
protected:
|
|
const int* array_;
|
|
size_t n_;
|
|
size_t pos_;
|
|
size_t& result_size_;
|
|
int* result_;
|
|
int* result_end_;
|
|
};
|
|
}
|
|
|
|
void
|
|
int_vector_vector_compress(const std::vector<int>& input,
|
|
std::vector<unsigned>& output)
|
|
{
|
|
int_vector_vector_compression c(input, output);
|
|
c.run();
|
|
}
|
|
|
|
const std::vector<unsigned int>*
|
|
int_array_vector_compress(const int* array, size_t n)
|
|
{
|
|
int_array_vector_compression c(array, n);
|
|
c.run();
|
|
return c.result();
|
|
}
|
|
|
|
void
|
|
int_array_array_compress(const int* array, size_t n,
|
|
int* dest, size_t& dest_size)
|
|
{
|
|
int_array_array_compression c(array, n, dest, dest_size);
|
|
c.run();
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
namespace
|
|
{
|
|
|
|
template<class Self>
|
|
class stream_decompression_base
|
|
{
|
|
static const unsigned int max_bits = sizeof(unsigned int) * 8;
|
|
|
|
public:
|
|
void refill()
|
|
{
|
|
if (unlikely(look_bits_ == 0))
|
|
{
|
|
look_bits_ = max_bits;
|
|
look_ = buffer_;
|
|
|
|
if (likely(self().have_comp_data()))
|
|
buffer_ = self().next_comp_data();
|
|
|
|
if (likely(buffer_bits_ != max_bits))
|
|
{
|
|
unsigned int fill_size = max_bits - buffer_bits_;
|
|
look_ <<= fill_size;
|
|
look_ |= buffer_ >> buffer_bits_;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
unsigned int fill_size = max_bits - look_bits_;
|
|
if (fill_size > buffer_bits_)
|
|
fill_size = buffer_bits_;
|
|
|
|
look_ <<= fill_size;
|
|
buffer_bits_ -= fill_size;
|
|
look_ |= (buffer_ >> buffer_bits_) & ((1 << fill_size) - 1);
|
|
look_bits_ += fill_size;
|
|
|
|
if (buffer_bits_ == 0)
|
|
{
|
|
if (likely(self().have_comp_data()))
|
|
buffer_ = self().next_comp_data();
|
|
|
|
unsigned int left = max_bits - look_bits_;
|
|
if (left != 0)
|
|
{
|
|
look_ <<= left;
|
|
look_ |= buffer_ >> look_bits_;
|
|
buffer_bits_ = look_bits_;
|
|
look_bits_ = max_bits;
|
|
}
|
|
else
|
|
{
|
|
buffer_bits_ = max_bits;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
unsigned int look_n_bits(unsigned int n)
|
|
{
|
|
if (unlikely(look_bits_ < n))
|
|
refill();
|
|
assert(n <= look_bits_);
|
|
return (look_ >> (look_bits_ - n)) & ((1 << n) - 1);
|
|
}
|
|
|
|
void skip_n_bits(unsigned int n)
|
|
{
|
|
assert (n <= look_bits_);
|
|
look_bits_ -= n;
|
|
}
|
|
|
|
unsigned int get_n_bits(unsigned int n)
|
|
{
|
|
if (unlikely(look_bits_ < n))
|
|
refill();
|
|
look_bits_ -= n;
|
|
return (look_ >> look_bits_) & ((1 << n) - 1);
|
|
}
|
|
|
|
unsigned int get_32_bits()
|
|
{
|
|
// std::cerr << "get_32" << std::endl;
|
|
if (likely(look_bits_ < 32))
|
|
refill();
|
|
unsigned int val = look_;
|
|
look_bits_ = 0;
|
|
refill();
|
|
return val;
|
|
}
|
|
|
|
void run()
|
|
{
|
|
if (unlikely(!self().have_comp_data()))
|
|
return;
|
|
|
|
look_ = self().next_comp_data();
|
|
look_bits_ = max_bits;
|
|
if (likely(self().have_comp_data()))
|
|
{
|
|
buffer_ = self().next_comp_data();
|
|
buffer_bits_ = max_bits;
|
|
}
|
|
else
|
|
{
|
|
buffer_ = 0;
|
|
buffer_bits_ = 0;
|
|
}
|
|
|
|
while (likely(!self().complete()))
|
|
{
|
|
unsigned int token = look_n_bits(3);
|
|
switch (token)
|
|
{
|
|
case 0x0: // 00[0]
|
|
case 0x1: // 00[1]
|
|
skip_n_bits(2);
|
|
self().push_data(0);
|
|
break;
|
|
case 0x2: // 010
|
|
skip_n_bits(3);
|
|
self().push_data(1);
|
|
break;
|
|
case 0x3: // 011
|
|
skip_n_bits(3);
|
|
self().push_data(2 + get_n_bits(2));
|
|
break;
|
|
case 0x4: // 100
|
|
skip_n_bits(3);
|
|
self().push_data(6 + get_n_bits(4));
|
|
break;
|
|
case 0x5: // 101
|
|
skip_n_bits(3);
|
|
self().repeat(1 + get_n_bits(3));
|
|
break;
|
|
case 0x6: // 110
|
|
skip_n_bits(3);
|
|
self().repeat(9 + get_n_bits(5));
|
|
break;
|
|
case 0x7: // 111
|
|
skip_n_bits(3);
|
|
self().push_data(get_32_bits());
|
|
break;
|
|
default:
|
|
assert(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
protected:
|
|
Self& self()
|
|
{
|
|
return static_cast<Self&>(*this);
|
|
}
|
|
|
|
const Self& self() const
|
|
{
|
|
return static_cast<const Self&>(*this);
|
|
}
|
|
|
|
unsigned int look_;
|
|
unsigned int look_bits_;
|
|
unsigned int buffer_;
|
|
unsigned int buffer_bits_;
|
|
};
|
|
|
|
class int_vector_vector_decompression:
|
|
public stream_decompression_base<int_vector_vector_decompression>
|
|
{
|
|
public:
|
|
int_vector_vector_decompression(const std::vector<unsigned int>& array,
|
|
std::vector<int>& res, size_t size)
|
|
: prev_(0), array_(array),
|
|
pos_(array.begin()), end_(array.end()),
|
|
result_(res), size_(size)
|
|
{
|
|
result_.reserve(size);
|
|
}
|
|
|
|
bool complete() const
|
|
{
|
|
return size_ == 0;
|
|
}
|
|
|
|
void push_data(int i)
|
|
{
|
|
prev_ = i;
|
|
result_.push_back(i);
|
|
--size_;
|
|
}
|
|
|
|
void repeat(unsigned int i)
|
|
{
|
|
size_ -= i;
|
|
while (i--)
|
|
result_.push_back(prev_);
|
|
}
|
|
|
|
bool have_comp_data() const
|
|
{
|
|
return pos_ != end_;
|
|
}
|
|
|
|
unsigned int next_comp_data()
|
|
{
|
|
return *pos_++;
|
|
}
|
|
|
|
protected:
|
|
int prev_;
|
|
const std::vector<unsigned int>& array_;
|
|
std::vector<unsigned int>::const_iterator pos_;
|
|
std::vector<unsigned int>::const_iterator end_;
|
|
std::vector<int>& result_;
|
|
size_t size_;
|
|
};
|
|
|
|
class int_vector_array_decompression:
|
|
public stream_decompression_base<int_vector_array_decompression>
|
|
{
|
|
public:
|
|
int_vector_array_decompression(const std::vector<unsigned int>* array,
|
|
int* res,
|
|
size_t size)
|
|
: prev_(0), array_(array), n_(array->size()), pos_(0), result_(res),
|
|
size_(size)
|
|
{
|
|
}
|
|
|
|
bool complete() const
|
|
{
|
|
return size_ == 0;
|
|
}
|
|
|
|
void push_data(int i)
|
|
{
|
|
prev_ = i;
|
|
*result_++ = i;
|
|
--size_;
|
|
}
|
|
|
|
void repeat(unsigned int i)
|
|
{
|
|
size_ -= i;
|
|
while (i--)
|
|
*result_++ = prev_;
|
|
}
|
|
|
|
bool have_comp_data() const
|
|
{
|
|
return pos_ < n_;
|
|
}
|
|
|
|
unsigned int next_comp_data()
|
|
{
|
|
return (*array_)[pos_++];
|
|
}
|
|
|
|
protected:
|
|
int prev_;
|
|
const std::vector<unsigned int>* array_;
|
|
size_t n_;
|
|
size_t pos_;
|
|
int* result_;
|
|
size_t size_;
|
|
};
|
|
|
|
class int_array_array_decompression:
|
|
public stream_decompression_base<int_array_array_decompression>
|
|
{
|
|
public:
|
|
int_array_array_decompression(const int* array,
|
|
size_t array_size,
|
|
int* res,
|
|
size_t size)
|
|
: prev_(0), array_(array), n_(array_size), pos_(0), result_(res),
|
|
size_(size)
|
|
{
|
|
}
|
|
|
|
bool complete() const
|
|
{
|
|
return size_ == 0;
|
|
}
|
|
|
|
void push_data(int i)
|
|
{
|
|
prev_ = i;
|
|
*result_++ = i;
|
|
--size_;
|
|
}
|
|
|
|
void repeat(unsigned int i)
|
|
{
|
|
size_ -= i;
|
|
while (i--)
|
|
*result_++ = prev_;
|
|
}
|
|
|
|
bool have_comp_data() const
|
|
{
|
|
return pos_ < n_;
|
|
}
|
|
|
|
unsigned int next_comp_data()
|
|
{
|
|
return array_[pos_++];
|
|
}
|
|
|
|
protected:
|
|
int prev_;
|
|
const int* array_;
|
|
size_t n_;
|
|
size_t pos_;
|
|
int* result_;
|
|
size_t size_;
|
|
};
|
|
|
|
}
|
|
|
|
void
|
|
int_vector_vector_decompress(const std::vector<unsigned int>& input,
|
|
std::vector<int>& output, size_t size)
|
|
{
|
|
int_vector_vector_decompression c(input, output, size);
|
|
c.run();
|
|
}
|
|
|
|
void
|
|
int_vector_array_decompress(const std::vector<unsigned int>* array, int* res,
|
|
size_t size)
|
|
{
|
|
int_vector_array_decompression c(array, res, size);
|
|
c.run();
|
|
}
|
|
|
|
void
|
|
int_array_array_decompress(const int* array, size_t array_size,
|
|
int* res, size_t size)
|
|
{
|
|
int_array_array_decompression c(array, array_size, res, size);
|
|
c.run();
|
|
}
|
|
|
|
|
|
}
|