Friday, August 3, 2012

Making C++ available through native calls

So if you need some raw of performance in your application what do you do? You drop down into a native development language like C. Well what if you want to code with OOP goodness and use C++. Regular calls from Java, Ruby, etc. will not get to your C++ functions because C++ adds all sorts of template madness when it exports your programs symbols.

So what do you do?


extern "C"

so if your want to wrap your c++ classes in a single method called helloWorld (or MYLIB_helloWorld to not be confused with other helloWorlds!):
extern "C" MYLIB_helloWorld();
in your header file.....then in your cpp file you can just do the normal
MYLIB_helloWorld();
Yay! Now you can make native calls from higher level languages like java or even ruby without them getting pissed off at the c++ template symbols. Will post examples soon.

Cygwin and SSH server

To setup SSH on windows using cygwin....first install cygwin! Make sure you check off the ssh package. Then open up the cygwin shell and do the following:

ssh-host-config -y

when prompted with the CYGWIN=
tty ntsec
and then finally
cygrunsrv -S sshd
You might hit ERRORS wither on first start or 2 years later! Most common that I've run into are:
  • Win32 error 1069: The service did not start due to a logon failure.
  • Error starting a service: QueryServiceStatus: Win32 error 1062: The service has not been started.
For the first one go to services in windows and lookup CYGWINsshd. In properties under "log on" tab make sure your user (usually cyg_server) AND password match the on in your cygwin. So type
passwd cyg_server
in your cygwin shell to change it ... For the second problem just repeat the steps from STEP one to reconfigure.. Cheers!

Wednesday, July 18, 2012

String Palindrome Algorithm

The last of these old school algorithms. Funny how trivial they should be after all these years but they still trip me up. Hence these posts and practices!

class Pal {



 public static void main(String args[]) {

  //System.out.println(findPalIter(args[0]));
  System.out.println(findPalRec(args[0]));


 }



 public static boolean findPalIter(String str) {

  char[] orig = str.toCharArray();
  char[] backward = new char[orig.length];

  int j=0;
  for( int i=orig.length-1; i >=0; i-- ) {


   backward[j++] = orig[i];



  }

  if( str.equals( new String(backward)))
   return true;


  return false;




 }


 private static boolean findPalRec(String str) {

  char[] chars = str.toCharArray();

  if( chars.length == 0 || chars.length == 1 ) 
   return true;

  if( chars[0] == chars[chars.length-1])
   return findPalRec( str.substring( 1, chars.length-1 ) );
  else
   return false;

 }







}


https://github.com/asharif/StringPalindrome

String Anagram Algorithm

As a continuation of the last post, here is an algorithm for finding anagrams from a string
public class Anag {



        public static void main(String args[]){

                char[] anag = new char[args[0].length()];
                findAnag(args[0], new String(anag), 0);




        }



        public static void findAnag(String orig, String anag, int index) {
                if ( orig.toCharArray().length == 0 ) {

                        System.out.println( anag );
                        return;


                }

                char[] origChars = orig.toCharArray();
                char[] anagChars = anag.toCharArray();

                for( int i=0; i < origChars.length; i++ ) {

                        anagChars[index] = origChars[i];
                        index++;
                        String strStillLeft = orig.substring(0, i) + orig.substring(i+1, orig.length());
                        findAnag( strStillLeft, new String(anagChars), index);
                        index--;

                }




        }











}

https://github.com/asharif/StringAnag

String Combination Algorithm

Just needed to brush up some old but important algorithms so I thought I'd post them here and on github for my own future reference and anyone elses. This example I did in Java:

class Combo {



        public static void main ( String args[] ) {

                char[] combos = new char[args[0].length()];
                findCombos ( args[0], new String(combos), 0 );

        }



        private static void findCombos( String str, String combos, int index ) {

                char[] comboChars = combos.toCharArray();


                if ( index == str.length()) {

                        System.out.println( new String(comboChars) );

                        return;

                }

                for ( int i = 0; i < str.length(); i++ ) {
                        char[] strChars = str.toCharArray();
                        comboChars[index] = strChars[i];
                        index++;
                        findCombos(str, new String(comboChars), index);
                        index--;

                }




        }


}

Here it is on github: https://github.com/asharif/StringCombo

Monday, July 16, 2012

packit4me.com (2D/3D Bin Packing)

(UPDATE 06/04/2013) This project is now available @ http://www.packit4me.com/
3D/2D bin packing. Given multiple bins and items to be packed into the bins, the algorithm attempts to find a good solutions of how to pack. I got so many requests for this that I made it into a real life project. Check out the site. It has comprehensive documentation on how to call the API with all the popular languages (Java, php, C#, ruby, python). I hope it works for you! If not just email me or make some posts here. I'll try to setup some ticket system soon to take requests.

Friday, March 2, 2012