2 * @brief ldap bdb back-end ID List functions */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 * Copyright 2000-2011 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>.
20 #include <sys/types.h>
24 /** @defgroup internal MDB Internals
27 /** @defgroup idls ID List Management
30 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
32 #if 0 /* superseded by append/sort */
33 static unsigned mdb_midl_search( IDL ids, 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
47 cursor = base + pivot;
48 val = CMP( ids[cursor + 1], id );
53 } 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_FIRST(ids) && id <= MDB_IDL_LAST(ids))
77 if (id < MDB_IDL_FIRST(ids))
79 else if (id > MDB_IDL_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--)
120 int mdb_midl_append( IDL ids, ID id )
123 if (ids[0] >= MDB_IDL_UM_MAX)
130 /* Quicksort + Insertion sort for small arrays */
133 #define SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
136 mdb_midl_sort( ID *ids )
138 /* Max possible depth of int-indexed tree * 2 items/level */
139 int istack[sizeof(int)*CHAR_BIT * 2];
140 int i,j,k,l,ir,jstack;
147 if (ir - l < SMALL) { /* Insertion sort */
148 for (j=l+1;j<=ir;j++) {
150 for (i=j-1;i>=1;i--) {
151 if (ids[i] >= a) break;
156 if (jstack == 0) break;
157 ir = istack[jstack--];
158 l = istack[jstack--];
160 k = (l + ir) >> 1; /* Choose median of left, center, right */
161 SWAP(ids[k], ids[l+1]);
162 if (ids[l] < ids[ir]) {
163 SWAP(ids[l], ids[ir]);
165 if (ids[l+1] < ids[ir]) {
166 SWAP(ids[l+1], ids[ir]);
168 if (ids[l] < ids[l+1]) {
169 SWAP(ids[l], ids[l+1]);
175 do i++; while(ids[i] > a);
176 do j--; while(ids[j] < a);
185 istack[jstack-1] = i;
188 istack[jstack] = j-1;
189 istack[jstack-1] = l;
196 unsigned mdb_mid2l_search( ID2L ids, ID id )
199 * binary search of id in ids
200 * if found, returns position of id
201 * if not found, returns first position greater than id
206 unsigned n = ids[0].mid;
210 cursor = base + pivot;
211 val = CMP( id, ids[cursor + 1].mid );
216 } else if ( val > 0 ) {
232 int mdb_mid2l_insert( ID2L ids, ID2 *id )
236 x = mdb_mid2l_search( ids, id->mid );
244 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
249 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
256 for (i=ids[0].mid; i>x; i--)