Link to home
Start Free TrialLog in
Avatar of mastiSoft
mastiSoft

asked on

Fast way to shift data to left

Hi.
I urgently need help.
How can I shift arrays elements to left ( not using Array.Copy)
Example:
System.Buffer.BlockCopy(pixels1d, Globals.stride, pixels1d, 0, pixels1d.Length - Globals.stride);

Open in new window

here I prepare empty space at the end of pixels1d to write a new values.
Array.Copy and System.Buffer.BlockCopy working fine on PC and laptops but not on tablets.
Just in case you try to convince me how good Array.Copy and System.Buffer.BlockCopy  I tell that they are not fast enough for me using app. on tablet.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

x.RemoveAt(0)  where x is your array should do the same.
hmm, what use-case? What about using a ring buffer instead of an array?
Avatar of mastiSoft
mastiSoft

ASKER

To Andy: Please check error information in attached file.
To : ste5an. Will be buffer manipulation faster then arrays. If I use buffer how do I shift data?
2019-05-21-04_44_13-GAS---Microsoft-.png
OK.  That means you are not using a collection.  Have you considered using an ArrayList (or even just a List) to handle the data.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8

A possible alternative could be to use a mix of unmanaged and managed code so you could use pointers.
How do I shift an block of data from ArrayList?
I mean first  block of  bytes have to disappear, all data moving to the left and at the end I'll have free bytes  to save a new data there.
As I know I can remove from list element with some index  or matching some value. Can I remove entire block of data with some size from ArrayList? Use for next will slow down the process.  This process happens at least 300 times in second and block can have different size (up to 32767).
ArrayList.RemoveRange will remove a block of data.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.removerange?view=netframework-4.8

You would then have to add the new data to the ArrayList.  

The removal should be very rapid, especially compared to copy or move operations.  The removal just reassigns a 'pointer' to where the array starts, very little other work in memory is performed at that point wheras copy obviously performs a lot of work.  The move I don't know.
The downside is the addition to the ArrayList will involve some extra work.  The big question is just which is worst, you would probably have to test it under your real conditions (but I suspect the RemoveRange and Add actions will be less work especially with large numbers of items - up to 32767).
Hi Andy.
this all is amazing with list array, but I need this manipulation to create a writeable bitmap at the end.
bitmap.WritePixels() accept byte array not byte list and when I call
image=  pixels1d.ToArray();

Open in new window

all advantage
with list gone. It is actually working worse then with array.
 for (int i1 = 0; i1 < samples; i1++)
            {
            //get value from data 
                if (val > 255) val = 255;
                if (val < 0) val = 0;
                Color c = colors[val];
                int ii = indexPP + 3 * i1;
                pixels1d.Insert(ii, c.R);
                ii = ii + 1;
                pixels1d.Insert(ii, c.G);
                ii = ii + 1;
                pixels1d.Insert(ii, c.B);
}

Open in new window


image=  pixels1d.ToArray()
 Globals.bitmap.WritePixels(new Int32Rect(0, 0,samples, traces), image, Globals.stride, 0);

Open in new window



This is a good idea to work with list with big data blocks but unfortunately not for my application.
That's why I asked  "hmm, what use-case?" in the first place..

Maybe you should start describing what you're trying to achieve (top-down).
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial