none
Better Performance Solution for custom class Matrix RRS feed

  • Question

  • 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

    Wednesday, May 20, 2020 3:57 AM

Answers

  • Solution Update ( partially based on Bresenham's line algorithm )

    public string MakeBitString( int x1, int y1, int x2, int y2, int count )
    {
    	x1 &= 7;
    	x2 &= 7;
    	y1 &= 7;
    	y2 &= 7;
    
    	string temp = "";
    
    	int xc = ( x1 < x2 ? 1 : ( x1 > x2 ? -1 : 0 ) );
    	int yc = ( y1 < y2 ? 1 : ( y1 > y2 ? -1 : 0 ) );
    
    	do
    	{
    		temp += ( this._matrix[ x1, y1 ] == true ? "1" : "0" );
    		x1 += xc;
    		y1 += yc;
    		count--;
    
    	} while ( count > 0 );
    
    	return temp;
    }

     The above code works correctly for my purpose and allows for future upgrades along with

    base class Matrix.  Not entirely what I wanted but the following code is how my project

    implemented its usage.

    public string[] GetTransBits()
    {
    	List<string> strList = new List<string>();
    
    	strList.Add( this.MakeBitString( 0, 0, 0, 0, 1 ) );
    	strList.Add( this.MakeBitString( 0, 1, 1, 0, 2 ) );
    	strList.Add( this.MakeBitString( 0, 2, 2, 0, 3 ) );
    	strList.Add( this.MakeBitString( 0, 3, 3, 0, 4 ) );
    	strList.Add( this.MakeBitString( 0, 4, 4, 0, 5 ) );
    	strList.Add( this.MakeBitString( 0, 5, 5, 0, 6 ) );
    	strList.Add( this.MakeBitString( 0, 6, 6, 0, 7 ) );
    
    	strList.Add( this.MakeBitString( 0, 7, 7, 0, 8 ) );
    
    	strList.Add( this.MakeBitString( 1, 7, 7, 1, 7 ) );
    	strList.Add( this.MakeBitString( 2, 7, 7, 2, 6 ) );
    	strList.Add( this.MakeBitString( 3, 7, 7, 3, 5 ) );
    	strList.Add( this.MakeBitString( 4, 7, 7, 4, 4 ) );
    	strList.Add( this.MakeBitString( 5, 7, 7, 5, 3 ) );
    	strList.Add( this.MakeBitString( 6, 7, 7, 6, 2 ) );
    	strList.Add( this.MakeBitString( 7, 7, 7, 7, 1 ) );
    
    	return strList.ToArray();
    }

     Thanks for all suggestions and replies. :)

    • Marked as answer by User3DX Saturday, May 23, 2020 8:39 PM
    Saturday, May 23, 2020 8:39 PM

All replies

  • Hi User3DX,

    Thank you for posting here.

    I replaced these codes with loops.

    In order to see the results more intuitively, each value of the two-dimensional array corresponds to its subscript.

                byte[,] mapping = new byte[8, 8] { { 00,01,02,03,04,05,06,07 },
                                                   { 10,11,12,13,14,15,16,17 },
                                                   { 20,21,22,23,24,25,26,27 },
                                                   { 30,31,32,33,34,35,36,37 },
                                                   { 40,41,42,43,44,45,46,47 },
                                                   { 50,51,52,53,54,55,56,57 },
                                                   { 60,61,62,63,64,65,66,67 },
                                                   { 70,71,72,73,74,75,76,77 },
                };
               
    
                List<string> strs = new List<string>();
    
                for (int i = 0; i < mapping.GetLength(0); i++)
                {
                  
                    if (i<7)
                    {
                        string next = "";
                        for (int j = 0; j <= i; j++)
                        {
                            next += mapping[i - j, j] + " ";
                        }
                        strs.Add(next);
                    }
                    else
                    {
                     
                        for (int k = 0; k < mapping.GetLength(1); k++)
                        {
                            string next = "";
                            int sum = i + k;
                            for (int j = k; j <= i; j++)
                            {
                                next += mapping[sum-j, j] + " ";
                            }
                            strs.Add(next);
                        }
                    }
                }
    
                foreach (var item in strs)
                {
                    Console.WriteLine(item);
                }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 20, 2020 9:56 AM
  • Hello,

     Thank you for the replay and very useful code. However, in your example

    there is still hard-coded data. I would rather not as for a future upgrade,

    from 8x8 to ?x? dimension.  I know from looking at the pattern of

    coordinates that it looks simple. The most I have figured was 3 value

    objects with +/- changes to row and column.

     

     Thanks :)

    Wednesday, May 20, 2020 5:30 PM
  • Hi,

    I made changes to the previous code, and now it can adapt to more situations.

            static void Main(string[] args)
            {
    
                byte[,] mapping = new byte[9, 8] { { 00,01,02,03,04,05,06,07 },
                                                   { 10,11,12,13,14,15,16,17 },
                                                   { 20,21,22,23,24,25,26,27 },
                                                   { 30,31,32,33,34,35,36,37},
                                                   { 40,41,42,43,44,45,46,47 },
                                                   { 50,51,52,53,54,55,56,57 },
                                                   { 60,61,62,63,64,65,66,67},
                                                   { 70,71,72,73,74,75,76,77 },
                                                   { 80,81,82,83,84,85,86,87 }
                };
    
                List<string> res = DoWork(mapping);
                foreach (var item in res)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
            public static List<string> DoWork(byte[,] arr)
            {
                List<string> strs = new List<string>();
                if (arr == null)
                {
                    throw new Exception("arr cannot be null");
                }
           
                int row = arr.GetLength(0);
                int col = arr.GetLength(1);
                for (int k = 0; k <= col * 2 - 1; k++)
                {     
                    int sum = k;
                    string next = "";
                    for (int j = 0; j < col; j++)
                    {
                        int i = sum - j;
                        if (IsValidIndex(i, row) && IsValidIndex(j, col))
                        {
                           next+= arr[i,j] + " ";
                        }
                    }
                    strs.Add(next);
                }
                return strs;
            }
            private static bool IsValidIndex(int i, int n)
            {
                return i >= 0 && i < n;
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 21, 2020 5:45 AM
  • Solution Update ( partially based on Bresenham's line algorithm )

    public string MakeBitString( int x1, int y1, int x2, int y2, int count )
    {
    	x1 &= 7;
    	x2 &= 7;
    	y1 &= 7;
    	y2 &= 7;
    
    	string temp = "";
    
    	int xc = ( x1 < x2 ? 1 : ( x1 > x2 ? -1 : 0 ) );
    	int yc = ( y1 < y2 ? 1 : ( y1 > y2 ? -1 : 0 ) );
    
    	do
    	{
    		temp += ( this._matrix[ x1, y1 ] == true ? "1" : "0" );
    		x1 += xc;
    		y1 += yc;
    		count--;
    
    	} while ( count > 0 );
    
    	return temp;
    }

     The above code works correctly for my purpose and allows for future upgrades along with

    base class Matrix.  Not entirely what I wanted but the following code is how my project

    implemented its usage.

    public string[] GetTransBits()
    {
    	List<string> strList = new List<string>();
    
    	strList.Add( this.MakeBitString( 0, 0, 0, 0, 1 ) );
    	strList.Add( this.MakeBitString( 0, 1, 1, 0, 2 ) );
    	strList.Add( this.MakeBitString( 0, 2, 2, 0, 3 ) );
    	strList.Add( this.MakeBitString( 0, 3, 3, 0, 4 ) );
    	strList.Add( this.MakeBitString( 0, 4, 4, 0, 5 ) );
    	strList.Add( this.MakeBitString( 0, 5, 5, 0, 6 ) );
    	strList.Add( this.MakeBitString( 0, 6, 6, 0, 7 ) );
    
    	strList.Add( this.MakeBitString( 0, 7, 7, 0, 8 ) );
    
    	strList.Add( this.MakeBitString( 1, 7, 7, 1, 7 ) );
    	strList.Add( this.MakeBitString( 2, 7, 7, 2, 6 ) );
    	strList.Add( this.MakeBitString( 3, 7, 7, 3, 5 ) );
    	strList.Add( this.MakeBitString( 4, 7, 7, 4, 4 ) );
    	strList.Add( this.MakeBitString( 5, 7, 7, 5, 3 ) );
    	strList.Add( this.MakeBitString( 6, 7, 7, 6, 2 ) );
    	strList.Add( this.MakeBitString( 7, 7, 7, 7, 1 ) );
    
    	return strList.ToArray();
    }

     Thanks for all suggestions and replies. :)

    • Marked as answer by User3DX Saturday, May 23, 2020 8:39 PM
    Saturday, May 23, 2020 8:39 PM