1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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>.
20 #include <ac/string.h>
25 #define IDL_MAX(x,y) ( x > y ? x : y )
26 #define IDL_MIN(x,y) ( x < y ? x : y )
28 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
30 #define IDL_LRU_DELETE( bdb, e ) do { \
31 if ( e->idl_lru_prev != NULL ) { \
32 e->idl_lru_prev->idl_lru_next = e->idl_lru_next; \
34 bdb->bi_idl_lru_head = e->idl_lru_next; \
36 if ( e->idl_lru_next != NULL ) { \
37 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev; \
39 bdb->bi_idl_lru_tail = e->idl_lru_prev; \
43 #define IDL_LRU_ADD( bdb, e ) do { \
44 e->idl_lru_next = bdb->bi_idl_lru_head; \
45 if ( e->idl_lru_next != NULL ) { \
46 e->idl_lru_next->idl_lru_prev = (e); \
48 (bdb)->bi_idl_lru_head = (e); \
49 e->idl_lru_prev = NULL; \
50 if ( (bdb)->bi_idl_lru_tail == NULL ) { \
51 (bdb)->bi_idl_lru_tail = (e); \
56 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
58 const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
61 if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
62 if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
63 return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
67 static void idl_check( ID *ids )
69 if( BDB_IDL_IS_RANGE( ids ) ) {
70 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
73 for( i=1; i < ids[0]; i++ ) {
74 assert( ids[i+1] > ids[i] );
80 static void idl_dump( ID *ids )
82 if( BDB_IDL_IS_RANGE( ids ) ) {
83 Debug( LDAP_DEBUG_ANY,
84 "IDL: range ( %ld - %ld )\n",
85 (long) BDB_IDL_RANGE_FIRST( ids ),
86 (long) BDB_IDL_RANGE_LAST( ids ) );
90 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
92 for( i=1; i<=ids[0]; i++ ) {
94 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
99 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
104 #endif /* IDL_DEBUG > 1 */
105 #endif /* IDL_DEBUG > 0 */
107 unsigned bdb_idl_search( ID *ids, ID id )
109 #define IDL_BINARY_SEARCH 1
110 #ifdef IDL_BINARY_SEARCH
112 * binary search of id in ids
113 * if found, returns position of id
114 * if not found, returns first postion greater than id
127 cursor = base + pivot;
128 val = IDL_CMP( id, ids[cursor + 1] );
133 } else if ( val > 0 ) {
149 /* (reverse) linear search */
156 for( i=ids[0]; i; i-- ) {
166 int bdb_idl_insert( ID *ids, ID id )
171 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
177 if (BDB_IDL_IS_RANGE( ids )) {
178 /* if already in range, treat as a dup */
179 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
181 if (id < BDB_IDL_FIRST(ids))
183 else if (id > BDB_IDL_LAST(ids))
188 x = bdb_idl_search( ids, id );
196 if ( x <= ids[0] && ids[x] == id ) {
201 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
204 ids[2] = ids[ids[0]-1];
205 } else if ( ids[ids[0]-1] < id ) {
208 ids[2] = ids[ids[0]-1];
214 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
228 static int idl_delete( ID *ids, ID id )
230 unsigned x = bdb_idl_search( ids, id );
233 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
246 if( x > ids[0] || ids[x] != id ) {
250 } else if ( --ids[0] == 0 ) {
256 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
274 if ( key->size == 4 /* LUTIL_HASH_BYTES */ ) {
275 unsigned char *c = key->data;
276 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
283 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
284 * copy the cached IDL into it, otherwise just return the status.
288 struct bdb_info *bdb,
293 bdb_idl_cache_entry_t idl_tmp;
294 bdb_idl_cache_entry_t *matched_idl_entry;
295 int rc = LDAP_NO_SUCH_OBJECT;
297 DBT2bv( key, &idl_tmp.kstr );
299 ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
300 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
302 if ( matched_idl_entry != NULL ) {
303 if ( matched_idl_entry->idl && ids )
304 BDB_IDL_CPY( ids, matched_idl_entry->idl );
305 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
306 IDL_LRU_DELETE( bdb, matched_idl_entry );
307 IDL_LRU_ADD( bdb, matched_idl_entry );
308 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
309 if ( matched_idl_entry->idl )
314 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
321 struct bdb_info *bdb,
327 bdb_idl_cache_entry_t idl_tmp;
328 bdb_idl_cache_entry_t *ee;
330 DBT2bv( key, &idl_tmp.kstr );
332 ee = (bdb_idl_cache_entry_t *) ch_malloc(
333 sizeof( bdb_idl_cache_entry_t ) );
335 if ( rc == DB_NOTFOUND || BDB_IDL_IS_ZERO( ids )) {
338 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
339 BDB_IDL_CPY( ee->idl, ids );
341 ee->idl_lru_prev = NULL;
342 ee->idl_lru_next = NULL;
343 ber_dupbv( &ee->kstr, &idl_tmp.kstr );
344 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
345 if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
346 bdb_idl_entry_cmp, avl_dup_error ))
348 ch_free( ee->kstr.bv_val );
351 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
354 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
355 IDL_LRU_ADD( bdb, ee );
356 if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
358 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
359 ee = bdb->bi_idl_lru_tail;
360 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
361 bdb_idl_entry_cmp ) == NULL ) {
362 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
363 "AVL delete failed\n",
366 IDL_LRU_DELETE( bdb, ee );
368 --bdb->bi_idl_cache_size;
369 ch_free( ee->kstr.bv_val );
375 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
376 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
381 struct bdb_info *bdb,
385 bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
386 DBT2bv( key, &idl_tmp.kstr );
388 ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
389 matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
391 if ( matched_idl_entry != NULL ) {
392 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
393 bdb_idl_entry_cmp ) == NULL ) {
394 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
395 "AVL delete failed\n",
398 --bdb->bi_idl_cache_size;
399 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
400 IDL_LRU_DELETE( bdb, matched_idl_entry );
401 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
402 free( matched_idl_entry->kstr.bv_val );
403 if ( matched_idl_entry->idl )
404 free( matched_idl_entry->idl );
405 free( matched_idl_entry );
407 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
420 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
422 DBT data, key2, *kptr;
428 int flags = bdb->bi_db_opflags | DB_MULTIPLE;
431 /* If using BerkeleyDB 4.0, the buf must be large enough to
432 * grab the entire IDL in one get(), otherwise BDB will leak
433 * resources on subsequent get's. We can safely call get()
434 * twice - once for the data, and once to get the DB_NOTFOUND
435 * result meaning there's no more data. See ITS#2040 for details.
436 * This bug is fixed in BDB 4.1 so a smaller buffer will work if
437 * stack space is too limited.
439 * configure now requires Berkeley DB 4.1.
441 #if DB_VERSION_FULL < 0x04010000
442 # define BDB_ENOUGH 5
444 /* We sometimes test with tiny IDLs, and BDB always wants buffers
445 * that are at least one page in size.
447 # if BDB_IDL_DB_SIZE < 4096
448 # define BDB_ENOUGH 2048
450 # define BDB_ENOUGH 1
453 ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
457 Debug( LDAP_DEBUG_ARGS,
458 "bdb_idl_fetch_key: %s\n",
459 bdb_show_key( key, keybuf ), 0, 0 );
461 assert( ids != NULL );
463 if ( saved_cursor && *saved_cursor ) {
465 } else if ( get_flag == LDAP_FILTER_GE ) {
466 opflag = DB_SET_RANGE;
467 } else if ( get_flag == LDAP_FILTER_LE ) {
473 /* only non-range lookups can use the IDL cache */
474 if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
475 rc = bdb_idl_cache_get( bdb, db, key, ids );
476 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
482 data.ulen = sizeof(buf);
483 data.flags = DB_DBT_USERMEM;
485 /* If we're not reusing an existing cursor, get a new one */
486 if( opflag != DB_NEXT ) {
487 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
489 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
490 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
494 cursor = *saved_cursor;
497 /* If this is a LE lookup, save original key so we can determine
498 * when to stop. If this is a GE lookup, save the key since it
499 * will be overwritten.
501 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
503 key2.flags = DB_DBT_USERMEM;
504 key2.ulen = sizeof(keybuf);
506 key2.size = key->size;
507 AC_MEMCPY( keybuf, key->data, key->size );
513 rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
515 /* skip presence key on range inequality lookups */
516 while (rc == 0 && kptr->size != len) {
517 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
519 /* If we're doing a LE compare and the new key is greater than
520 * our search key, we're done
522 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
523 key->data, key->size ) > 0 ) {
531 DB_MULTIPLE_INIT( ptr, &data );
533 DB_MULTIPLE_NEXT(ptr, &data, j, len);
539 rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
541 if ( rc == DB_NOTFOUND ) rc = 0;
543 /* On disk, a range is denoted by 0 in the first element */
545 if (ids[0] != BDB_IDL_RANGE_SIZE) {
546 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
547 "range size mismatch: expected %d, got %ld\n",
548 BDB_IDL_RANGE_SIZE, ids[0], 0 );
549 cursor->c_close( cursor );
552 BDB_IDL_RANGE( ids, ids[2], ids[3] );
554 data.size = BDB_IDL_SIZEOF(ids);
557 if ( saved_cursor && rc == 0 ) {
558 if ( !*saved_cursor )
559 *saved_cursor = cursor;
563 rc2 = cursor->c_close( cursor );
565 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
566 "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
570 if( rc == DB_NOTFOUND ) {
573 } else if( rc != 0 ) {
574 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
575 "get failed: %s (%d)\n",
576 db_strerror(rc), rc, 0 );
579 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
580 /* size not multiple of ID size */
581 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
582 "odd size: expected %ld multiple, got %ld\n",
583 (long) sizeof( ID ), (long) data.size, 0 );
586 } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
588 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
589 "get size mismatch: expected %ld, got %ld\n",
590 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
594 if ( bdb->bi_idl_cache_max_size ) {
595 bdb_idl_cache_put( bdb, db, key, ids, rc );
610 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
614 ID lo, hi, nlo, nhi, nid;
619 Debug( LDAP_DEBUG_ARGS,
620 "bdb_idl_insert_key: %lx %s\n",
621 (long) id, bdb_show_key( key, buf ), 0 );
624 assert( id != NOID );
626 if ( bdb->bi_idl_cache_size ) {
627 bdb_idl_cache_del( bdb, db, key );
631 data.size = sizeof( ID );
632 data.ulen = data.size;
633 data.flags = DB_DBT_USERMEM;
635 BDB_ID2DISK( id, &nid );
637 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
639 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
640 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
644 /* Fetch the first data item for this key, to see if it
645 * exists and if it's a range.
647 rc = cursor->c_get( cursor, key, &data, DB_SET );
651 /* not a range, count the number of items */
653 rc = cursor->c_count( cursor, &count, 0 );
658 if ( count >= BDB_IDL_DB_MAX ) {
659 /* No room, convert to a range */
663 key2.dlen = key2.ulen;
664 key2.flags |= DB_DBT_PARTIAL;
666 BDB_DISK2ID( &nlo, &lo );
669 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
670 if ( rc != 0 && rc != DB_NOTFOUND ) {
671 err = "c_get next_nodup";
674 if ( rc == DB_NOTFOUND ) {
675 rc = cursor->c_get( cursor, key, &data, DB_LAST );
681 rc = cursor->c_get( cursor, key, &data, DB_PREV );
687 BDB_DISK2ID( &nhi, &hi );
688 /* Update hi/lo if needed, then delete all the items
694 } else if ( id > hi ) {
699 /* Don't fetch anything, just position cursor */
700 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
701 data.dlen = data.ulen = 0;
702 rc = cursor->c_get( cursor, key, &data, DB_SET );
707 rc = cursor->c_del( cursor, 0 );
709 err = "c_del range1";
712 /* Delete all the records */
713 for ( i=1; i<count; i++ ) {
714 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_DUP );
716 err = "c_get next_dup";
719 rc = cursor->c_del( cursor, 0 );
725 /* Store the range marker */
726 data.size = data.ulen = sizeof(ID);
727 data.flags = DB_DBT_USERMEM;
729 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
735 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
741 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
747 /* There's room, just store it */
751 /* It's a range, see if we need to rewrite
756 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
761 BDB_DISK2ID( &nlo, &lo );
764 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
769 BDB_DISK2ID( &nhi, &hi );
771 if ( id < lo || id > hi ) {
772 /* Delete the current lo/hi */
773 rc = cursor->c_del( cursor, 0 );
779 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
786 } else if ( rc == DB_NOTFOUND ) {
787 put1: data.data = &nid;
788 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
789 /* Don't worry if it's already there */
790 if ( rc != 0 && rc != DB_KEYEXIST ) {
795 /* initial c_get failed, nothing was done */
797 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
798 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
799 cursor->c_close( cursor );
802 rc = cursor->c_close( cursor );
804 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
805 "c_close failed: %s (%d)\n",
806 db_strerror(rc), rc, 0 );
819 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
823 ID lo, hi, tmp, nid, nlo, nhi;
828 Debug( LDAP_DEBUG_ARGS,
829 "bdb_idl_delete_key: %lx %s\n",
830 (long) id, bdb_show_key( key, buf ), 0 );
832 assert( id != NOID );
834 if ( bdb->bi_idl_cache_max_size ) {
835 bdb_idl_cache_del( bdb, db, key );
838 BDB_ID2DISK( id, &nid );
842 data.size = sizeof( id );
843 data.ulen = data.size;
844 data.flags = DB_DBT_USERMEM;
846 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
848 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
849 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
852 /* Fetch the first data item for this key, to see if it
853 * exists and if it's a range.
855 rc = cursor->c_get( cursor, key, &data, DB_SET );
859 /* Not a range, just delete it */
861 /* position to correct item */
863 rc = cursor->c_get( cursor, key, &data, DB_GET_BOTH );
869 rc = cursor->c_del( cursor, 0 );
875 /* It's a range, see if we need to rewrite
879 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
884 BDB_DISK2ID( &nlo, &lo );
886 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
891 BDB_DISK2ID( &nhi, &hi );
892 if ( id == lo || id == hi ) {
896 } else if ( id == hi ) {
901 /* The range has collapsed... */
902 rc = db->del( db, tid, key, 0 );
909 /* reposition on lo slot */
911 cursor->c_get( cursor, key, &data, DB_PREV );
913 rc = cursor->c_del( cursor, 0 );
920 BDB_ID2DISK( id, &nid );
922 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
931 /* initial c_get failed, nothing was done */
933 if ( rc != DB_NOTFOUND ) {
934 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
935 "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
937 cursor->c_close( cursor );
940 rc = cursor->c_close( cursor );
942 Debug( LDAP_DEBUG_ANY,
943 "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
944 db_strerror(rc), rc, 0 );
952 * idl_intersection - return a = a intersection b
955 bdb_idl_intersection(
961 ID cursora = 0, cursorb = 0, cursorc;
964 if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
969 idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
970 idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
971 if ( idmin > idmax ) {
974 } else if ( idmin == idmax ) {
980 if ( BDB_IDL_IS_RANGE( a ) ) {
981 if ( BDB_IDL_IS_RANGE(b) ) {
982 /* If both are ranges, just shrink the boundaries */
987 /* Else swap so that b is the range, a is a list */
995 /* If a range completely covers the list, the result is
996 * just the list. If idmin to idmax is contiguous, just
997 * turn it into a range.
999 if ( BDB_IDL_IS_RANGE( b )
1000 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
1001 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
1002 if (idmax - idmin + 1 == a[0])
1011 /* Fine, do the intersection one element at a time.
1012 * First advance to idmin in both IDLs.
1014 cursora = cursorb = idmin;
1015 ida = bdb_idl_first( a, &cursora );
1016 idb = bdb_idl_first( b, &cursorb );
1019 while( ida <= idmax || idb <= idmax ) {
1022 ida = bdb_idl_next( a, &cursora );
1023 idb = bdb_idl_next( b, &cursorb );
1024 } else if ( ida < idb ) {
1025 ida = bdb_idl_next( a, &cursora );
1027 idb = bdb_idl_next( b, &cursorb );
1033 BDB_IDL_CPY( b, a );
1040 * idl_union - return a = a union b
1048 ID cursora = 0, cursorb = 0, cursorc;
1050 if ( BDB_IDL_IS_ZERO( b ) ) {
1054 if ( BDB_IDL_IS_ZERO( a ) ) {
1055 BDB_IDL_CPY( a, b );
1059 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1060 over: ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1061 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1068 ida = bdb_idl_first( a, &cursora );
1069 idb = bdb_idl_first( b, &cursorb );
1073 /* The distinct elements of a are cat'd to b */
1074 while( ida != NOID || idb != NOID ) {
1076 if( ++cursorc > BDB_IDL_UM_MAX ) {
1080 ida = bdb_idl_next( a, &cursora );
1084 ida = bdb_idl_next( a, &cursora );
1085 idb = bdb_idl_next( b, &cursorb );
1089 /* b is copied back to a in sorted order */
1094 while (cursorb <= b[0] || cursorc <= a[0]) {
1099 if (cursorb <= b[0] && b[cursorb] < idb)
1100 a[cursora++] = b[cursorb++];
1113 * bdb_idl_notin - return a intersection ~b (or a minus b)
1122 ID cursora = 0, cursorb = 0;
1124 if( BDB_IDL_IS_ZERO( a ) ||
1125 BDB_IDL_IS_ZERO( b ) ||
1126 BDB_IDL_IS_RANGE( b ) )
1128 BDB_IDL_CPY( ids, a );
1132 if( BDB_IDL_IS_RANGE( a ) ) {
1133 BDB_IDL_CPY( ids, a );
1137 ida = bdb_idl_first( a, &cursora ),
1138 idb = bdb_idl_first( b, &cursorb );
1142 while( ida != NOID ) {
1143 if ( idb == NOID ) {
1144 /* we could shortcut this */
1145 ids[++ids[0]] = ida;
1146 ida = bdb_idl_next( a, &cursora );
1148 } else if ( ida < idb ) {
1149 ids[++ids[0]] = ida;
1150 ida = bdb_idl_next( a, &cursora );
1152 } else if ( ida > idb ) {
1153 idb = bdb_idl_next( b, &cursorb );
1156 ida = bdb_idl_next( a, &cursora );
1157 idb = bdb_idl_next( b, &cursorb );
1165 ID bdb_idl_first( ID *ids, ID *cursor )
1169 if ( ids[0] == 0 ) {
1174 if ( BDB_IDL_IS_RANGE( ids ) ) {
1175 if( *cursor < ids[1] ) {
1184 pos = bdb_idl_search( ids, *cursor );
1186 if( pos > ids[0] ) {
1194 ID bdb_idl_next( ID *ids, ID *cursor )
1196 if ( BDB_IDL_IS_RANGE( ids ) ) {
1197 if( ids[2] < ++(*cursor) ) {
1203 if ( ++(*cursor) <= ids[0] ) {
1204 return ids[*cursor];
1210 /* Add one ID to an unsorted list. We still maintain a lo/hi reference
1211 * for fast range compaction.
1213 int bdb_idl_append_one( ID *ids, ID id )
1215 if (BDB_IDL_IS_RANGE( ids )) {
1216 /* if already in range, treat as a dup */
1217 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
1219 if (id < BDB_IDL_FIRST(ids))
1221 else if (id > BDB_IDL_LAST(ids))
1232 } else if ( ids[0] > 1 && id > ids[2] ) {
1239 if ( ids[0] >= BDB_IDL_UM_MAX ) {
1247 /* Append unsorted list b to unsorted list a. Both lists must have their
1248 * lowest value in slot 1 and highest value in slot 2.
1250 int bdb_idl_append( ID *a, ID *b )
1254 if ( BDB_IDL_IS_ZERO( b ) ) {
1258 if ( BDB_IDL_IS_ZERO( a ) ) {
1259 BDB_IDL_CPY( a, b );
1263 if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ||
1264 a[0] + b[0] >= BDB_IDL_UM_MAX ) {
1265 ida = IDL_MIN( a[1], b[1] );
1266 idb = IDL_MAX( a[2], b[2] );
1273 if ( b[1] < a[1] ) {
1282 if ( b[0] > 1 && b[2] > a[2] ) {
1293 AC_MEMCPY(a+a[0]+1, b+3, i * sizeof(ID));
1300 /* Quicksort + Insertion sort for small arrays */
1303 #define SWAP(a,b) a^=b;b^=a;a^=b /* Swap integers without temp var */
1306 bdb_idl_sort( ID *ids )
1308 int istack[BDB_IDL_LOGN*4];
1309 int i,j,k,l,ir,jstack;
1312 if ( BDB_IDL_IS_RANGE( ids ))
1319 if (ir - l < SMALL) { /* Insertion sort */
1320 for (j=l+1;j<=ir;j++) {
1322 for (i=j-1;i>=1;i--) {
1323 if (ids[i] <= a) break;
1328 if (jstack == 0) break;
1329 ir = istack[jstack--];
1330 l = istack[jstack--];
1332 k = (l + ir) >> 1; /* Choose median of left, center, right */
1333 SWAP(ids[k], ids[l+1]);
1334 if (ids[l] > ids[ir]) {
1335 SWAP(ids[l], ids[ir]);
1337 if (ids[l+1] > ids[ir]) {
1338 SWAP(ids[l+1], ids[ir]);
1340 if (ids[l] > ids[l+1]) {
1341 SWAP(ids[l], ids[l+1]);
1347 do i++; while(ids[i] < a);
1348 do j--; while(ids[j] > a);
1350 SWAP(ids[i],ids[j]);
1355 assert(jstack <= BDB_IDL_LOGN*4);
1356 if (ir-i+1 >= j-1) {
1357 istack[jstack] = ir;
1358 istack[jstack-1] = i;
1361 istack[jstack] = j-1;
1362 istack[jstack-1] = l;