]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.c
Fix 66c839f0292d9aea12bf93ef6568ac2bd2ca0fc8
[openldap] / libraries / liblmdb / midl.c
1 /**     @file midl.c
2  *      @brief ldap bdb back-end ID List functions */
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2000-2013 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17
18 #include <limits.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <assert.h>
24 #include "midl.h"
25
26 /** @defgroup internal  MDB Internals
27  *      @{
28  */
29 /** @defgroup idls      ID List Management
30  *      @{
31  */
32 #define CMP(x,y)         ( (x) < (y) ? -1 : (x) > (y) )
33
34 #if 0   /* superseded by append/sort */
35 static unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
36 {
37         /*
38          * binary search of id in ids
39          * if found, returns position of id
40          * if not found, returns first position greater than id
41          */
42         unsigned base = 0;
43         unsigned cursor = 1;
44         int val = 0;
45         unsigned n = ids[0];
46
47         while( 0 < n ) {
48                 unsigned pivot = n >> 1;
49                 cursor = base + pivot + 1;
50                 val = CMP( ids[cursor], id );
51
52                 if( val < 0 ) {
53                         n = pivot;
54
55                 } else if ( val > 0 ) {
56                         base = cursor;
57                         n -= pivot + 1;
58
59                 } else {
60                         return cursor;
61                 }
62         }
63         
64         if( val > 0 ) {
65                 ++cursor;
66         }
67         return cursor;
68 }
69
70 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
71 {
72         unsigned x, i;
73
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))
77                         return -1;
78                 if (id < MDB_IDL_RANGE_FIRST(ids))
79                         ids[1] = id;
80                 else if (id > MDB_IDL_RANGE_LAST(ids))
81                         ids[2] = id;
82                 return 0;
83         }
84
85         x = mdb_midl_search( ids, id );
86         assert( x > 0 );
87
88         if( x < 1 ) {
89                 /* internal error */
90                 return -2;
91         }
92
93         if ( x <= ids[0] && ids[x] == id ) {
94                 /* duplicate */
95                 assert(0);
96                 return -1;
97         }
98
99         if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
100                 if( id < ids[1] ) {
101                         ids[1] = id;
102                         ids[2] = ids[ids[0]-1];
103                 } else if ( ids[ids[0]-1] < id ) {
104                         ids[2] = id;
105                 } else {
106                         ids[2] = ids[ids[0]-1];
107                 }
108                 ids[0] = MDB_NOID;
109         
110         } else {
111                 /* insert id */
112                 for (i=ids[0]; i>x; i--)
113                         ids[i] = ids[i-1];
114                 ids[x] = id;
115         }
116
117         return 0;
118 }
119 #endif
120
121 MDB_IDL mdb_midl_alloc(int num)
122 {
123         MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
124         if (ids)
125                 *ids++ = num;
126         return ids;
127 }
128
129 void mdb_midl_free(MDB_IDL ids)
130 {
131         if (ids)
132                 free(ids-1);
133 }
134
135 int mdb_midl_shrink( MDB_IDL *idp )
136 {
137         MDB_IDL ids = *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;
141                 *idp = ids;
142                 return 1;
143         }
144         return 0;
145 }
146
147 int mdb_midl_grow( MDB_IDL *idp, int num )
148 {
149         MDB_IDL idn = *idp-1;
150         /* grow it */
151         idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
152         if (!idn)
153                 return ENOMEM;
154         *idn++ += num;
155         *idp = idn;
156         return 0;
157 }
158
159 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
160 {
161         MDB_IDL ids = *idp;
162         /* Too big? */
163         if (ids[0] >= ids[-1]) {
164                 if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
165                         return ENOMEM;
166                 ids = *idp;
167         }
168         ids[0]++;
169         ids[ids[0]] = id;
170         return 0;
171 }
172
173 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
174 {
175         MDB_IDL ids = *idp;
176         /* Too big? */
177         if (ids[0] + app[0] >= ids[-1]) {
178                 if (mdb_midl_grow(idp, app[0]))
179                         return ENOMEM;
180                 ids = *idp;
181         }
182         memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
183         ids[0] += app[0];
184         return 0;
185 }
186
187 /* Quicksort + Insertion sort for small arrays */
188
189 #define SMALL   8
190 #define SWAP(a,b)       { itmp=(a); (a)=(b); (b)=itmp; }
191
192 void
193 mdb_midl_sort( MDB_IDL ids )
194 {
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;
198         MDB_ID a, itmp;
199
200         ir = (int)ids[0];
201         l = 1;
202         jstack = 0;
203         for(;;) {
204                 if (ir - l < SMALL) {   /* Insertion sort */
205                         for (j=l+1;j<=ir;j++) {
206                                 a = ids[j];
207                                 for (i=j-1;i>=1;i--) {
208                                         if (ids[i] >= a) break;
209                                         ids[i+1] = ids[i];
210                                 }
211                                 ids[i+1] = a;
212                         }
213                         if (jstack == 0) break;
214                         ir = istack[jstack--];
215                         l = istack[jstack--];
216                 } else {
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]);
221                         }
222                         if (ids[l+1] < ids[ir]) {
223                                 SWAP(ids[l+1], ids[ir]);
224                         }
225                         if (ids[l] < ids[l+1]) {
226                                 SWAP(ids[l], ids[l+1]);
227                         }
228                         i = l+1;
229                         j = ir;
230                         a = ids[l+1];
231                         for(;;) {
232                                 do i++; while(ids[i] > a);
233                                 do j--; while(ids[j] < a);
234                                 if (j < i) break;
235                                 SWAP(ids[i],ids[j]);
236                         }
237                         ids[l+1] = ids[j];
238                         ids[j] = a;
239                         jstack += 2;
240                         if (ir-i+1 >= j-l) {
241                                 istack[jstack] = ir;
242                                 istack[jstack-1] = i;
243                                 ir = j-1;
244                         } else {
245                                 istack[jstack] = j-1;
246                                 istack[jstack-1] = l;
247                                 l = i;
248                         }
249                 }
250         }
251 }
252
253 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
254 {
255         /*
256          * binary search of id in ids
257          * if found, returns position of id
258          * if not found, returns first position greater than id
259          */
260         unsigned base = 0;
261         unsigned cursor = 1;
262         int val = 0;
263         unsigned n = (unsigned)ids[0].mid;
264
265         while( 0 < n ) {
266                 unsigned pivot = n >> 1;
267                 cursor = base + pivot + 1;
268                 val = CMP( id, ids[cursor].mid );
269
270                 if( val < 0 ) {
271                         n = pivot;
272
273                 } else if ( val > 0 ) {
274                         base = cursor;
275                         n -= pivot + 1;
276
277                 } else {
278                         return cursor;
279                 }
280         }
281
282         if( val > 0 ) {
283                 ++cursor;
284         }
285         return cursor;
286 }
287
288 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
289 {
290         unsigned x, i;
291
292         x = mdb_mid2l_search( ids, id->mid );
293         assert( x > 0 );
294
295         if( x < 1 ) {
296                 /* internal error */
297                 return -2;
298         }
299
300         if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
301                 /* duplicate */
302                 return -1;
303         }
304
305         if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
306                 /* too big */
307                 return -2;
308
309         } else {
310                 /* insert id */
311                 ids[0].mid++;
312                 for (i=(unsigned)ids[0].mid; i>x; i--)
313                         ids[i] = ids[i-1];
314                 ids[x] = *id;
315         }
316
317         return 0;
318 }
319
320 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
321 {
322         /* Too big? */
323         if (ids[0].mid >= MDB_IDL_UM_MAX) {
324                 return -2;
325         }
326         ids[0].mid++;
327         ids[ids[0].mid] = *id;
328         return 0;
329 }
330
331 /** @} */
332 /** @} */