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>.
19 #include <sys/types.h>
23 /** @defgroup internal MDB Internals
26 /** @defgroup idls ID List Management
29 #define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
31 #if 0 /* superseded by append/sort */
32 static unsigned mdb_midl_search( IDL ids, ID id )
35 * binary search of id in ids
36 * if found, returns position of id
37 * if not found, returns first position greater than id
46 cursor = base + pivot;
47 val = CMP( ids[cursor + 1], id );
52 } else if ( val > 0 ) {
68 int mdb_midl_insert( IDL ids, ID id )
72 if (MDB_IDL_IS_RANGE( ids )) {
73 /* if already in range, treat as a dup */
74 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
76 if (id < MDB_IDL_FIRST(ids))
78 else if (id > MDB_IDL_LAST(ids))
83 x = mdb_midl_search( ids, id );
91 if ( x <= ids[0] && ids[x] == id ) {
97 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
100 ids[2] = ids[ids[0]-1];
101 } else if ( ids[ids[0]-1] < id ) {
104 ids[2] = ids[ids[0]-1];
110 for (i=ids[0]; i>x; i--)
119 int mdb_midl_append( IDL ids, ID id )
122 if (ids[0] >= MDB_IDL_UM_MAX)
129 /* Quicksort + Insertion sort for small arrays */
132 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
135 mdb_midl_sort( ID *ids )
137 int istack[16*sizeof(int)];
138 int i,j,k,l,ir,jstack;
145 if (ir - l < SMALL) { /* Insertion sort */
146 for (j=l+1;j<=ir;j++) {
148 for (i=j-1;i>=1;i--) {
149 if (ids[i] >= a) break;
154 if (jstack == 0) break;
155 ir = istack[jstack--];
156 l = istack[jstack--];
158 k = (l + ir) >> 1; /* Choose median of left, center, right */
159 SWAP(ids[k], ids[l+1]);
160 if (ids[l] < ids[ir]) {
161 SWAP(ids[l], ids[ir]);
163 if (ids[l+1] < ids[ir]) {
164 SWAP(ids[l+1], ids[ir]);
166 if (ids[l] < ids[l+1]) {
167 SWAP(ids[l], ids[l+1]);
173 do i++; while(ids[i] > a);
174 do j--; while(ids[j] < a);
183 istack[jstack-1] = i;
186 istack[jstack] = j-1;
187 istack[jstack-1] = l;
194 unsigned mdb_mid2l_search( ID2L ids, ID id )
197 * binary search of id in ids
198 * if found, returns position of id
199 * if not found, returns first position greater than id
204 unsigned n = ids[0].mid;
208 cursor = base + pivot;
209 val = CMP( id, ids[cursor + 1].mid );
214 } else if ( val > 0 ) {
230 int mdb_mid2l_insert( ID2L ids, ID2 *id )
234 x = mdb_mid2l_search( ids, id->mid );
242 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
247 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
254 for (i=ids[0].mid; i>x; i--)