This commit is contained in:
2
optimizer/.gitignore
vendored
Normal file
2
optimizer/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
perf.data*
|
||||
704
optimizer/Cargo.lock
generated
Normal file
704
optimizer/Cargo.lock
generated
Normal file
@@ -0,0 +1,704 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
|
||||
|
||||
[[package]]
|
||||
name = "arbitrary"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
||||
|
||||
[[package]]
|
||||
name = "canvas"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arbitrary",
|
||||
"criterion",
|
||||
"itertools 0.14.0",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed87a9d530bb41a67537289bafcac159cb3ee28460e0a4571123d2a778a6a882"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64f4f3f3c77c94aff3c7e9aac9a2ca1974a5adf392a8bb751e827d6d127ab966"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1c047a62b0cc3e145fa84415a3191f628e980b194c2755aa12300a4e6cbd928"
|
||||
dependencies = [
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"itertools 0.13.0",
|
||||
"num-traits",
|
||||
"oorandom",
|
||||
"plotters",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b1bcc0dc7dfae599d84ad0b1a55f80cde8af3725da8313b528da95ef783e338"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools 0.13.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||
dependencies = [
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"r-efi",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "459196ed295495a68f7d7fe1d84f6c4b7ff0e21fe3017b2f283c6fac3ad803c9"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crunchy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.174"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||
|
||||
[[package]]
|
||||
name = "plotters"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"plotters-backend",
|
||||
"plotters-svg",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters-backend"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
|
||||
|
||||
[[package]]
|
||||
name = "plotters-svg"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
|
||||
dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "5.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
|
||||
dependencies = [
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
|
||||
dependencies = [
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.141"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.14.2+wasi-0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
|
||||
dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.59.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
7
optimizer/Cargo.toml
Normal file
7
optimizer/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[workspace]
|
||||
resolver = "3"
|
||||
members = ["canvas"]
|
||||
[profile.release]
|
||||
debug = "line-tables-only"
|
||||
[build]
|
||||
rustflags = ["-C", "force-frame-pointers=yes"]
|
||||
2
optimizer/README.md
Normal file
2
optimizer/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
[](https://gitea.thasky.one/lukas/canvas-boardgame/actions)
|
||||
1
optimizer/canvas/.gitignore
vendored
Normal file
1
optimizer/canvas/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
optimizer/canvas/Cargo.lock
generated
Normal file
7
optimizer/canvas/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "canvas"
|
||||
version = "0.1.0"
|
||||
16
optimizer/canvas/Cargo.toml
Normal file
16
optimizer/canvas/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "canvas"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
arbitrary = "1.4.1"
|
||||
itertools = "0.14.0"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.7", features = ["html_reports"] }
|
||||
rand = "0.9.2"
|
||||
|
||||
[[bench]]
|
||||
name = "painting"
|
||||
harness = false
|
||||
73
optimizer/canvas/benches/painting.rs
Normal file
73
optimizer/canvas/benches/painting.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use arbitrary::{Arbitrary, Unstructured};
|
||||
use canvas::{Card, draw_painting};
|
||||
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
|
||||
use rand::{Rng, rngs::StdRng};
|
||||
use std::{hint::black_box, time::Duration};
|
||||
|
||||
use crate::random_card::RandomCard;
|
||||
|
||||
mod random_card;
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
let cards = vec![
|
||||
Card::from_dbg_string(".ttt."),
|
||||
Card::from_dbg_string("t.l.."),
|
||||
Card::from_dbg_string("ll..pt"),
|
||||
];
|
||||
assert_eq!(draw_painting(&cards, &[]), Some([0, 1, 2]));
|
||||
c.bench_function("painting optimizer", |b| {
|
||||
b.iter(|| {
|
||||
draw_painting(&black_box(cards.clone()), &[]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn random_card_pool<const N: usize>(rng: &mut StdRng) -> Vec<Card> {
|
||||
<[RandomCard; N] as Arbitrary>::arbitrary(&mut Unstructured::new(&rng.random::<[u8; 32]>()))
|
||||
.unwrap()
|
||||
.map(|x| x.0)
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
fn from_elem(c: &mut Criterion) {
|
||||
use rand::SeedableRng;
|
||||
// Define a seed. This can be any value that implements the `SeedableRng` trait.
|
||||
let seed: [u8; 32] = [
|
||||
8u64.to_be_bytes(),
|
||||
8u64.to_be_bytes(),
|
||||
8u64.to_be_bytes(),
|
||||
8u64.to_be_bytes(),
|
||||
]
|
||||
.concat()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
// Create a new RNG with the specified seed.
|
||||
let mut rng: StdRng = SeedableRng::from_seed(seed);
|
||||
// Generate a random number using the seeded RNG.
|
||||
let card_pool = [
|
||||
random_card_pool::<4>(&mut rng),
|
||||
random_card_pool::<8>(&mut rng),
|
||||
random_card_pool::<16>(&mut rng),
|
||||
random_card_pool::<32>(&mut rng),
|
||||
random_card_pool::<64>(&mut rng),
|
||||
random_card_pool::<128>(&mut rng),
|
||||
];
|
||||
|
||||
let mut group = c.benchmark_group("from_elem");
|
||||
for size in card_pool.iter() {
|
||||
group.bench_with_input(BenchmarkId::from_parameter(size.len()), size, |b, size| {
|
||||
b.iter(|| {
|
||||
draw_painting(&black_box(size.clone()), &[])
|
||||
});
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(name = benches;
|
||||
config = Criterion::default()
|
||||
.warm_up_time(Duration::from_millis(100))
|
||||
.measurement_time(Duration::from_millis(500));
|
||||
targets = criterion_benchmark, from_elem);
|
||||
criterion_main!(benches);
|
||||
30
optimizer/canvas/benches/random_card.rs
Normal file
30
optimizer/canvas/benches/random_card.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use arbitrary::{Arbitrary, Unstructured};
|
||||
use canvas::{Card, CardField};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RandomCard(pub(crate) Card);
|
||||
#[derive(Debug)]
|
||||
struct RandomCardField(CardField);
|
||||
|
||||
impl<'a> Arbitrary<'a> for RandomCardField {
|
||||
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
|
||||
let i = u.int_in_range(0..=15)?;
|
||||
if let Ok(w) = CardField::try_from_bits(i) {
|
||||
Ok(RandomCardField(w))
|
||||
} else {
|
||||
Ok(RandomCardField(CardField::Empty))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Arbitrary<'a> for RandomCard {
|
||||
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
|
||||
let mut r = vec![];
|
||||
for _ in 0..5 {
|
||||
r.push(u.arbitrary::<RandomCardField>()?)
|
||||
}
|
||||
let w: [RandomCardField; 5] = r.try_into().unwrap();
|
||||
let r = Card::from(w.map(|x: RandomCardField| x.0));
|
||||
Ok(RandomCard(r))
|
||||
}
|
||||
}
|
||||
121
optimizer/canvas/src/card.rs
Normal file
121
optimizer/canvas/src/card.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use crate::{CardField, Icon};
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
// There are 60 cards in the base set
|
||||
pub struct Card {
|
||||
field: u32,
|
||||
}
|
||||
|
||||
pub struct CardFieldIter {
|
||||
index: u8,
|
||||
field: u32,
|
||||
}
|
||||
|
||||
impl Iterator for CardFieldIter {
|
||||
type Item = CardField;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.index < 5 {
|
||||
let old_index = self.index;
|
||||
self.index += 1;
|
||||
let bitmask = (1 << (CardField::BITPACKED_SIZE as u8)) - 1;
|
||||
let result = (self.field >> (CardField::BITPACKED_SIZE as u8 * old_index)) & bitmask;
|
||||
Some(CardField::try_from_bits(result as u8).unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[CardField; 5]> for Card {
|
||||
fn from(value: [CardField; 5]) -> Self {
|
||||
let field = value
|
||||
.iter()
|
||||
.map(|x| x.to_bits())
|
||||
.zip((0u8..).step_by(CardField::BITPACKED_SIZE))
|
||||
.map(|(a, shift_count)| (a as u32) << (shift_count))
|
||||
.fold(0u32, |a, b| a | b);
|
||||
Self { field }
|
||||
}
|
||||
}
|
||||
|
||||
impl Card {
|
||||
pub fn field_iter(&self) -> CardFieldIter {
|
||||
CardFieldIter {
|
||||
index: 0,
|
||||
field: self.field,
|
||||
}
|
||||
}
|
||||
pub const fn empty() -> Self {
|
||||
Self { field: u32::MAX }
|
||||
}
|
||||
pub fn from_dbg_string(text: &str) -> Self {
|
||||
let mut result = [CardField::Empty; 5];
|
||||
let mut current_field = 0;
|
||||
let mut char_iter = text.chars();
|
||||
let match_icon = |x: char| {
|
||||
let field = match x {
|
||||
's' => Icon::Square,
|
||||
'c' => Icon::Circle,
|
||||
't' => Icon::Triangle,
|
||||
'l' => Icon::Color,
|
||||
_ => return None,
|
||||
};
|
||||
Some(field)
|
||||
};
|
||||
while let Some(c) = char_iter.next() {
|
||||
if let Some(f) = match_icon(c) {
|
||||
result[current_field] = CardField::Icon(f);
|
||||
current_field += 1;
|
||||
continue;
|
||||
}
|
||||
let field = match c {
|
||||
'p' => {
|
||||
let f = match_icon(char_iter.next().expect("No icon after point symbol"))
|
||||
.expect("Incorrect icon after point symbol");
|
||||
CardField::PointsPer(f)
|
||||
}
|
||||
'd' => {
|
||||
let f1 =
|
||||
match_icon(char_iter.next().expect("No first icon after double symbol"))
|
||||
.expect("Incorrect first icon after double symbol");
|
||||
let f2 = match_icon(
|
||||
char_iter
|
||||
.next()
|
||||
.expect("No second icon after double symbol"),
|
||||
)
|
||||
.expect("Incorrect second icon after double symbol");
|
||||
CardField::DoubleIcon(f1, f2)
|
||||
}
|
||||
'.' => CardField::Empty,
|
||||
'-' => continue,
|
||||
_ => panic!("Unknown character {c}"),
|
||||
};
|
||||
|
||||
result[current_field] = field;
|
||||
current_field += 1;
|
||||
}
|
||||
result.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{CardField, Icon, card::Card};
|
||||
|
||||
#[test]
|
||||
fn test_correctness() {
|
||||
let card = Card::from_dbg_string("l..t.");
|
||||
println!("{:0x}", card.field);
|
||||
assert_eq!(
|
||||
card.field_iter().collect::<Vec<_>>(),
|
||||
vec![
|
||||
CardField::Icon(Icon::Color),
|
||||
CardField::Empty,
|
||||
CardField::Empty,
|
||||
CardField::Icon(Icon::Triangle),
|
||||
CardField::Empty,
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
123
optimizer/canvas/src/icon_bitfield.rs
Normal file
123
optimizer/canvas/src/icon_bitfield.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
use crate::Card;
|
||||
use crate::CardField;
|
||||
use crate::Icon;
|
||||
use crate::Painting;
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct IconBitfield {
|
||||
circle: u8,
|
||||
square: u8,
|
||||
triangle: u8,
|
||||
color: u8,
|
||||
points: u8,
|
||||
}
|
||||
|
||||
impl IconBitfield {
|
||||
pub fn get_icon_bits(&self, icon: Icon) -> u8 {
|
||||
match icon {
|
||||
Icon::Circle => self.circle,
|
||||
Icon::Square => self.square,
|
||||
Icon::Triangle => self.triangle,
|
||||
Icon::Color => self.color,
|
||||
}
|
||||
}
|
||||
pub fn get_point_bits(&self) -> u8 {
|
||||
self.points
|
||||
}
|
||||
|
||||
fn add_icon(&mut self, icon: Icon, index: u8) {
|
||||
let field = match icon {
|
||||
Icon::Circle => &mut self.circle,
|
||||
Icon::Square => &mut self.square,
|
||||
Icon::Triangle => &mut self.triangle,
|
||||
Icon::Color => &mut self.color,
|
||||
};
|
||||
*field |= 1 << index;
|
||||
}
|
||||
pub fn add_field(&mut self, field: CardField, index: u8) {
|
||||
debug_assert!(index < 5);
|
||||
match field {
|
||||
CardField::Icon(icon) => self.add_icon(icon, index),
|
||||
CardField::DoubleIcon(icon1, icon2) => {
|
||||
self.add_icon(icon1, index);
|
||||
self.add_icon(icon2, index);
|
||||
}
|
||||
CardField::PointsPer(_) => self.points |= 1 << index,
|
||||
CardField::Empty => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(card: &Card) -> Self {
|
||||
let mut result = Self::default();
|
||||
(0u8..)
|
||||
.zip(card.field_iter())
|
||||
.for_each(|(index, field)| result.add_field(field, index));
|
||||
result
|
||||
}
|
||||
|
||||
pub fn from_painting(painting: &Painting) -> Self {
|
||||
let mut bits = Self::new(&painting[0]);
|
||||
bits.layer_below(&Self::new(&painting[1]))
|
||||
.layer_below(&Self::new(&painting[2]));
|
||||
bits
|
||||
}
|
||||
|
||||
pub fn all_fields(&self) -> u8 {
|
||||
self.circle | self.square | self.triangle | self.color | self.points
|
||||
}
|
||||
|
||||
/// Result of putting the other card below this one
|
||||
pub fn layer_below(&mut self, other: &Self) -> &mut Self {
|
||||
// 1 x 0 = 0
|
||||
// 0 x 0 = 0
|
||||
// 0 x 1 = 1
|
||||
// 1 x 1 = 0
|
||||
// Should be !a & b
|
||||
|
||||
let bitmask = !self.all_fields() & other.all_fields();
|
||||
self.circle |= other.circle & bitmask;
|
||||
self.square |= other.square & bitmask;
|
||||
self.triangle |= other.triangle & bitmask;
|
||||
self.color |= other.color & bitmask;
|
||||
self.points |= other.points & bitmask;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{Icon, icon_bitfield::IconBitfield};
|
||||
|
||||
#[test]
|
||||
fn test_add_field() {
|
||||
let mut x: IconBitfield = Default::default();
|
||||
x.add_icon(Icon::Circle, 2);
|
||||
let expected = IconBitfield {
|
||||
circle: 0b00100,
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(x, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layer_below() {
|
||||
let mut a: IconBitfield = IconBitfield {
|
||||
color: 0b00010,
|
||||
points: 0b01000,
|
||||
..Default::default()
|
||||
};
|
||||
let b = IconBitfield {
|
||||
triangle: a.get_point_bits(),
|
||||
square: 0b10000,
|
||||
..Default::default()
|
||||
};
|
||||
let expected = IconBitfield {
|
||||
color: a.get_icon_bits(Icon::Color),
|
||||
points: a.get_point_bits(),
|
||||
square: b.get_icon_bits(Icon::Square),
|
||||
..Default::default()
|
||||
};
|
||||
a.layer_below(&b);
|
||||
assert_eq!(a, expected);
|
||||
}
|
||||
}
|
||||
152
optimizer/canvas/src/lib.rs
Normal file
152
optimizer/canvas/src/lib.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
#![allow(dead_code)]
|
||||
#![warn(clippy::unwrap_used, clippy::expect_used)]
|
||||
use crate::objective::Objective;
|
||||
|
||||
mod card;
|
||||
mod icon_bitfield;
|
||||
mod objective;
|
||||
mod optimizer;
|
||||
|
||||
pub use card::Card;
|
||||
|
||||
pub use optimizer::draw_painting;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Shop {
|
||||
cards: [Card; 5],
|
||||
coins: [u8; 5],
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Icon {
|
||||
Circle = 0b00,
|
||||
Square = 0b01,
|
||||
Triangle = 0b10,
|
||||
Color = 0b11,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for Icon {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
ICON_ORDER
|
||||
.get(value as usize)
|
||||
.map(|x| Ok(*x))
|
||||
.unwrap_or(Err(()))
|
||||
}
|
||||
}
|
||||
const ICON_ORDER: [Icon; 4] = [Icon::Circle, Icon::Square, Icon::Triangle, Icon::Color];
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub enum CardField {
|
||||
Icon(Icon),
|
||||
PointsPer(Icon),
|
||||
DoubleIcon(Icon, Icon),
|
||||
#[default]
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl CardField {
|
||||
pub const BITPACKED_SIZE: usize = 4;
|
||||
pub fn try_from_bits(bits: u8) -> Result<Self, ()> {
|
||||
{
|
||||
let bits = bits & ((1 << Self::BITPACKED_SIZE) - 1);
|
||||
if (bits) == 0b1111 {
|
||||
Ok(Self::Empty)
|
||||
} else if (bits & (1 << 3)) != 0 {
|
||||
let bits = bits & ((1 << 3) - 1);
|
||||
if bits == 5 {
|
||||
Ok(Self::DoubleIcon(Icon::Triangle, Icon::Color))
|
||||
} else if bits >= 3 {
|
||||
Ok(Self::DoubleIcon(
|
||||
Icon::Square,
|
||||
Icon::try_from(bits + 2 - 3)?,
|
||||
))
|
||||
} else {
|
||||
Ok(Self::DoubleIcon(Icon::Circle, Icon::try_from(bits + 1)?))
|
||||
}
|
||||
} else if bits & (1 << 2) != 0 {
|
||||
let bits = bits & ((1 << 2) - 1);
|
||||
Ok(Self::PointsPer(Icon::try_from(bits)?))
|
||||
} else {
|
||||
Ok(Self::Icon(Icon::try_from(bits)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
/// There are only 4 + 4 + 6 + 1 possibilities when double icons are sorted
|
||||
fn to_bits(self) -> u8 {
|
||||
match self {
|
||||
CardField::Icon(icon) => icon as u8,
|
||||
CardField::PointsPer(icon) => icon as u8 | (1 << 2),
|
||||
CardField::DoubleIcon(mut a, mut b) => {
|
||||
debug_assert_ne!(a, b);
|
||||
if a > b {
|
||||
(a, b) = (b, a);
|
||||
}
|
||||
let tag = match a {
|
||||
// 0..=2
|
||||
Icon::Circle => ICON_ORDER.iter().position(|x| b == *x).unwrap() as u8 - 1,
|
||||
// 3..=4
|
||||
Icon::Square => ICON_ORDER.iter().position(|x| b == *x).unwrap() as u8 - 2 + 3,
|
||||
// 5
|
||||
Icon::Triangle => {
|
||||
debug_assert_eq!(b, Icon::Color);
|
||||
5
|
||||
}
|
||||
Icon::Color => panic!("Color as first double is not possible"),
|
||||
};
|
||||
tag | (1 << 3)
|
||||
}
|
||||
CardField::Empty => (1 << 4) - 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Player {
|
||||
cards: Vec<Card>,
|
||||
coins: u8,
|
||||
paintings: Vec<[Card; 3]>,
|
||||
}
|
||||
|
||||
type Painting = [Card; 3];
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Board {
|
||||
players: Vec<Player>,
|
||||
shop: Shop,
|
||||
objectives: [CriteriaCard; 4],
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
struct PointThresholds(u32);
|
||||
|
||||
impl PointThresholds {
|
||||
const BITFIELD_SIZE: usize = 4;
|
||||
pub fn len(&self) -> usize {
|
||||
(self.0.ilog2() as usize).div_ceil(Self::BITFIELD_SIZE)
|
||||
}
|
||||
pub fn new(points: &[u8]) -> Self {
|
||||
let mut result = Self(0);
|
||||
for (index, p) in points.iter().enumerate() {
|
||||
result.set(index, *p);
|
||||
}
|
||||
result
|
||||
}
|
||||
fn set(&mut self, index: usize, value: u8) {
|
||||
debug_assert!(value < (1 << Self::BITFIELD_SIZE));
|
||||
self.0 |= u32::from(value) << (index * Self::BITFIELD_SIZE);
|
||||
}
|
||||
pub fn get(&self, index: usize) -> u8 {
|
||||
let bitmask = (1u8 << Self::BITFIELD_SIZE) - 1;
|
||||
(self.0 >> (index * Self::BITFIELD_SIZE)) as u8 & bitmask
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CriteriaCard {
|
||||
objective: Objective,
|
||||
// Points depending on how often the criteria was met
|
||||
points: PointThresholds,
|
||||
}
|
||||
184
optimizer/canvas/src/objective.rs
Normal file
184
optimizer/canvas/src/objective.rs
Normal file
@@ -0,0 +1,184 @@
|
||||
use crate::{icon_bitfield::IconBitfield, Icon, Painting, ICON_ORDER};
|
||||
|
||||
// There are 12 objectives/criteria in the base set
|
||||
#[derive(Debug)]
|
||||
pub enum Objective {
|
||||
AllFields,
|
||||
/// Must use every icon in the vector at least once
|
||||
IconsUsed(IconSet),
|
||||
/// The count of all icons in the vec `.0` combined is exactly `.1`
|
||||
ExactlyNIcons((IconSet, u8)),
|
||||
/// These icons are in fields exactly next to each other
|
||||
/// One icon can only be the neighbor of one other icon
|
||||
Neighbors(Vec<(Icon, Icon)>),
|
||||
/// Both icons are on one card, but not adjacent
|
||||
DistantNeighbors(Vec<(Icon, Icon)>),
|
||||
NInARow(Icon, u8),
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct IconSet(u8);
|
||||
|
||||
pub struct IconSetIterator {
|
||||
icon_set: IconSet,
|
||||
index: u8,
|
||||
}
|
||||
|
||||
impl From<&[Icon]> for IconSet {
|
||||
fn from(value: &[Icon]) -> Self {
|
||||
let v = value
|
||||
.iter()
|
||||
.map(|x| 1 << ICON_ORDER.iter().position(|v| x == v).unwrap())
|
||||
.fold(0, |a, b| a | b);
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl IconSet {
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.count_ones() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for IconSetIterator {
|
||||
type Item = Icon;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while self.index < 4 {
|
||||
let old_index = self.index;
|
||||
self.index += 1;
|
||||
|
||||
if self.icon_set.0 & (1 << old_index) != 0 {
|
||||
return Some(*ICON_ORDER.get(old_index as usize).unwrap());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for IconSet {
|
||||
type Item = Icon;
|
||||
type IntoIter = IconSetIterator;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IconSetIterator {
|
||||
icon_set: self,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Objective {
|
||||
pub fn count_matches_bitfield(&self, bitfield: &IconBitfield) -> u8 {
|
||||
match self {
|
||||
Objective::AllFields => {
|
||||
if bitfield.all_fields() == 0b11111 {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Objective::IconsUsed(icons) => {
|
||||
let bits = bitfield;
|
||||
if icons.into_iter().all(|i| bits.get_icon_bits(i) > 0) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Objective::ExactlyNIcons((icons, count)) => {
|
||||
let bits = bitfield;
|
||||
|
||||
if icons
|
||||
.into_iter()
|
||||
.map(|i| bits.get_icon_bits(i).count_ones())
|
||||
.sum::<u32>() as u8
|
||||
== *count
|
||||
{
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Objective::NInARow(icon, count) => {
|
||||
let mut bits = bitfield.get_icon_bits(*icon);
|
||||
let bitmask = (1 << count) - 1;
|
||||
let mut res_count = 0;
|
||||
while bits > 0 {
|
||||
if (bits) & bitmask == bitmask {
|
||||
res_count += 1;
|
||||
bits >>= count;
|
||||
} else {
|
||||
bits >>= 1;
|
||||
}
|
||||
}
|
||||
res_count
|
||||
// Objective::Neighbors(items) => todo!(),
|
||||
// Objective::DistantNeighbors(items) => todo!(),
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count_matches(&self, painting: &Painting) -> u8 {
|
||||
self.count_matches_bitfield(&IconBitfield::from_painting(painting))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
Card, Icon, Painting,
|
||||
objective::{IconSet, Objective},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_all_fields() {
|
||||
let v: Painting = [
|
||||
Card::from_dbg_string("...ct"),
|
||||
Card::from_dbg_string("s.s.."),
|
||||
Card::from_dbg_string(".l.t."),
|
||||
];
|
||||
assert_eq!(Objective::AllFields.count_matches(&v), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_n_in_a_row() {
|
||||
let v: Painting = [
|
||||
Card::from_dbg_string("...ll"),
|
||||
Card::from_dbg_string("ll..."),
|
||||
Card::from_dbg_string("..t.."),
|
||||
];
|
||||
assert_eq!(Objective::NInARow(Icon::Color, 2).count_matches(&v), 2);
|
||||
let tricky_two: Painting = [
|
||||
Card::from_dbg_string("...ll"),
|
||||
Card::from_dbg_string("tl..."),
|
||||
Card::from_dbg_string("..lc."),
|
||||
];
|
||||
assert_eq!(
|
||||
Objective::NInARow(Icon::Color, 2).count_matches(&tricky_two),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_icons_used() {
|
||||
let v: Painting = [
|
||||
Card::from_dbg_string("...ll"),
|
||||
Card::from_dbg_string("ll..."),
|
||||
Card::from_dbg_string("..t.."),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
Objective::IconsUsed(IconSet::from([Icon::Color].as_slice())).count_matches(&v),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
Objective::IconsUsed(IconSet::from([Icon::Triangle].as_slice())).count_matches(&v),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
Objective::IconsUsed(IconSet::from([Icon::Circle].as_slice())).count_matches(&v),
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
90
optimizer/canvas/src/optimizer.rs
Normal file
90
optimizer/canvas/src/optimizer.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{CardField, CriteriaCard, card::Card, icon_bitfield::IconBitfield};
|
||||
|
||||
struct OneIndexIterator {
|
||||
n: u64,
|
||||
offset: u8,
|
||||
}
|
||||
|
||||
impl From<u64> for OneIndexIterator {
|
||||
fn from(value: u64) -> Self {
|
||||
Self {
|
||||
n: value,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for OneIndexIterator {
|
||||
type Item = u8;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.n == 0 {
|
||||
return None;
|
||||
}
|
||||
let new_offset = self.n.trailing_zeros() as u8;
|
||||
self.n >>= new_offset + 1;
|
||||
let result = self.offset + new_offset;
|
||||
self.offset += new_offset + 1;
|
||||
Some(result)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_painting(cards: &[Card], obj: &[(&CriteriaCard, u8)]) -> Option<[usize; 3]> {
|
||||
let bitfields = cards.iter().map(IconBitfield::new).collect::<Vec<_>>();
|
||||
let mut best: Option<([usize; 3], u8)> = None;
|
||||
for stack in (0..cards.len()).permutations(3) {
|
||||
let stack_indices: [_; 3] = stack.try_into().unwrap();
|
||||
let stack = stack_indices.map(|x| cards[x]);
|
||||
let maps = stack_indices.map(|x| &bitfields[x]);
|
||||
let mut painting = maps[0].clone();
|
||||
painting.layer_below(maps[1]).layer_below(maps[2]);
|
||||
let criteria_points = obj
|
||||
.iter()
|
||||
.map(|(criteria, reached)| {
|
||||
criteria.points.get(usize::from(
|
||||
criteria.objective.count_matches_bitfield(&painting) + *reached,
|
||||
))
|
||||
})
|
||||
.sum::<u8>();
|
||||
let bonus_points = OneIndexIterator::from(u64::from(painting.get_point_bits()))
|
||||
.map(|bit_index| {
|
||||
let matched_icon = stack
|
||||
.iter()
|
||||
.map(|x| x.field_iter().nth(usize::from(bit_index)).unwrap())
|
||||
.find_map(|x| match x {
|
||||
CardField::PointsPer(icon) => Some(icon),
|
||||
_ => None,
|
||||
})
|
||||
.expect("Point bits lied");
|
||||
painting.get_icon_bits(matched_icon).count_ones() as u8 * 2
|
||||
})
|
||||
.sum::<u8>();
|
||||
let total_points = bonus_points + criteria_points;
|
||||
let stack_result = (stack_indices, total_points);
|
||||
if let Some(bp) = &mut best {
|
||||
if bp.1 < total_points {
|
||||
*bp = stack_result;
|
||||
}
|
||||
} else {
|
||||
best = Some(stack_result)
|
||||
}
|
||||
}
|
||||
best.map(|x| x.0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{card::Card, optimizer::draw_painting};
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
let cards = vec![
|
||||
Card::from_dbg_string(".ttt."),
|
||||
Card::from_dbg_string("t.l.."),
|
||||
Card::from_dbg_string("ll..pt"),
|
||||
];
|
||||
assert_eq!(draw_painting(&cards, &[]), Some([0, 1, 2]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user