Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Span Loop Extensions #72

Open
jeremyknight-me opened this issue Sep 15, 2022 · 0 comments
Open

Add Span Loop Extensions #72

jeremyknight-me opened this issue Sep 15, 2022 · 0 comments

Comments

@jeremyknight-me
Copy link
Owner

Quick code sample would look like:

void Main()
{
    var list = Enumerable.Range(0, 100).ToList();
    list.SpanIterate(l => { Console.WriteLine(l); });
}

internal static class MyExtensions 
{
    /// <summary>ONLY USE ON LISTS THAT CANNOT CHANGE DURING PROCESSING</summary>
    internal static void SpanIterate<T>(this List<T> list, Action<T> action) {
        var span = CollectionsMarshal.AsSpan(list);
        for (var i = 0; i < span.Length; i++) {
            action(span[i]);
        }
    }

    internal static void SpanIterate<T>(this IReadOnlyList<T> list, Action<T> action)
    {
        var span = CollectionsMarshal.AsSpan(list.ToList());
        for (var i = 0; i < span.Length; i++)
        {
            action(span[i]);
        }
    }
}
@jeremyknight-me jeremyknight-me modified the milestone: v2 Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant