2 * @brief ldap bdb back-end ID List functions */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2000-2016 The OpenLDAP Foundation.
7 * Portions Copyright 2001-2017 Howard Chu, Symas Corp.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <sys/types.h>
26 /** @defgroup internal LMDB Internals
29 /** @defgroup idls ID List Management
32 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
34 unsigned mdb_midl_search( MDB_IDL ids, MDB_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 #if 0 /* superseded by append/sort */
70 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
74 x = mdb_midl_search( ids, id );
82 if ( x <= ids[0] && ids[x] == id ) {
88 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
95 for (i=ids[0]; i>x; i--)
104 MDB_IDL mdb_midl_alloc(int num)
106 MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
114 void mdb_midl_free(MDB_IDL ids)
120 void mdb_midl_shrink( MDB_IDL *idp )
123 if (*(--ids) > MDB_IDL_UM_MAX &&
124 (ids = realloc(ids, (MDB_IDL_UM_MAX+2) * sizeof(MDB_ID))))
126 *ids++ = MDB_IDL_UM_MAX;
131 static int mdb_midl_grow( MDB_IDL *idp, int num )
133 MDB_IDL idn = *idp-1;
135 idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
143 int mdb_midl_need( MDB_IDL *idp, unsigned num )
148 num = (num + num/4 + (256 + 2)) & -256;
149 if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
157 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
161 if (ids[0] >= ids[-1]) {
162 if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
171 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
175 if (ids[0] + app[0] >= ids[-1]) {
176 if (mdb_midl_grow(idp, app[0]))
180 memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
185 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
187 MDB_ID *ids = *idp, len = ids[0];
189 if (len + n > ids[-1]) {
190 if (mdb_midl_grow(idp, n | MDB_IDL_UM_MAX))
201 void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge )
203 MDB_ID old_id, merge_id, i = merge[0], j = idl[0], k = i+j, total = k;
204 idl[0] = (MDB_ID)-1; /* delimiter for idl scan below */
207 merge_id = merge[i--];
208 for (; old_id < merge_id; old_id = idl[--j])
215 /* Quicksort + Insertion sort for small arrays */
218 #define MIDL_SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
221 mdb_midl_sort( MDB_IDL ids )
223 /* Max possible depth of int-indexed tree * 2 items/level */
224 int istack[sizeof(int)*CHAR_BIT * 2];
225 int i,j,k,l,ir,jstack;
232 if (ir - l < SMALL) { /* Insertion sort */
233 for (j=l+1;j<=ir;j++) {
235 for (i=j-1;i>=1;i--) {
236 if (ids[i] >= a) break;
241 if (jstack == 0) break;
242 ir = istack[jstack--];
243 l = istack[jstack--];
245 k = (l + ir) >> 1; /* Choose median of left, center, right */
246 MIDL_SWAP(ids[k], ids[l+1]);
247 if (ids[l] < ids[ir]) {
248 MIDL_SWAP(ids[l], ids[ir]);
250 if (ids[l+1] < ids[ir]) {
251 MIDL_SWAP(ids[l+1], ids[ir]);
253 if (ids[l] < ids[l+1]) {
254 MIDL_SWAP(ids[l], ids[l+1]);
260 do i++; while(ids[i] > a);
261 do j--; while(ids[j] < a);
263 MIDL_SWAP(ids[i],ids[j]);
270 istack[jstack-1] = i;
273 istack[jstack] = j-1;
274 istack[jstack-1] = l;
281 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
284 * binary search of id in ids
285 * if found, returns position of id
286 * if not found, returns first position greater than id
291 unsigned n = (unsigned)ids[0].mid;
294 unsigned pivot = n >> 1;
295 cursor = base + pivot + 1;
296 val = CMP( id, ids[cursor].mid );
301 } else if ( val > 0 ) {
316 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
320 x = mdb_mid2l_search( ids, id->mid );
327 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
332 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
339 for (i=(unsigned)ids[0].mid; i>x; i--)
347 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
350 if (ids[0].mid >= MDB_IDL_UM_MAX) {
354 ids[ids[0].mid] = *id;