mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 22:38:14 +00:00
As discussed in the 2026-06-14 toolchain open meeting, `Range` is a better fit for the prelude than as a general library in `Core`. This PR moves `Range` into the prelude, and updates the build infrastructure and various tests to reflect that change.
42 lines
1.2 KiB
Plaintext
42 lines
1.2 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
package Core library "prelude/range";
|
|
|
|
import library "prelude/iterate";
|
|
import library "prelude/operators";
|
|
import library "prelude/types/int";
|
|
import library "prelude/types/int_literal";
|
|
import library "prelude/types/optional";
|
|
|
|
class IntRange(N: IntLiteral) {
|
|
fn Make(start: Int(N), end: Int(N)) -> Self {
|
|
return {.start = start, .end = end};
|
|
}
|
|
|
|
impl as Iterate where .CursorType = Int(N) and .ElementType = Int(N) {
|
|
fn NewCursor(self) -> Int(N) { return self.start; }
|
|
fn Next(self, cursor: Int(N)*) -> Optional(Int(N)) {
|
|
var value: Int(N) = *cursor;
|
|
if (value < self.end) {
|
|
++*cursor;
|
|
return Optional(Int(N)).Some(value);
|
|
} else {
|
|
return Optional(Int(N)).None();
|
|
}
|
|
}
|
|
}
|
|
|
|
private var start: Int(N);
|
|
private var end: Int(N);
|
|
}
|
|
|
|
fn Range(end: i32) -> IntRange(32) {
|
|
return IntRange(32).Make(0, end);
|
|
}
|
|
|
|
fn InclusiveRange(start: i32, end: i32) -> IntRange(32) {
|
|
return IntRange(32).Make(start, end + 1);
|
|
}
|