tla-ts 0.0.4-dev.4819fb7 → 0.0.4-dev.55f2117
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -126
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +996 -0
- package/dist/prove.js +996 -0
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +38 -44
- package/dist/transfer.js +38 -44
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +21 -12
- package/src/ast.ts +2 -2
- package/src/check.ts +8 -7
- package/src/{cli-options.ts → cli/options.ts} +1 -1
- package/src/{run-cli.ts → cli/run.ts} +302 -100
- package/src/cli.ts +1 -1
- package/src/codegen.ts +26 -7
- package/src/config.ts +2 -2
- package/src/environment.ts +9 -9
- package/src/errors.ts +89 -30
- package/src/evaluate.ts +58 -23
- package/src/index.ts +10 -0
- package/src/levels.ts +1 -1
- package/src/live-check.ts +1 -1
- package/src/liveness.ts +1 -1
- package/src/{obligation-action.ts → obligations/action.ts} +247 -69
- package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
- package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
- package/src/{obligations.ts → obligations/index.ts} +32 -16
- package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
- package/src/{obligation-module.ts → obligations/module.ts} +113 -121
- package/src/obligations/pickle.ts +98 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/{obligation-source.ts → obligations/source.ts} +154 -99
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/parallel-worker.ts +4 -20
- package/src/parallel.ts +13 -46
- package/src/parser.ts +1 -1
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -2
- package/src/{pcal-parse.ts → pcal/parse.ts} +3 -3
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +2 -2
- package/src/{pcal-translate.ts → pcal/translate.ts} +2 -2
- package/src/{pcal-validate.ts → pcal/validate.ts} +6 -6
- package/src/prove/decide-pool.ts +95 -0
- package/src/prove/deciders.ts +33 -0
- package/src/prove/first-order.ts +78 -0
- package/src/prove/isabelle/LICENSE +36 -0
- package/src/prove/isabelle/LICENSE-tlapm +26 -0
- package/src/prove/isabelle/decide.ts +47 -0
- package/src/prove/isabelle/encode.ts +446 -0
- package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prove/isabelle/isar/method.ts +367 -0
- package/src/prove/isabelle/isar/proof.ts +28 -0
- package/src/prove/isabelle/isar/statement.ts +57 -0
- package/src/prove/isabelle/isar/theory.ts +192 -0
- package/src/prove/isabelle/isar/type-check.ts +278 -0
- package/src/prove/isabelle/provers/blast/blast.ts +258 -0
- package/src/prove/isabelle/provers/blast/branch.ts +322 -0
- package/src/prove/isabelle/provers/blast/prove.ts +347 -0
- package/src/prove/isabelle/provers/blast/rules.ts +330 -0
- package/src/prove/isabelle/provers/blast/state.ts +180 -0
- package/src/prove/isabelle/provers/blast/term.ts +546 -0
- package/src/prove/isabelle/provers/clasimp.ts +150 -0
- package/src/prove/isabelle/provers/classical.ts +389 -0
- package/src/prove/isabelle/provers/hypsubst.ts +433 -0
- package/src/prove/isabelle/pure/bicompose.ts +174 -0
- package/src/prove/isabelle/pure/consts.ts +32 -0
- package/src/prove/isabelle/pure/conv.ts +79 -0
- package/src/prove/isabelle/pure/drule.ts +264 -0
- package/src/prove/isabelle/pure/envir.ts +331 -0
- package/src/prove/isabelle/pure/goal.ts +53 -0
- package/src/prove/isabelle/pure/interrupt.ts +16 -0
- package/src/prove/isabelle/pure/list.ts +12 -0
- package/src/prove/isabelle/pure/logic.ts +330 -0
- package/src/prove/isabelle/pure/name.ts +98 -0
- package/src/prove/isabelle/pure/net.ts +215 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +631 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +338 -0
- package/src/prove/isabelle/pure/rewriter.ts +602 -0
- package/src/prove/isabelle/pure/search.ts +68 -0
- package/src/prove/isabelle/pure/seq.ts +133 -0
- package/src/prove/isabelle/pure/simplifier.ts +106 -0
- package/src/prove/isabelle/pure/table.ts +201 -0
- package/src/prove/isabelle/pure/tactic.ts +168 -0
- package/src/prove/isabelle/pure/tactical.ts +264 -0
- package/src/prove/isabelle/pure/term-ord.ts +105 -0
- package/src/prove/isabelle/pure/term.ts +688 -0
- package/src/prove/isabelle/pure/thm.ts +783 -0
- package/src/prove/isabelle/pure/type.ts +121 -0
- package/src/prove/isabelle/pure/unify.ts +753 -0
- package/src/prove/isabelle/pure/variable.ts +25 -0
- package/src/prove/isabelle/tla/classical-setup.ts +101 -0
- package/src/prove/isabelle/tla/context.ts +74 -0
- package/src/prove/isabelle/tla/logic-data.ts +5 -0
- package/src/prove/isabelle/tla/logic.ts +215 -0
- package/src/prove/isabelle/tla/simpdata.ts +241 -0
- package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prove/ocaml.ts +22 -0
- package/src/prove/prep/action.ts +508 -0
- package/src/prove/prep/defs.ts +33 -0
- package/src/prove/prep/elab.ts +156 -0
- package/src/prove/prep/enabled-axioms.ts +189 -0
- package/src/prove/prep/enabled.ts +43 -0
- package/src/prove/prep/fingerprint.ts +388 -0
- package/src/prove/prep/flatten.ts +70 -0
- package/src/prove/prep/level-comparison.ts +529 -0
- package/src/prove/prep/md5.ts +67 -0
- package/src/prove/prep/pragma.ts +221 -0
- package/src/prove/prep/trivial.ts +22 -0
- package/src/prove/prove-worker.ts +22 -0
- package/src/prove/prove.ts +498 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +255 -0
- package/src/prove/ptl/sat.ts +151 -0
- package/src/prove/ptl/tableau.ts +276 -0
- package/src/prove/smt/axiomatize.ts +69 -0
- package/src/prove/smt/axioms.ts +209 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +636 -0
- package/src/prove/smt/encode.ts +164 -0
- package/src/prove/smt/extensionality.ts +65 -0
- package/src/prove/smt/flatten.ts +112 -0
- package/src/prove/smt/format.ts +154 -0
- package/src/prove/smt/instances.ts +181 -0
- package/src/prove/smt/rewrite.ts +772 -0
- package/src/prove/smt/simplify.ts +57 -0
- package/src/prove/smt/solver/act-cache.ts +265 -0
- package/src/prove/smt/solver/arith-rewriter.ts +962 -0
- package/src/prove/smt/solver/array.ts +22 -0
- package/src/prove/smt/solver/asserted-formulas.ts +469 -0
- package/src/prove/smt/solver/ast-lt.ts +157 -0
- package/src/prove/smt/solver/ast-util.ts +91 -0
- package/src/prove/smt/solver/ast.ts +1315 -0
- package/src/prove/smt/solver/auf-solver.ts +572 -0
- package/src/prove/smt/solver/case-split-queue.ts +178 -0
- package/src/prove/smt/solver/cg-table.ts +175 -0
- package/src/prove/smt/solver/clause.ts +276 -0
- package/src/prove/smt/solver/conflict-resolution.ts +343 -0
- package/src/prove/smt/solver/context.ts +1292 -0
- package/src/prove/smt/solver/ctx-simplify.ts +334 -0
- package/src/prove/smt/solver/der.ts +224 -0
- package/src/prove/smt/solver/dyn-ack.ts +141 -0
- package/src/prove/smt/solver/e-matching.ts +659 -0
- package/src/prove/smt/solver/enode.ts +259 -0
- package/src/prove/smt/solver/expr-ref.ts +21 -0
- package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prove/smt/solver/fingerprints.ts +92 -0
- package/src/prove/smt/solver/flat-literals.ts +51 -0
- package/src/prove/smt/solver/goal.ts +138 -0
- package/src/prove/smt/solver/hash.ts +173 -0
- package/src/prove/smt/solver/hashtable.ts +166 -0
- package/src/prove/smt/solver/internalizer.ts +1381 -0
- package/src/prove/smt/solver/lbool.ts +5 -0
- package/src/prove/smt/solver/literal.ts +24 -0
- package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prove/smt/solver/lp/constraints.ts +78 -0
- package/src/prove/smt/solver/lp/containers.ts +296 -0
- package/src/prove/smt/solver/lp/core-solver.ts +504 -0
- package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prove/smt/solver/lp/dep-intervals.ts +642 -0
- package/src/prove/smt/solver/lp/dependency.ts +51 -0
- package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prove/smt/solver/lp/explanation.ts +46 -0
- package/src/prove/smt/solver/lp/gomory.ts +255 -0
- package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prove/smt/solver/lp/hnf.ts +281 -0
- package/src/prove/smt/solver/lp/int-cube.ts +295 -0
- package/src/prove/smt/solver/lp/int-solver.ts +514 -0
- package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prove/smt/solver/lp/lar-term.ts +62 -0
- package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prove/smt/solver/lp/nex.ts +120 -0
- package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prove/smt/solver/lp/nla-core.ts +968 -0
- package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prove/smt/solver/lp/nla-order.ts +307 -0
- package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prove/smt/solver/lp/nla-types.ts +124 -0
- package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prove/smt/solver/lp/pdd.ts +971 -0
- package/src/prove/smt/solver/lp/random-updater.ts +138 -0
- package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prove/smt/solver/lp/union-find.ts +96 -0
- package/src/prove/smt/solver/mam-compiler.ts +847 -0
- package/src/prove/smt/solver/mam-interpreter.ts +400 -0
- package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prove/smt/solver/model-checker.ts +347 -0
- package/src/prove/smt/solver/model-evaluator.ts +262 -0
- package/src/prove/smt/solver/model-finder.ts +155 -0
- package/src/prove/smt/solver/model-generator.ts +97 -0
- package/src/prove/smt/solver/model-macro-solver.ts +728 -0
- package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prove/smt/solver/nlsat/explain.ts +607 -0
- package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prove/smt/solver/nlsat/levelwise.ts +737 -0
- package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prove/smt/solver/nlsat/solver.ts +911 -0
- package/src/prove/smt/solver/nlsat/types.ts +43 -0
- package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prove/smt/solver/nnf.ts +469 -0
- package/src/prove/smt/solver/pattern-inference.ts +351 -0
- package/src/prove/smt/solver/poly-rewriter.ts +477 -0
- package/src/prove/smt/solver/propagate-values.ts +83 -0
- package/src/prove/smt/solver/proto-model.ts +551 -0
- package/src/prove/smt/solver/pull-quant.ts +139 -0
- package/src/prove/smt/solver/qe-lite-der.ts +526 -0
- package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prove/smt/solver/qe-lite.ts +381 -0
- package/src/prove/smt/solver/qi-queue.ts +362 -0
- package/src/prove/smt/solver/quant-tactics.ts +117 -0
- package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prove/smt/solver/quantifier-manager.ts +186 -0
- package/src/prove/smt/solver/random-gen.ts +10 -0
- package/src/prove/smt/solver/rational.ts +165 -0
- package/src/prove/smt/solver/ref-vector.ts +62 -0
- package/src/prove/smt/solver/relevancy.ts +247 -0
- package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prove/smt/solver/rewriter.ts +843 -0
- package/src/prove/smt/solver/rlimit.ts +79 -0
- package/src/prove/smt/solver/smtlib-parser.ts +526 -0
- package/src/prove/smt/solver/solver.ts +71 -0
- package/src/prove/smt/solver/sort.ts +35 -0
- package/src/prove/smt/solver/sticky-generations.ts +69 -0
- package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prove/smt/solver/theory-lra-model.ts +243 -0
- package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prove/smt/solver/theory-lra-state.ts +43 -0
- package/src/prove/smt/solver/theory-lra.ts +56 -0
- package/src/prove/smt/solver/var-subst.ts +258 -0
- package/src/prove/smt/solver/z3-license.txt +10 -0
- package/src/prove/smt/table.ts +170 -0
- package/src/prove/tlapm/cache.ts +236 -0
- package/src/prove/tlapm/ocaml-marshal.ts +166 -0
- package/src/prove/tlapm/tlapm.ts +96 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +18 -0
- package/src/prove/zenon/LICENSE +32 -0
- package/src/prove/zenon/check/check.ts +105 -0
- package/src/prove/zenon/check/definitions.ts +144 -0
- package/src/prove/zenon/check/equality.ts +182 -0
- package/src/prove/zenon/check/functions.ts +62 -0
- package/src/prove/zenon/check/keys.ts +125 -0
- package/src/prove/zenon/check/propositional.ts +104 -0
- package/src/prove/zenon/check/records.ts +188 -0
- package/src/prove/zenon/check/relations.ts +121 -0
- package/src/prove/zenon/check/rewrites.ts +224 -0
- package/src/prove/zenon/check/sets.ts +302 -0
- package/src/prove/zenon/check/terms.ts +127 -0
- package/src/prove/zenon/check/tla.ts +126 -0
- package/src/prove/zenon/decide.ts +113 -0
- package/src/prove/zenon/encode.ts +379 -0
- package/src/prove/zenon/eqrel.ts +300 -0
- package/src/prove/zenon/equality-rules.ts +435 -0
- package/src/prove/zenon/expr.ts +551 -0
- package/src/prove/zenon/index-table.ts +350 -0
- package/src/prove/zenon/isar-case.ts +513 -0
- package/src/prove/zenon/isar.ts +1249 -0
- package/src/prove/zenon/limits.ts +15 -0
- package/src/prove/zenon/llproof.ts +173 -0
- package/src/prove/zenon/mltoll.ts +1566 -0
- package/src/prove/zenon/node.ts +297 -0
- package/src/prove/zenon/ocaml.ts +169 -0
- package/src/prove/zenon/parse.ts +247 -0
- package/src/prove/zenon/phrase.ts +191 -0
- package/src/prove/zenon/proof.ts +198 -0
- package/src/prove/zenon/propositional-rules.ts +387 -0
- package/src/prove/zenon/rules.ts +94 -0
- package/src/prove/zenon/search.ts +288 -0
- package/src/prove/zenon/tla/functions.ts +151 -0
- package/src/prove/zenon/tla/llproof.ts +480 -0
- package/src/prove/zenon/tla/records.ts +225 -0
- package/src/prove/zenon/tla/rewrites.ts +447 -0
- package/src/prove/zenon/tla/sets.ts +304 -0
- package/src/prove/zenon/tla/terms.ts +162 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/randomization.ts +1 -1
- package/src/resolve.ts +1 -1
- package/src/runtime.ts +2 -2
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +14 -14
- package/src/{sany-levels.ts → sany/levels.ts} +2 -2
- package/src/{sany-lexer.ts → sany/lexer.ts} +1 -1
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +15 -15
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -3
- package/src/{sany-source.ts → sany/source.ts} +6 -6
- package/src/{sany-xml.ts → sany/xml.ts} +5 -5
- package/src/spec-description.ts +1 -1
- package/src/{standard-levels.ts → standard/levels.ts} +1 -1
- package/src/{standard-modules.ts → standard/modules.ts} +17 -14
- package/src/{standard-ranges.ts → standard/ranges.ts} +1 -1
- package/src/state-graph.ts +1 -1
- package/src/successors.ts +25 -6
- package/src/symmetry.ts +1 -1
- package/src/{tlc-messages.ts → tlc/messages.ts} +2 -2
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +58 -35
- package/src/{tlc-replay.ts → tlc/replay.ts} +5 -5
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +14 -8
- package/src/{tlc-version.ts → tlc/version.ts} +8 -7
- package/src/values.ts +81 -36
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- package/dist/run-cli.cjs +0 -55
- package/dist/run-cli.js +0 -55
- package/src/value-sources.ts +0 -29
- /package/src/{sany-nodes.ts → sany/nodes.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/lp/random_updater_def.h, and shift_var and
|
|
2
|
+
// get_freedom_interval_for_column in src/math/lp/int_solver.cpp), MIT license, see
|
|
3
|
+
// z3-license.txt. Moves non-basic columns to random values inside the interval that keeps every
|
|
4
|
+
// row feasible, so that columns with equal values, which theory combination would assume equal,
|
|
5
|
+
// become different.
|
|
6
|
+
import { getLcm, Rational } from "../rational";
|
|
7
|
+
import { IndexedUintSet } from "./containers";
|
|
8
|
+
import type { LarSolver } from "./lar-solver";
|
|
9
|
+
import { Impq } from "./numeric-pair";
|
|
10
|
+
|
|
11
|
+
const RANGE = 100000;
|
|
12
|
+
|
|
13
|
+
interface FreedomInterval {
|
|
14
|
+
lower: Impq | undefined;
|
|
15
|
+
upper: Impq | undefined;
|
|
16
|
+
multiple: Rational;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// get_freedom_interval_for_column: the values of a non-basic column that keep the basic columns
|
|
20
|
+
// that depend on it within their bounds.
|
|
21
|
+
const getFreedomInterval = (solver: LarSolver, column: number): FreedomInterval | undefined => {
|
|
22
|
+
const core = solver.core;
|
|
23
|
+
if (core.isBase(column) || solver.isFixed(column)) return undefined;
|
|
24
|
+
const value = solver.getValue(column);
|
|
25
|
+
let lower: Impq | undefined;
|
|
26
|
+
let upper: Impq | undefined;
|
|
27
|
+
let multiple = 1n;
|
|
28
|
+
const setLower = (candidate: Impq) => {
|
|
29
|
+
if (lower === undefined || candidate.compare(lower) > 0) lower = candidate;
|
|
30
|
+
};
|
|
31
|
+
const setUpper = (candidate: Impq) => {
|
|
32
|
+
if (upper === undefined || candidate.compare(upper) < 0) upper = candidate;
|
|
33
|
+
};
|
|
34
|
+
if (core.hasLowerBound(column)) setLower(solver.getLowerBound(column).sub(value));
|
|
35
|
+
if (core.hasUpperBound(column)) setUpper(solver.getUpperBound(column).sub(value));
|
|
36
|
+
const getDelta = (coeff: Rational, first: Impq, second: Impq) => {
|
|
37
|
+
if (coeff.equals(Rational.one)) return first.sub(second);
|
|
38
|
+
if (coeff.equals(Rational.minusOne)) return second.sub(first);
|
|
39
|
+
return first.sub(second).div(coeff);
|
|
40
|
+
};
|
|
41
|
+
for (const cell of core.matrix.columns[column]) {
|
|
42
|
+
const coeff = core.matrix.getVal(cell);
|
|
43
|
+
const basic = core.basis[cell.row];
|
|
44
|
+
const basicValue = solver.getValue(basic);
|
|
45
|
+
if (solver.isInt(basic) && !coeff.isInt() && basicValue.isInt())
|
|
46
|
+
multiple = getLcm(multiple, coeff.den);
|
|
47
|
+
if (lower !== undefined && upper !== undefined && lower.equals(upper)) continue;
|
|
48
|
+
if (coeff.isNegative()) {
|
|
49
|
+
if (core.hasLowerBound(basic))
|
|
50
|
+
setLower(getDelta(coeff, basicValue, solver.getLowerBound(basic)));
|
|
51
|
+
if (core.hasUpperBound(basic))
|
|
52
|
+
setUpper(getDelta(coeff, basicValue, solver.getUpperBound(basic)));
|
|
53
|
+
} else {
|
|
54
|
+
if (core.hasUpperBound(basic))
|
|
55
|
+
setLower(getDelta(coeff, basicValue, solver.getUpperBound(basic)));
|
|
56
|
+
if (core.hasLowerBound(basic))
|
|
57
|
+
setUpper(getDelta(coeff, basicValue, solver.getLowerBound(basic)));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const shiftedLower = lower?.add(value);
|
|
61
|
+
const shiftedUpper = upper?.add(value);
|
|
62
|
+
if (
|
|
63
|
+
shiftedLower !== undefined &&
|
|
64
|
+
shiftedUpper !== undefined &&
|
|
65
|
+
shiftedLower.compare(shiftedUpper) > 0
|
|
66
|
+
)
|
|
67
|
+
return undefined;
|
|
68
|
+
return { lower: shiftedLower, upper: shiftedUpper, multiple: Rational.of(multiple) };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// int_solver::shift_var.
|
|
72
|
+
const shiftVar = (solver: LarSolver, column: number) => {
|
|
73
|
+
if (solver.isFixed(column) || solver.isBase(column) || solver.settings.isCanceled()) return false;
|
|
74
|
+
const interval = getFreedomInterval(solver, column);
|
|
75
|
+
if (interval === undefined) return false;
|
|
76
|
+
const settings = solver.settings;
|
|
77
|
+
const value = solver.getValue(column);
|
|
78
|
+
const multiple = interval.multiple;
|
|
79
|
+
const getRandomShift = (range: number) =>
|
|
80
|
+
new Impq(multiple.mul(Rational.fromNumber(settings.getRandomNext() % (range + 1))));
|
|
81
|
+
if (interval.lower === undefined && interval.upper === undefined) {
|
|
82
|
+
solver.setValueForNbasicColumn(column, getRandomShift(RANGE).add(value));
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const isInt = solver.isInt(column);
|
|
86
|
+
const lower =
|
|
87
|
+
interval.lower === undefined
|
|
88
|
+
? undefined
|
|
89
|
+
: isInt
|
|
90
|
+
? new Impq(interval.lower.ceil())
|
|
91
|
+
: interval.lower;
|
|
92
|
+
const upper =
|
|
93
|
+
interval.upper === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: isInt
|
|
96
|
+
? new Impq(interval.upper.floor())
|
|
97
|
+
: interval.upper;
|
|
98
|
+
if (lower !== undefined && upper !== undefined && lower.compare(upper) >= 0) return false;
|
|
99
|
+
if (upper === undefined) {
|
|
100
|
+
solver.setValueForNbasicColumn(column, value.add(getRandomShift(RANGE)));
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (lower === undefined) {
|
|
104
|
+
solver.setValueForNbasicColumn(column, value.sub(getRandomShift(RANGE)));
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
const high = upper.sub(value).div(multiple).floor();
|
|
108
|
+
const low = lower.sub(value).div(multiple).ceil();
|
|
109
|
+
const span = high.sub(low);
|
|
110
|
+
if (!span.isPositive()) return false;
|
|
111
|
+
const range = span.compare(Rational.fromNumber(RANGE)) < 0 ? Number(span.num) : RANGE;
|
|
112
|
+
const step = low.add(Rational.fromNumber(settings.getRandomNext() % (range + 1)));
|
|
113
|
+
solver.setValueForNbasicColumn(column, value.add(new Impq(multiple.mul(step))));
|
|
114
|
+
return true;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// random_updater::update over the given columns.
|
|
118
|
+
export const randomUpdate = (solver: LarSolver, columns: number[]) => {
|
|
119
|
+
const varSet = new IndexedUintSet();
|
|
120
|
+
for (const column of columns) varSet.add(column);
|
|
121
|
+
const shift = (column: number) => {
|
|
122
|
+
if (!shiftVar(solver, column)) return false;
|
|
123
|
+
for (const cell of solver.core.matrix.columns[column]) {
|
|
124
|
+
const basic = solver.core.basis[cell.row];
|
|
125
|
+
if (varSet.has(basic)) varSet.delete(basic);
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
};
|
|
129
|
+
for (const column of varSet.elems.slice()) {
|
|
130
|
+
if (!varSet.has(column)) continue;
|
|
131
|
+
if (!solver.isBase(column)) {
|
|
132
|
+
shift(column);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
for (const cell of solver.getRow(solver.core.heading[column]))
|
|
136
|
+
if (!solver.isBase(cell.column) && !solver.isFixed(cell.column) && shift(cell.column)) break;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/lp/static_matrix.h, src/math/lp/static_matrix_def.h), MIT license, see
|
|
2
|
+
// z3-license.txt. The sparse tableau: every row cell knows the offset of its column cell and back,
|
|
3
|
+
// and cells are removed by moving the last cell of the strip into their place, so the order of
|
|
4
|
+
// the cells is Z3's.
|
|
5
|
+
import { Rational } from "../rational";
|
|
6
|
+
import type { LarTerm } from "./lar-term";
|
|
7
|
+
|
|
8
|
+
const getTermCells = (term: LarTerm) =>
|
|
9
|
+
[...term.coeffs].map((entry) => ({ column: entry[0], coeff: entry[1] }));
|
|
10
|
+
|
|
11
|
+
export interface RowCell {
|
|
12
|
+
column: number;
|
|
13
|
+
offset: number;
|
|
14
|
+
coeff: Rational;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ColumnCell {
|
|
18
|
+
row: number;
|
|
19
|
+
offset: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class StaticMatrix {
|
|
23
|
+
rows: RowCell[][] = [];
|
|
24
|
+
columns: ColumnCell[][] = [];
|
|
25
|
+
private rowOffsets: number[] = [];
|
|
26
|
+
|
|
27
|
+
getRowCount() {
|
|
28
|
+
return this.rows.length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getColumnCount() {
|
|
32
|
+
return this.columns.length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getVal(cell: ColumnCell) {
|
|
36
|
+
return this.rows[cell.row][cell.offset].coeff;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
addRow() {
|
|
40
|
+
this.rows.push([]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
addColumn() {
|
|
44
|
+
this.columns.push([]);
|
|
45
|
+
this.rowOffsets.push(-1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
popColumn() {
|
|
49
|
+
this.columns.pop();
|
|
50
|
+
this.rowOffsets.pop();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
addColumnsUpTo(column: number) {
|
|
54
|
+
while (column >= this.columns.length) this.addColumn();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
clear() {
|
|
58
|
+
this.rows = [];
|
|
59
|
+
this.columns = [];
|
|
60
|
+
this.rowOffsets = [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// set: appends a nonzero cell to the row and to the column.
|
|
64
|
+
set(row: number, column: number, coeff: Rational) {
|
|
65
|
+
if (coeff.isZero()) return;
|
|
66
|
+
this.addNewElement(row, column, coeff);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addNewElement(row: number, column: number, coeff: Rational) {
|
|
70
|
+
const rowCells = this.rows[row];
|
|
71
|
+
const columnCells = this.columns[column];
|
|
72
|
+
rowCells.push({ column, offset: columnCells.length, coeff });
|
|
73
|
+
columnCells.push({ row, offset: rowCells.length - 1 });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
removeElement(rowCells: RowCell[], cell: RowCell) {
|
|
77
|
+
const columnOffset = cell.offset;
|
|
78
|
+
const columnCells = this.columns[cell.column];
|
|
79
|
+
const rowOffset = columnCells[columnOffset].offset;
|
|
80
|
+
if (columnOffset !== columnCells.length - 1) {
|
|
81
|
+
const moved = columnCells[columnCells.length - 1];
|
|
82
|
+
columnCells[columnOffset] = moved;
|
|
83
|
+
this.rows[moved.row][moved.offset].offset = columnOffset;
|
|
84
|
+
}
|
|
85
|
+
if (rowOffset !== rowCells.length - 1) {
|
|
86
|
+
const tail = rowCells[rowCells.length - 1];
|
|
87
|
+
rowCells[rowOffset] = tail;
|
|
88
|
+
this.columns[tail.column][tail.offset].offset = rowOffset;
|
|
89
|
+
}
|
|
90
|
+
columnCells.pop();
|
|
91
|
+
rowCells.pop();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private scanRowToOffsets(rowCells: RowCell[]) {
|
|
95
|
+
for (let index = 0; index < rowCells.length; index++)
|
|
96
|
+
this.rowOffsets[rowCells[index].column] = index;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// pivot_row_to_row_given_cell: eliminates the pivot column from the row of the cell by adding a
|
|
100
|
+
// multiple of the pivot row.
|
|
101
|
+
pivotRowToRowGivenCell(pivotRow: number, cell: ColumnCell, pivotColumn: number) {
|
|
102
|
+
const target = cell.row;
|
|
103
|
+
const targetCells = this.rows[target];
|
|
104
|
+
const alpha = this.getVal(cell).neg();
|
|
105
|
+
this.removeElement(targetCells, targetCells[cell.offset]);
|
|
106
|
+
this.scanRowToOffsets(targetCells);
|
|
107
|
+
const previousSize = targetCells.length;
|
|
108
|
+
let cancelled = 0;
|
|
109
|
+
for (const pivotCell of this.rows[pivotRow]) {
|
|
110
|
+
const column = pivotCell.column;
|
|
111
|
+
if (column === pivotColumn) continue;
|
|
112
|
+
const offset = this.rowOffsets[column];
|
|
113
|
+
if (offset === -1) {
|
|
114
|
+
this.addNewElement(target, column, alpha.mul(pivotCell.coeff));
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const targetCell = targetCells[offset];
|
|
118
|
+
targetCell.coeff = targetCell.coeff.add(pivotCell.coeff.mul(alpha));
|
|
119
|
+
if (targetCell.coeff.isZero()) cancelled++;
|
|
120
|
+
}
|
|
121
|
+
for (let index = 0; index < previousSize; index++)
|
|
122
|
+
this.rowOffsets[targetCells[index].column] = -1;
|
|
123
|
+
for (let index = previousSize; index-- > 0 && cancelled > 0;) {
|
|
124
|
+
if (!targetCells[index].coeff.isZero()) continue;
|
|
125
|
+
this.removeElement(targetCells, targetCells[index]);
|
|
126
|
+
cancelled--;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
multiplyRow(row: number, alpha: Rational) {
|
|
131
|
+
for (const cell of this.rows[row]) cell.coeff = cell.coeff.mul(alpha);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
divideRow(row: number, alpha: Rational) {
|
|
135
|
+
for (const cell of this.rows[row]) cell.coeff = cell.coeff.div(alpha);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// add_rows: adds row from multiplied by alpha to row to.
|
|
139
|
+
addRows(alpha: Rational, from: number, to: number) {
|
|
140
|
+
this.addCellsToRow(to, this.rows[from], alpha, -1);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// pivot_row_to_row_given_cell_with_sign: eliminates the pivot column from the row of the cell by
|
|
144
|
+
// adding the pivot row times the sign.
|
|
145
|
+
pivotRowToRowGivenCellWithSign(
|
|
146
|
+
pivotRow: number,
|
|
147
|
+
cell: ColumnCell,
|
|
148
|
+
pivotColumn: number,
|
|
149
|
+
sign: number,
|
|
150
|
+
) {
|
|
151
|
+
this.pivotCellsToRow(this.rows[pivotRow], cell, pivotColumn, sign);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// pivot_term_to_row_given_cell: the same with the cells of a term in place of the pivot row.
|
|
155
|
+
pivotTermToRowGivenCell(term: LarTerm, cell: ColumnCell, pivotColumn: number, sign: number) {
|
|
156
|
+
this.pivotCellsToRow(getTermCells(term), cell, pivotColumn, sign);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// add_term_to_row: adds the term multiplied by alpha to the row.
|
|
160
|
+
addTermToRow(alpha: Rational, term: LarTerm, row: number) {
|
|
161
|
+
for (const entry of term.coeffs) this.addColumnsUpTo(entry[0]);
|
|
162
|
+
this.addCellsToRow(row, getTermCells(term), alpha, -1);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private pivotCellsToRow(
|
|
166
|
+
cells: readonly { column: number; coeff: Rational }[],
|
|
167
|
+
cell: ColumnCell,
|
|
168
|
+
pivotColumn: number,
|
|
169
|
+
sign: number,
|
|
170
|
+
) {
|
|
171
|
+
const target = cell.row;
|
|
172
|
+
const alpha = this.getVal(cell).neg().mul(Rational.fromNumber(sign));
|
|
173
|
+
const targetCells = this.rows[target];
|
|
174
|
+
this.removeElement(targetCells, targetCells[cell.offset]);
|
|
175
|
+
this.addCellsToRow(target, cells, alpha, pivotColumn);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The cells times alpha added to the row, skipping a column, and the cells that cancel removed
|
|
179
|
+
// from the end.
|
|
180
|
+
private addCellsToRow(
|
|
181
|
+
row: number,
|
|
182
|
+
cells: readonly { column: number; coeff: Rational }[],
|
|
183
|
+
alpha: Rational,
|
|
184
|
+
skippedColumn: number,
|
|
185
|
+
) {
|
|
186
|
+
const targetCells = this.rows[row];
|
|
187
|
+
this.scanRowToOffsets(targetCells);
|
|
188
|
+
const previousSize = targetCells.length;
|
|
189
|
+
for (const source of cells) {
|
|
190
|
+
if (source.column === skippedColumn) continue;
|
|
191
|
+
const offset = this.rowOffsets[source.column];
|
|
192
|
+
if (offset === -1) this.addNewElement(row, source.column, alpha.mul(source.coeff));
|
|
193
|
+
else targetCells[offset].coeff = targetCells[offset].coeff.add(source.coeff.mul(alpha));
|
|
194
|
+
}
|
|
195
|
+
for (let index = 0; index < previousSize; index++)
|
|
196
|
+
this.rowOffsets[targetCells[index].column] = -1;
|
|
197
|
+
for (let index = targetCells.length; index-- > 0;)
|
|
198
|
+
if (targetCells[index].coeff.isZero()) this.removeElement(targetCells, targetCells[index]);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
transposeRows(first: number, second: number) {
|
|
202
|
+
const saved = this.rows[first];
|
|
203
|
+
this.rows[first] = this.rows[second];
|
|
204
|
+
this.rows[second] = saved;
|
|
205
|
+
for (const cell of this.rows[first]) this.columns[cell.column][cell.offset].row = first;
|
|
206
|
+
for (const cell of this.rows[second]) this.columns[cell.column][cell.offset].row = second;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// fill_last_row_with_pivoting: the row of a new basic column for a term, with the basic columns
|
|
210
|
+
// of the term substituted by their rows.
|
|
211
|
+
fillLastRowWithPivoting(term: LarTerm, basic: number, basisHeading: number[]) {
|
|
212
|
+
const work: Rational[] = [];
|
|
213
|
+
const index: number[] = [];
|
|
214
|
+
const getWork = (column: number) => work[column] ?? Rational.zero;
|
|
215
|
+
const eraseFromIndex = (column: number) => {
|
|
216
|
+
const position = index.indexOf(column);
|
|
217
|
+
if (position !== -1) index.splice(position, 1);
|
|
218
|
+
};
|
|
219
|
+
work[basic] = Rational.one;
|
|
220
|
+
index.push(basic);
|
|
221
|
+
for (const entry of term.coeffs) {
|
|
222
|
+
work[entry[0]] = entry[1].neg();
|
|
223
|
+
index.push(entry[0]);
|
|
224
|
+
}
|
|
225
|
+
const pivotOut = (column: number) => {
|
|
226
|
+
const rowIndex = basisHeading[column];
|
|
227
|
+
if (rowIndex < 0) return;
|
|
228
|
+
const alpha = getWork(column);
|
|
229
|
+
if (alpha.isZero()) return;
|
|
230
|
+
for (const cell of this.rows[rowIndex]) {
|
|
231
|
+
if (cell.column === column) continue;
|
|
232
|
+
const wasZero = getWork(cell.column).isZero();
|
|
233
|
+
work[cell.column] = getWork(cell.column).sub(alpha.mul(cell.coeff));
|
|
234
|
+
if (wasZero) index.push(cell.column);
|
|
235
|
+
else if (getWork(cell.column).isZero()) eraseFromIndex(cell.column);
|
|
236
|
+
}
|
|
237
|
+
work[column] = Rational.zero;
|
|
238
|
+
eraseFromIndex(column);
|
|
239
|
+
};
|
|
240
|
+
pivotOut(basic);
|
|
241
|
+
for (const entry of term.coeffs) pivotOut(entry[0]);
|
|
242
|
+
const lastRow = this.rows.length - 1;
|
|
243
|
+
for (const column of index) this.set(lastRow, column, getWork(column));
|
|
244
|
+
this.set(lastRow, this.columns.length - 1, Rational.one);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Ported from Z3 (src/util/trail.h, src/util/union_find.h), MIT license, see z3-license.txt. A
|
|
2
|
+
// scoped undo trail, and the union-find over it whose merges a pop undoes, which nla's variable
|
|
3
|
+
// equivalences and monomial congruence classes use.
|
|
4
|
+
import { shrink } from "../array";
|
|
5
|
+
|
|
6
|
+
export class TrailStack {
|
|
7
|
+
private trail: (() => void)[] = [];
|
|
8
|
+
private scopes: number[] = [];
|
|
9
|
+
|
|
10
|
+
push(undo: () => void) {
|
|
11
|
+
this.trail.push(undo);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
pushScope() {
|
|
15
|
+
this.scopes.push(this.trail.length);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
popScope(count: number) {
|
|
19
|
+
if (count === 0) return;
|
|
20
|
+
const size = this.scopes[this.scopes.length - count];
|
|
21
|
+
shrink(this.scopes, this.scopes.length - count);
|
|
22
|
+
while (this.trail.length > size) this.trail.pop()?.();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The handlers of union_find's context: r2 becomes the root of r1's class.
|
|
27
|
+
export interface UnionFindHandler {
|
|
28
|
+
mergeEh?: (r2: number, r1: number, v2: number, v1: number) => void;
|
|
29
|
+
afterMergeEh?: (r2: number, r1: number, v2: number, v1: number) => void;
|
|
30
|
+
unmergeEh?: (r2: number, r1: number) => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class UnionFind {
|
|
34
|
+
private finds: number[] = [];
|
|
35
|
+
private sizes: number[] = [];
|
|
36
|
+
private nexts: number[] = [];
|
|
37
|
+
|
|
38
|
+
handler: UnionFindHandler = {};
|
|
39
|
+
|
|
40
|
+
constructor(private trail: TrailStack) {}
|
|
41
|
+
|
|
42
|
+
get numVars() {
|
|
43
|
+
return this.finds.length;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
mkVar() {
|
|
47
|
+
const value = this.finds.length;
|
|
48
|
+
this.finds.push(value);
|
|
49
|
+
this.sizes.push(1);
|
|
50
|
+
this.nexts.push(value);
|
|
51
|
+
this.trail.push(() => {
|
|
52
|
+
this.finds.pop();
|
|
53
|
+
this.sizes.pop();
|
|
54
|
+
this.nexts.pop();
|
|
55
|
+
});
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
find(input: number) {
|
|
60
|
+
let value = input;
|
|
61
|
+
while (this.finds[value] !== value) value = this.finds[value];
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
next(value: number) {
|
|
66
|
+
return this.nexts[value];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
merge(inputV1: number, inputV2: number) {
|
|
70
|
+
const root1 = this.find(inputV1);
|
|
71
|
+
const root2 = this.find(inputV2);
|
|
72
|
+
if (root1 === root2) return;
|
|
73
|
+
const isSwapped = this.sizes[root1] > this.sizes[root2];
|
|
74
|
+
const r1 = isSwapped ? root2 : root1;
|
|
75
|
+
const r2 = isSwapped ? root1 : root2;
|
|
76
|
+
const v1 = isSwapped ? inputV2 : inputV1;
|
|
77
|
+
const v2 = isSwapped ? inputV1 : inputV2;
|
|
78
|
+
this.handler.mergeEh?.(r2, r1, v2, v1);
|
|
79
|
+
this.finds[r1] = r2;
|
|
80
|
+
this.sizes[r2] += this.sizes[r1];
|
|
81
|
+
this.swapNexts(r1, r2);
|
|
82
|
+
this.trail.push(() => {
|
|
83
|
+
this.sizes[r2] -= this.sizes[r1];
|
|
84
|
+
this.finds[r1] = r1;
|
|
85
|
+
this.swapNexts(r1, r2);
|
|
86
|
+
this.handler.unmergeEh?.(r2, r1);
|
|
87
|
+
});
|
|
88
|
+
this.handler.afterMergeEh?.(r2, r1, v2, v1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private swapNexts(first: number, second: number) {
|
|
92
|
+
const saved = this.nexts[first];
|
|
93
|
+
this.nexts[first] = this.nexts[second];
|
|
94
|
+
this.nexts[second] = saved;
|
|
95
|
+
}
|
|
96
|
+
}
|