Skip to content

Commit

Permalink
Extends Base.string to domains (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzaare authored Feb 14, 2023
1 parent 6109519 commit 3b79daf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstraintDomains"
uuid = "5800fd60-8556-4464-8d61-84ebf7a0bedb"
authors = ["Jean-François Baffier"]
version = "0.3.6"
version = "0.3.7"

[deps]
ConstraintCommons = "e37357d9-0691-492f-a822-e5ea6a920954"
Expand Down
16 changes: 16 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Construct an [`EmptyDomain`](@ref).
"""
domain() = EmptyDomain()

domain(values...) = domain(collect(values))

"""
Base.in(value, d <: AbstractDomain)
Fallback method for `value ∈ d` that returns `false`.
Expand Down Expand Up @@ -80,6 +82,20 @@ function Base.rand(d::V) where {D<:AbstractDomain,U<:Union{Vector{D},Set{D}},V<:
return map(rand, d)
end

"""
Base.string(D::Vector{<:AbstractDomain})
Base.string(d<:AbstractDomain)
Extends the `string` method to (a vector of) domains.
"""
function Base.string(D::Vector{<:AbstractDomain})
return replace(
"[$(prod(d -> string(d) * ',', D)[1:end-1])]",
" " => "",
)
end
Base.string(d::AbstractDomain) = replace(string(d.domain), " " => "")

## SECTION - Test Items
@testitem "EmptyDomain" tags = [:domains, :empty] begin
ed = domain()
Expand Down
8 changes: 7 additions & 1 deletion src/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ Defines the size of an interval as its `span`.
"""
size(i::I) where {I <: Interval} = span(i)

## SECTION - Test Items
function Base.string(d::Intervals)
return replace(
"[$(prod(i -> "$(string(i)[2:end-1]),", d.domain)[1:end-1])]",
" " => "",
)
end

@testitem "ContinuousDomain" tags = [:domains, :continuous] default_imports=false begin
using ConstraintDomains, Intervals, Test
# import Base:size
Expand Down
5 changes: 5 additions & 0 deletions src/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ d1 = domain(1:5)
d2 = domain([53.69, 89.2, 0.12])
d3 = domain([2//3, 89//123])
d4 = domain(4.3)
d5 = domain(1,42,86.9)
```
"""
domain(values) = SetDomain(values)
Expand Down Expand Up @@ -106,6 +107,10 @@ function to_domains(X, d::D) where {D <: DiscreteDomain}
return fill(d, n)
end

Base.string(d::RangeDomain) = replace("$(d.domain[1])..$(d.domain[end])", " " => "")

Base.string(d::SetDomain) = replace(string(sort!(collect(d.domain))), " " => "")

## SECTION - Test Items
@testitem "DiscreteDomain" tags = [:domain, :discrete, :set] begin
d1 = domain([4,3,2,1])
Expand Down

0 comments on commit 3b79daf

Please sign in to comment.