Range consolidation: Difference between revisions

Content added Content deleted
m (Formatting.)
Line 1,187: Line 1,187:
else:
else:
a[0] < b[0]
a[0] < b[0]



proc consolidate[T](rangeList: varargs[Range[T]]): seq[Range[T]] =
proc consolidate[T](rangeList: varargs[Range[T]]): seq[Range[T]] =
## Consolidate a list of ranges of type T.
## Consolidate a list of ranges of type T.


# Build a list of normalized ranges.
# Build a sorted list of normalized ranges.
var list: seq[Range[T]]
var list: seq[Range[T]]
for item in rangeList:
for item in rangeList:
list.add if item[0] <= item[1]: item else: [item[1], item[0]]
list.add if item[0] <= item[1]: item else: [item[1], item[0]]
# Sort the list.
list.sort()
list.sort()

# Build the consolidated list starting from "smallest" range.
# Build the consolidated list starting from "smallest" range.
result.add list[0]
result.add list[0]
Line 1,206: Line 1,207:
else:
else:
result.add rangeMax
result.add rangeMax



proc `$`[T](r: Range[T]): string {.inline.} =
proc `$`[T](r: Range[T]): string {.inline.} =