From d559c1fcbae8b6fbe326898a7c7e73dbfa810a9c Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 21 Sep 2023 18:19:14 -0400 Subject: [PATCH] Format --- .../src/SmallVectors/src/SmallVectors.jl | 22 +-- .../src/abstractsmallvector/deque.jl | 18 +- .../src/msmallvector/msmallvector.jl | 11 +- .../src/smallvector/smallvector.jl | 8 +- .../src/subsmallvector/subsmallvector.jl | 23 ++- NDTensors/src/SmallVectors/test/runtests.jl | 170 +++++++++++------- 6 files changed, 159 insertions(+), 93 deletions(-) diff --git a/NDTensors/src/SmallVectors/src/SmallVectors.jl b/NDTensors/src/SmallVectors/src/SmallVectors.jl index 04c0d6de4f..e3f7083795 100644 --- a/NDTensors/src/SmallVectors/src/SmallVectors.jl +++ b/NDTensors/src/SmallVectors/src/SmallVectors.jl @@ -1,16 +1,16 @@ module SmallVectors - using StaticArrays +using StaticArrays - export SmallVector, MSmallVector, SubSmallVector +export SmallVector, MSmallVector, SubSmallVector - struct NotImplemented <: Exception - msg::String - end - NotImplemented() = NotImplemented("Not implemented.") +struct NotImplemented <: Exception + msg::String +end +NotImplemented() = NotImplemented("Not implemented.") - include("abstractsmallvector/abstractsmallvector.jl") - include("abstractsmallvector/deque.jl") - include("msmallvector/msmallvector.jl") - include("smallvector/smallvector.jl") - include("subsmallvector/subsmallvector.jl") +include("abstractsmallvector/abstractsmallvector.jl") +include("abstractsmallvector/deque.jl") +include("msmallvector/msmallvector.jl") +include("smallvector/smallvector.jl") +include("subsmallvector/subsmallvector.jl") end diff --git a/NDTensors/src/SmallVectors/src/abstractsmallvector/deque.jl b/NDTensors/src/SmallVectors/src/abstractsmallvector/deque.jl index 55e3d8352b..5dd872cc73 100644 --- a/NDTensors/src/SmallVectors/src/abstractsmallvector/deque.jl +++ b/NDTensors/src/SmallVectors/src/abstractsmallvector/deque.jl @@ -82,14 +82,16 @@ end @inline function Base.reverse!(vec::AbstractSmallVector) start, stop = firstindex(vec), lastindex(vec) r = stop - @inbounds for i in start:Base.midpoint(start, stop-1) + @inbounds for i in start:Base.midpoint(start, stop - 1) vec[i], vec[r] = vec[r], vec[i] r -= 1 end return vec end -@inline function Base.reverse!(vec::AbstractSmallVector, start::Integer, stop::Integer=lastindex(v)) +@inline function Base.reverse!( + vec::AbstractSmallVector, start::Integer, stop::Integer=lastindex(v) +) reverse!(smallview(vec, start, stop)) return vec end @@ -137,7 +139,9 @@ end # https://github.com/JuliaLang/julia/blob/bed2cd540a11544ed4be381d471bbf590f0b745e/base/sort.jl#L722-L736 # https://en.wikipedia.org/wiki/Insertion_sort#:~:text=Insertion%20sort%20is%20a%20simple,%2C%20heapsort%2C%20or%20merge%20sort. # Alternatively could use `TupleTools.jl` or `StaticArrays.jl` for out-of-place sorting. -@inline function Base.sort!(vec::AbstractSmallVector; lt=isless, by=identity, rev::Bool=false) +@inline function Base.sort!( + vec::AbstractSmallVector; lt=isless, by=identity, rev::Bool=false +) lo, hi = firstindex(vec), lastindex(vec) lo_plus_1 = (lo + 1) @inbounds for i in lo_plus_1:hi @@ -205,7 +209,9 @@ function mergesorted(vec::AbstractSmallVector, item; kwargs...) return convert(similar_type(vec), mvec) end -@inline function mergesortedunique!(vec::AbstractSmallVector, item::AbstractVector; kwargs...) +@inline function mergesortedunique!( + vec::AbstractSmallVector, item::AbstractVector; kwargs... +) for x in item insertsortedunique!(vec, x; kwargs...) end @@ -220,7 +226,9 @@ function mergesortedunique(vec::AbstractSmallVector, item; kwargs...) return vec end -Base.@propagate_inbounds function Base.copyto!(vec::AbstractSmallVector, item::AbstractVector) +Base.@propagate_inbounds function Base.copyto!( + vec::AbstractSmallVector, item::AbstractVector +) for i in eachindex(item) vec[i] = item[i] end diff --git a/NDTensors/src/SmallVectors/src/msmallvector/msmallvector.jl b/NDTensors/src/SmallVectors/src/msmallvector/msmallvector.jl index e31046435d..71dca00814 100644 --- a/NDTensors/src/SmallVectors/src/msmallvector/msmallvector.jl +++ b/NDTensors/src/SmallVectors/src/msmallvector/msmallvector.jl @@ -7,8 +7,12 @@ mutable struct MSmallVector{S,T} <: AbstractSmallVector{T} end # Constructors -MSmallVector{S}(buffer::AbstractVector, len::Int) where {S} = MSmallVector{S,eltype(buffer)}(buffer, len) -MSmallVector(buffer::AbstractVector, len::Int) = MSmallVector{length(buffer),eltype(buffer)}(buffer, len) +function MSmallVector{S}(buffer::AbstractVector, len::Int) where {S} + return MSmallVector{S,eltype(buffer)}(buffer, len) +end +function MSmallVector(buffer::AbstractVector, len::Int) + return MSmallVector{length(buffer),eltype(buffer)}(buffer, len) +end """ `MSmallVector` constructor, uses `MVector` as a buffer. @@ -59,7 +63,8 @@ end @inline function Base.resize!(vec::MSmallVector, len::Integer) len < 0 && throw(ArgumentError("New length must be ≥ 0.")) - len > maxlength(vec) && throw(ArgumentError("New length $len must be ≤ the maximum length $(maxlength(vec)).")) + len > maxlength(vec) && + throw(ArgumentError("New length $len must be ≤ the maximum length $(maxlength(vec)).")) vec.length = len return vec end diff --git a/NDTensors/src/SmallVectors/src/smallvector/smallvector.jl b/NDTensors/src/SmallVectors/src/smallvector/smallvector.jl index 872b80ac30..777425e9c7 100644 --- a/NDTensors/src/SmallVectors/src/smallvector/smallvector.jl +++ b/NDTensors/src/SmallVectors/src/smallvector/smallvector.jl @@ -12,8 +12,12 @@ end @inline setlength(vec::SmallVector, length) = SmallVector(vec.buffer, length) # Constructors -SmallVector{S}(buffer::AbstractVector, len::Int) where {S} = SmallVector{S,eltype(buffer)}(buffer, len) -SmallVector(buffer::AbstractVector, len::Int) = SmallVector{length(buffer),eltype(buffer)}(buffer, len) +function SmallVector{S}(buffer::AbstractVector, len::Int) where {S} + return SmallVector{S,eltype(buffer)}(buffer, len) +end +function SmallVector(buffer::AbstractVector, len::Int) + return SmallVector{length(buffer),eltype(buffer)}(buffer, len) +end """ `SmallVector` constructor, uses `SVector` as buffer storage. diff --git a/NDTensors/src/SmallVectors/src/subsmallvector/subsmallvector.jl b/NDTensors/src/SmallVectors/src/subsmallvector/subsmallvector.jl index 8156e4bb74..1c68778125 100644 --- a/NDTensors/src/SmallVectors/src/subsmallvector/subsmallvector.jl +++ b/NDTensors/src/SmallVectors/src/subsmallvector/subsmallvector.jl @@ -22,8 +22,12 @@ Base.parent(vec::SubMSmallVector) = vec.parent # buffer interface buffer(vec::AbstractSubSmallVector) = buffer(parent(vec)) -smallview(vec::SmallVector, start::Integer, stop::Integer) = SubSmallVector(vec, start, stop) -smallview(vec::MSmallVector, start::Integer, stop::Integer) = SubMSmallVector(vec, start, stop) +function smallview(vec::SmallVector, start::Integer, stop::Integer) + return SubSmallVector(vec, start, stop) +end +function smallview(vec::MSmallVector, start::Integer, stop::Integer) + return SubMSmallVector(vec, start, stop) +end function smallview(vec::SubSmallVector, start::Integer, stop::Integer) return SubSmallVector(parent(vec), vec.start + start - 1, vec.start + stop - 1) @@ -33,8 +37,12 @@ function smallview(vec::SubMSmallVector, start::Integer, stop::Integer) end # Constructors -SubSmallVector(vec::AbstractVector, start::Integer, stop::Integer) = SubSmallVector{eltype(vec),typeof(vec)}(vec, start, stop) -SubMSmallVector(vec::AbstractVector, start::Integer, stop::Integer) = SubMSmallVector{eltype(vec),typeof(vec)}(vec, start, stop) +function SubSmallVector(vec::AbstractVector, start::Integer, stop::Integer) + return SubSmallVector{eltype(vec),typeof(vec)}(vec, start, stop) +end +function SubMSmallVector(vec::AbstractVector, start::Integer, stop::Integer) + return SubMSmallVector{eltype(vec),typeof(vec)}(vec, start, stop) +end # Accessors Base.size(vec::AbstractSubSmallVector) = (vec.stop - vec.start + 1,) @@ -43,7 +51,9 @@ Base.@propagate_inbounds function Base.getindex(vec::AbstractSubSmallVector, ind return parent(vec)[index + vec.start - 1] end -Base.@propagate_inbounds function Base.setindex!(vec::AbstractSubSmallVector, item, index::Integer) +Base.@propagate_inbounds function Base.setindex!( + vec::AbstractSubSmallVector, item, index::Integer +) buffer(vec)[index + vec.start - 1] = item return vec end @@ -58,7 +68,8 @@ end @inline function Base.resize!(vec::SubMSmallVector, len::Integer) len < 0 && throw(ArgumentError("New length must be ≥ 0.")) - len > maxlength(vec) - vec.start + 1 && throw(ArgumentError("New length $len must be ≤ the maximum length $(maxlength(vec)).")) + len > maxlength(vec) - vec.start + 1 && + throw(ArgumentError("New length $len must be ≤ the maximum length $(maxlength(vec)).")) vec.stop = vec.start + len - 1 return vec end diff --git a/NDTensors/src/SmallVectors/test/runtests.jl b/NDTensors/src/SmallVectors/test/runtests.jl index 48a08bf167..e58746bac3 100644 --- a/NDTensors/src/SmallVectors/test/runtests.jl +++ b/NDTensors/src/SmallVectors/test/runtests.jl @@ -1,78 +1,116 @@ module TestSmallVectors - include("SmallVectors/src/SmallVectors.jl") - using .SmallVectors - using StaticArrays - using Test +include("SmallVectors/src/SmallVectors.jl") +using .SmallVectors +using StaticArrays +using Test - function test() - @testset "SmallVectors" begin - x = SmallVector{10}([1, 3, 5]) - mx = MSmallVector(x) +function test() + @testset "SmallVectors" begin + x = SmallVector{10}([1, 3, 5]) + mx = MSmallVector(x) - @test x isa SmallVector{10,Int} - @test mx isa MSmallVector{10,Int} - @test eltype(x) === Int - @test eltype(mx) === Int + @test x isa SmallVector{10,Int} + @test mx isa MSmallVector{10,Int} + @test eltype(x) === Int + @test eltype(mx) === Int - # TODO: Test construction has zero allocations. - # TODO: Extend construction to arbitrary collections, like tuple. + # TODO: Test construction has zero allocations. + # TODO: Extend construction to arbitrary collections, like tuple. - # conversion - @test @inferred(SmallVector(x)) == x - @test @allocated(SmallVector(x)) == 0 - @test @inferred(SmallVector(mx)) == x - @test @allocated(SmallVector(mx)) == 0 - - # length - @test @inferred(length(x)) == 3 - @test @allocated(length(x)) == 0 - @test @inferred(length(SmallVectors.buffer(x))) == 10 - @test @allocated(length(SmallVectors.buffer(x))) == 0 + # conversion + @test @inferred(SmallVector(x)) == x + @test @allocated(SmallVector(x)) == 0 + @test @inferred(SmallVector(mx)) == x + @test @allocated(SmallVector(mx)) == 0 - item = 115 - no_broken = (false, false, false, false) - for (f!, f, ans, args, f!_impl_broken, f!_noalloc_broken, f_impl_broken, f_noalloc_broken) in [ - (:push!, :push, [1, 3, 5, item], (item,), no_broken...), - (:append!, :(SmallVectors.append), [1, 3, 5, item], ([item],), no_broken...), - (:prepend!, :(SmallVectors.prepend), [item, 1, 3, 5], ([item],), no_broken...), - # (:splice!, :(SmallVectors.splice), [1, item, 3], (2, [item],), true, true, true, true), # Not implemented - (:pushfirst!, :pushfirst, [item, 1, 3, 5], (item,), no_broken...), - (:setindex!, :setindex, [1, item, 5], (item, 2), no_broken...), - (:pop!, :pop, [1, 3], (), no_broken...), - (:popfirst!, :popfirst, [3, 5], (), no_broken...), - (:insert!, :insert, [1, item, 3, 5], (2, item), no_broken...), - (:deleteat!, :deleteat, [1, 5], (2,), no_broken...), - (:circshift!, :circshift, [5, 1, 3], (1,), no_broken...), - (:sort!, :sort, [1, 3, 5], (), no_broken...), - (:(SmallVectors.insertsorted!), :(SmallVectors.insertsorted), [1, 2, 3, 5], (2,), no_broken...), - (:(SmallVectors.insertsorted!), :(SmallVectors.insertsorted), [1, 3, 3, 5], (3,), no_broken...), - (:(SmallVectors.insertsortedunique!), :(SmallVectors.insertsortedunique), [1, 2, 3, 5], (2,), no_broken...), - (:(SmallVectors.insertsortedunique!), :(SmallVectors.insertsortedunique), [1, 3, 5], (3,), no_broken...), - (:(SmallVectors.mergesorted!), :(SmallVectors.mergesorted), [1, 2, 3, 3, 5], ([2, 3],), no_broken...), - (:(SmallVectors.mergesortedunique!), :(SmallVectors.mergesortedunique), [1, 2, 3, 5], ([2, 3],), no_broken...), - ] - mx_tmp = copy(mx) - @eval begin - @test @inferred($f!(copy($mx), $args...)) == $ans broken=$f!_impl_broken - @test @allocated($f!($mx_tmp, $args...)) == 0 broken=$f!_noalloc_broken - @test @inferred($f($x, $args...)) == $ans broken=$f_impl_broken - @test @allocated($f($x, $args...)) == 0 broken=$f_noalloc_broken - end - end + # length + @test @inferred(length(x)) == 3 + @test @allocated(length(x)) == 0 + @test @inferred(length(SmallVectors.buffer(x))) == 10 + @test @allocated(length(SmallVectors.buffer(x))) == 0 - # Separated out since for some reason it breaks the `@inferred` - # check when `kwargs` are interpolated into `@eval`. - ans, kwargs = [5, 3, 1], (; rev=true) + item = 115 + no_broken = (false, false, false, false) + for ( + f!, f, ans, args, f!_impl_broken, f!_noalloc_broken, f_impl_broken, f_noalloc_broken + ) in [ + (:push!, :push, [1, 3, 5, item], (item,), no_broken...), + (:append!, :(SmallVectors.append), [1, 3, 5, item], ([item],), no_broken...), + (:prepend!, :(SmallVectors.prepend), [item, 1, 3, 5], ([item],), no_broken...), + # (:splice!, :(SmallVectors.splice), [1, item, 3], (2, [item],), true, true, true, true), # Not implemented + (:pushfirst!, :pushfirst, [item, 1, 3, 5], (item,), no_broken...), + (:setindex!, :setindex, [1, item, 5], (item, 2), no_broken...), + (:pop!, :pop, [1, 3], (), no_broken...), + (:popfirst!, :popfirst, [3, 5], (), no_broken...), + (:insert!, :insert, [1, item, 3, 5], (2, item), no_broken...), + (:deleteat!, :deleteat, [1, 5], (2,), no_broken...), + (:circshift!, :circshift, [5, 1, 3], (1,), no_broken...), + (:sort!, :sort, [1, 3, 5], (), no_broken...), + ( + :(SmallVectors.insertsorted!), + :(SmallVectors.insertsorted), + [1, 2, 3, 5], + (2,), + no_broken..., + ), + ( + :(SmallVectors.insertsorted!), + :(SmallVectors.insertsorted), + [1, 3, 3, 5], + (3,), + no_broken..., + ), + ( + :(SmallVectors.insertsortedunique!), + :(SmallVectors.insertsortedunique), + [1, 2, 3, 5], + (2,), + no_broken..., + ), + ( + :(SmallVectors.insertsortedunique!), + :(SmallVectors.insertsortedunique), + [1, 3, 5], + (3,), + no_broken..., + ), + ( + :(SmallVectors.mergesorted!), + :(SmallVectors.mergesorted), + [1, 2, 3, 3, 5], + ([2, 3],), + no_broken..., + ), + ( + :(SmallVectors.mergesortedunique!), + :(SmallVectors.mergesortedunique), + [1, 2, 3, 5], + ([2, 3],), + no_broken..., + ), + ] mx_tmp = copy(mx) - @test @inferred(sort!(copy(mx); kwargs...)) == ans - @test @allocated(sort!(mx_tmp; kwargs...)) == 0 - @test @inferred(sort(x; kwargs...)) == ans - @test @allocated(sort(x; kwargs...)) == 0 - - ans, args = [1, 3, 5, item], ([item],) - @test @inferred(vcat(x, args...)) == ans - @test @allocated(vcat(x, args...)) == 0 + @eval begin + @test @inferred($f!(copy($mx), $args...)) == $ans broken = $f!_impl_broken + @test @allocated($f!($mx_tmp, $args...)) == 0 broken = $f!_noalloc_broken + @test @inferred($f($x, $args...)) == $ans broken = $f_impl_broken + @test @allocated($f($x, $args...)) == 0 broken = $f_noalloc_broken + end end + + # Separated out since for some reason it breaks the `@inferred` + # check when `kwargs` are interpolated into `@eval`. + ans, kwargs = [5, 3, 1], (; rev=true) + mx_tmp = copy(mx) + @test @inferred(sort!(copy(mx); kwargs...)) == ans + @test @allocated(sort!(mx_tmp; kwargs...)) == 0 + @test @inferred(sort(x; kwargs...)) == ans + @test @allocated(sort(x; kwargs...)) == 0 + + ans, args = [1, 3, 5, item], ([item],) + @test @inferred(vcat(x, args...)) == ans + @test @allocated(vcat(x, args...)) == 0 end end +end TestSmallVectors.test()