M.A
M.A

Reputation: 1133

Best way to access 2D data

Would like to know which is the best way to access 2D data in C#(.Net Framework 3.5) I am trying to do a simple masking application that will mask a set of numbers (0-9) to a series of letters. The number that needs to be masked is the columnName and the codition is the rowName.

I have used both jagged array and 2D arrays. Are there any other ways to do so which is efficient in terms of coding and running the console application. Using the rowName (string) and columnName (int) I should be able to select the data

So if the number i am masking is 5 and the condition is "third" than the masked value should be ce

aa ab ac ad ae af
ba bb bc bd be bf
ca cb cc cd ce cf
da db dc dd de df
ea eb ec ed ee ef

Its is very tiring process if the masking table changes, as i will need to go alot of places to edit the code. I must be able to access the other around also.

If i give ba and the condition "second" than i should be able to retrieve the columnName as 1.

So in other words all the data in column 1 will have a value of 1 no mather what the condition is.

Which will it be better ENUMs,List or other STD containers.

Please advice. And a sinnpet of how to access the data will be appreciated.

Thanks

Upvotes: 0

Views: 85

Answers (1)

You have two main problems here. One is the coding and second is method of access to data.

Regarding the data access, the best option would be pipe architecture. Best choice for that would be reading directly bytes from resource.

Your coding table is just matrix and can be stored in form of single array

char[] coding = {'','a','b','c','d','e',...};

int coder = nextCoder(); //3
int value = nextValue(); //5 

char x = coding[coder]; //c
char y = coding[value]; //e

For this example I assume that your input has only ANSII , this mean for input like 1234567 you will have to read row of byte[] 31,32,33,34,35,36,37,38.

byte[] input  =    {x31,x32,x33,x34,x35,x36,x37,x38};
byte[] coding = {x00,x61,x62,x63,x64,x65,...} //'','a','b','c','d','e'

What we can notice is that integers form 0 to 9 are coded from 30 to 39 and letters from a to e are coded from 61 to 66.

So for values like:

byte coder = nextCoder(); //3
byte value = nextValue(); //35 

We can use that arithmetic instead of arrays

byte x = 60 + coder; //63
byte y = 30 + value; //65

So finally to encode

byte[] line = new byte[lineLenght]; //We create our buffers. 
byte[] code = new byte(line.length*2);

while(hasMoreLines) {

  readLine(line);

  for(int i=0, j=0; i < line.length; i++) {
   code[j++] = x60 + coder;
   code[j++] = x30 + line[i];
  }
  writeLine(code);
}

For this example your pipe line requires only two byte arrays for storing input and output while processing.

Upvotes: 2

Related Questions