Appearance
| 1 | namespace IR.Program is | |
| 2 | use Collections.LIST | |
| 3 | ||
| 4 | /// Groups class numbers into the fewest runs of consecutive numbers, | |
| 5 | /// so a test against a set of classes is one comparison a run. | |
| 6 | class CLASS_ID_RANGES is | |
| 7 | /// The runs `ids` falls into, lowest first, each as its lowest and | |
| 8 | /// highest number. Repeated numbers count once. | |
| 9 | of(ids: Collections.Iterable[int]) -> Collections.List[(low: int, high: int)] static is | |
| 10 | let sorted = LIST[int](ids) | |
| 11 | ||
| 12 | sorted.sort() | |
| 13 | ||
| 14 | let result = LIST[(low: int, high: int)]() | |
| 15 | ||
| 16 | for id in sorted do | |
| 17 | if result.count > 0 /\ id <= result[result.count - 1].high + 1 then | |
| 18 | let last = result[result.count - 1] | |
| 19 | ||
| 20 | if id > last.high then | |
| 21 | result[result.count - 1] = (last.low, id) | |
| 22 | fi | |
| 23 | else | |
| 24 | result.add((id, id)) | |
| 25 | fi | |
| 26 | od | |
| 27 | ||
| 28 | return result | |
| 29 | si | |
| 30 | si | |
| 31 | si |