Skip to content

Commit

Permalink
Use TypeUtils package
Browse files Browse the repository at this point in the history
  • Loading branch information
emmt committed Jul 13, 2023
1 parent 409fcb7 commit de4a9be
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 51 deletions.
12 changes: 11 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# User visible changes in `ArrayTools` package

## Version 0.3.0

- Use package `TypeUtils` and re-export some of its methods: `as`, `as_eltype`,
`convert_eltype`, and `promote_eltype`.

- Method `to_type(T,x)` has been deprecated in favor of `as(T,x)` found in
package `TypeUtils`.

- `promote_eltype()` with no arguments returns the same result as
`promote_type()` and `UndefinedType` has been deleted.

## Version 0.2.7

- Rename `@assert_same_indices` as `@assert_same_axes` which is more specific
Expand Down Expand Up @@ -38,7 +49,6 @@

- Some code ahs been simplified.


## Version 0.2.1

- `isfastarray` and `isflatarray` deprecated in favor of `is_fast_array` and
Expand Down
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name = "ArrayTools"
uuid = "1dc0ca97-c5ce-4e77-ac6d-c576ac9d7f27"
authors = ["Éric Thiébaut <https://github.com/emmt>"]
version = "0.2.7"
version = "0.3.0"

[deps]
TypeUtils = "c3b1956e-8857-4d84-9b79-890df85b1e67"

[compat]
TypeUtils = "≥0.2.2"
julia = "1.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random"]
19 changes: 15 additions & 4 deletions src/ArrayTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export
MaybeArrayAxes,
MaybeArrayAxis,
RubberIndex,
UndefinedType,
all_match,
allof,
anyof,
Expand All @@ -22,7 +21,6 @@ export
common_indices,
has_standard_indexing,
noneof,
promote_eltype,
reversemap,
all_indices,
same_axes,
Expand All @@ -35,25 +33,38 @@ export
to_axis,
to_int,
to_size,
to_type,
# storage trait
# to_type, # FIXME: deprecated, use `as(T,x)` instead

# Re-exports from TypeUtils:
as,
as_eltype,
convert_eltype,
promote_eltype,

# Storage trait:
StorageType,
AnyStorage,
FlatStorage,
to_flat_array,
is_flat_array,

# Fast arrays and indexing trait:
IndexingType,
FastIndexing,
AnyIndexing,
to_fast_array,
is_fast_array,

# Macros:
@assert_same_axes

using TypeUtils

using Base: OneTo, axes1, tail
import Base: dotview, getindex, setindex!, to_indices

@deprecate to_type(T, x) as(T, x) true

include("traits.jl")
include("utils.jl")
include("indexing.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ sub-type of [`ArrayAxis`](@ref) which is an alias to `AbstractUnitRange{Int}`.
"""
to_axis(ind::ArrayAxis) = ind
to_axis(ind::AbstractUnitRange{<:Integer}) = to_type(ArrayAxis, ind)
to_axis(ind::AbstractUnitRange{<:Integer}) = as(ArrayAxis, ind)
to_axis(ind::Int) = Base.OneTo(ind)
to_axis(ind::Integer) = to_axis(to_type(Int, ind))
to_axis(ind::Integer) = to_axis(as(Int, ind))

"""
ArrayAxes{N}
Expand Down
35 changes: 1 addition & 34 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
# General purpose methods.
#

"""
to_type(T, x)
yields `x` converted to type `T`, the result is asserted to be of type `T`.
"""
to_type(::Type{T}, x::T) where {T} = x
to_type(::Type{T}, x::Any) where {T} = convert(T, x)::T

"""
to_int(x)
Expand All @@ -21,7 +12,7 @@ converts `x` to a similar object whose values are all of type `Int`. Argument
already of the correct type.
"""
to_int(x::Integer) = to_type(Int, x)
to_int(x::Integer) = as(Int, x)
to_int(I::AbstractUnitRange{Int}) = I
to_int(I::AbstractUnitRange{<:Integer}) = to_int(first(I)):to_int(last(I))
to_int(I::Base.OneTo{Int}) = I
Expand All @@ -34,30 +25,6 @@ to_int(A::AbstractArray{<:Integer,N}) where {N} = map(to_int, A)
to_int(x::Tuple{Vararg{Int}}) = x
to_int(x::Tuple{Vararg{Integer}}) = map(to_int, x)

"""
UndefinedType
is a type used to represent undefined type in `promote_type`. It is an
abstract type so that `isbitstype(UndefinedType)` is false.
"""
abstract type UndefinedType end

"""
promote_eltype(args...)
yields the promoted element type of its arguments. Arguments `args...` may be
anything implementing the `eltype` method.
"""
promote_eltype(arg) = eltype(arg)
promote_eltype(args...) = promote_type(map(eltype, args)...)
promote_eltype() = UndefinedType

Base.promote_type(T::Type, ::Type{UndefinedType}) = T
Base.promote_type(::Type{UndefinedType}, T::Type) = T
Base.promote_type(::Type{UndefinedType}, ::Type{UndefinedType}) = UndefinedType

"""
all_match(val, f, args...) -> bool
Expand Down
14 changes: 6 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ atol = 1e-6
@testset "Miscellaneous " begin
# Promotion of element types.
for (T1,T2,T3) in ((Float32,Float64,Int), (Int16,Int32,Int64))
@test promote_eltype() === UndefinedType
@test promote_eltype() === promote_type()
@test promote_eltype(zeros(T1,1)) == promote_type(T1)
@test promote_eltype(AbstractVector{T2}) == promote_type(T2)
@test promote_eltype(zeros(T2,4), zeros(T3,2,3)) == promote_type(T2,T3)
Expand All @@ -70,15 +70,13 @@ atol = 1e-6
promote_type(T1,T2,T3)
@test promote_eltype(DenseMatrix{T1}, zeros(T2,5), AbstractVector{T3}) ==
promote_type(T1,T2,T3)
@test promote_type(UndefinedType, Int) === Int
@test promote_type(Bool, UndefinedType) === Bool
@test promote_type(UndefinedType, UndefinedType) === UndefinedType
end
# Conversions.
@test to_type(Int32, 3) === Int32(3)
@test to_type(Float32, 3) === Float32(3)
@test to_type(Vector{Float32}, [1,2,3]) == Float32[1,2,3]
@test isa(to_type(Vector{Float32}, [1,2,3]), Vector{Float32})
@test_deprecated to_type(Int32, 3)
@test as(Int32, 3) === Int32(3)
@test as(Float32, 3) === Float32(3)
@test as(Vector{Float32}, [1,2,3]) == Float32[1,2,3]
@test as(Vector{Float32}, [1,2,3]) isa Vector{Float32}
@test to_int(2) === 2
@test to_int(0x03) === 3
@test to_int(Base.OneTo(5)) === Base.OneTo(5)
Expand Down

2 comments on commit de4a9be

@emmt
Copy link
Owner Author

@emmt emmt commented on de4a9be Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register branch=master

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/87440

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" de4a9be6aed9ed63238558f65e21d8180fdd66d5
git push origin v0.3.0

Please sign in to comment.