From b3caa0c6352c0b7295ef7e9594e5a318769a110c Mon Sep 17 00:00:00 2001 From: Azzaare Date: Fri, 13 Aug 2021 15:43:03 +0900 Subject: [PATCH 1/3] Add a conversion from IntervalsFold to VectorFold when the interval is a point --- src/common.jl | 9 +++++++++ src/intervals.jl | 6 ++++++ test/vector.jl | 3 +++ 3 files changed, 18 insertions(+) diff --git a/src/common.jl b/src/common.jl index 01c5b07..4e62020 100644 --- a/src/common.jl +++ b/src/common.jl @@ -104,6 +104,15 @@ A dispatcher to construct a folded vector. The `kind` of vector can be set to ei function make_vector_fold(pattern, gap, fold, kind = :mutable) return make_vector_fold(pattern, gap, fold, Val(kind)) 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" + pt = pattern(isf) + @assert is_point(pt) str + + return make_vector_fold([value(pt, :a)], gap(isf), folds(isf), kind) +end function Base.print_array(io::IO, X::AbstractVectorFold) print(io, "\tPattern: $(pattern(X))\n\tGap: $(gap(X))\n\tFolds: $(folds(X))") diff --git a/src/intervals.jl b/src/intervals.jl index ad028cf..9efffe6 100644 --- a/src/intervals.jl +++ b/src/intervals.jl @@ -52,6 +52,12 @@ Base.isempty(i::Interval) = size(i) == 0 && (opened(i, :a) || opened(i, :b)) Base.ndims(::Interval) = 1 Base.rand(i::Interval) = rand() * size(i) + value(i, :a) +""" + is_point(i::Interval) +Check if the interval `i` is simple point. +""" +is_point(i) = value(i, :a) == value(i, :b) && !isempty(i) + mutable struct IntervalsFold{T <: Real} #<: PatternFold{T, Interval{T}} pattern::Interval{T} gap::T diff --git a/test/vector.jl b/test/vector.jl index 461c96e..4f2a7c6 100644 --- a/test/vector.jl +++ b/test/vector.jl @@ -43,4 +43,7 @@ v22 = fold(w2) @test unfold(v22) == w2 + + isf = IntervalsFold(Interval((0.0, true), (0.0, true)), 1.0, 5) + @test unfold(make_vector_fold(isf)) == [i for i in 0.0:4.0] end From 63bfbe148b97fc3cf594ffefbc9a44f782b39c18 Mon Sep 17 00:00:00 2001 From: Azzaare Date: Fri, 13 Aug 2021 15:53:45 +0900 Subject: [PATCH 2/3] Add is_points --- src/PatternFolds.jl | 2 ++ src/common.jl | 5 ++--- src/intervals.jl | 2 ++ test/vector.jl | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/PatternFolds.jl b/src/PatternFolds.jl index c0af992..7d1945f 100644 --- a/src/PatternFolds.jl +++ b/src/PatternFolds.jl @@ -18,6 +18,8 @@ export closed export fold export folds export gap +export is_point +export is_points export length export make_vector_fold export opened diff --git a/src/common.jl b/src/common.jl index 4e62020..f0c2c85 100644 --- a/src/common.jl +++ b/src/common.jl @@ -108,10 +108,9 @@ 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" - pt = pattern(isf) - @assert is_point(pt) str + @assert is_points(isf) str - return make_vector_fold([value(pt, :a)], gap(isf), folds(isf), kind) + return make_vector_fold([value(pattern(isf), :a)], gap(isf), folds(isf), kind) end function Base.print_array(io::IO, X::AbstractVectorFold) diff --git a/src/intervals.jl b/src/intervals.jl index 9efffe6..cbf27b6 100644 --- a/src/intervals.jl +++ b/src/intervals.jl @@ -120,3 +120,5 @@ end Base.size(isf::IntervalsFold) = size(isf.pattern) * folds(isf) Base.eltype(::Type{<:IntervalsFold{T}}) where {T} = Interval{T} + +is_points(isf) = is_point(pattern(isf)) diff --git a/test/vector.jl b/test/vector.jl index 4f2a7c6..0361d0b 100644 --- a/test/vector.jl +++ b/test/vector.jl @@ -45,5 +45,6 @@ @test unfold(v22) == w2 isf = IntervalsFold(Interval((0.0, true), (0.0, true)), 1.0, 5) + @test is_points(isf) @test unfold(make_vector_fold(isf)) == [i for i in 0.0:4.0] end From 5aa6f9014ea26a4255c9c4bf56f76af75fffd6b0 Mon Sep 17 00:00:00 2001 From: Azzaare Date: Fri, 13 Aug 2021 16:51:33 +0900 Subject: [PATCH 3/3] Add b_equal_a --- src/intervals.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/intervals.jl b/src/intervals.jl index cbf27b6..2c8dfe8 100644 --- a/src/intervals.jl +++ b/src/intervals.jl @@ -39,6 +39,8 @@ end b_isless(i₁, i₂) = b_ismore(i₂, i₁) +b_eq_a(i₁, i₂) = b(i₁) == a(i₂) + function Base.in(val, i::Interval) (x, y) = (value(i, :a), value(i, :b)) lesser = closed(i, :a) ? x ≤ val : x < val @@ -58,6 +60,12 @@ Check if the interval `i` is simple point. """ is_point(i) = value(i, :a) == value(i, :b) && !isempty(i) +# function Base.intersect(i₁::Interval, i₂::Interval) +# !a_isless(i₁, i₂) && return intersect(i₂, i₁) +# b_ismore(i₁, i₂) && return i₂ +# value(i₁, :b) ∈ i₂ +# end + mutable struct IntervalsFold{T <: Real} #<: PatternFold{T, Interval{T}} pattern::Interval{T} gap::T