2 * @brief ldap bdb back-end ID List functions */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2000-2013 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>
26 /** @defgroup internal MDB Internals
29 /** @defgroup idls ID List Management
32 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
34 #if 0 /* superseded by append/sort */
35 static unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
38 * binary search of id in ids
39 * if found, returns position of id
40 * if not found, returns first position greater than id
48 unsigned pivot = n >> 1;
49 cursor = base + pivot + 1;
50 val = CMP( ids[cursor], id );
55 } else if ( val > 0 ) {
70 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
74 if (MDB_IDL_IS_RANGE( ids )) {
75 /* if already in range, treat as a dup */
76 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
78 if (id < MDB_IDL_RANGE_FIRST(ids))
80 else if (id > MDB_IDL_RANGE_LAST(ids))
85 x = mdb_midl_search( ids, id );
93 if ( x <= ids[0] && ids[x] == id ) {
99 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
102 ids[2] = ids[ids[0]-1];
103 } else if ( ids[ids[0]-1] < id ) {
106 ids[2] = ids[ids[0]-1];
112 for (i=ids[0]; i>x; i--)
121 MDB_IDL mdb_midl_alloc(int num)
123 MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
129 void mdb_midl_free(MDB_IDL ids)
135 int mdb_midl_shrink( MDB_IDL *idp )
138 if (*(--ids) > MDB_IDL_UM_MAX) {
139 ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID));
140 *ids++ = MDB_IDL_UM_MAX;
147 int mdb_midl_grow( MDB_IDL *idp, int num )
149 MDB_IDL idn = *idp-1;
151 idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
159 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
163 if (ids[0] >= ids[-1]) {
164 if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
173 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
177 if (ids[0] + app[0] >= ids[-1]) {
178 if (mdb_midl_grow(idp, app[0]))
182 memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
187 /* Quicksort + Insertion sort for small arrays */
190 #define SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
193 mdb_midl_sort( MDB_IDL ids )
195 /* Max possible depth of int-indexed tree * 2 items/level */
196 int istack[sizeof(int)*CHAR_BIT * 2];
197 int i,j,k,l,ir,jstack;
204 if (ir - l < SMALL) { /* Insertion sort */
205 for (j=l+1;j<=ir;j++) {
207 for (i=j-1;i>=1;i--) {
208 if (ids[i] >= a) break;
213 if (jstack == 0) break;
214 ir = istack[jstack--];
215 l = istack[jstack--];
217 k = (l + ir) >> 1; /* Choose median of left, center, right */
218 SWAP(ids[k], ids[l+1]);
219 if (ids[l] < ids[ir]) {
220 SWAP(ids[l], ids[ir]);
222 if (ids[l+1] < ids[ir]) {
223 SWAP(ids[l+1], ids[ir]);
225 if (ids[l] < ids[l+1]) {
226 SWAP(ids[l], ids[l+1]);
232 do i++; while(ids[i] > a);
233 do j--; while(ids[j] < a);
242 istack[jstack-1] = i;
245 istack[jstack] = j-1;
246 istack[jstack-1] = l;
253 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
256 * binary search of id in ids
257 * if found, returns position of id
258 * if not found, returns first position greater than id
263 unsigned n = (unsigned)ids[0].mid;
266 unsigned pivot = n >> 1;
267 cursor = base + pivot + 1;
268 val = CMP( id, ids[cursor].mid );
273 } else if ( val > 0 ) {
288 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
292 x = mdb_mid2l_search( ids, id->mid );
300 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
305 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
312 for (i=(unsigned)ids[0].mid; i>x; i--)
320 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
323 if (ids[0].mid >= MDB_IDL_UM_MAX) {
327 ids[ids[0].mid] = *id;