From c1f76fe6a9a4fef0662058251fc6151e6eaa7c86 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 31 Mar 2022 15:25:15 -0500 Subject: [PATCH] added variant of iter.as_iterator for arrays by pointer --- core/container/iter.onyx | 42 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/core/container/iter.onyx b/core/container/iter.onyx index b7b3efe5..11f5b781 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -437,7 +437,47 @@ from_array :: (arr: [] $T) -> Iterator(^T) { }; } -#match as_iterator (x: ^[..] $T) -> Iterator(^T) { +#match as_iterator (x: ^[..] $T) -> Iterator(T) { + Context :: struct (T: type_expr) { + arr: ^[..] T; + current: u32; + } + + c := make(Context(T)); + c.arr = x; + c.current = 0; + + next :: (use _: ^Context($T)) -> (T, bool) { + if current < arr.count { + defer current += 1; + return arr.data[current], true; + + } else { + return null, false; + } + } + + close :: (data: rawptr) { + cfree(data); + } + + remove :: (use _: ^Context($T)) { + // + // This is current - 1 because current will have already + // been incremented by the time this element calls #remove. + array :: package core.array + array.delete(arr, current - 1); + } + + return .{ + data = c, + next = #solidify next { T = T }, + close = close, + remove = #solidify remove { T = T }, + }; +} + +#match as_iterator (x: ^[..] $T, by_pointer: bool) -> Iterator(^T) { Context :: struct (T: type_expr) { arr: ^[..] T; current: u32; -- 2.25.1