Reduce cloning

Signed-off-by: Julius Koskela <julius.koskela@unikie.com>
This commit is contained in:
Julius Koskela 2023-12-29 18:40:53 +02:00
parent dfb76ae745
commit 27384a3044
Signed by: julius
GPG Key ID: 5A7B7F4897C2914B
8 changed files with 753 additions and 188 deletions

588
Cargo.lock generated
View File

@ -2,24 +2,209 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
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.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "bytemuck"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6"
[[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.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656"
[[package]]
name = "ciborium-ll"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9"
dependencies = [
"anstyle",
"clap_lex",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools 0.10.5",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fca89a0e215bab21874660c67903c5f143333cab1da83d041c7ded6053774751"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e3681d554572a651dda4186cd47240627c3d0114d45a95f6ad27f2f22e7548d"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3a430a770ebd84726f584a90ee7f020d28db52c6d02138900f22341f866d39c"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "getrandom"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "getset"
version = "0.1.2"
@ -32,6 +217,38 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "is-terminal"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.12.0"
@ -47,19 +264,54 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "manifold"
version = "0.1.0"
dependencies = [
"bytemuck",
"criterion",
"getset",
"itertools",
"itertools 0.12.0",
"num",
"rand",
"serde",
"serde_json",
"static_assertions",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "num"
version = "0.4.1"
@ -136,6 +388,52 @@ dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "plotters"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609"
[[package]]
name = "plotters-svg"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab"
dependencies = [
"plotters-backend",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -178,12 +476,113 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustix"
version = "0.38.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "ryu"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[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.193"
@ -243,6 +642,16 @@ dependencies = [
"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.12"
@ -254,3 +663,180 @@ name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.43",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
[[package]]
name = "web-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View File

@ -2,8 +2,20 @@
name = "manifold"
version = "0.1.0"
edition = "2021"
license = "MIT/Apache-2.0"
authors = ["Julius Koskela <julius.koskela@nordic-dev.net>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
description = """
GDSL is a graph data-structure library including graph containers,
connected node strutures and efficient algorithms on those structures.
Nodes are independent of a graph container and can be used as connected
smart pointers.
"""
repository = "https://nordic-dev.net/julius/manifold"
keywords = ["data-structures", "algorithms", "containers"]
categories = ["data-structures", "algorithms", "mathematics", "science"]
[dependencies]
bytemuck = "1.14.0"
@ -13,3 +25,7 @@ num = "0.4.1"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
static_assertions = "1.1.0"
[dev-dependencies]
rand = "0.8.5"
criterion = "0.5.1"

View File

@ -1,4 +1,4 @@
# Tensorc
# Mainfold
```rust
// Create two tensors with different ranks and shapes

View File

@ -6,88 +6,93 @@ pub struct Axis<'a, T: Value, const R: usize> {
#[getset(get = "pub")]
tensor: &'a Tensor<T, R>,
#[getset(get = "pub")]
shape: Shape<R>,
#[getset(get = "pub")]
dim: usize,
#[getset(get = "pub")]
len: usize,
}
impl<'a, T: Value, const R: usize> Axis<'a, T, R> {
pub fn new(tensor: &'a Tensor<T, R>, dim: usize) -> Self {
assert!(dim < R, "Axis out of bounds");
Self {
tensor,
shape: tensor.shape(),
dim,
len: tensor.shape().get(dim),
}
Self { tensor, dim }
}
pub fn iter_level(&self, level: usize) -> AxisIterator<'a, T, R> {
assert!(level < self.len, "Level out of bounds");
let mut index = Idx::new(self.shape.clone(), [0; R]);
pub fn len(&self) -> usize {
self.tensor.shape().get(self.dim)
}
pub fn shape(&self) -> &Shape<R> {
self.tensor.shape()
}
pub fn iter_level(&'a self, level: usize) -> AxisIterator<'a, T, R> {
assert!(level < self.len(), "Level out of bounds");
let mut index = Idx::new(self.shape(), [0; R]);
index.set_axis(self.dim, level);
AxisIterator::new(self.clone())
.set_start(level)
.set_end(level + 1)
AxisIterator::new(self).set_start(level).set_end(level + 1)
}
}
#[derive(Clone, Debug, Getters, MutGetters)]
pub struct AxisIterator<'a, T: Value, const R: usize> {
#[getset(get = "pub")]
axis: Axis<'a, T, R>,
axis: &'a Axis<'a, T, R>,
#[getset(get = "pub", get_mut = "pub")]
index: Idx<R>,
index: Idx<'a, R>,
#[getset(get = "pub")]
end: Option<usize>,
}
impl<'a, T: Value, const R: usize> AxisIterator<'a, T, R> {
pub fn new(axis: Axis<'a, T, R>) -> Self {
pub fn new(axis: &'a Axis<'a, T, R>) -> Self {
Self {
axis: axis.clone(),
index: Idx::new(axis.shape().clone(), [0; R]),
axis,
index: Idx::new(axis.shape(), [0; R]),
end: None,
}
}
pub fn set_start(self, start: usize) -> Self {
assert!(start < self.axis.len, "Start out of bounds");
let mut index = Idx::new(self.axis.shape().clone(), [0; R]);
assert!(start < self.axis().len(), "Start out of bounds");
let mut index = Idx::new(self.axis().shape(), [0; R]);
index.set_axis(self.axis.dim, start);
Self {
axis: self.axis.clone(),
axis: self.axis(),
index,
end: None,
}
}
pub fn set_end(self, end: usize) -> Self {
assert!(end <= self.axis.len, "End out of bounds");
assert!(end <= self.axis().len(), "End out of bounds");
Self {
axis: self.axis.clone(),
index: self.index.clone(),
axis: self.axis(),
index: self.index().clone(),
end: Some(end),
}
}
pub fn set_level(self, level: usize) -> Self {
assert!(level < self.axis.len, "Level out of bounds");
assert!(level < self.axis().len(), "Level out of bounds");
self.set_start(level).set_end(level + 1)
}
pub fn disassemble(self) -> Vec<Self> {
let mut result = Vec::new();
for i in 0..self.axis().len {
result.push(Self::new(self.axis.clone()).set_level(i));
}
result
// pub fn disassemble(self) -> Vec<Self> {
// let mut result = Vec::new();
// for i in 0..self.axis().len() {
// result.push(Self::new(self.axis()).set_level(i));
// }
// result
// }
// pub fn disassemble(&'a self) -> impl Iterator<Item = Self> + 'a {
// (0..self.axis().len()).map(move |i| Self::new(self.axis()).set_level(i))
// }
pub fn level(&'a self, level: usize) -> impl Iterator<Item = &'a T> + 'a {
Self::new(self.axis()).set_level(level)
}
pub fn axis_max_idx(&self) -> usize {
self.end().unwrap_or(*self.axis().len())
self.end().unwrap_or(self.axis().len())
}
pub fn axis_idx(&self) -> usize {
@ -113,12 +118,12 @@ impl<'a, T: Value, const R: usize> Iterator for AxisIterator<'a, T, R> {
}
}
impl<'a, T: Value, const R: usize> IntoIterator for Axis<'a, T, R> {
impl<'a, T: Value, const R: usize> IntoIterator for &'a Axis<'a, T, R> {
type Item = &'a T;
type IntoIter = AxisIterator<'a, T, R>;
fn into_iter(self) -> Self::IntoIter {
AxisIterator::new(self)
AxisIterator::new(&self)
}
}
@ -165,20 +170,7 @@ where
let shape = Shape::new(shape);
let li = laxes
.clone()
.into_iter()
.map(|axis| axis.into_iter())
.collect::<Vec<_>>();
let ri = raxes
.clone()
.into_iter()
.map(|axis| axis.into_iter())
.collect::<Vec<_>>();
let result = contract_axes(li.try_into().unwrap(), ri.try_into().unwrap());
println!("result: {:?}", result);
let result = contract_axes(laxes, raxes);
let mut t = Tensor::new_with_buffer(shape, result[0].clone());
@ -202,8 +194,8 @@ pub fn contract_axes<
const S: usize,
const N: usize,
>(
laxes: [AxisIterator<'a, T, R>; N],
raxes: [AxisIterator<'a, T, S>; N],
laxes: [Axis<'a, T, R>; N],
raxes: [Axis<'a, T, S>; N],
) -> Vec<Vec<T>>
where
[(); R - N]:,
@ -211,23 +203,29 @@ where
{
let mut result = vec![];
let laxes = laxes.into_iter().map(|axis| axis.disassemble());
let raxes = raxes.into_iter().map(|axis| axis.disassemble());
let axes = laxes.zip(raxes);
let axes = laxes.into_iter().zip(raxes);
for (laxis, raxis) in axes {
for (laxis, raxis) in axes {
let mut axes_result: Vec<T> = vec![];
for rlevel in raxis {
for llevel in laxis.clone() {
let mut sum = T::zero();
for (lv, rv) in
llevel.clone().into_iter().zip(rlevel.clone().into_iter())
{
sum = sum + *lv * *rv;
}
axes_result.push(sum);
}
}
for i in 0..raxis.len() {
println!("raxis: {}", i);
for j in 0..laxis.len() {
println!("laxis: {}", j);
let mut sum = T::zero();
let llevel = laxis.into_iter();
let llevel = llevel.level(j);
let rlevel = raxis.into_iter();
let rlevel = rlevel.level(i);
let zip = llevel.zip(rlevel);
for (lv, rv) in zip {
println!("{} * {} = {}", lv, rv, *lv * *rv);
println!("{} + {} = {}", sum, *lv * *rv, sum + *lv * *rv);
sum = sum + *lv * *rv;
}
println!("sum: {}", sum);
axes_result.push(sum);
}
}
result.push(axes_result);
}
@ -251,7 +249,7 @@ mod tests {
let contracted_tensor: Tensor<i32, 2> =
contract(&a, &b, [Axis::new(&a, 1)], [Axis::new(&b, 0)]);
assert_eq!(contracted_tensor.shape(), Shape::new([2, 2]));
assert_eq!(contracted_tensor.shape(), &Shape::new([2, 2]));
assert_eq!(
contracted_tensor.buffer(),
&[7, 10, 15, 22],
@ -259,7 +257,7 @@ mod tests {
);
}
#[test]
#[test]
fn test_tensor_contraction_23x32() {
// Define two 2D tensors (matrices)
// Tensor A is 2x3
@ -272,7 +270,7 @@ mod tests {
let contracted_tensor: Tensor<i32, 2> =
contract(&a, &b, [Axis::new(&a, 1)], [Axis::new(&b, 0)]);
assert_eq!(contracted_tensor.shape(), Shape::new([3, 3]));
assert_eq!(contracted_tensor.shape(), &Shape::new([3, 3]));
assert_eq!(
contracted_tensor.buffer(),
&[9, 12, 15, 19, 26, 33, 29, 40, 51],
@ -280,35 +278,35 @@ mod tests {
);
}
#[test]
fn test_axis_iterator_disassemble() {
// Creating a 2x2 Tensor for testing
let tensor = Tensor::from([[1.0, 2.0], [3.0, 4.0]]);
// #[test]
// fn test_axis_iterator_disassemble() {
// // Creating a 2x2 Tensor for testing
// let tensor = Tensor::from([[1.0, 2.0], [3.0, 4.0]]);
// Testing iteration over the first axis (axis = 0)
let axis = Axis::new(&tensor, 0);
// // Testing iteration over the first axis (axis = 0)
// let axis = Axis::new(&tensor, 0);
let mut axis_iter = axis.into_iter().disassemble();
// let mut axis_iter = axis.into_iter().disassemble();
assert_eq!(axis_iter[0].next(), Some(&1.0));
assert_eq!(axis_iter[0].next(), Some(&2.0));
assert_eq!(axis_iter[0].next(), None);
assert_eq!(axis_iter[1].next(), Some(&3.0));
assert_eq!(axis_iter[1].next(), Some(&4.0));
assert_eq!(axis_iter[1].next(), None);
// assert_eq!(axis_iter[0].next(), Some(&1.0));
// assert_eq!(axis_iter[0].next(), Some(&2.0));
// assert_eq!(axis_iter[0].next(), None);
// assert_eq!(axis_iter[1].next(), Some(&3.0));
// assert_eq!(axis_iter[1].next(), Some(&4.0));
// assert_eq!(axis_iter[1].next(), None);
// Resetting the iterator for the second axis (axis = 1)
let axis = Axis::new(&tensor, 1);
// // Resetting the iterator for the second axis (axis = 1)
// let axis = Axis::new(&tensor, 1);
let mut axis_iter = axis.into_iter().disassemble();
// let mut axis_iter = axis.into_iter().disassemble();
assert_eq!(axis_iter[0].next(), Some(&1.0));
assert_eq!(axis_iter[0].next(), Some(&3.0));
assert_eq!(axis_iter[0].next(), None);
assert_eq!(axis_iter[1].next(), Some(&2.0));
assert_eq!(axis_iter[1].next(), Some(&4.0));
assert_eq!(axis_iter[1].next(), None);
}
// assert_eq!(axis_iter[0].next(), Some(&1.0));
// assert_eq!(axis_iter[0].next(), Some(&3.0));
// assert_eq!(axis_iter[0].next(), None);
// assert_eq!(axis_iter[1].next(), Some(&2.0));
// assert_eq!(axis_iter[1].next(), Some(&4.0));
// assert_eq!(axis_iter[1].next(), None);
// }
#[test]
fn test_axis_iterator() {

View File

@ -1,35 +1,38 @@
use super::*;
use std::cmp::Ordering;
use std::ops::{Add, Sub};
use getset::{Getters, MutGetters};
#[derive(Clone, Copy, Debug)]
pub struct Idx<const R: usize> {
#[derive(Clone, Copy, Debug, Getters, MutGetters)]
pub struct Idx<'a, const R: usize> {
#[getset(get = "pub", get_mut = "pub")]
indices: [usize; R],
shape: Shape<R>,
#[getset(get = "pub")]
shape: &'a Shape<R>,
}
impl<const R: usize> Idx<R> {
pub const fn zero(shape: Shape<R>) -> Self {
impl<'a, const R: usize> Idx<'a, R> {
pub const fn zero(shape: &'a Shape<R>) -> Self {
Self {
indices: [0; R],
shape,
shape: shape,
}
}
pub fn max(shape: Shape<R>) -> Self {
pub fn last(shape: &'a Shape<R>) -> Self {
let max_indices =
shape.as_array().map(|dim_size| dim_size.saturating_sub(1));
Self {
indices: max_indices,
shape,
shape: shape,
}
}
pub fn new(shape: Shape<R>, indices: [usize; R]) -> Self {
pub fn new(shape: &'a Shape<R>, indices: [usize; R]) -> Self {
if !shape.check_indices(indices) {
panic!("indices out of bounds");
}
Self { indices, shape }
Self { indices, shape: shape }
}
pub fn is_zero(&self) -> bool {
@ -204,23 +207,6 @@ impl<const R: usize> Idx<R> {
.0
}
// pub fn inc_axis(&mut self, axis: usize) -> bool {
// if axis >= R {
// // Axis is out of bounds
// return false;
// }
// let dim_size = self.shape.get(axis);
// if self.indices[axis] + 1 < dim_size {
// // Increment the index if it's not at its maximum
// self.indices[axis] += 1;
// true
// } else {
// // Index is at its maximum for this axis
// false
// }
// }
pub fn set_axis(&mut self, axis: usize, value: usize) {
assert!(axis < R, "Axis out of bounds");
// assert!(value < self.shape.get(axis), "Value out of bounds");
@ -241,43 +227,31 @@ impl<const R: usize> Idx<R> {
assert!(axis < R, "Axis out of bounds");
self.indices[axis]
}
pub fn indices(&self) -> &[usize; R] {
&self.indices
}
pub fn shape(&self) -> Shape<R> {
self.shape
}
pub fn shape_mut(&mut self) -> &mut Shape<R> {
&mut self.shape
}
}
// --- blanket impls ---
impl<const R: usize> PartialEq for Idx<R> {
impl<'a, const R: usize> PartialEq for Idx<'a, R> {
fn eq(&self, other: &Self) -> bool {
self.flat() == other.flat()
}
}
impl<const R: usize> Eq for Idx<R> {}
impl<'a, const R: usize> Eq for Idx<'a, R> {}
impl<const R: usize> PartialOrd for Idx<R> {
impl<'a, const R: usize> PartialOrd for Idx<'a, R> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.flat().partial_cmp(&other.flat())
}
}
impl<const R: usize> Ord for Idx<R> {
impl<'a, const R: usize> Ord for Idx<'a, R> {
fn cmp(&self, other: &Self) -> Ordering {
self.flat().cmp(&other.flat())
}
}
impl<const R: usize> Index<usize> for Idx<R> {
impl<'a, const R: usize> Index<usize> for Idx<'a, R> {
type Output = usize;
fn index(&self, index: usize) -> &Self::Output {
@ -285,39 +259,39 @@ impl<const R: usize> Index<usize> for Idx<R> {
}
}
impl<const R: usize> IndexMut<usize> for Idx<R> {
impl<'a, const R: usize> IndexMut<usize> for Idx<'a, R> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.indices[index]
}
}
impl<const R: usize> From<(Shape<R>, [usize; R])> for Idx<R> {
fn from((shape, indices): (Shape<R>, [usize; R])) -> Self {
impl<'a, const R: usize> From<(&'a Shape<R>, [usize; R])> for Idx<'a, R> {
fn from((shape, indices): (&'a Shape<R>, [usize; R])) -> Self {
assert!(shape.check_indices(indices));
Self::new(shape, indices)
}
}
impl<const R: usize> From<(Shape<R>, usize)> for Idx<R> {
fn from((shape, flat_index): (Shape<R>, usize)) -> Self {
impl<'a, const R: usize> From<(&'a Shape<R>, usize)> for Idx<'a, R> {
fn from((shape, flat_index): (&'a Shape<R>, usize)) -> Self {
let indices = shape.index_from_flat(flat_index).indices;
Self::new(shape, indices)
}
}
impl<const R: usize> From<Shape<R>> for Idx<R> {
fn from(shape: Shape<R>) -> Self {
impl<'a, const R: usize> From<&'a Shape<R>> for Idx<'a, R> {
fn from(shape: &'a Shape<R>) -> Self {
Self::zero(shape)
}
}
impl<T: Value, const R: usize> From<Tensor<T, R>> for Idx<R> {
fn from(tensor: Tensor<T, R>) -> Self {
impl<'a, T: Value, const R: usize> From<&'a Tensor<T, R>> for Idx<'a, R> {
fn from(tensor: &'a Tensor<T, R>) -> Self {
Self::zero(tensor.shape())
}
}
impl<const R: usize> std::fmt::Display for Idx<R> {
impl<'a, const R: usize> std::fmt::Display for Idx<'a, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
for (i, (&idx, &dim_size)) in self
@ -337,7 +311,7 @@ impl<const R: usize> std::fmt::Display for Idx<R> {
// ---- Arithmetic Operations ----
impl<const R: usize> Add for Idx<R> {
impl<'a, const R: usize> Add for Idx<'a, R> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
@ -355,7 +329,7 @@ impl<const R: usize> Add for Idx<R> {
}
}
impl<const R: usize> Sub for Idx<R> {
impl<'a, const R: usize> Sub for Idx<'a, R> {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
@ -375,13 +349,13 @@ impl<const R: usize> Sub for Idx<R> {
// ---- Iterator ----
pub struct IdxIterator<const R: usize> {
current: Idx<R>,
pub struct IdxIterator<'a, const R: usize> {
current: Idx<'a, R>,
end: bool,
}
impl<const R: usize> IdxIterator<R> {
pub fn new(shape: Shape<R>) -> Self {
impl<'a, const R: usize> IdxIterator<'a, R> {
pub fn new(shape: &'a Shape<R>) -> Self {
Self {
current: Idx::zero(shape),
end: false,
@ -389,8 +363,8 @@ impl<const R: usize> IdxIterator<R> {
}
}
impl<const R: usize> Iterator for IdxIterator<R> {
type Item = Idx<R>;
impl<'a, const R: usize> Iterator for IdxIterator<'a, R> {
type Item = Idx<'a, R>;
fn next(&mut self) -> Option<Self::Item> {
if self.end {
@ -403,9 +377,9 @@ impl<const R: usize> Iterator for IdxIterator<R> {
}
}
impl<const R: usize> IntoIterator for Idx<R> {
type Item = Idx<R>;
type IntoIter = IdxIterator<R>;
impl<'a, const R: usize> IntoIterator for Idx<'a, R> {
type Item = Idx<'a, R>;
type IntoIter = IdxIterator<'a, R>;
fn into_iter(self) -> Self::IntoIter {
IdxIterator {

View File

@ -54,7 +54,7 @@ mod tests {
let product = tensor1.tensor_product(&tensor2);
// Check shape of the resulting tensor
assert_eq!(product.shape(), Shape::new([2, 2, 2]));
assert_eq!(*product.shape(), Shape::new([2, 2, 2]));
// Check buffer of the resulting tensor
let expected_buffer = [5, 6, 10, 12, 15, 18, 20, 24];
@ -159,7 +159,7 @@ mod tests {
#[test]
fn test_dec_method() {
let shape = Shape::new([3, 3, 3]); // Example shape for a 3x3x3 tensor
let mut index = Idx::zero(shape);
let mut index = Idx::zero(&shape);
// Increment the index to the maximum
for _ in 0..26 {
@ -168,7 +168,7 @@ mod tests {
}
// Check if the index is at the maximum
assert_eq!(index, Idx::new(shape, [2, 2, 2]));
assert_eq!(index, Idx::new(&shape, [2, 2, 2]));
// Decrement step by step and check the index
let expected_indices = [
@ -204,7 +204,7 @@ mod tests {
for (i, &expected) in expected_indices.iter().enumerate() {
assert_eq!(
index,
Idx::new(shape, expected),
Idx::new(&shape, expected),
"Failed at index {}",
i
);
@ -213,6 +213,6 @@ mod tests {
// Finally, the index should reach [0, 0, 0]
index.dec();
assert_eq!(index, Idx::zero(shape));
assert_eq!(index, Idx::zero(&shape));
}
}

View File

@ -65,18 +65,18 @@ impl<const R: usize> Shape<R> {
}
indices.reverse(); // Reverse the indices to match the original dimension order
Idx::new(*self, indices)
Idx::new(self, indices)
}
pub const fn index_zero(&self) -> Idx<R> {
Idx::zero(*self)
Idx::zero(self)
}
pub fn index_max(&self) -> Idx<R> {
let max_indices =
self.0
.map(|dim_size| if dim_size > 0 { dim_size - 1 } else { 0 });
Idx::new(*self, max_indices)
Idx::new(self, max_indices)
}
pub fn remove_dims<const NAX: usize>(
@ -173,6 +173,6 @@ where
T: Value,
{
fn from(tensor: Tensor<T, R>) -> Self {
tensor.shape()
*tensor.shape()
}
}

View File

@ -1,8 +1,11 @@
use super::*;
use getset::{Getters, MutGetters};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Getters, MutGetters)]
pub struct Tensor<T, const R: usize> {
#[getset(get = "pub", get_mut = "pub")]
buffer: Vec<T>,
#[getset(get = "pub")]
shape: Shape<R>,
}
@ -41,19 +44,7 @@ impl<T: Value, const R: usize> Tensor<T, R> {
}
pub fn idx(&self) -> Idx<R> {
Idx::from(self.clone())
}
pub fn shape(&self) -> Shape<R> {
self.shape
}
pub fn buffer(&self) -> &[T] {
&self.buffer
}
pub fn buffer_mut(&mut self) -> &mut [T] {
&mut self.buffer
Idx::from(self)
}
pub fn get(&self, index: Idx<R>) -> &T {
@ -181,7 +172,7 @@ impl<T: Value, const R: usize> Tensor<T, R> {
// ---- Indexing ----
impl<T: Value, const R: usize> Index<Idx<R>> for Tensor<T, R> {
impl<'a, T: Value, const R: usize> Index<Idx<'a, R>> for Tensor<T, R> {
type Output = T;
fn index(&self, index: Idx<R>) -> &Self::Output {
@ -189,7 +180,7 @@ impl<T: Value, const R: usize> Index<Idx<R>> for Tensor<T, R> {
}
}
impl<T: Value, const R: usize> IndexMut<Idx<R>> for Tensor<T, R> {
impl<'a, T: Value, const R: usize> IndexMut<Idx<'a, R>> for Tensor<T, R> {
fn index_mut(&mut self, index: Idx<R>) -> &mut Self::Output {
&mut self.buffer[index.flat()]
}
@ -240,7 +231,7 @@ impl<T: Value, const R: usize> std::fmt::Display for Tensor<T, R> {
pub struct TensorIterator<'a, T: Value, const R: usize> {
tensor: &'a Tensor<T, R>,
index: Idx<R>,
index: Idx<'a, R>,
}
impl<'a, T: Value, const R: usize> TensorIterator<'a, T, R> {