{"id":19093674,"url":"https://github.com/juliaarrays/stackviews.jl","last_synced_at":"2025-04-30T12:45:38.753Z","repository":{"id":39117264,"uuid":"355872051","full_name":"JuliaArrays/StackViews.jl","owner":"JuliaArrays","description":"no more 🐱🐱","archived":false,"fork":false,"pushed_at":"2024-04-01T20:12:41.000Z","size":40,"stargazers_count":19,"open_issues_count":4,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-14T22:38:44.293Z","etag":null,"topics":["julia"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuliaArrays.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-04-08T11:03:15.000Z","updated_at":"2024-12-05T09:47:17.000Z","dependencies_parsed_at":"2023-11-17T18:30:21.942Z","dependency_job_id":null,"html_url":"https://github.com/JuliaArrays/StackViews.jl","commit_stats":{"total_commits":13,"total_committers":3,"mean_commits":4.333333333333333,"dds":"0.23076923076923073","last_synced_commit":"c39de856a1b50d819e0e58476cb9bca98fc28ea3"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaArrays%2FStackViews.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaArrays%2FStackViews.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaArrays%2FStackViews.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaArrays%2FStackViews.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuliaArrays","download_url":"https://codeload.github.com/JuliaArrays/StackViews.jl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251703207,"owners_count":21630200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["julia"],"created_at":"2024-11-09T03:25:38.582Z","updated_at":"2025-04-30T12:45:38.726Z","avatar_url":"https://github.com/JuliaArrays.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StackViews\n\n[![Build Status](https://github.com/JuliaArrays/StackViews.jl/workflows/CI/badge.svg)](https://github.com/JuliaArrays/StackViews.jl/actions)\n[![Coverage](https://codecov.io/gh/JuliaArrays/StackViews.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaArrays/StackViews.jl)\n\n`StackViews` provides only one array type: `StackView`. There are multiple ways to understand `StackView`:\n\n- inverse of `eachslice`\n- `cat` variant\n- view object\n- lazy version of `repeat` special case\n\n## `StackView` as the inverse of `eachslice`\n\n`StackView` can be seen as the inverse of `eachslice`:\n\n```julia\njulia\u003e using StackViews\n\njulia\u003e X = rand(100, 100);\n\njulia\u003e StackView(collect(eachslice(X, dims=1)), 1) == X\ntrue\n```\n\n## `StackView` as a variant of `cat`\n\n`StackView` works very similar to `cat` and its `vcat`/`hcat`/`hvcat` variants in Base.\n\n```julia\njulia\u003e using StackViews\n\njulia\u003e A = reshape(collect(1:6), 2, 3)\n2×3 Matrix{Int64}:\n 1  3  5\n 2  4  6\n\njulia\u003e B = reshape(collect(7:12), 2, 3)\n2×3 Matrix{Int64}:\n 7   9  11\n 8  10  12\n\njulia\u003e StackView(A, B; dims=3) # mostly equivalent to `cat(A, B, dims=3)`\n2×3×2 StackView{Int64, 3, 3, ...}:\n[:, :, 1] =\n 1  3  5\n 2  4  6\n\n[:, :, 2] =\n 7   9  11\n 8  10  12\n```\n\nUnlike `cat`s, `StackView` always creats new dimension:\n\n```julia\njulia\u003e StackView(A, B; dims=1) # `cat(A, B, dims=1)` outputs 4×3 Matrix\n2×2×3 StackView{Int64, 3, 1, ...}:\n[:, :, 1] =\n 1  2\n 7  8\n\n[:, :, 2] =\n 3   4\n 9  10\n\n[:, :, 3] =\n  5   6\n 11  12\n\njulia\u003e StackView(A, B; dims=2) # `cat(A, B, dims=2)` outputs 2×6 Matrix\n2×2×3 StackView{Int64, 3, 2, ...}:\n[:, :, 1] =\n 1  7\n 2  8\n\n[:, :, 2] =\n 3   9\n 4  10\n\n[:, :, 3] =\n 5  11\n 6  12\n```\n\n## `StackView` as a view object\n\nWithout `StackView`, people use `reshape` + `cat` for the previous examples:\n\n```julia\njulia\u003e StackView(A, B, dims=1) == cat(map(x-\u003ereshape(x, 1, axes(x)...), (A, B))...; dims=1)\ntrue\n```\n\nbut `StackView` is only a view and thus don't create any memory:\n\n```julia\nframes = [rand(1000, 1000) for _ in 1:100];\n@btime StackView($frames, Val(1)); # 143.905 ns (0 allocations: 0 bytes)\n@btime cat(map(x-\u003ereshape(x, 1, axes(x)...), $frames)...; dims=1); # 1.127 s (1119 allocations: 763.06 MiB)\n```\n\nOf course, since it is a view, if you modify it, original arrays get modified, too:\n\n```julia\nA = [1, 2, 3, 4]\nB = [5, 6, 7, 8]\nsv = StackView(A, B)\nfill!(sv, -1) # A and B are modified\nA == B == fill(-1, 4) # true\n```\n\nIf indexed in contiguous memeory order, it has almost zero overhead for `getindex` and `setindex!`:\n\n```julia\nfunction arrsum_cart(A::AbstractArray)\n    rst = zero(eltype(A))\n    @inbounds for I in CartesianIndices(A)\n        rst += A[I]\n    end\n    return rst\nend\n\nAs = StackView(frames);\nAc = cat(map(x-\u003ereshape(x, axes(x)..., 1), frames)...; dims=3);\nAs == Ac # true\n\n@btime arrsum_cart($As); # 122.703 ms (0 allocations: 0 bytes)\n@btime arrsum_cart($Ac); # 123.813 ms (0 allocations: 0 bytes)\n```\n\n## `StackView` as a lazy version of `repeat` special case\n\n`StackView` allows you to stack the same array object multiple times, which makes a special\nversion of `repeat` when there's only one none-1 repeat count:\n\n```julia\nA = rand(1000, 1000);\nn = 100;\nStackView([A for _ in 1:n]) == repeat(A, ntuple(_-\u003e1, ndims(A))..., n) # true\n@btime StackView([$A for _ in 1:$n]); # 403.156 ns (2 allocations: 1.75 KiB)\n@btime repeat($A, ntuple(_-\u003e1, ndims($A))..., $n) # 590.043 ms (4 allocations: 762.94 MiB)\n```\n\n## More examples\n\nWhen arrays are of different types and sizes, `StackView` just kills `cat`s:\n\n```julia\njulia\u003e using StackViews, PaddedViews\n\njulia\u003e A = collect(reshape(1:8, 2, 4));\n\njulia\u003e B = collect(reshape(9:16, 4, 2));\n\njulia\u003e StackView(paddedviews(-1, A, B), 3)\n4×4×2 StackView{Int64, 3, 3, ...}:\n[:, :, 1] =\n  1   3   5   7\n  2   4   6   8\n -1  -1  -1  -1\n -1  -1  -1  -1\n\n[:, :, 2] =\n  9  13  -1  -1\n 10  14  -1  -1\n 11  15  -1  -1\n 12  16  -1  -1\n\njulia\u003e StackView(sym_paddedviews(-1, A, B), 3)\n4×4×2 StackView{Int64, 3, 3, ...):\n[:, :, 1] =\n -1  -1  -1  -1\n  1   3   5   7\n  2   4   6   8\n -1  -1  -1  -1\n\n[:, :, 2] =\n -1   9  13  -1\n -1  10  14  -1\n -1  11  15  -1\n -1  12  16  -1\n```\n\nThere is some mind work here but by chaining more views you can get some interesting result:\n\n```julia\nusing PaddedViews, StackViews\nusing ImageCore, ImageShow, TestImages, ColorVectorSpace\n\ntoucan = testimage(\"toucan\") # 150×162 RGBA image\nmoon = testimage(\"moon\") # 256×256 Gray image\n\n# equivalently, you can just use `mosaic(toucan, moon; nrow=1)`\nmosaicview(StackView(sym_paddedviews(zero(RGB), toucan, moon)); nrow=1)\n```\n\n![](https://user-images.githubusercontent.com/1525481/97542758-4b5ade80-1995-11eb-87cc-5fd2b0ba23fc.png)\n\n## Other similar packages\n\n- There's a lot of overlap between this and [LazyStack.jl](https://github.com/mcabbott/LazyStack.jl); I didn't notice its existance when I wrote this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliaarrays%2Fstackviews.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliaarrays%2Fstackviews.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliaarrays%2Fstackviews.jl/lists"}