Better Performance Solution for custom class Matrix

  • Thread starter Thread starter User3DX
  • Start date Start date
U

User3DX

Guest
Hello,


The follow snippet has hard coded section that I would really prefer loop use.

Code

public void Transcode()
{
byte[ , ] mapping = new byte[ 8, 8 ];

int y = 0;

foreach ( Binary.Digit bd in this._matrix )
{
for ( int x = 0; x < 8; x++ )
{
mapping[ y, x ] = (byte) ( bd.GetBit( x ) == false ? 0 : 1 );
}

y++;
}

string next = "";

// Looking for better solution to
// the following lines of code
//
next = mapping[ 0, 0 ].ToString();
this.CheckDictionary( next );

next = mapping[ 1, 0 ].ToString() + mapping[ 0, 1 ].ToString();
this.CheckDictionary( next );

next = mapping[ 2, 0 ].ToString() + mapping[ 1, 1 ].ToString() + mapping[ 0, 2 ];
this.CheckDictionary( next );

next = mapping[ 3, 0 ].ToString() + mapping[ 2, 1 ].ToString() + mapping[ 1, 2 ] + mapping[ 0, 3 ];
this.CheckDictionary( next );

next = mapping[ 4, 0 ].ToString() + mapping[ 3, 1 ].ToString() + mapping[ 2, 2 ] + mapping[ 1, 3 ] + mapping[ 0, 4 ];
this.CheckDictionary( next );

next = mapping[ 5, 0 ].ToString() + mapping[ 4, 1 ].ToString() + mapping[ 3, 2 ] + mapping[ 2, 3 ] + mapping[ 1, 4 ] + mapping[ 0, 5 ];
this.CheckDictionary( next );

next = mapping[ 6, 0 ].ToString() + mapping[ 5, 1 ].ToString() + mapping[ 4, 2 ] + mapping[ 3, 3 ] + mapping[ 2, 4 ] + mapping[ 1, 5 ] + mapping[ 0, 6 ];
this.CheckDictionary( next );

next = mapping[ 7, 0 ].ToString() + mapping[ 6, 1 ].ToString() + mapping[ 5, 2 ] + mapping[ 4, 3 ] + mapping[ 3, 4 ] + mapping[ 2, 5 ] + mapping[ 1, 6 ] + mapping[ 0, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 1 ].ToString() + mapping[ 6, 2 ].ToString() + mapping[ 5, 3 ] + mapping[ 4, 4 ] + mapping[ 3, 5 ] + mapping[ 2, 6 ] + mapping[ 1, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 2 ].ToString() + mapping[ 6, 3 ].ToString() + mapping[ 5, 4 ] + mapping[ 4, 5 ] + mapping[ 3, 6 ] + mapping[ 2, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 3 ].ToString() + mapping[ 6, 4 ].ToString() + mapping[ 5, 5 ] + mapping[ 4, 6 ] + mapping[ 3, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 4 ].ToString() + mapping[ 6, 5 ].ToString() + mapping[ 5, 6 ] + mapping[ 4, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 5 ].ToString() + mapping[ 6, 6 ].ToString() + mapping[ 5, 7 ];
this.CheckDictionary( next );

next = mapping[ 7, 6 ].ToString() + mapping[ 6, 7 ].ToString();
this.CheckDictionary( next );

next = mapping[ 7, 7 ].ToString();
this.CheckDictionary( next );
}

private void CheckDictionary( string str )
{
if ( this._dictionary.ContainsKey( str ) )
{
this._dictionary[ str ]++;

return;
}

this._dictionary.Add( str, 1 );
}
If anyone can help then its very appreciated. Thanks

Continue reading...
 
Back
Top