/
show.jl
159 lines (148 loc) · 5.53 KB
/
show.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Show for elements
Base.show(io::IO, h::HyperPlane) = print(io, "HyperPlane($(h.a), $(h.β))")
Base.show(io::IO, h::HalfSpace) = print(io, "HalfSpace($(h.a), $(h.β))")
Base.show(io::IO, l::Line) = print(io, "Line($(l.a))")
Base.show(io::IO, r::Ray) = print(io, "Ray($(r.a))")
Base.summary(it::Polyhedra.AbstractRepIterator{T, ElemT}) where {T, ElemT} = "$(length(it))-element iterator of $ElemT"
# Inspired from Base.show_vector
function show_repit(io::IO, v::Polyhedra.AbstractRepIterator, print_prefix::Bool, start_str::String, end_str::String, join_str::String=",")
if print_prefix
print(io, Base.typeinfo_prefix(io, v))
end
io = IOContext(io, :typeinfo => eltype(v), :compact => get(io, :compact, true))
limited = get(io, :limit, false)
limited = true
if limited && length(v) > 20
Base.show_delim_array(io, v, start_str, join_str, " \u2026 " * end_str, false, 1, 20)
else
Base.show_delim_array(io, v, start_str, join_str, end_str, false)
end
end
# Inspired from Base.showarray with repr=false and Base.print_matrix
function show_repit(io::IO, it::Polyhedra.AbstractRepIterator, ::MIME"text/plain")
print(io, summary(it))
if !isempty(it)
if !get(io, :limit, false)
screenheight = screenwidth = typemax(Int)
else
sz = displaysize(io)
# We use -4 as this is what is used in Base.print_matrix.
# This probably accounts for the summary and the vertical dots
screenheight, screenwidth = sz[1] - 4, sz[2]
end
if screenheight < length(it)
Base.show_delim_array(io, it, ":\n ", "\n", "\n \u22ee", false, 1, screenheight)
else
Base.show_delim_array(io, it, ":\n ", "\n", "", false)
end
end
end
Base.show(io::IO, v::Polyhedra.AbstractRepIterator) = show_repit(io, v, true, "[", "]")
Base.show(io::IO, mime::MIME"text/plain", it::Polyhedra.AbstractRepIterator) = show_repit(io, it, mime)
Base.summary(v::VRepresentation) = "V-representation $(typeof(v))"
Base.summary(h::HRepresentation) = "H-representation $(typeof(h))"
Base.summary(p::Polyhedron) = "Polyhedron $(typeof(p))"
show_reps(io::IO, args::Tuple, start_str::String, join_str::String, first::Bool) = first
function show_reps(io::IO, args::Tuple, start_str::String, join_str::String, first::Bool, rep, reps...)
if !isempty(rep)
if first
print(io, start_str)
first = false
else
print(io, join_str)
end
show_repit(io, rep, args...)
end
show_reps(io::IO, args, start_str, join_str, first, reps...)
end
show_vreps(io::IO, rep::HRepresentation, start_str::String, join_str::String, args...) = true
show_vreps(io::IO, rep::VRepresentation, start_str::String, join_str::String, args...) = show_reps(io, args, start_str, join_str, true, vreps(rep)...)
function show_vreps(io::IO, rep::Polyhedron, start_str::String, join_str::String, args...)
if vrepiscomputed(rep)
show_reps(io, args, start_str, join_str, true, vreps(rep)...)
else
true
end
end
show_hreps(io::IO, rep::VRepresentation, start_str::String, join_str::String, args...) = true
show_hreps(io::IO, rep::HRepresentation, start_str::String, join_str::String, args...) = show_reps(io, args, start_str, join_str, true, hreps(rep)...)
function show_hreps(io::IO, rep::Polyhedron, start_str::String, join_str::String, args...)
if hrepiscomputed(rep)
show_reps(io, args, start_str, join_str, true, hreps(rep)...)
else
true
end
end
_has_vrep(p::VRepresentation) = true
_has_vrep(p::HRepresentation) = false
_has_vrep(p::Polyhedron) = vrepiscomputed(p)
function Base.show(io::IO, rep::Rep)
first = show_hreps(io, rep, "", " ∩ ", false, "", "", " ∩")
if _has_vrep(rep) && !first
print(io, " : ")
end
show_vreps(io, rep, "", " + ", false, "convexhull(", ")")
end
function Base.show(io::IO, mime::MIME"text/plain", rep::Rep)
print(io, summary(rep))
show_hreps(io, rep, ":\n", ",\n", mime)
show_vreps(io, rep, ":\n", ",\n", mime)
end
#_length(hrep::HRepresentation) = nhyperplanes(hrep) + nhalfspaces(hrep)
#_length(vrep::VRepresentation) = nsympoints(vrep) + npoints(vrep) + nlines(vrep) + nrays(vrep)
#
#function _print(io::IO, it::ElemIt{<:HRepElement})
# for h in it
# print(io, " $(h.β)")
# for j in eachindex(h.a)
# print(io, " $(-h.a[j])")
# end
# println(io)
# end
#end
#
#function _print(io::IO, it::ElemIt{<:VRepElement})
# for v in it
# print(io, " $(Int(ispoint(v)))")
# c = coord(v)
# for j = eachindex(c)
# print(io, " $(c[j])")
# end
# println(io)
# end
#end
#
#function Base.show(io::IO, rep::Representation{T}) where {T}
# if typeof(rep) <: HRepresentation
# print(io, "H")
# ls = BitSet(1:nhyperplanes(rep))
# else
# print(io, "V")
# ls = BitSet(1:nsympoints(rep)) ∪ BitSet(nsympoints(rep) + npoints(rep) + (1:nlines(rep)))
# end
# println(io, "-representation")
#
# if !isempty(ls)
# print(io, "linearity $(length(ls))");
# for i in ls
# print(io, " $i")
# end
# println(io)
# end
#
# println(io, "begin")
# if T <: AbstractFloat
# typename = "real"
# elseif T <: Integer
# typename = "integer"
# else
# typename = "rational"
# end
# println(io, " $(_length(rep)) $(N+1) $typename")
# if typeof(rep) <: HRepresentation
# _print.(io, hreps(rep))
# else
# _print.(io, vreps(rep))
# end
# print(io, "end")
#end