Back to index

openldap  2.4.31
midl.c
Go to the documentation of this file.
00001 
00003 /* $OpenLDAP$ */
00004 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00005  *
00006  * Copyright 2000-2012 The OpenLDAP Foundation.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 
00018 #include <limits.h>
00019 #include <string.h>
00020 #include <stdlib.h>
00021 #include <sys/types.h>
00022 #include <assert.h>
00023 #include "midl.h"
00024 
00031 #define CMP(x,y)      ( (x) < (y) ? -1 : (x) > (y) )
00032 
00033 #if 0  /* superseded by append/sort */
00034 static unsigned mdb_midl_search( IDL ids, ID id )
00035 {
00036        /*
00037         * binary search of id in ids
00038         * if found, returns position of id
00039         * if not found, returns first position greater than id
00040         */
00041        unsigned base = 0;
00042        unsigned cursor = 1;
00043        int val = 0;
00044        unsigned n = ids[0];
00045 
00046        while( 0 < n ) {
00047               unsigned pivot = n >> 1;
00048               cursor = base + pivot + 1;
00049               val = CMP( ids[cursor], id );
00050 
00051               if( val < 0 ) {
00052                      n = pivot;
00053 
00054               } else if ( val > 0 ) {
00055                      base = cursor;
00056                      n -= pivot + 1;
00057 
00058               } else {
00059                      return cursor;
00060               }
00061        }
00062        
00063        if( val > 0 ) {
00064               ++cursor;
00065        }
00066        return cursor;
00067 }
00068 
00069 int mdb_midl_insert( IDL ids, ID id )
00070 {
00071        unsigned x, i;
00072 
00073        if (MDB_IDL_IS_RANGE( ids )) {
00074               /* if already in range, treat as a dup */
00075               if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
00076                      return -1;
00077               if (id < MDB_IDL_RANGE_FIRST(ids))
00078                      ids[1] = id;
00079               else if (id > MDB_IDL_RANGE_LAST(ids))
00080                      ids[2] = id;
00081               return 0;
00082        }
00083 
00084        x = mdb_midl_search( ids, id );
00085        assert( x > 0 );
00086 
00087        if( x < 1 ) {
00088               /* internal error */
00089               return -2;
00090        }
00091 
00092        if ( x <= ids[0] && ids[x] == id ) {
00093               /* duplicate */
00094               assert(0);
00095               return -1;
00096        }
00097 
00098        if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
00099               if( id < ids[1] ) {
00100                      ids[1] = id;
00101                      ids[2] = ids[ids[0]-1];
00102               } else if ( ids[ids[0]-1] < id ) {
00103                      ids[2] = id;
00104               } else {
00105                      ids[2] = ids[ids[0]-1];
00106               }
00107               ids[0] = NOID;
00108        
00109        } else {
00110               /* insert id */
00111               for (i=ids[0]; i>x; i--)
00112                      ids[i] = ids[i-1];
00113               ids[x] = id;
00114        }
00115 
00116        return 0;
00117 }
00118 #endif
00119 
00120 IDL mdb_midl_alloc()
00121 {
00122        IDL ids = malloc((MDB_IDL_UM_MAX+1) * sizeof(ID));
00123        *ids++ = MDB_IDL_UM_MAX;
00124        return ids;
00125 }
00126 
00127 void mdb_midl_free(IDL ids)
00128 {
00129        free(ids-1);
00130 }
00131 
00132 int mdb_midl_shrink( IDL *idp )
00133 {
00134        IDL ids = *idp;
00135        if (ids[-1] > MDB_IDL_UM_MAX) {
00136               ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(ID));
00137               *ids++ = MDB_IDL_UM_MAX;
00138               *idp = ids;
00139               return 1;
00140        }
00141        return 0;
00142 }
00143 
00144 int mdb_midl_append( IDL *idp, ID id )
00145 {
00146        IDL ids = *idp;
00147        /* Too big? */
00148        if (ids[0] >= ids[-1]) {
00149               IDL idn = ids-1;
00150               /* grow it */
00151               idn = realloc(idn, (*idn + MDB_IDL_UM_MAX + 1) * sizeof(ID));
00152               if (!idn)
00153                      return -1;
00154               *idn++ += MDB_IDL_UM_MAX;
00155               ids = idn;
00156               *idp = ids;
00157        }
00158        ids[0]++;
00159        ids[ids[0]] = id;
00160        return 0;
00161 }
00162 
00163 int mdb_midl_append_list( IDL *idp, IDL app )
00164 {
00165        IDL ids = *idp;
00166        /* Too big? */
00167        if (ids[0] + app[0] >= ids[-1]) {
00168               IDL idn = ids-1;
00169               /* grow it */
00170               idn = realloc(idn, (*idn + app[-1]) * sizeof(ID));
00171               if (!idn)
00172                      return -1;
00173               *idn++ += app[-1];
00174               ids = idn;
00175               *idp = ids;
00176        }
00177        memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(ID));
00178        ids[0] += app[0];
00179        return 0;
00180 }
00181 
00182 /* Quicksort + Insertion sort for small arrays */
00183 
00184 #define SMALL 8
00185 #define       SWAP(a,b)     { itmp=(a); (a)=(b); (b)=itmp; }
00186 
00187 void
00188 mdb_midl_sort( IDL ids )
00189 {
00190        /* Max possible depth of int-indexed tree * 2 items/level */
00191        int istack[sizeof(int)*CHAR_BIT * 2];
00192        int i,j,k,l,ir,jstack;
00193        ID a, itmp;
00194 
00195        ir = ids[0];
00196        l = 1;
00197        jstack = 0;
00198        for(;;) {
00199               if (ir - l < SMALL) {       /* Insertion sort */
00200                      for (j=l+1;j<=ir;j++) {
00201                             a = ids[j];
00202                             for (i=j-1;i>=1;i--) {
00203                                    if (ids[i] >= a) break;
00204                                    ids[i+1] = ids[i];
00205                             }
00206                             ids[i+1] = a;
00207                      }
00208                      if (jstack == 0) break;
00209                      ir = istack[jstack--];
00210                      l = istack[jstack--];
00211               } else {
00212                      k = (l + ir) >> 1;   /* Choose median of left, center, right */
00213                      SWAP(ids[k], ids[l+1]);
00214                      if (ids[l] < ids[ir]) {
00215                             SWAP(ids[l], ids[ir]);
00216                      }
00217                      if (ids[l+1] < ids[ir]) {
00218                             SWAP(ids[l+1], ids[ir]);
00219                      }
00220                      if (ids[l] < ids[l+1]) {
00221                             SWAP(ids[l], ids[l+1]);
00222                      }
00223                      i = l+1;
00224                      j = ir;
00225                      a = ids[l+1];
00226                      for(;;) {
00227                             do i++; while(ids[i] > a);
00228                             do j--; while(ids[j] < a);
00229                             if (j < i) break;
00230                             SWAP(ids[i],ids[j]);
00231                      }
00232                      ids[l+1] = ids[j];
00233                      ids[j] = a;
00234                      jstack += 2;
00235                      if (ir-i+1 >= j-1) {
00236                             istack[jstack] = ir;
00237                             istack[jstack-1] = i;
00238                             ir = j-1;
00239                      } else {
00240                             istack[jstack] = j-1;
00241                             istack[jstack-1] = l;
00242                             l = i;
00243                      }
00244               }
00245        }
00246 }
00247 
00248 unsigned mdb_mid2l_search( ID2L ids, ID id )
00249 {
00250        /*
00251         * binary search of id in ids
00252         * if found, returns position of id
00253         * if not found, returns first position greater than id
00254         */
00255        unsigned base = 0;
00256        unsigned cursor = 1;
00257        int val = 0;
00258        unsigned n = ids[0].mid;
00259 
00260        while( 0 < n ) {
00261               unsigned pivot = n >> 1;
00262               cursor = base + pivot + 1;
00263               val = CMP( id, ids[cursor].mid );
00264 
00265               if( val < 0 ) {
00266                      n = pivot;
00267 
00268               } else if ( val > 0 ) {
00269                      base = cursor;
00270                      n -= pivot + 1;
00271 
00272               } else {
00273                      return cursor;
00274               }
00275        }
00276 
00277        if( val > 0 ) {
00278               ++cursor;
00279        }
00280        return cursor;
00281 }
00282 
00283 int mdb_mid2l_insert( ID2L ids, ID2 *id )
00284 {
00285        unsigned x, i;
00286 
00287        x = mdb_mid2l_search( ids, id->mid );
00288        assert( x > 0 );
00289 
00290        if( x < 1 ) {
00291               /* internal error */
00292               return -2;
00293        }
00294 
00295        if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
00296               /* duplicate */
00297               return -1;
00298        }
00299 
00300        if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
00301               /* too big */
00302               return -2;
00303 
00304        } else {
00305               /* insert id */
00306               ids[0].mid++;
00307               for (i=ids[0].mid; i>x; i--)
00308                      ids[i] = ids[i-1];
00309               ids[x] = *id;
00310        }
00311 
00312        return 0;
00313 }