C++ int array test for quicksort

/***************************************************************************
 *   Copyright (C) 2010 by papp   *
*   vortex_32@freemail.hu
***************************************************************************/

//#ifdef HAVE_CONFIG_H
//#include <config.h>
//#endif

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

typedef int (*ptrAbstractComparer) (void* ptrA,void* ptrB);
typedef int (*ptrAbstractUnaryCallback) (void* ptrA);
typedef int (*ptrAbstractSwap) (void* ptrA,void* ptrB);

extern "C" {void quick_sort_ex(int stride,ptrAbstractComparer ptrComparer,void* ptr_u, void* ptr_v,ptrAbstractSwap ptrSwap);}

template <class T, int N>
class Array{
    T mArray [N];

  static int Swap(T*  ptrA,T* ptrB)
  {
     T tmp = *ptrA;
     *ptrA = *ptrB;
     *ptrB = tmp;
     return 1;
  }

  static int Swap(void*  ptrA,void* ptrB)
  {
    return Swap((T*) ptrA,(T*) ptrB);
  }

  static int Compare(T* ptrA,T* ptrB)       
  {
    if(*ptrA == *ptrB) 
       return 0;
    else if(*ptrA < *ptrB)
       return -1;
    else
       return 1;
    //return *ptrA-*ptrB; this is invalid for strings, etc.
  }
  static int Compare(void* ptrA,void* ptrB)  { return Compare((T*) ptrA,(T*) ptrB ); }

  public:

    Array(){}

    Array(T (&init)[N])
    {
    for(int i= 0;i<N;i++)
    {
       mArray[i] = init[i];
    }
    }
    T& operator[](const int Index)   { return mArray[Index]; }
    const int getLength(){return N;}
    void QuickSort()              {   quick_sort_ex(sizeof(T),&Compare ,&(mArray[0]),&(mArray[N-1]),&Swap); return; }
    void QuickSort(int i,int j)   {   quick_sort_ex(sizeof(T),&Compare ,&(mArray[i]),&(mArray[j]),&Swap); return; }
    void QuickSort(int (*Comparer) (T* ptrA,T* ptrB))             
    {  
       quick_sort_ex(sizeof(T),(ptrAbstractComparer) Comparer ,&(mArray[0]),&(mArray[N-1]),&Swap); return;
    }
    void QuickSort(int i,int j,int (*Comparer) (T* ptrA,T* ptrB))
    {
       quick_sort_ex(sizeof(T),(ptrAbstractComparer) Comparer ,&(mArray[i]),&(mArray[j]),&Swap); return;
    }

};

int intCompare(int* ptrA,int* ptrB) // this is our custom comparator
{
  if(ptrA && ptrB)
  {
    printf("compare %i with %i \n",*ptrA,*ptrB);
    return *ptrA-*ptrB;
  }
  else
  {
     return 1;
   }
}

int main(int argc, char *argv[])
{
  int init[] = {10,7,5,97,2,3,48,2};

  Array<int,8> a(init);

  a.QuickSort(&intCompare);

  int x;
  for(x=0;x<a.getLength();x++)
  {
    printf("%i",a[x]);
    printf("\n");    
  }

  return EXIT_SUCCESS;
}

Posted in Kategorizálatlan | Leave a comment

C++ interface 4 asm quicksort (type == string)

Posted in Kategorizálatlan | Leave a comment

C++ interface

Posted in Kategorizálatlan | Leave a comment

QuickSort in Assembly

Here is my own HAND MADE (assembly 32bit 8086) quicksort implementation in JPG!

(next time : C++ interface )

 

Posted in Számítógép és internet | Leave a comment

Ekranoplan

Posted in Utazás | Leave a comment

Olvasási gyakorlat

Ez egy jó cikk : Covariance and Contravariance in C# 4.0

Előtte ezt is érdemes elolvasni :
Liskov substitution principle ;meg ezt is ha már belejöttünk az olvasásba : Wiki (Ez utóbbi nekem annyira nem jött be, de vannak benne jó dolgok.)

Posted in C# | Leave a comment

Kolompeer swap without 3th variable

private void KolompeerSwap(ref int X,ref int Y)
{
X ^= Y;
Y ^= X;
X ^= Y;
}
Posted in C# | Leave a comment