iterate.carbon 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. package Core library "prelude/iterate";
  5. export import library "prelude/copy";
  6. export import library "prelude/destroy";
  7. export import library "prelude/types";
  8. export import library "prelude/operators";
  9. interface Iterate {
  10. // TODO: Support iterating ranges of non-copyable values.
  11. let ElementType:! Copy & Destroy;
  12. let CursorType:! type;
  13. fn NewCursor[self: Self]() -> CursorType;
  14. fn Next[self: Self](cursor: CursorType*) -> Optional(ElementType);
  15. }
  16. impl forall [T:! Copy & Destroy, N:! IntLiteral()]
  17. array(T, N) as Iterate
  18. where .ElementType = T and .CursorType = i32 {
  19. fn NewCursor[unused self: Self]() -> i32 { return 0; }
  20. fn Next[self: Self](cursor: i32*) -> Optional(T) {
  21. if (*cursor < N) {
  22. ++*cursor;
  23. return Optional(T).Some(self[*cursor - 1]);
  24. } else {
  25. return Optional(T).None();
  26. }
  27. }
  28. }