数组范围补码 [英] Array range complement

查看:75
本文介绍了数组范围补码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以覆盖[]以在数组中具有一定范围的补余?

Is there a way to overwrite [] to have complement of range in array?

julia> a=[1:8...]
8-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8

julia> a[-1] == a[2:8]
julia> a[-(1:3)] == a[4:8]
julia> a[-end] == a[1:7]

推荐答案

我以前没有研究过索引的内部原理,但是乍看之下,以下内容可能会奏效而不会破坏太多:

I haven't looked into the internals of indexing before, but at a first glance, the following might work without breaking too much:

immutable Not{T}
    idx::T
end    

if :to_indices in names(Base)
    # 0.6
    import Base: to_indices, uncolon, tail, _maybetail

    @inline to_indices(A, inds, I::Tuple{Not, Vararg{Any}}) =
       (setdiff(uncolon(inds, (:, tail(I)...)), I[1].idx), to_indices(A, _maybetail(inds), tail(I))...)       
else
    # 0.5
    import Base: getindex, _getindex

    not_index(a::AbstractArray, I, i::Int) = I
    not_index(a::AbstractArray, I::Not, i::Int) = setdiff(indices(a, i), I.idx)

    getindex(a::AbstractArray, I::Not) = getindex(a, setdiff(linearindices(a), I.idx))
    _getindex(::Base.LinearIndexing, a::AbstractArray, I::Vararg{Union{Real, AbstractArray, Colon, Not}}) = 
        Base._getindex(Base.linearindexing(a), a, (not_index(a, idx, i) for (i,idx) in enumerate(I))...)
end

例如:

julia> a = reshape(1:9, (3, 3))
3×3 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
1  4  7
2  5  8
3  6  9

julia> a[Not(2:8)]
2-element Array{Int64,1}:
1
9

julia> a[Not(1:2), :]
1×3 Array{Int64,2}:
3  6  9


julia> a[Not(end), end]
2-element Array{Int64,1}:
7
8

我不在乎性能,也没有进行广泛的测试,因此可以肯定地改善了情况.

I didn't care for performance and also did no extensive testing, so things can certainly be improved.

我用评论中链接的他的github评论中的Matt B.版本替换了0.6的代码.

I replaced the code for 0.6 with Matt B. version from his github comment linked in the comments.

由于他对0.6的数组索引实现的出色设计,只需要扩展一个函数即可为getindexsetindexview(例如

Thanks to his great design of the array indexing implementation for 0.6, only a single function needs to be extended to get complement indexing for getindex, setindex and view, e.g.,

julia> view(a, Not(2:8))
2-element SubArray{Int64,1,UnitRange{Int64},Tuple{Array{Int64,1}},false}:
1
9

# collect because ranges are immutable
julia> b = collect(a); b[Not(2), Not(2)] = 10; b
3×3 Array{Int64,2}:
10  4  10
 2  5   8
10  6  10  

这篇关于数组范围补码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆