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 namespace internal {
00031 
00032 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b);
00033 static int ptr_compare_0(size_t *compare_length, unsigned char **a, unsigned char **b);
00034 static int ptr_compare_1(size_t *compare_length, unsigned char **a, unsigned char **b);
00035 static int ptr_compare_2(size_t *compare_length, unsigned char **a, unsigned char **b);
00036 static int ptr_compare_3(size_t *compare_length, unsigned char **a, unsigned char **b);
00037 
00038   /* Get a pointer to a optimal byte-compare function for a given size */
00039 
00040 qsort2_cmp get_ptr_compare (size_t size)
00041 {
00042   if (size < 4)
00043     return (qsort2_cmp) ptr_compare;
00044   switch (size & 3) {
00045     case 0: return (qsort2_cmp) ptr_compare_0;
00046     case 1: return (qsort2_cmp) ptr_compare_1;
00047     case 2: return (qsort2_cmp) ptr_compare_2;
00048     case 3: return (qsort2_cmp) ptr_compare_3;
00049     }
00050   return 0;         /* Impossible */
00051 }
00052 
00053 
00054   /*
00055     Compare two keys to see which is smaller.
00056     Loop unrolled to make it quick !!
00057   */
00058 
00059 #define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
00060 
00061 static int ptr_compare(size_t *compare_length, unsigned char **a, unsigned char **b)
00062 {
00063   int length= *compare_length;
00064   unsigned char *first,*last;
00065 
00066   first= *a; last= *b;
00067   while (--length)
00068   {
00069     if (*first++ != *last++)
00070       return (int) first[-1] - (int) last[-1];
00071   }
00072   return (int) first[0] - (int) last[0];
00073 }
00074 
00075 
00076 static int ptr_compare_0(size_t *compare_length,unsigned char **a, unsigned char **b)
00077 {
00078   int length= *compare_length;
00079   unsigned char *first,*last;
00080 
00081   first= *a; last= *b;
00082  loop:
00083   cmp(0);
00084   cmp(1);
00085   cmp(2);
00086   cmp(3);
00087   if ((length-=4))
00088   {
00089     first+=4;
00090     last+=4;
00091     goto loop;
00092   }
00093   return (0);
00094 }
00095 
00096 
00097 static int ptr_compare_1(size_t *compare_length,unsigned char **a, unsigned char **b)
00098 {
00099   int length= *compare_length-1;
00100   unsigned char *first,*last;
00101 
00102   first= *a+1; last= *b+1;
00103   cmp(-1);
00104  loop:
00105   cmp(0);
00106   cmp(1);
00107   cmp(2);
00108   cmp(3);
00109   if ((length-=4))
00110   {
00111     first+=4;
00112     last+=4;
00113     goto loop;
00114   }
00115   return (0);
00116 }
00117 
00118 static int ptr_compare_2(size_t *compare_length,unsigned char **a, unsigned char **b)
00119 {
00120   int length= *compare_length-2;
00121   unsigned char *first,*last;
00122 
00123   first= *a +2 ; last= *b +2;
00124   cmp(-2);
00125   cmp(-1);
00126  loop:
00127   cmp(0);
00128   cmp(1);
00129   cmp(2);
00130   cmp(3);
00131   if ((length-=4))
00132   {
00133     first+=4;
00134     last+=4;
00135     goto loop;
00136   }
00137   return (0);
00138 }
00139 
00140 static int ptr_compare_3(size_t *compare_length,unsigned char **a, unsigned char **b)
00141 {
00142   int length= *compare_length-3;
00143   unsigned char *first,*last;
00144 
00145   first= *a +3 ; last= *b +3;
00146   cmp(-3);
00147   cmp(-2);
00148   cmp(-1);
00149  loop:
00150   cmp(0);
00151   cmp(1);
00152   cmp(2);
00153   cmp(3);
00154   if ((length-=4))
00155   {
00156     first+=4;
00157     last+=4;
00158     goto loop;
00159   }
00160   return (0);
00161 }
00162 
00163 void my_store_ptr(unsigned char *buff, size_t pack_length, my_off_t pos)
00164 {
00165   switch (pack_length) {
00166 #if SIZEOF_OFF_T > 4
00167   case 8: mi_int8store(buff,pos); break;
00168   case 7: mi_int7store(buff,pos); break;
00169   case 6: mi_int6store(buff,pos); break;
00170   case 5: mi_int5store(buff,pos); break;
00171 #endif
00172   case 4: mi_int4store(buff,pos); break;
00173   case 3: mi_int3store(buff,pos); break;
00174   case 2: mi_int2store(buff,pos); break;
00175   case 1: buff[0]= (unsigned char) pos; break;
00176   default: assert(0);
00177   }
00178   return;
00179 }
00180 
00181 my_off_t my_get_ptr(unsigned char *ptr, size_t pack_length)
00182 {
00183   my_off_t pos;
00184   switch (pack_length) {
00185 #if SIZEOF_OFF_T > 4
00186   case 8: pos= (my_off_t) mi_uint8korr(ptr); break;
00187   case 7: pos= (my_off_t) mi_uint7korr(ptr); break;
00188   case 6: pos= (my_off_t) mi_uint6korr(ptr); break;
00189   case 5: pos= (my_off_t) mi_uint5korr(ptr); break;
00190 #endif
00191   case 4: pos= (my_off_t) mi_uint4korr(ptr); break;
00192   case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
00193   case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
00194   case 1: pos= (my_off_t) *(unsigned char*) ptr; break;
00195   default: assert(0); return 0;
00196   }
00197  return pos;
00198 }
00199 
00200 } /* namespace internal */
00201 } /* namespace drizzled */