Is there a sparse Span<T> for multiple Memory<T> sections?

483 Views Asked by At

Consider:

        public struct Foo
        {
            public float value;
        }

        static void Main(string[] args)
        {
            var array = new Foo[10];

            var block1 = new Memory<Foo>(array, 0, 3);
            var block2 = new Memory<Foo>(array, 4, 6);

            var span1 = block1.Span;
            var span2 = block2.Span;

            foreach (ref var  f in span1) { /* do work */ }
            foreach (ref var  f in span2) { /* do work */ }

            var blocks = new[] { block1, block2 };

            foreach (var block in blocks)
            {
                foreach (ref var f in block.Span)
                {
                    /* do work */
                }
            }

            // preferred would be:
            foreach (ref var f in new Span<Foo>(blocks))
            {
                /* do work */
            }
        }

Is there a span structure that lets you cover multiple memory blocks? I could write a custom enumerator, but calling Memory.Span for each element is slow.

1

There are 1 best solutions below

0
Michał Bryłka On

I don't know how you obtain your data but you might consider: ReadOnlySequence