6

Good afternoon, I have a c# jagged array with true and false values (or 0's and 1's) and I want to reverse the values like:

1 1 1 1
0 1 1 0
1 1 1 1
1 0 0 1

to became:

0 0 0 0
1 0 0 1
0 0 0 0
0 1 1 0

Is there an easy way to do it not looping it? something like !myJaggedArray??

Nauzet
  • 661
  • 8
  • 20

2 Answers2

12

There is no built-in operation for inverting an array like that, but you can use LINQ to do the inversion without explicit loops:

var res = myJaggedArray.Select(a => a.Select(n => 1-n).ToArray()).ToArray();

The 1-n trick is a common way of replacing zeros with ones and ones with zeros without using conditional execution.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • what you guys think will be faster to work with (lopps, operations etc), a bool[][] or int[][]? what about bitarrays? – Nauzet Jul 18 '15 at 17:10
  • @Ralstlin If using `BitArray`s is an option, you could do it with a single array of `BitArray`s, and use the `Not` operation to do inversion really quickly. This would be the most economical option for 2D matrices of moderate to large size. – Sergey Kalinichenko Jul 18 '15 at 17:25
  • instead of using `1-n` you can use `1^n` – Abdullah Saleem Jul 18 '15 at 17:34
  • Thanks @dasblinkenlight, Looking into it, i found this link: http://stackoverflow.com/questions/16471759/is-bitarray-faster-in-c-sharp-for-getting-a-bit-value-than-a-simple-conjuction-w and seems that an array of bools is faster. I really need to be as fast as posible because I am working on Unity3D and with a matrix of bools i want to create a 3D space as fast as posible. I use an Spiral algorithm that run over the matrix to get the minimum number of squares and his position and with that information I want to generate 3D vertices to draw a Mesh. So speed is really important here. – Nauzet Jul 18 '15 at 19:04
  • I did a test with bool[][], vs bitArray[] size: 10.000x10.000 BoolJagged Creation: 580ms BoolJagged Loop: 510ms BoolJagged Random Access: 16025ms BitArray Creation: 2059ms BitArray Loop: 1534ms BitArray Random Access: 15947ms Not a bit difference in 100.000.000 Random access – Nauzet Jul 18 '15 at 20:34
  • @Ralstlin The difference in memory use should be quite high: `BitArray` should be 8 times smaller than an array of `bool`s. – Sergey Kalinichenko Jul 18 '15 at 20:35
  • Yes, you are right, but i will be modifying the 3D objects in runtime, i am not sure if 8 times smaller is better than 3 times faster, when i will have 2-3 100x100, 50-60 5x20 and 100-150 1x10 Thats is like 37k-38k bools – Nauzet Jul 18 '15 at 21:26
  • Note that this creates a bunch of new arrays and puts them in a new "outer" array. Another goal could be to reverse all values in-place, so that people holding references to the original jagged array could "see" the change. The question was not clear which was desired. – Jeppe Stig Nielsen Jul 19 '15 at 08:38
8

There is no built-in function, but you can use LINQ.

int[][] input = new[]
{
  new[] { 1, 1, 1, 1 },
  new[] { 0, 1, 1, 0 },
  new[] { 1, 1, 1, 1 },
  new[] { 1, 0, 0, 1 }
};
int[][] output = input.Select(row => row.Select(value => value == 1 ? 0 : 1).ToArray()).ToArray();

For logical values:

bool[][] input = new[]
{
  new[] { true,  true,  true,  true },
  new[] { false, true,  true,  false },
  new[] { true,  true,  true,  true },
  new[] { true,  false, false, true }
};
bool[][] output = input.Select(row => row.Select(value => !value).ToArray()).ToArray();
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31