Comments (6)
If you are implying for setindex
rather than inplace setindex!
version. No.
You can do it with Accesssors.jl.
julia> using Accessors
julia> a = (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
julia> @set a[[2, 5]] = (-1, -1)
(1, -1, 3, 4, -1)
julia> using Chairmarks
julia> @b @set a[[2, 5]] = (-1, -1)
41.047 ns (2.02 allocs: 128.760 bytes)
from julia.
Base.setindex
itself isn't public API. Although perhaps it should be?
Guess I'll make a package.
from julia.
I quickly tried something like this below, but it is not going to be performant version.
g(c) = j -> (j in i) && (c += 1) > 0 ? v[c] : x[j]
ntuple(g(0), Val{N}())
julia> @b setindex(x, v, i)
391.071 ns (4.11 allocs: 131.657 bytes)
g(c) = j -> (j in i) && (c += 1) > 0 ? v[c] : x[j]
ntuple(g(0), length(x))
Second is relatively more performant but still 10x slower than accessors.jl version above.
from julia.
Also this scanning version is better
function setindex(x::Tuple, v::Tuple, i::Tuple)
N = length(x)
is = ntuple(j -> ifelse(j in i, 1, 0), Val{N}())
vs = ntuple(Val{N}()) do j
if is[j] === 1
return v[sum(is[1:j])]
else
return x[j]
end
end
return vs
end
julia> @b setindex(x, v, i)
70.008 ns (3.02 allocs: 112.650 bytes)
from julia.
simply iterating through works too
function setindex(x::Tuple, v::Tuple, i::Tuple)
out = x
for (val, idx) in zip(v, i)
out = Base.setindex(out, val, idx)
end
return out
end
julia> @b setindex(x, v, i)
22.901 ns (1.01 allocs: 48.403 bytes)
This is better than Accesssors.jl
version.
Onesweep algorithm is possible I think. That can give best solution. I couldn't come up with one yet.
from julia.
Since single setindex in base is taking roughly 16ns
, then
julia> @b Base.setindex(x, 1, 4)
15.798 ns (1.01 allocs: 48.424 bytes)
This is best so far.
simply iterating through works too
function setindex(x::Tuple, v::Tuple, i::Tuple) out = x for (val, idx) in zip(v, i) out = Base.setindex(out, val, idx) end return out end julia> @b setindex(x, v, i) 22.901 ns (1.01 allocs: 48.403 bytes)This is better than
Accesssors.jl
version.Onesweep algorithm is possible I think. That can give best solution. I couldn't come up with one yet.
from julia.
Related Issues (20)
- `@code_llvm` with `debuginfo=:none` does not remove all debug info on Julia 1.11 HOT 4
- Conflation of UInt8 and Int8 in search functions
- `map(f, ::String)` requires `f` to return an `AbstractChar` HOT 4
- Add a concept of memory-backed contiguous collection in Base HOT 9
- Inconsistent behaviour for `findnext` HOT 1
- Docs/Manual/Constructors/Case Study: Rational - Leads to error in `show` with imaginary values
- Error parsing Cmds that interpolate Cmds HOT 2
- Attempting to access `Vector{Core.Compiler.DomTreeNode} at index [0]` when optimizing a function HOT 2
- PkgId objects change by printing and returning from function HOT 2
- at-kwdef generates incorrect generic constructors for structs with type parameters HOT 2
- Google Developer Groups
- It is now possible to update globals in a different module HOT 11
- implement method dispatch for a union of functions
- Alias analysis sometimes too strict for SubArrays HOT 1
- reintepret reshape of 0-dimensional array HOT 1
- default `dims = ndims(x)` in `eachslice(x; dims = ndims(x))` to make `eachslice` the inverse `stack` HOT 1
- `bytesavailable` wrong when piping input HOT 2
- Binaries for prereleases of patch versions HOT 4
- `GenericIOBuffer` assumes data is stored contiguously in memory
- `Base.operator_associativity(:?)` incorrectly returns `:left`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.