Skip to content
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
2 changes: 2 additions & 0 deletions src/PatternFolds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))")
Expand Down
16 changes: 16 additions & 0 deletions src/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
4 changes: 4 additions & 0 deletions test/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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