2 * @brief ldap bdb back-end ID List functions */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2000-2015 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>.
22 #include <sys/types.h>
25 /** @defgroup internal LMDB Internals
28 /** @defgroup idls ID List Management
31 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
33 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
36 * binary search of id in ids
37 * if found, returns position of id
38 * if not found, returns first position greater than id
46 unsigned pivot = n >> 1;
47 cursor = base + pivot + 1;
48 val = CMP( ids[cursor], id );
53 } else if ( val > 0 ) {
68 #if 0 /* superseded by append/sort */
69 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
73 x = mdb_midl_search( ids, id );
81 if ( x <= ids[0] && ids[x] == id ) {
87 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
94 for (i=ids[0]; i>x; i--)
103 MDB_IDL mdb_midl_alloc(int num)
105 MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
113 void mdb_midl_free(MDB_IDL ids)
119 void mdb_midl_shrink( MDB_IDL *idp )
122 if (*(--ids) > MDB_IDL_UM_MAX &&
123 (ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID))))
125 *ids++ = MDB_IDL_UM_MAX;
130 static int mdb_midl_grow( MDB_IDL *idp, int num )
132 MDB_IDL idn = *idp-1;
134 idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
142 int mdb_midl_need( MDB_IDL *idp, unsigned num )
147 num = (num + num/4 + (256 + 2)) & -256;
148 if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
156 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
160 if (ids[0] >= ids[-1]) {
161 if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
170 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
174 if (ids[0] + app[0] >= ids[-1]) {
175 if (mdb_midl_grow(idp, app[0]))
179 memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
184 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
186 MDB_ID *ids = *idp, len = ids[0];
188 if (len + n > ids[-1]) {
189 if (mdb_midl_grow(idp, n | MDB_IDL_UM_MAX))
200 void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge )
202 MDB_ID old_id, merge_id, i = merge[0], j = idl[0], k = i+j, total = k;
203 idl[0] = (MDB_ID)-1; /* delimiter for idl scan below */
206 merge_id = merge[i--];
207 for (; old_id < merge_id; old_id = idl[--j])
214 /* Quicksort + Insertion sort for small arrays */
217 #define MIDL_SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
220 mdb_midl_sort( MDB_IDL ids )
222 /* Max possible depth of int-indexed tree * 2 items/level */
223 int istack[sizeof(int)*CHAR_BIT * 2];
224 int i,j,k,l,ir,jstack;
231 if (ir - l < SMALL) { /* Insertion sort */
232 for (j=l+1;j<=ir;j++) {
234 for (i=j-1;i>=1;i--) {
235 if (ids[i] >= a) break;
240 if (jstack == 0) break;
241 ir = istack[jstack--];
242 l = istack[jstack--];
244 k = (l + ir) >> 1; /* Choose median of left, center, right */
245 MIDL_SWAP(ids[k], ids[l+1]);
246 if (ids[l] < ids[ir]) {
247 MIDL_SWAP(ids[l], ids[ir]);
249 if (ids[l+1] < ids[ir]) {
250 MIDL_SWAP(ids[l+1], ids[ir]);
252 if (ids[l] < ids[l+1]) {
253 MIDL_SWAP(ids[l], ids[l+1]);
259 do i++; while(ids[i] > a);
260 do j--; while(ids[j] < a);
262 MIDL_SWAP(ids[i],ids[j]);
269 istack[jstack-1] = i;
272 istack[jstack] = j-1;
273 istack[jstack-1] = l;
280 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
283 * binary search of id in ids
284 * if found, returns position of id
285 * if not found, returns first position greater than id
290 unsigned n = (unsigned)ids[0].mid;
293 unsigned pivot = n >> 1;
294 cursor = base + pivot + 1;
295 val = CMP( id, ids[cursor].mid );
300 } else if ( val > 0 ) {
315 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
319 x = mdb_mid2l_search( ids, id->mid );
326 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
331 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
338 for (i=(unsigned)ids[0].mid; i>x; i--)
346 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
349 if (ids[0].mid >= MDB_IDL_UM_MAX) {
353 ids[ids[0].mid] = *id;