Returned fragments of an array

I need to get slices of an array from an array. It pains me to use Array.Copy (). new ArraySegment (..). Array returns the original [complete] array. Below is what I came up with, but I feel pretty lame. Is there a better way to do this?



class Program
{
    static void Main(string[] args)
    {
        var arr = new ArraySegment<byte>(new byte[5] { 5, 4, 3, 2, 1 }, 0, 2).ArrayFragment();
        for (int i = 0; i < arr.Length; i++)
            Console.WriteLine(i);
        Console.Read();
    }
}

static class Extensions
{
    public static T[] ArrayFragment<T>(this ArraySegment<T> segment)
    {
        var arr = new T[segment.Count];
        Array.Copy(segment.Array, segment.Offset, arr, 0, segment.Count);
        return arr;
    }
}

      

code>

Thanks in advance.

Update: The above was just an example. I have a method: byte [] CalculateXXX (byte [] key, byte [] message); I am doing massive manipulations inside this method. I want to return part of an array. ArraySegment does not implement IEnumerable and does not return an array only with a new segment ArraySegment (arr ...). Array returns the complete original array.



var rval = new byte [4]; // new ArraySegment (finalOutputBuffer, 0, 4) .SegmentedArray (); Array.Copy (finalOutputBuffer, 0, rval, 0, 4);

I found what I had to do above to get back a slice of the array. Was just wondering if there is a better way to return the fragments of an array [as a new array].

+1


source to share


3 answers


Vyas, I am very sorry that you sent this useless heap ****

. It's been a long time since I was using ArraySegment

it and I just assumed that it implemented (more or less) a serial interface. Someone (John?) Please tell me what drugs were used during the implementation of this useless structure.

Finally, to keep the long story short, the best solution is probably to implement your own version ArraySegment

, just by doing it right.



I don't understand your problem using ArraySegment

. There is no additional overhead here if that's what you mean, since the original data is not copied. Rather, it ArraySegment

offers easy viewing of the data.

Th make this work, change the type (return) from T[]

to IEnumerable<T>

or, if you need indexed access, to IList<T>

. It is generally preferred to use interface types in method signatures and avoid array types entirely. The rationale is very simple: it makes problems like yours go away and makes the interface more flexible and reliable in the future. It also provides better information hiding because it hides parts of the implementation that are not relevant to the consumer of the method.

+1


source


Define better. What is the disadvantage of ArraySegment? What's your problem that it doesn't solve?


Edit : Ok, now I understand your point, but this is wrong. It may be a mistake in the sense that you feel like she should be doing more, but she is doing exactly what she should be doing. It allows you to pass information to a piece of code about the array you want to use and which part of the array to use.

It doesn't provide IEnumerable or anything that gives you a narrow view of the array, i.e. x [0] does not give you the first segment element, and you cannot skip it.



So, yes, this is useless in and of itself, but you won't be able to get something that is an array at heart, and yet it is also a segment of a larger array.

You can easily create your own collection-like class that references an array and uses the array as storage and also provides indexing, enumeration, etc.

But that's not what this framework does. I guess the main problem is that they made you expect more from her on their behalf.

+1


source


Another side effect of poor iterator design in C # is btw. There are many examples of similar ones (just like "lame") where just a good design can transmit or indicate or control segments (aka range) without any Arkhangian shickanery .. copy semantics or not, array is also a well-defined concept.

Just use C ++. :-)

0


source







All Articles