Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto sciml format with JuliaFormatter #34

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "sciml"
28 changes: 14 additions & 14 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using PatternFolds
using Documenter

DocMeta.setdocmeta!(PatternFolds, :DocTestSetup, :(using PatternFolds); recursive=true)
DocMeta.setdocmeta!(PatternFolds, :DocTestSetup, :(using PatternFolds); recursive = true)

makedocs(;
modules=[PatternFolds],
authors="Jean-Francois Baffier",
repo="https://github.com/Humans-of-Julia/PatternFolds.jl/blob/{commit}{path}#{line}",
sitename="PatternFolds.jl",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://Humans-of-Julia.github.io/PatternFolds.jl",
assets=String[],
modules = [PatternFolds],
authors = "Jean-Francois Baffier",
repo = "https://github.com/Humans-of-Julia/PatternFolds.jl/blob/{commit}{path}#{line}",
sitename = "PatternFolds.jl",
format = Documenter.HTML(;
prettyurls = get(ENV, "CI", "false") == "true",
canonical = "https://Humans-of-Julia.github.io/PatternFolds.jl",
assets = String[]
),
pages=[
"Home" => "index.md",
],
pages = [
"Home" => "index.md"
]
)

deploydocs(;
repo="github.com/Humans-of-Julia/PatternFolds.jl",
devbranch="main",
repo = "github.com/Humans-of-Julia/PatternFolds.jl",
devbranch = "main"
)
4 changes: 2 additions & 2 deletions perf/allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using PatternFolds

function alloc() # 0.2.x
# Intervals
itv = Interval{Open,Closed}(0.0, 1.0)
itv = Interval{Open, Closed}(0.0, 1.0)
i = IntervalsFold(itv, 2.0, 1000)

@info "Checking IntervalsFold" i pattern(i) gap(i) folds(i) size(i) length(i)
Expand Down Expand Up @@ -62,5 +62,5 @@ using PatternFolds
# end

# Actual call to PerfChecker
alloc_check(title, dependencies, targets, alloc, alloc; path=@__DIR__)
alloc_check(title, dependencies, targets, alloc, alloc; path = @__DIR__)
end
6 changes: 3 additions & 3 deletions perf/bench.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ target = PatternFolds

function bench() # 0.2.x
# Intervals
itv = Interval{Open,Closed}(0.0, 1.0)
itv = Interval{Open, Closed}(0.0, 1.0)
i = IntervalsFold(itv, 2.0, 1000)

unfold(i)
Expand Down Expand Up @@ -52,7 +52,7 @@ end
# return nothing
# end

t = @benchmark bench() evals = 1 samples = 1000 seconds = 3600
t = @benchmark bench() evals=1 samples=1000 seconds=3600

# Actual call to PerfChecker
store_benchmark(t, target; path=@__DIR__)
store_benchmark(t, target; path = @__DIR__)
4 changes: 2 additions & 2 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function fold(v::V, depth = 0; kind = :mutable) where {T <: Real, V <: AbstractV
for i in 1:(l ÷ 2)
gap = v[i + 1] - v[1]
fold, r = divrem(l, i)
if r == 0 && check_pattern(v, i, gap, fold)
if r == 0 && check_pattern(v, i, gap, fold)
# return VectorFold(fold(v[1:i], depth + 1), gap, fold)
return make_vector_fold(v[1:i], gap, fold, kind)
end
Expand All @@ -102,7 +102,7 @@ end
function make_vector_fold(isf; kind = :mutable)
vf = kind == :mutable ? "VectorFold" : "IVectorFold"
str = "The pattern of the interval is not a point." *
" The IntervalsFold cannot be converted to a $vf"
" The IntervalsFold cannot be converted to a $vf"
@assert is_points(isf) str

return make_vector_fold([value(pattern(isf), :a)], gap(isf), folds(isf), kind)
Expand Down
36 changes: 18 additions & 18 deletions src/immutable_vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
VectorFold{T,V <: AbstractVector{T}}
A folded vector structure that extends the methods of AbstractVector to a folded structure.
"""
struct IVectorFold{T,V <: AbstractVector{T}} <: AbstractVectorFold{T}
struct IVectorFold{T, V <: AbstractVector{T}} <: AbstractVectorFold{T}
pattern::V
gap::T
folds::Int
Expand All @@ -20,7 +20,7 @@ pattern(vf, index) = pattern(vf)[index]
unfold(vf::VectorFold; from=1, to=folds(vf))
Construct the unfolded version of `vf` (with the same type as `pattern(vf)`) based. Please note that using an iterator on `vf` avoid memory allocation, which is not the case of `unfold`.
"""
function unfold(vf::IVectorFold; from=1, to=folds(vf))
function unfold(vf::IVectorFold; from = 1, to = folds(vf))
pl = pattern_length(vf)
ul = (to - from + 1) * pl
v = typeof(pattern(vf))(undef, ul)
Expand All @@ -45,38 +45,38 @@ Base.iterate(iter::IVectorFold) = (pattern(iter, 1), 1) # Base case iterate meth
function Base.iterate(iter::IVectorFold, state::Int)
state ≥ length(iter) && return nothing

next_state = state + 1
next_state = state + 1
pl = pattern_length(iter)

pattern_counter = mod1(next_state, pl)
fold_counter = state ÷ pl
elem = pattern(iter, pattern_counter) + (fold_counter * gap(iter))
pattern_counter = mod1(next_state, pl)
fold_counter = state ÷ pl
elem = pattern(iter, pattern_counter) + (fold_counter * gap(iter))

return elem, next_state
return elem, next_state
end

# Reverse iterate method
function Base.iterate(r_iter::Base.Iterators.Reverse{IVectorFold{T,V}},
state::Int = length(r_iter.itr)
) where {T,V}
state < 1 && return nothing
function Base.iterate(r_iter::Base.Iterators.Reverse{IVectorFold{T, V}},
state::Int = length(r_iter.itr)
) where {T, V}
state < 1 && return nothing

next_state = state - 1
iter = r_iter.itr
next_state = state - 1
iter = r_iter.itr
pl = pattern_length(iter)

pattern_counter = mod1(state, pl)
fold_counter = next_state ÷ pl
elem = pattern(iter, pattern_counter) + (fold_counter * gap(iter))
pattern_counter = mod1(state, pl)
fold_counter = next_state ÷ pl
elem = pattern(iter, pattern_counter) + (fold_counter * gap(iter))

return elem, next_state
return elem, next_state
end

# Folding a vector to give a suitable VectorFold
check_pattern(v, w, gap) = all(i -> i == gap, w - v)

function check_pattern(v, i, gap, fold)
for j in 1:(fold -1)
for j in 1:(fold - 1)
v_start, v_end = (j - 1) * i + 1, j * i
w_start, w_end = j * i + 1, (j + 1) * i
!check_pattern(v[v_start:v_end], v[w_start:w_end], gap) && return false
Expand Down
46 changes: 23 additions & 23 deletions src/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Base.ndims(::Interval) = 1

function Base.rand(i::Interval{T,L,R}) where {T,L,R}
function Base.rand(i::Interval{T, L, R}) where {T, L, R}
# α = max(_minfloat(T), first(i))
# β = min(_maxfloat(T), last(i))
μ = maxintfloat(T, Int)
Expand All @@ -12,7 +12,7 @@ function Base.rand(i::Interval{T,L,R}) where {T,L,R}
δ = β - α
# if δ === Inf
if δ > μ
return rand(rand() < 0.5 ? α..zero(T) : zero(T)..β)
return rand(rand() < 0.5 ? α .. zero(T) : zero(T) .. β)
else
# r = α + exp10(log10(δ) * rand())
r = α + δ * rand()
Expand All @@ -22,14 +22,14 @@ function Base.rand(i::Interval{T,L,R}) where {T,L,R}
end

# TODO - Optimise the type of Intervals.Bound (currently abstract super type)
mutable struct IntervalsFold{T<:AbstractFloat,L<:Intervals.Bound,R<:Intervals.Bound}
pattern::Interval{T,L,R}
mutable struct IntervalsFold{T <: AbstractFloat, L <: Intervals.Bound, R <: Intervals.Bound}
pattern::Interval{T, L, R}
gap::T
folds::Int
current::Int
end

IntervalsFold(p, g, f, c=1) = IntervalsFold(p, g, f, c)
IntervalsFold(p, g, f, c = 1) = IntervalsFold(p, g, f, c)

@forward IntervalsFold.pattern Base.isempty, Base.ndims

Expand All @@ -42,16 +42,16 @@ function pattern(isf::IntervalsFold)
return isf.pattern + (-distortion)
end

function unfold(isf::IntervalsFold{T,L,R}) where {T,L,R}
function unfold(isf::IntervalsFold{T, L, R}) where {T, L, R}
reset_pattern!(isf)
x = first(pattern(isf))
y = last(pattern(isf))
g = gap(isf)
f = folds(isf)
return [Interval{T,L,R}(x + g * i, y + g * i) for i in 0:(f - 1)]
return [Interval{T, L, R}(x + g * i, y + g * i) for i in 0:(f - 1)]
end

function set_fold!(isf::IntervalsFold, new_fold=isf.current + 1)
function set_fold!(isf::IntervalsFold, new_fold = isf.current + 1)
if new_fold != isf.current && 0 < new_fold ≤ isf.folds
distortion = gap(isf) * (new_fold - isf.current)
isf.pattern += distortion
Expand All @@ -78,7 +78,7 @@ end

# Reverse iterate method
function Base.iterate(
r_iter::Base.Iterators.Reverse{<:IntervalsFold}, state::Int=length(r_iter.itr)
r_iter::Base.Iterators.Reverse{<:IntervalsFold}, state::Int = length(r_iter.itr)
)
state < 1 && return nothing
iter = r_iter.itr
Expand All @@ -96,7 +96,7 @@ Base.size(isf::IntervalsFold) = span(isf.pattern) * folds(isf)

Base.length(isf::IntervalsFold) = folds(isf)

Base.eltype(::Type{<:IntervalsFold{T,L,R}}) where {T,L,R} = Interval{T,L,R}
Base.eltype(::Type{<:IntervalsFold{T, L, R}}) where {T, L, R} = Interval{T, L, R}

is_points(isf) = is_point(pattern(isf))

Expand All @@ -123,21 +123,21 @@ function Base.rand(v::V) where {V <: Set{<:IntervalsFold}}
end

@testitem "IntervalsFold" tags=[:intervals] begin
i01 = Interval{Open,Closed}(0.0, 1.0)
i23 = Interval{Open,Closed}(2.0, 3.0)
i45 = Interval{Open,Closed}(4.0, 5.0)
i67 = Interval{Open,Closed}(6.0, 7.0)
i89 = Interval{Open,Closed}(8.0, 9.0)
i01 = Interval{Open, Closed}(0.0, 1.0)
i23 = Interval{Open, Closed}(2.0, 3.0)
i45 = Interval{Open, Closed}(4.0, 5.0)
i67 = Interval{Open, Closed}(6.0, 7.0)
i89 = Interval{Open, Closed}(8.0, 9.0)
isf_dict = Dict([
IntervalsFold(i01, 2.0, 5) => Dict(
:pattern => i01,
:gap => 2.0,
:folds => 5,
:length => 5,
:size => 5.0,
:unfold => [i01, i23, i45, i67, i89],
:reverse => reverse([i01, i23, i45, i67, i89]),
),
:pattern => i01,
:gap => 2.0,
:folds => 5,
:length => 5,
:size => 5.0,
:unfold => [i01, i23, i45, i67, i89],
:reverse => reverse([i01, i23, i45, i67, i89])
)
])

for (isf, results) in isf_dict
Expand Down
40 changes: 20 additions & 20 deletions src/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
A mutable structure for folded vector that extends the methods of AbstractVector. Compared to `IVectorFold`, this tructure is about 20% faster using iterators.
Note that this structure keep an active pointer to the `current` *unfolded* pattern. However, its external behavior is similar to `IVectorFold`.
"""
mutable struct VectorFold{T,V<:AbstractVector{T}} <: AbstractVectorFold{T}
mutable struct VectorFold{T, V <: AbstractVector{T}} <: AbstractVectorFold{T}
pattern::V
gap::T
folds::Int
current::Int
end

VectorFold(p, g, f; c=1) = VectorFold(p, g, f, c)
VectorFold(p, g, f; c = 1) = VectorFold(p, g, f, c)

pattern_length(mvf::VectorFold) = length(mvf.pattern)

Expand All @@ -25,7 +25,7 @@ pattern(mvf::VectorFold, index) = pattern(mvf)[index]
set_fold!(mvf::VectorFold, new_fold = mvf.current + 1)
Set the *unfolded* pattern to `new_fold`. By default move the next *fold* after `current`.
"""
function set_fold!(mvf, new_fold=mvf.current + 1)
function set_fold!(mvf, new_fold = mvf.current + 1)
if new_fold != mvf.current && 0 < new_fold ≤ mvf.folds
distortion = gap(mvf) * (new_fold - mvf.current)
mvf.pattern .+= distortion
Expand Down Expand Up @@ -55,8 +55,8 @@ end

# Reverse iterate method
function Base.iterate(
r_iter::Base.Iterators.Reverse{VectorFold{T,V}}, state::Int=length(r_iter.itr)
) where {T,V}
r_iter::Base.Iterators.Reverse{VectorFold{T, V}}, state::Int = length(r_iter.itr)
) where {T, V}
state < 1 && return nothing

iter = r_iter.itr
Expand All @@ -81,7 +81,7 @@ end
Base.rand(mvf::VectorFold, n::Int) = map(_ -> rand(mvf), 1:n)

# Specific dispatch for MVectorFold
function unfold(mvf::VectorFold; from=1, to=folds(mvf))
function unfold(mvf::VectorFold; from = 1, to = folds(mvf))
pl = pattern_length(mvf)
ul = (to - from + 1) * pl
v = typeof(mvf.pattern)(undef, ul)
Expand All @@ -101,22 +101,22 @@ make_vector_fold(pattern, gap, fold, ::Val{:mutable}) = VectorFold(pattern, gap,

@testitem "VectorFold" tags=[:vectors] begin
vf_dict = Dict([
make_vector_fold([1,2], 10, 5, :immutable) => Dict(
:pattern => [1,2],
make_vector_fold([1, 2], 10, 5, :immutable) => Dict(
:pattern => [1, 2],
:gap => 10,
:folds => 5,
:length => 10,
:unfold => [1,2,11,12,21,22,31,32,41,42],
:reverse => reverse([1,2,11,12,21,22,31,32,41,42]),
:unfold => [1, 2, 11, 12, 21, 22, 31, 32, 41, 42],
:reverse => reverse([1, 2, 11, 12, 21, 22, 31, 32, 41, 42])
),
make_vector_fold([1,2], 10, 5, :mutable) => Dict(
:pattern => [1,2],
make_vector_fold([1, 2], 10, 5, :mutable) => Dict(
:pattern => [1, 2],
:gap => 10,
:folds => 5,
:length => 10,
:unfold => [1,2,11,12,21,22,31,32,41,42],
:reverse => reverse([1,2,11,12,21,22,31,32,41,42]),
),
:unfold => [1, 2, 11, 12, 21, 22, 31, 32, 41, 42],
:reverse => reverse([1, 2, 11, 12, 21, 22, 31, 32, 41, 42])
)
])

for (vf, results) in vf_dict
Expand All @@ -131,22 +131,22 @@ make_vector_fold(pattern, gap, fold, ::Val{:mutable}) = VectorFold(pattern, gap,
@test collect(Iterators.reverse(vf)) == results[:reverse]
@test reverse(collect(vf)) == results[:reverse]
end
@test isempty(make_vector_fold(Vector(),1,1,:immutable))
@test isempty(make_vector_fold(Vector(),1,1))
@test isempty(make_vector_fold(Vector(), 1, 1, :immutable))
@test isempty(make_vector_fold(Vector(), 1, 1))

v1 = make_vector_fold([42,3,45,6],13,4)
v1 = make_vector_fold([42, 3, 45, 6], 13, 4)
w1 = unfold(v1)
v11 = fold(w1)

@test unfold(v11) == w1

v2 = make_vector_fold([34,34,43,43],10,3)
v2 = make_vector_fold([34, 34, 43, 43], 10, 3)
w2 = unfold(v2)
v22 = fold(w2)

@test unfold(v22) == w2

v3 = make_vector_fold([42,3,45,6],13,4,:immutable)
v3 = make_vector_fold([42, 3, 45, 6], 13, 4, :immutable)
w3 = unfold(v3)
v33 = fold(w3)

Expand Down
Loading