Drizzled Public API Documentation

ptr_cmp.cc

00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /*
00017   get_ptr_compare(len) returns a pointer to a optimal byte-compare function
00018   for a array of stringpointer where all strings have size len.
00019   The bytes are compare as unsigned chars.
00020   */
00021 
00022 #include <config.h>
00023 #include <drizzled/internal/my_sys.h>
00024 
00025 #include <assert.h>
00026 
00027 #include <plugin/myisam/myisampack.h>
00028 
00029 namespace drizzled
00030 {
00031 namespace internal
00032 {
00033 
00034 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b);
00035 static int ptr_compare_0(size_t *compare_length, unsigned char **a, unsigned char **b);
00036 static int ptr_compare_1(size_t *compare_length, unsigned char **a, unsigned char **b);
00037 static int ptr_compare_2(size_t *compare_length, unsigned char **a, unsigned char **b);
00038 static int ptr_compare_3(size_t *compare_length, unsigned char **a, unsigned char **b);
00039 
00040   /* Get a pointer to a optimal byte-compare function for a given size */
00041 
00042 qsort2_cmp get_ptr_compare (size_t size)
00043 {
00044   if (size < 4)
00045     return (qsort2_cmp) ptr_compare;
00046   switch (size & 3) {
00047     case 0: return (qsort2_cmp) ptr_compare_0;
00048     case 1: return (qsort2_cmp) ptr_compare_1;
00049     case 2: return (qsort2_cmp) ptr_compare_2;
00050     case 3: return (qsort2_cmp) ptr_compare_3;
00051     }
00052   return 0;         /* Impossible */
00053 }
00054 
00055 
00056   /*
00057     Compare two keys to see which is smaller.
00058     Loop unrolled to make it quick !!
00059   */
00060 
00061 #define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
00062 
00063 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b)
00064 {
00065   int length= *compare_length;
00066   unsigned char *first,*last;
00067 
00068   first= *a; last= *b;
00069   while (--length)
00070   {
00071     if (*first++ != *last++)
00072       return (int) first[-1] - (int) last[-1];
00073   }
00074   return (int) first[0] - (int) last[0];
00075 }
00076 
00077 
00078 static int ptr_compare_0(size_t *compare_length,unsigned char **a, unsigned char **b)
00079 {
00080   int length= *compare_length;
00081   unsigned char *first,*last;
00082 
00083   first= *a; last= *b;
00084  loop:
00085   cmp(0);
00086   cmp(1);
00087   cmp(2);
00088   cmp(3);
00089   if ((length-=4))
00090   {
00091     first+=4;
00092     last+=4;
00093     goto loop;
00094   }
00095   return (0);
00096 }
00097 
00098 
00099 static int ptr_compare_1(size_t *compare_length,unsigned char **a, unsigned char **b)
00100 {
00101   int length= *compare_length-1;
00102   unsigned char *first,*last;
00103 
00104   first= *a+1; last= *b+1;
00105   cmp(-1);
00106  loop:
00107   cmp(0);
00108   cmp(1);
00109   cmp(2);
00110   cmp(3);
00111   if ((length-=4))
00112   {
00113     first+=4;
00114     last+=4;
00115     goto loop;
00116   }
00117   return (0);
00118 }
00119 
00120 static int ptr_compare_2(size_t *compare_length,unsigned char **a, unsigned char **b)
00121 {
00122   int length= *compare_length-2;
00123   unsigned char *first,*last;
00124 
00125   first= *a +2 ; last= *b +2;
00126   cmp(-2);
00127   cmp(-1);
00128  loop:
00129   cmp(0);
00130   cmp(1);
00131   cmp(2);
00132   cmp(3);
00133   if ((length-=4))
00134   {
00135     first+=4;
00136     last+=4;
00137     goto loop;
00138   }
00139   return (0);
00140 }
00141 
00142 static int ptr_compare_3(size_t *compare_length,unsigned char **a, unsigned char **b)
00143 {
00144   int length= *compare_length-3;
00145   unsigned char *first,*last;
00146 
00147   first= *a +3 ; last= *b +3;
00148   cmp(-3);
00149   cmp(-2);
00150   cmp(-1);
00151  loop:
00152   cmp(0);
00153   cmp(1);
00154   cmp(2);
00155   cmp(3);
00156   if ((length-=4))
00157   {
00158     first+=4;
00159     last+=4;
00160     goto loop;
00161   }
00162   return (0);
00163 }
00164 
00165 void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos)
00166 {
00167   switch (pack_length) {
00168 #if SIZEOF_OFF_T > 4
00169   case 8: mi_int8store(buff,pos); break;
00170   case 7: mi_int7store(buff,pos); break;
00171   case 6: mi_int6store(buff,pos); break;
00172   case 5: mi_int5store(buff,pos); break;
00173 #endif
00174   case 4: mi_int4store(buff,pos); break;
00175   case 3: mi_int3store(buff,pos); break;
00176   case 2: mi_int2store(buff,pos); break;
00177   case 1: buff[0]= (unsigned char) pos; break;
00178   default: assert(0);
00179   }
00180   return;
00181 }
00182 
00183 my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length)
00184 {
00185   my_off_t pos;
00186   switch (pack_length) {
00187 #if SIZEOF_OFF_T > 4
00188   case 8: pos= (my_off_t) mi_uint8korr(ptr); break;
00189   case 7: pos= (my_off_t) mi_uint7korr(ptr); break;
00190   case 6: pos= (my_off_t) mi_uint6korr(ptr); break;
00191   case 5: pos= (my_off_t) mi_uint5korr(ptr); break;
00192 #endif
00193   case 4: pos= (my_off_t) mi_uint4korr(ptr); break;
00194   case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
00195   case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
00196   case 1: pos= (my_off_t) *(unsigned char*) ptr; break;
00197   default: assert(0); return 0;
00198   }
00199  return pos;
00200 }
00201 
00202 } /* namespace internal */
00203 } /* namespace drizzled */