Skip to content

Commit b8c4e26

Browse files
committed
Replace #354
1 parent ae0828e commit b8c4e26

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

docs/src/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ArrayInterface.can_change_size
3232
ArrayInterface.can_setindex
3333
ArrayInterface.device
3434
ArrayInterface.defines_strides
35+
ArrayInterface.ensures_all_unique
36+
ArrayInterface.ensures_sorted
3537
ArrayInterface.fast_matrix_colors
3638
ArrayInterface.fast_scalar_indexing
3739
ArrayInterface.indices_do_not_alias

src/ArrayInterface.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,60 @@ struct BandedMatrixIndex <: ArrayInterface.MatrixIndex
827827
isrow::Bool
828828
end
829829

830+
"""
831+
ensures_all_unique(T::Type) -> Bool
832+
833+
Returns `true` if all instances of type `T` are composed of a unique set of elements.
834+
This does not require that `T` subtypes `AbstractSet` or implements the `AbstractSet`
835+
interface.
836+
837+
# Examples
838+
839+
```julia
840+
julia> ArrayInterface.ensures_all_unique(BitSet())
841+
true
842+
843+
julia> ArrayInterface.ensures_all_unique([])
844+
false
845+
846+
julia> ArrayInterface.ensures_all_unique(typeof(1:10))
847+
true
848+
849+
julia> ArrayInterface.ensures_all_unique(LinRange(1, 1, 10))
850+
false
851+
```
852+
"""
853+
ensures_all_unique(@nospecialize T::Type{<:Union{AbstractSet,AbstractDict}}) = true
854+
ensures_all_unique(@nospecialize T::Type{<:LinRange}) = false
855+
ensures_all_unique(@nospecialize T::Type{<:AbstractRange}) = true
856+
@inline function ensures_all_unique(T::Type)
857+
is_forwarding_wrapper(T) ? ensures_all_unique(parent_type(T)) : false
858+
end
859+
ensures_all_unique(@nospecialize(x)) = ensures_all_unique(typeof(x))
860+
861+
"""
862+
ensures_sorted(T::Type) -> Bool
863+
864+
Returns `true` if all instances of `T` are sorted.
865+
866+
# Examples
867+
868+
```julia
869+
julia> ArrayInterface.ensures_sorted(BitSet())
870+
true
871+
872+
julia> ArrayInterface.ensures_sorted([])
873+
false
874+
875+
julia> ArrayInterface.ensures_sorted(1:10)
876+
true
877+
```
878+
"""
879+
ensures_sorted(@nospecialize(T::Type{BitSet})) = true
880+
ensures_sorted(@nospecialize( T::Type{<:AbstractRange})) = true
881+
ensures_sorted(T::Type) = is_forwarding_wrapper(T) ? ensures_sorted(parent_type(T)) : false
882+
ensures_sorted(@nospecialize(x)) = ensures_sorted(typeof(x))
883+
830884
## Extensions
831885

832886
import Requires

test/core.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,17 @@ end
245245
@test !ArrayInterface.indices_do_not_alias(Transpose{Matrix{Float64},Matrix{Matrix{Float64}}})
246246
@test !ArrayInterface.indices_do_not_alias(typeof(view(fill(rand(4,4),4,4)', 2:3, 1:2)))
247247
@test !ArrayInterface.indices_do_not_alias(typeof(view(rand(4,4)', StepRangeLen(1,0,5), 1:2)))
248-
end
248+
end
249+
250+
@testset "ensures_all_unique" begin
251+
@test ArrayInterface.ensures_all_unique(BitSet())
252+
@test !ArrayInterface.ensures_all_unique([])
253+
@test ArrayInterface.ensures_all_unique(1:10)
254+
@test !ArrayInterface.ensures_all_unique(LinRange(1, 1, 10))
255+
end
256+
257+
@testset "ensures_sorted" begin
258+
@test ArrayInterface.ensures_sorted(BitSet())
259+
@test !ArrayInterface.ensures_sorted([])
260+
@test ArrayInterface.ensures_sorted(1:10)
261+
end

0 commit comments

Comments
 (0)