]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.c
Silence stupid warnings
[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 <sys/types.h>
22 #include <assert.h>
23 #include "midl.h"
24
25 /** @defgroup internal  MDB Internals
26  *      @{
27  */
28 /** @defgroup idls      ID List Management
29  *      @{
30  */
31 #define CMP(x,y)         ( (x) < (y) ? -1 : (x) > (y) )
32
33 #if 0   /* superseded by append/sort */
34 static 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 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
70 {
71         unsigned x, i;
72
73         if (MDB_IDL_IS_RANGE( ids )) {
74                 /* if already in range, treat as a dup */
75                 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
76                         return -1;
77                 if (id < MDB_IDL_RANGE_FIRST(ids))
78                         ids[1] = id;
79                 else if (id > MDB_IDL_RANGE_LAST(ids))
80                         ids[2] = id;
81                 return 0;
82         }
83
84         x = mdb_midl_search( ids, id );
85         assert( x > 0 );
86
87         if( x < 1 ) {
88                 /* internal error */
89                 return -2;
90         }
91
92         if ( x <= ids[0] && ids[x] == id ) {
93                 /* duplicate */
94                 assert(0);
95                 return -1;
96         }
97
98         if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
99                 if( id < ids[1] ) {
100                         ids[1] = id;
101                         ids[2] = ids[ids[0]-1];
102                 } else if ( ids[ids[0]-1] < id ) {
103                         ids[2] = id;
104                 } else {
105                         ids[2] = ids[ids[0]-1];
106                 }
107                 ids[0] = MDB_NOID;
108         
109         } else {
110                 /* insert id */
111                 for (i=ids[0]; i>x; i--)
112                         ids[i] = ids[i-1];
113                 ids[x] = id;
114         }
115
116         return 0;
117 }
118 #endif
119
120 MDB_IDL mdb_midl_alloc(void)
121 {
122         MDB_IDL ids = malloc((MDB_IDL_UM_MAX+1) * sizeof(MDB_ID));
123         *ids++ = MDB_IDL_UM_MAX;
124         return ids;
125 }
126
127 void mdb_midl_free(MDB_IDL ids)
128 {
129         free(ids-1);
130 }
131
132 int mdb_midl_shrink( MDB_IDL *idp )
133 {
134         MDB_IDL ids = *idp;
135         if (*(--ids) > MDB_IDL_UM_MAX) {
136                 ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID));
137                 *ids++ = MDB_IDL_UM_MAX;
138                 *idp = ids;
139                 return 1;
140         }
141         return 0;
142 }
143
144 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
145 {
146         MDB_IDL ids = *idp;
147         /* Too big? */
148         if (ids[0] >= ids[-1]) {
149                 MDB_IDL idn = ids-1;
150                 /* grow it */
151                 idn = realloc(idn, (*idn + MDB_IDL_UM_MAX + 1) * sizeof(MDB_ID));
152                 if (!idn)
153                         return -1;
154                 *idn++ += MDB_IDL_UM_MAX;
155                 ids = idn;
156                 *idp = ids;
157         }
158         ids[0]++;
159         ids[ids[0]] = id;
160         return 0;
161 }
162
163 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
164 {
165         MDB_IDL ids = *idp;
166         /* Too big? */
167         if (ids[0] + app[0] >= ids[-1]) {
168                 MDB_IDL idn = ids-1;
169                 /* grow it */
170                 idn = realloc(idn, (*idn + app[-1]) * sizeof(MDB_ID));
171                 if (!idn)
172                         return -1;
173                 *idn++ += app[-1];
174                 ids = idn;
175                 *idp = ids;
176         }
177         memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
178         ids[0] += app[0];
179         return 0;
180 }
181
182 /* Quicksort + Insertion sort for small arrays */
183
184 #define SMALL   8
185 #define SWAP(a,b)       { itmp=(a); (a)=(b); (b)=itmp; }
186
187 void
188 mdb_midl_sort( MDB_IDL ids )
189 {
190         /* Max possible depth of int-indexed tree * 2 items/level */
191         int istack[sizeof(int)*CHAR_BIT * 2];
192         int i,j,k,l,ir,jstack;
193         MDB_ID a, itmp;
194
195         ir = (int)ids[0];
196         l = 1;
197         jstack = 0;
198         for(;;) {
199                 if (ir - l < SMALL) {   /* Insertion sort */
200                         for (j=l+1;j<=ir;j++) {
201                                 a = ids[j];
202                                 for (i=j-1;i>=1;i--) {
203                                         if (ids[i] >= a) break;
204                                         ids[i+1] = ids[i];
205                                 }
206                                 ids[i+1] = a;
207                         }
208                         if (jstack == 0) break;
209                         ir = istack[jstack--];
210                         l = istack[jstack--];
211                 } else {
212                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
213                         SWAP(ids[k], ids[l+1]);
214                         if (ids[l] < ids[ir]) {
215                                 SWAP(ids[l], ids[ir]);
216                         }
217                         if (ids[l+1] < ids[ir]) {
218                                 SWAP(ids[l+1], ids[ir]);
219                         }
220                         if (ids[l] < ids[l+1]) {
221                                 SWAP(ids[l], ids[l+1]);
222                         }
223                         i = l+1;
224                         j = ir;
225                         a = ids[l+1];
226                         for(;;) {
227                                 do i++; while(ids[i] > a);
228                                 do j--; while(ids[j] < a);
229                                 if (j < i) break;
230                                 SWAP(ids[i],ids[j]);
231                         }
232                         ids[l+1] = ids[j];
233                         ids[j] = a;
234                         jstack += 2;
235                         if (ir-i+1 >= j-l) {
236                                 istack[jstack] = ir;
237                                 istack[jstack-1] = i;
238                                 ir = j-1;
239                         } else {
240                                 istack[jstack] = j-1;
241                                 istack[jstack-1] = l;
242                                 l = i;
243                         }
244                 }
245         }
246 }
247
248 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
249 {
250         /*
251          * binary search of id in ids
252          * if found, returns position of id
253          * if not found, returns first position greater than id
254          */
255         unsigned base = 0;
256         unsigned cursor = 1;
257         int val = 0;
258         unsigned n = (unsigned)ids[0].mid;
259
260         while( 0 < n ) {
261                 unsigned pivot = n >> 1;
262                 cursor = base + pivot + 1;
263                 val = CMP( id, ids[cursor].mid );
264
265                 if( val < 0 ) {
266                         n = pivot;
267
268                 } else if ( val > 0 ) {
269                         base = cursor;
270                         n -= pivot + 1;
271
272                 } else {
273                         return cursor;
274                 }
275         }
276
277         if( val > 0 ) {
278                 ++cursor;
279         }
280         return cursor;
281 }
282
283 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
284 {
285         unsigned x, i;
286
287         x = mdb_mid2l_search( ids, id->mid );
288         assert( x > 0 );
289
290         if( x < 1 ) {
291                 /* internal error */
292                 return -2;
293         }
294
295         if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
296                 /* duplicate */
297                 return -1;
298         }
299
300         if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
301                 /* too big */
302                 return -2;
303
304         } else {
305                 /* insert id */
306                 ids[0].mid++;
307                 for (i=(unsigned)ids[0].mid; i>x; i--)
308                         ids[i] = ids[i-1];
309                 ids[x] = *id;
310         }
311
312         return 0;
313 }
314
315 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
316 {
317         /* Too big? */
318         if (ids[0].mid >= MDB_IDL_UM_MAX) {
319                 return -2;
320         }
321         ids[0].mid++;
322         ids[ids[0].mid] = *id;
323         return 0;
324 }
325
326 /** @} */
327 /** @} */