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. |
func Pt.Min() => min(this.s, this.e) |
||
func Pt. |
func Pt.Max() => max(this.s, this.e) |
||
func Pt. |
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. |
right.Max() >= left.Min() |
||
when left. |
when left.Max() > right.Max() |
||
else left. |
else left.Max() >= right.Min() |
||
func consolidate(left, right) => Pt(min(left. |
func consolidate(left, right) => Pt(min(left.Min(), right.Min()), max(left.Max(), right.Max())) |
||
func normalize(range) => Pt(range. |
func normalize(range) => Pt(range.Min(), range.Max()) |
||
for list in rng { |
for list in rng { |
||
var z = list. |
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. |
break list.RemoveAt(z) |
||
} |
} |
||
z -= 1 |
z -= 1 |
||
} |
} |
||
for i in list. |
for i in list.Indices() { |
||
list[i] = normalize(list[i]) |
list[i] = normalize(list[i]) |
||
} |
} |
||
list. |
list.Sort((x,y) => x.s - y.s) |
||
print(list) |
print(list) |
||
}</lang> |
}</lang> |