Skip to content

Commit 405b858

Browse files
authored
Fixed buffer indexing type, GetIndex. (#322)
1 parent f309f48 commit 405b858

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "6.0.18"
3+
version = "6.0.19"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"

lib/ArrayInterfaceCore/src/ArrayInterfaceCore.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,46 @@ is_forwarding_wrapper(T::Type) = false
6262
is_forwarding_wrapper(@nospecialize T::Type{<:Base.Slice}) = true
6363
is_forwarding_wrapper(@nospecialize x) = is_forwarding_wrapper(typeof(x))
6464

65+
"""
66+
GetIndex(buffer) = GetIndex{true}(buffer)
67+
GetIndex{check}(buffer) -> g
68+
69+
Wraps an indexable buffer in a function type that is indexed when called, so that `g(inds..)`
70+
is equivalent to `buffer[inds...]`. If `check` is `false`, then all indexing arguments are
71+
considered in-bounds. The default value for `check` is `true`, requiring bounds checking for
72+
each index.
73+
74+
!!! Warning
75+
Passing `false` as `check` may result in incorrect results/crashes/corruption for
76+
out-of-bounds indices, similar to inappropriate use of `@inbounds`. The user is
77+
responsible for ensuring this is correctly used.
78+
79+
# Examples
80+
81+
```julia
82+
julia> ArrayInterfaceCore.GetIndex(1:10)[3]
83+
3
84+
85+
julia> ArrayInterfaceCore.GetIndex{false}(1:10)[11] # shouldn't be in-bounds
86+
11
87+
88+
```
89+
90+
"""
91+
struct GetIndex{CB,B} <: Function
92+
buffer::B
93+
94+
GetIndex{true,B}(b) where {B} = new{true,B}(b)
95+
GetIndex{false,B}(b) where {B} = new{false,B}(b)
96+
GetIndex{check}(b::B) where {check,B} = GetIndex{check,B}(b)
97+
GetIndex(b) = GetIndex{true}(b)
98+
end
99+
100+
buffer(g::GetIndex) = getfield(g, :buffer)
101+
102+
Base.@propagate_inbounds @inline (g::GetIndex{true})(inds...) = buffer(g)[inds...]
103+
@inline (g::GetIndex{false})(inds...) = @inbounds(buffer(g)[inds...])
104+
65105
"""
66106
can_change_size(::Type{T}) -> Bool
67107

0 commit comments

Comments
 (0)