inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/sort
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_sort
00005 #define inlib_sort
00006 
00007 #include "cstr"
00008 
00009 #include <vector>
00010 #include <string>
00011 
00012 extern "C" {
00013 
00014 // since extern "C" does not take into account namespace,
00015 // we prefix with inlib_
00016 
00017 typedef int (*inlib_sort_func_t)(const void*,const void*);
00018 
00019 }
00020 
00021 namespace inlib {
00022 namespace sort {
00023 
00024 inline int strlen_greater(char** a1,char** a2){
00025   size_t l1 = ::strlen(*a1);
00026   size_t l2 = ::strlen(*a2);
00027   if(l1==l2) return 0;
00028   return (l1>l2)?1:-1;
00029 }
00030 inline int strlen_lesser(char** a1,char** a2){
00031   size_t l1 = ::strlen(*a1);
00032   size_t l2 = ::strlen(*a2);
00033   if(l1==l2) return 0;
00034   return (l1<l2)?1:-1;
00035 }
00036 
00037 inline void strlen(std::vector<std::string>& a_text,bool a_increasing = true){
00038   unsigned int linen = a_text.size();
00039   if(linen<=1) return;
00040   char** lines = new char*[linen];
00041  {for(unsigned int i=0;i<linen;i++) 
00042     lines[i] = str_dup(a_text[i].c_str());}
00043   if(a_increasing) {
00044     ::qsort(lines,(size_t)linen,(size_t)sizeof(char*),
00045             (inlib_sort_func_t)strlen_greater);
00046   } else {
00047     ::qsort(lines,(size_t)linen,(size_t)sizeof(char*),
00048             (inlib_sort_func_t)strlen_lesser);
00049   }
00050   for(unsigned int i=0;i<linen;i++) {
00051     a_text[i] = lines[i];
00052     str_del(lines[i]);
00053   }
00054   delete [] lines;
00055 }
00056 
00057 template <class T>
00058 class item {
00059 public:
00060   T m_value;
00061 public:
00062   static int greater(item* a1,item* a2){
00063     if((*a1).m_value==(*a2).m_value) return 0;
00064     return ((*a1).m_value>(*a2).m_value)?1:-1;
00065   }
00066   static int lesser(item* a1,item* a2){
00067     if((*a1).m_value==(*a2).m_value) return 0;
00068     return ((*a1).m_value<(*a2).m_value)?1:-1;
00069   }
00070 };
00071 
00072 template <class T>
00073 inline void sort(std::vector<T>& a_vec,bool a_increasing = true) {
00074   unsigned int l = a_vec.size();
00075   if(l<=1) return;
00076   item<T>* items = new item<T>[l];
00077  {for(unsigned int i=0;i<l;i++) items[i].m_value = a_vec[i];}
00078 
00079   if(a_increasing) {
00080     ::qsort(items,(size_t)l,(size_t)sizeof(item<T>),
00081             (inlib_sort_func_t)item<T>::greater);
00082   } else {
00083     ::qsort(items,(size_t)l,(size_t)sizeof(item<T>),
00084             (inlib_sort_func_t)item<T>::lesser);
00085   }
00086 
00087  {for(unsigned int i=0;i<l;i++) a_vec[i] = items[i].m_value;}
00088 
00089   delete [] items;
00090 }
00091 
00092 }}
00093 
00094 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines