Range consolidation: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Now uses new core library method.)
Line 711: Line 711:


<lang dyalect>type Pt(s, e)
<lang dyalect>type Pt(s, e)

func Pt.min() => min(this::s, this::e)
func Pt.Min() => min(this.s, this.e)
func Pt.max() => max(this::s, this::e)
func Pt.Max() => max(this.s, this.e)
func Pt.toString() => "(\(this::s), \(this::e))"
func Pt.ToString() => "(\(this.s), \(this.e))"

let rng = [
let rng = [
[ Pt(1.1, 2.2) ],
[ Pt(1.1, 2.2) ],
Line 723: Line 723:
[ Pt(1.0, 3.0), Pt(-6, -1), Pt(-4, -5), Pt(8, 2), Pt(-6, -6) ]
[ Pt(1.0, 3.0), Pt(-6, -1), Pt(-4, -5), Pt(8, 2), Pt(-6, -6) ]
]
]

func overlap(left, right) =>
func overlap(left, right) =>
right.max() >= left.min()
right.Max() >= left.Min()
when left.max() > right.max()
when left.Max() > right.Max()
else left.max() >= right.min()
else left.Max() >= right.Min()
func consolidate(left, right) => Pt(min(left.min(), right.min()), max(left.max(), right.max()))
func consolidate(left, right) => Pt(min(left.Min(), right.Min()), max(left.Max(), right.Max()))
func normalize(range) => Pt(range.min(), range.max())
func normalize(range) => Pt(range.Min(), range.Max())
for list in rng {
for list in rng {
var z = list.len() - 1
var z = list.Length() - 1
while z >= 1 {
while z >= 1 {
for y in (z - 1)^-1..0 when overlap(list[z], list[y]) {
for y in (z - 1)^-1..0 when overlap(list[z], list[y]) {
list[y] = consolidate(list[z], list[y])
list[y] = consolidate(list[z], list[y])
break list.removeAt(z)
break list.RemoveAt(z)
}
}
z -= 1
z -= 1
}
}
for i in list.indices() {
for i in list.Indices() {
list[i] = normalize(list[i])
list[i] = normalize(list[i])
}
}
list.sort((x,y) => x::s - y::s)
list.Sort((x,y) => x.s - y.s)
print(list)
print(list)
}</lang>
}</lang>