]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.c
Add Caveat: Readers need write access. Whitespace.
[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 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
35 {
36         /*
37          * binary search of id in ids
38          * if found, returns position of id
39          * if not found, returns first position greater than id
40          */
41         unsigned base = 0;
42         unsigned cursor = 1;
43         int val = 0;
44         unsigned n = ids[0];
45
46         while( 0 < n ) {
47                 unsigned pivot = n >> 1;
48                 cursor = base + pivot + 1;
49                 val = CMP( ids[cursor], id );
50
51                 if( val < 0 ) {
52                         n = pivot;
53
54                 } else if ( val > 0 ) {
55                         base = cursor;
56                         n -= pivot + 1;
57
58                 } else {
59                         return cursor;
60                 }
61         }
62
63         if( val > 0 ) {
64                 ++cursor;
65         }
66         return cursor;
67 }
68
69 #if 0   /* superseded by append/sort */
70 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
71 {
72         unsigned x, i;
73
74         x = mdb_midl_search( ids, id );
75         assert( x > 0 );
76
77         if( x < 1 ) {
78                 /* internal error */
79                 return -2;
80         }
81
82         if ( x <= ids[0] && ids[x] == id ) {
83                 /* duplicate */
84                 assert(0);
85                 return -1;
86         }
87
88         if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
89                 /* no room */
90                 --ids[0];
91                 return -2;
92
93         } else {
94                 /* insert id */
95                 for (i=ids[0]; i>x; i--)
96                         ids[i] = ids[i-1];
97                 ids[x] = id;
98         }
99
100         return 0;
101 }
102 #endif
103
104 MDB_IDL mdb_midl_alloc(int num)
105 {
106         MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
107         if (ids) {
108                 *ids++ = num;
109                 *ids = 0;
110         }
111         return ids;
112 }
113
114 void mdb_midl_free(MDB_IDL ids)
115 {
116         if (ids)
117                 free(ids-1);
118 }
119
120 int mdb_midl_shrink( MDB_IDL *idp )
121 {
122         MDB_IDL ids = *idp;
123         if (*(--ids) > MDB_IDL_UM_MAX &&
124                 (ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID))))
125         {
126                 *ids++ = MDB_IDL_UM_MAX;
127                 *idp = ids;
128                 return 1;
129         }
130         return 0;
131 }
132
133 static int mdb_midl_grow( MDB_IDL *idp, int num )
134 {
135         MDB_IDL idn = *idp-1;
136         /* grow it */
137         idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
138         if (!idn)
139                 return ENOMEM;
140         *idn++ += num;
141         *idp = idn;
142         return 0;
143 }
144
145 int mdb_midl_need( MDB_IDL *idp, unsigned num )
146 {
147         MDB_IDL ids = *idp;
148         num += ids[0];
149         if (num > ids[-1]) {
150                 num = (num + num/4 + (256 + 2)) & -256;
151                 if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
152                         return ENOMEM;
153                 *ids++ = num -= 2;
154                 *idp = ids;
155         }
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 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
188 {
189         MDB_ID *ids = *idp, len = ids[0];
190         /* Too big? */
191         if (len + n > ids[-1]) {
192                 if (mdb_midl_grow(idp, n | MDB_IDL_UM_MAX))
193                         return ENOMEM;
194                 ids = *idp;
195         }
196         ids[0] = len + n;
197         ids += len;
198         while (n)
199                 ids[n--] = id++;
200         return 0;
201 }
202
203 /* Quicksort + Insertion sort for small arrays */
204
205 #define SMALL   8
206 #define MIDL_SWAP(a,b)  { itmp=(a); (a)=(b); (b)=itmp; }
207
208 void
209 mdb_midl_sort( MDB_IDL ids )
210 {
211         /* Max possible depth of int-indexed tree * 2 items/level */
212         int istack[sizeof(int)*CHAR_BIT * 2];
213         int i,j,k,l,ir,jstack;
214         MDB_ID a, itmp;
215
216         ir = (int)ids[0];
217         l = 1;
218         jstack = 0;
219         for(;;) {
220                 if (ir - l < SMALL) {   /* Insertion sort */
221                         for (j=l+1;j<=ir;j++) {
222                                 a = ids[j];
223                                 for (i=j-1;i>=1;i--) {
224                                         if (ids[i] >= a) break;
225                                         ids[i+1] = ids[i];
226                                 }
227                                 ids[i+1] = a;
228                         }
229                         if (jstack == 0) break;
230                         ir = istack[jstack--];
231                         l = istack[jstack--];
232                 } else {
233                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
234                         MIDL_SWAP(ids[k], ids[l+1]);
235                         if (ids[l] < ids[ir]) {
236                                 MIDL_SWAP(ids[l], ids[ir]);
237                         }
238                         if (ids[l+1] < ids[ir]) {
239                                 MIDL_SWAP(ids[l+1], ids[ir]);
240                         }
241                         if (ids[l] < ids[l+1]) {
242                                 MIDL_SWAP(ids[l], ids[l+1]);
243                         }
244                         i = l+1;
245                         j = ir;
246                         a = ids[l+1];
247                         for(;;) {
248                                 do i++; while(ids[i] > a);
249                                 do j--; while(ids[j] < a);
250                                 if (j < i) break;
251                                 MIDL_SWAP(ids[i],ids[j]);
252                         }
253                         ids[l+1] = ids[j];
254                         ids[j] = a;
255                         jstack += 2;
256                         if (ir-i+1 >= j-l) {
257                                 istack[jstack] = ir;
258                                 istack[jstack-1] = i;
259                                 ir = j-1;
260                         } else {
261                                 istack[jstack] = j-1;
262                                 istack[jstack-1] = l;
263                                 l = i;
264                         }
265                 }
266         }
267 }
268
269 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
270 {
271         /*
272          * binary search of id in ids
273          * if found, returns position of id
274          * if not found, returns first position greater than id
275          */
276         unsigned base = 0;
277         unsigned cursor = 1;
278         int val = 0;
279         unsigned n = (unsigned)ids[0].mid;
280
281         while( 0 < n ) {
282                 unsigned pivot = n >> 1;
283                 cursor = base + pivot + 1;
284                 val = CMP( id, ids[cursor].mid );
285
286                 if( val < 0 ) {
287                         n = pivot;
288
289                 } else if ( val > 0 ) {
290                         base = cursor;
291                         n -= pivot + 1;
292
293                 } else {
294                         return cursor;
295                 }
296         }
297
298         if( val > 0 ) {
299                 ++cursor;
300         }
301         return cursor;
302 }
303
304 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
305 {
306         unsigned x, i;
307
308         x = mdb_mid2l_search( ids, id->mid );
309         assert( x > 0 );
310
311         if( x < 1 ) {
312                 /* internal error */
313                 return -2;
314         }
315
316         if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
317                 /* duplicate */
318                 return -1;
319         }
320
321         if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
322                 /* too big */
323                 return -2;
324
325         } else {
326                 /* insert id */
327                 ids[0].mid++;
328                 for (i=(unsigned)ids[0].mid; i>x; i--)
329                         ids[i] = ids[i-1];
330                 ids[x] = *id;
331         }
332
333         return 0;
334 }
335
336 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
337 {
338         /* Too big? */
339         if (ids[0].mid >= MDB_IDL_UM_MAX) {
340                 return -2;
341         }
342         ids[0].mid++;
343         ids[ids[0].mid] = *id;
344         return 0;
345 }
346
347 /** @} */
348 /** @} */