2 * @brief ldap bdb back-end ID List functions */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2000-2012 The OpenLDAP Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
21 #include <sys/types.h>
25 /** @defgroup internal MDB Internals
28 /** @defgroup idls ID List Management
31 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
33 #if 0 /* superseded by append/sort */
34 static unsigned mdb_midl_search( IDL ids, ID id )
37 * binary search of id in ids
38 * if found, returns position of id
39 * if not found, returns first position greater than id
47 unsigned pivot = n >> 1;
48 cursor = base + pivot + 1;
49 val = CMP( ids[cursor], id );
54 } else if ( val > 0 ) {
69 int mdb_midl_insert( IDL ids, ID id )
73 if (MDB_IDL_IS_RANGE( ids )) {
74 /* if already in range, treat as a dup */
75 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
77 if (id < MDB_IDL_RANGE_FIRST(ids))
79 else if (id > MDB_IDL_RANGE_LAST(ids))
84 x = mdb_midl_search( ids, id );
92 if ( x <= ids[0] && ids[x] == id ) {
98 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
101 ids[2] = ids[ids[0]-1];
102 } else if ( ids[ids[0]-1] < id ) {
105 ids[2] = ids[ids[0]-1];
111 for (i=ids[0]; i>x; i--)
122 IDL ids = malloc((MDB_IDL_UM_MAX+1) * sizeof(ID));
123 *ids++ = MDB_IDL_UM_MAX;
127 void mdb_midl_free(IDL ids)
132 int mdb_midl_shrink( IDL *idp )
135 if (ids[-1] > MDB_IDL_UM_MAX) {
136 ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(ID));
137 *ids++ = MDB_IDL_UM_MAX;
144 int mdb_midl_append( IDL *idp, ID id )
148 if (ids[0] >= ids[-1]) {
151 idn = realloc(idn, (*idn + MDB_IDL_UM_MAX + 1) * sizeof(ID));
154 *idn++ += MDB_IDL_UM_MAX;
163 int mdb_midl_append_list( IDL *idp, IDL app )
167 if (ids[0] + app[0] >= ids[-1]) {
170 idn = realloc(idn, (*idn + app[-1]) * sizeof(ID));
177 memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(ID));
182 /* Quicksort + Insertion sort for small arrays */
185 #define SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
188 mdb_midl_sort( IDL ids )
190 /* Max possible depth of int-indexed tree * 2 items/level */
191 int istack[sizeof(int)*CHAR_BIT * 2];
192 int i,j,k,l,ir,jstack;
199 if (ir - l < SMALL) { /* Insertion sort */
200 for (j=l+1;j<=ir;j++) {
202 for (i=j-1;i>=1;i--) {
203 if (ids[i] >= a) break;
208 if (jstack == 0) break;
209 ir = istack[jstack--];
210 l = istack[jstack--];
212 k = (l + ir) >> 1; /* Choose median of left, center, right */
213 SWAP(ids[k], ids[l+1]);
214 if (ids[l] < ids[ir]) {
215 SWAP(ids[l], ids[ir]);
217 if (ids[l+1] < ids[ir]) {
218 SWAP(ids[l+1], ids[ir]);
220 if (ids[l] < ids[l+1]) {
221 SWAP(ids[l], ids[l+1]);
227 do i++; while(ids[i] > a);
228 do j--; while(ids[j] < a);
237 istack[jstack-1] = i;
240 istack[jstack] = j-1;
241 istack[jstack-1] = l;
248 unsigned mdb_mid2l_search( ID2L ids, ID id )
251 * binary search of id in ids
252 * if found, returns position of id
253 * if not found, returns first position greater than id
258 unsigned n = ids[0].mid;
261 unsigned pivot = n >> 1;
262 cursor = base + pivot + 1;
263 val = CMP( id, ids[cursor].mid );
268 } else if ( val > 0 ) {
283 int mdb_mid2l_insert( ID2L ids, ID2 *id )
287 x = mdb_mid2l_search( ids, id->mid );
295 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
300 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
307 for (i=ids[0].mid; i>x; i--)