fast way to reset 2d jagged array of fixed width - c#

I want to create a 2D jagged array (as previous so answers claims is faster on dot net than multidimensional one).
I want to do something like
Int [][] myarr = new int [2][3];
But C# doesn't seem to support this so I did use a for loop for the 2nd dimension instantiation.
Yet if I want to fastly reset I cant use memset equivalent as each new in the loop initiate the array in a possible different and not continouse place.
Is there a faster way to init and reset a fixed width jagged array ?

You can use LINQ and Enumerable.Range method, but internally there is still a loop performed:
int[][] myarr = Enumerable.Range(0, 2).Select(x => new int[3]).ToArray();

Related

How do I delete items in a multidimensional array and will it change the array length?

Say I have a 3d array:
int[,,] arr = new int[3, 2, 3];
How can I delete all items under one of the first dimension? And if that can be done, will that move up all the values under it? Or will the deleted items just take null values and be counted in the arr.GetLength(0) method?
I've found a C# version of the Visual Basic's ReDim method. https://stackoverflow.com/a/327958/9076546
Note that dynamic array sizes aren't the best for performance. ReDim was a mistake in my honest opinion...

How to Jagged List C#

I am trying to create a jagged array but due to the dynamic-ness of the data I am working with I do not want to waste resources creating a a large jagged array.
I am currently doing:
int[][][] data = new data[Int16.MaxValue][][];
I do not how big the data set is, or is there a better way than doing it via Lists?
Yes, you should use List<T>.
In this case, you would use List<List<List<int>>>.
Your array:
int[][][] data = new data[Int16.MaxValue][Int16.MaxValue][Int16.MaxValue];
will take up (2^16)^3 = 2^48 = way more storage space than you have,
not to mention that that declaration is not valid C#.
If you don't know how much space you need when you initialize, then it would be best to use a dynamically resizing list.
Use a variable similar to this:
List<List<List<int>>> data = new List<List<List<int>>>();
This variable allows you to add List<List<int>>'s to it, and those lists contain List<int>'s, which of course contain int's
If you absolutely do not want to use Lists, you can always replicate what Lists do under the hood: Create your array with a small number of elements, and when you reach the maximum, create a new array that is double the original's size, copy your existing array into it, and dispose of your original. Continue this pattern until you are done. I recommend using Lists instead, but this is how you would get around it if, for some reason, you just don't want to use Lists.
In fact, you can create jagged arrays without defining second and further dimensions.
int[][][] jagged = new int[256][][];
But at large datasets it is more effective to use streaming data - i.e., combinations of IEnumerable<T>.

Declaring an array with incremental values - is there a shortcut?

This question is probably pretty stupid, but I'm new to C# and I'm not sure if there are any shortcuts to do this. I have a dynamic array for which the range will always be 1-n, with n being variable. Is there anyway to declare an array and have it hold incremental values without looping?
Think along the lines of my array holding values 1-50. I'd like to declare an array as such (logically): double[] myArray = new double[] {1-50} or, more generically for my purposes double[] myArray = new double[] {1-n}. I don't know what made me think of this, I just thought I'd ask.
I am going to bind this array (or list) to a combo box in WPF. I guess setting a combo-box the same way would also work if there's a shortcut for that.
Sorry for the dumb question. =)
int n = 50;
var doubleArray = Enumerable.Range(1, n).Select(x => (double)x).ToArray();
That will generate a sequence of integers from 1 to n (in this case 50) and then cast each one to a double and create an array from those results.
You could use a List<T> which represents a dynamic array to which you could add elements.
System.Linq.Enumerable.Range can generate än enumeration of int. Cast the enumeration if you really want double.
System.Linq.Enumerable.Range(1,20).ToArray()
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

C#: Is it possible to position a 2darray in a certain position in another 2darray without iterating through it?

Is it possible to position a 2d array in a certain position in another 2d array without iterating through it?
public double[,] array2D;
array2D = new double[7,7];
public void fill1D(double[,] values)
{
Array2D = values; //Values holds a 5x5 array that i wan't to place from 1,1 in array2D
}
Thanks
No you can't (edit: without copying) , I'm sorry. You could mimic this perhaps with the ArraySegement (but you'd have to use a 1D base array. You may also care to read this paper that suggests plain rectangular arrays are relatively slow in C#.

Fast sum of values in a multidimensional array (C#)

With a 1D array, I can use the sum method to get the sum of all the values.
int[] array = {6,3,1};
Console.WriteLine(array.Sum());
With a multidimensional array (3D in my case), this can't be done. Obviously I could go all foreach on it, but this seems verbose and I suspect it will perform badly.
Is there a way to flatten the array? Or a nice way just to get the sum that I've not seen?
Sum does exactly foreach. There is no magic behind them. If you are so performance hungry use for instead of foreach. You can do this in parallel also, this operation can be easily parallelized.
this'll do the trick.
var i = array.SelectMany(j => j).Sum()
you could parallelize this in .net 4 like this
var i = array.AsParallel().SelectMany(k => k).Sum();
Why should a foreach perform badly? You have to read every value at least once to calculate the sum. There is no way around this (assuming "random" values, of course). So maybe there is a more beautyful way, but not a more performant one (speaking in terms of Big O).
If you have a jagged array and would like clean code you could use
int[][] array = { new []{ 6, 3, 1 }, new []{ 6, 3, 1 } };
Console.WriteLine(array.Sum(i => i.Sum()));

Categories

Resources