1 /* idl.c - ldap bdb back-end ID list functions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
18 #include <sys/types.h>
22 typedef unsigned long pgno_t;
24 #define CMP(x,y) ( (x) > (y) ? -1 : (x) < (y) )
26 unsigned mdb_midl_search( ID *ids, ID id )
29 * binary search of id in ids
30 * if found, returns position of id
31 * if not found, returns first position greater than id
40 cursor = base + pivot;
41 val = CMP( id, ids[cursor + 1] );
46 } else if ( val > 0 ) {
62 int mdb_midl_insert( ID *ids, ID id )
66 if (MDB_IDL_IS_RANGE( ids )) {
67 /* if already in range, treat as a dup */
68 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
70 if (id < MDB_IDL_FIRST(ids))
72 else if (id > MDB_IDL_LAST(ids))
77 x = mdb_midl_search( ids, id );
85 if ( x <= ids[0] && ids[x] == id ) {
90 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
93 ids[2] = ids[ids[0]-1];
94 } else if ( ids[ids[0]-1] < id ) {
97 ids[2] = ids[ids[0]-1];
103 for (i=ids[0]; i>x; i--)
111 unsigned mdb_midl2_search( MIDL2 *ids, MIDL2 *id )
114 * binary search of id in ids
115 * if found, returns position of id
116 * if not found, returns first position greater than id
121 unsigned n = ids[0].mid;
125 cursor = base + pivot;
126 val = CMP( ids[cursor + 1].mid, id->mid );
131 } else if ( val > 0 ) {
147 int mdb_midl2_insert( MIDL2 *ids, MIDL2 *id )
151 x = mdb_midl2_search( ids, id );
159 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
164 if ( ids[0].mid >= MDB_IDL_DB_MAX ) {
171 for (i=ids[0].mid; i>x; i--)