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 01c5b07..f0c2c85 100644 --- a/src/common.jl +++ b/src/common.jl @@ -104,6 +104,14 @@ 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" + @assert is_points(isf) str + + return make_vector_fold([value(pattern(isf), :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..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 @@ -52,6 +54,18 @@ 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) + +# 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 @@ -114,3 +128,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 461c96e..0361d0b 100644 --- a/test/vector.jl +++ b/test/vector.jl @@ -43,4 +43,8 @@ v22 = fold(w2) @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