Range consolidation: Difference between revisions

Line 506:
{{trans|C#}}
 
<lang dyalect>functype maxPt(xs, ye) {
 
if x > y {
func Pt.min() => min(this::s, this::e)
x
func Pt.max() => max(this::s, this::e)
} else {
 
y
let rng = }[
[ Pt(1.1, 2.2) ],
}
[ Pt(s: 6.1, e: 7.2), Pt(s: 7.2, e: 8.3) ],
[ Pt(s: 4.0, e: 3.0), Pt(s: 2, e: 1) ],
func min(x, y) {
[ Pt(s: 4.0, e: 3.0), Pt(s: 2, e: 1), Pt(s: -1, e: 2), Pt(s: 3.9, e: 10) ],
if x < y {
[ Pt(s: 1.0, e: 3.0), Pt(s: -6, e: -1), Pt(s: -4, e: -5), Pt(s: 8, e: 2), Pt(s: -6, e: -6) ]
x
]
} else {
 
y
func overlap(left, right) =>
}
right.max() >= left.min()
}
when left.max() > right.max()
func overlap( else left,.max() >= right.min() {
if max(left::s, left::e) > max(right::s, right::e) {
max(right::s, right::e) >= min(left::s, left::e)
} else {
max(left::s, left::e) >= min(right::s, right::e)
}
}
func consolidate(left, right) {=> Pt(min(left.min(), right.min()), max(left.max(), right.max()))
(s: min(min(left::s, left::e), min(right::s, right::e)), e: max(max(left::s, left::e), max(right::s, right::e)))
}
func normalize(range) {=> Pt(range.min(), range.max())
(s: min(range::s, range::e), e: max(range::s, range::e))
}
for list in [rng {
[var (s:z 1.1,= e: 2list.2len() ],- 1
x
[ (s: 6.1, e: 7.2), (s: 7.2, e: 8.3) ],
[ (s: 4.0, e: 3.0), (s: 2, e: 1) ],
[ (s: 4.0, e: 3.0), (s: 2, e: 1), (s: -1, e: 2), (s: 3.9, e: 10) ],
[ (s: 1.0, e: 3.0), (s: -6, e: -1), (s: -4, e: -5), (s: 8, e: 2), (s: -6, e: -6) ]
] {
var z = list.len()-1
while z >= 1 {
for y in (z - 1)^-1..0 when overlap(list[z], list[y]) {
iflist[y] = overlapconsolidate(list[z], list[y]) {
break list[y] = consolidate.removeAt(list[z], list[y])
list.removeAt(z)
break
}
}
z -= 1
}
y
for i in list.indices() {
list[i] = normalize(list[i])
}
x
list.sort((x,y) => x::s - y::s)
print(list)
Anonymous user