c# - Algorithm: Checking for conditions in 3D array -


this 1 hard explain! sorry that, here goes... have 3d array of data [x][y][z], , check 10 different combination conditions , keep data when it's match... example:

              x         y      z //mydata[1..1000000][1..10][1..10].foo // foo int  x[i].y[ii].z[iii].foo; // x container, y= 1 10 levels. , z= objects  //i want apply "filter" z objects...  

lets want find combinations sum of "foo" smaller , larger 2 numbers, , keep z objects

for next iteration want find lets "foo" prime number, still keeping z objects

and on more conditions, resulting in smaller , smaller list. doesn't matter in wich order performed. sort of know how it, end in nasty loops... ideas? maybe adding list faster deleting original list? in advance!

when want chain logic this, think want use linq. unfortunately, can cumbersome use linq on multidimensional arrays. helper methods, though, can convert data array more usable. first, let's build wrapper class object has 3 dimensions associated it:

public class threedimensionalarrayextension<t> {      public int x { get; set; }      public int y { get; set; }      public int z { get; set; }      public t value { get; set; } } 

next, let's write helper method converts 3-dimsensional arrays ienumerables of new type:

public static class threedimensionalarrayextensionmethods {     public static ienumerable<threedimensionalarrayextension<t>> convertarray<t>(this t[,,] foos) {         for(var x = 0; x < foos.getlength(0); x++) {             (var y = 0; y < foos.getlength(1); y++) {                 (var z = 0; z < foos.getlength(2); z++) {                     yield return new threedimensionalarrayextension<t> { x = x, y = y , z = z, value = foos[x, y, z] };                 }             }         }     } } 

note since using iterator block (the yield-return pattern), calling method not perform computation.

now can use power of linq on 3-dimensional array filter want!

mydata.convertarray().where(d => d.value.foo > 5)                      .where(d => isprime(d.value.foo))                      .where(...); 

edit: see you're using 3 nested classes , not multidimensional array assumed using. goal should still convert object ienumerable upon can chain linq queries filter or project data. in case, can do:

public static class threedimensionalarrayextensionmethods {     public static ienumerable<threedimensionalarrayextension<x>> convertarray(this x[] foos) {         for(var x = 0; x < foos.count(); x++) {             (var y = 0; y < foos[x].count(); y++) {                 (var z = 0; z < foos[x][y].count(); z++) {                     yield return new threedimensionalarrayextension<t> { x = x, y = y , z = z, value = foos[x][y][z] };                 }             }         }     } } 

and use same call convertarray followed filtering clauses described above.

if don't care x/y/z indices, can use selectmany project multidimensional list onto single dimensional list:

x.selectmany(y => y.selectmany(z => z)).where(z => z.foo > 5); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -