00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include "src/common/shared.hpp"
00019 #include "src/memory/memset.hpp"
00020 #include "src/memory/align.hpp"
00021 #include "src/memory/heap.hpp"
00022 #include "src/common/qsort.hpp"
00023 
00024 
00025 
00026 
00027 
00028 void element_swap(void *l1, void *l2, void *temp_buf, size_t size)
00029  {
00030         memmove(temp_buf,l1,size);
00031         memmove(l1,l2,size);
00032         memmove(l2,temp_buf,size);
00033  }
00034 
00035 
00036 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void*,const void*), void *temp_buf)
00037  {
00038 void *pivot, *less, *more, *last;
00039 void *buf;
00040         if (nmemb < 2) return;
00041 
00042         if (temp_buf == NULL)
00043                 buf=Memory::Heap::heap0.malloc(size, NO_ALIGN, FAC_OTHER|FAC_QSORTBUF, NULL);
00044         else
00045                 buf=temp_buf;
00046 
00047         less=pivot=base;
00048         more=last=(void*)((uint32)base+(nmemb-1)*size);
00049 
00050         while (less < more)
00051          {
00052                 while (compar(pivot,less) >= 0) {
00053                         if (less == last) break;
00054                         less=(void*)((uint32)less+size); }
00055                 while (compar(pivot,more) < 0)
00056                         more=(void*)((uint32)more-size);
00057                 if (less < more)
00058                         element_swap(less,more,buf,size);
00059          }
00060                 if (less < more)
00061                 {element_swap(pivot,less,buf,size); pivot=less;}
00062                 else
00063                 {element_swap(pivot,more,buf,size); pivot=more;}
00064 
00065         qsort(base,((uint32)pivot-(uint32)base)/size,size,compar, buf);
00066         qsort((void*)((uint32)pivot+size),nmemb-1-(((uint32)pivot-(uint32)base)/size),size,compar, buf);
00067 
00068         if (temp_buf==NULL)
00069                 Memory::Heap::heap0.free(buf);
00070  }
00071 
00072 
00073 int intcompar(const void *v1, const void *v2) {
00074         if (*(int*)v1 > *(int*)v2)
00075                 return 1;
00076         if (*(int*)v1 < *(int*)v2)
00077                 return -1;
00078         return 0;
00079  }
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087