Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# array question (split)

Question very simple - say, i got function, which receives array as its arguments

void calc(double[] data)

how do "split" this data in two subarrays and pass to sub functions like this

calc_sub(data(0, length/2));
cals_sub(data(length /2, length /2));

i hope, you got the idea - in c++ i would write this

void calc(double * data, int len)
{
   calc_sub(data, len / 2); //this one modifies data!!
   calc_sub(data + len / 2, len / 2); //this one modifies data too!!
}

How to do same in C# without unecesary memory copying? I would need 2 memory copies here. 1) from data to splitted data 2) calc_sub 3) from splitted data back to data! This is huge waste of time and memory!

like image 316
0xDEAD BEEF Avatar asked May 28 '26 14:05

0xDEAD BEEF


1 Answers

The easiest is probably using LINQs Take and Skip extension methods:

int half = data.Length / 2;
double[] sub1 = data.Take(half).ToArray();
double[] sub2 = data.Skip(half).ToArray();
like image 167
Darin Dimitrov Avatar answered May 30 '26 02:05

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!