| 1 |
/* OpenLDAP WiredTiger backend */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2002-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted only as authorized by the OpenLDAP |
| 10 |
* Public License. |
| 11 |
* |
| 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>. |
| 15 |
*/ |
| 16 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was developed by HAMANO Tsukasa <[email protected]> |
| 18 |
* based on back-bdb for inclusion in OpenLDAP Software. |
| 19 |
* WiredTiger is a product of MongoDB Inc. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
|
| 27 |
#include "back-wt.h" |
| 28 |
#include "idl.h" |
| 29 |
|
| 30 |
#define IDL_MAX(x,y) ( (x) > (y) ? (x) : (y) ) |
| 31 |
#define IDL_MIN(x,y) ( (x) < (y) ? (x) : (y) ) |
| 32 |
#define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) ) |
| 33 |
|
| 34 |
void wt_idl_check( ID *ids ) |
| 35 |
{ |
| 36 |
if( WT_IDL_IS_RANGE( ids ) ) { |
| 37 |
assert( WT_IDL_RANGE_FIRST(ids) <= WT_IDL_RANGE_LAST(ids) ); |
| 38 |
} else { |
| 39 |
ID i; |
| 40 |
for( i=1; i < ids[0]; i++ ) { |
| 41 |
assert( ids[i+1] > ids[i] ); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void wt_idl_dump( ID *ids ) |
| 47 |
{ |
| 48 |
if( WT_IDL_IS_RANGE( ids ) ) { |
| 49 |
Debug( LDAP_DEBUG_ANY, |
| 50 |
"IDL: range ( %ld - %ld )\n", |
| 51 |
(long) WT_IDL_RANGE_FIRST( ids ), |
| 52 |
(long) WT_IDL_RANGE_LAST( ids ) ); |
| 53 |
|
| 54 |
} else { |
| 55 |
ID i; |
| 56 |
Debug( LDAP_DEBUG_ANY, "IDL: size %ld\n", (long) ids[0] ); |
| 57 |
|
| 58 |
for( i=1; i<=ids[0]; i++ ) { |
| 59 |
Debug( LDAP_DEBUG_ANY, " %ld\n", (long) ids[i] ); |
| 60 |
} |
| 61 |
|
| 62 |
Debug( LDAP_DEBUG_ANY, "\n" ); |
| 63 |
} |
| 64 |
|
| 65 |
wt_idl_check( ids ); |
| 66 |
} |
| 67 |
|
| 68 |
unsigned wt_idl_search( ID *ids, ID id ) |
| 69 |
{ |
| 70 |
#define IDL_BINARY_SEARCH 1 |
| 71 |
#ifdef IDL_BINARY_SEARCH |
| 72 |
/* |
| 73 |
* binary search of id in ids |
| 74 |
* if found, returns position of id |
| 75 |
* if not found, returns first position greater than id |
| 76 |
*/ |
| 77 |
unsigned base = 0; |
| 78 |
unsigned cursor = 1; |
| 79 |
int val = 0; |
| 80 |
unsigned n = ids[0]; |
| 81 |
|
| 82 |
#if IDL_DEBUG > 0 |
| 83 |
idl_check( ids ); |
| 84 |
#endif |
| 85 |
|
| 86 |
while( 0 < n ) { |
| 87 |
unsigned pivot = n >> 1; |
| 88 |
cursor = base + pivot + 1; |
| 89 |
val = IDL_CMP( id, ids[cursor] ); |
| 90 |
|
| 91 |
if( val < 0 ) { |
| 92 |
n = pivot; |
| 93 |
|
| 94 |
} else if ( val > 0 ) { |
| 95 |
base = cursor; |
| 96 |
n -= pivot + 1; |
| 97 |
|
| 98 |
} else { |
| 99 |
return cursor; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if( val > 0 ) { |
| 104 |
++cursor; |
| 105 |
} |
| 106 |
return cursor; |
| 107 |
|
| 108 |
#else |
| 109 |
/* (reverse) linear search */ |
| 110 |
int i; |
| 111 |
|
| 112 |
#if IDL_DEBUG > 0 |
| 113 |
idl_check( ids ); |
| 114 |
#endif |
| 115 |
|
| 116 |
for( i=ids[0]; i; i-- ) { |
| 117 |
if( id > ids[i] ) { |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return i+1; |
| 123 |
#endif |
| 124 |
} |
| 125 |
|
| 126 |
int wt_idl_insert( ID *ids, ID id ) |
| 127 |
{ |
| 128 |
unsigned x; |
| 129 |
|
| 130 |
#if IDL_DEBUG > 1 |
| 131 |
Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x ); |
| 132 |
idl_dump( ids ); |
| 133 |
#elif IDL_DEBUG > 0 |
| 134 |
wt_idl_check( ids ); |
| 135 |
#endif |
| 136 |
|
| 137 |
if (WT_IDL_IS_RANGE( ids )) { |
| 138 |
/* if already in range, treat as a dup */ |
| 139 |
if (id >= WT_IDL_RANGE_FIRST(ids) && id <= WT_IDL_RANGE_LAST(ids)) |
| 140 |
return -1; |
| 141 |
if (id < WT_IDL_RANGE_FIRST(ids)) |
| 142 |
ids[1] = id; |
| 143 |
else if (id > WT_IDL_RANGE_LAST(ids)) |
| 144 |
ids[2] = id; |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
x = wt_idl_search( ids, id ); |
| 149 |
assert( x > 0 ); |
| 150 |
|
| 151 |
if( x < 1 ) { |
| 152 |
/* internal error */ |
| 153 |
return -2; |
| 154 |
} |
| 155 |
|
| 156 |
if ( x <= ids[0] && ids[x] == id ) { |
| 157 |
/* duplicate */ |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
if ( ++ids[0] >= WT_IDL_DB_MAX ) { |
| 162 |
if( id < ids[1] ) { |
| 163 |
ids[1] = id; |
| 164 |
ids[2] = ids[ids[0]-1]; |
| 165 |
} else if ( ids[ids[0]-1] < id ) { |
| 166 |
ids[2] = id; |
| 167 |
} else { |
| 168 |
ids[2] = ids[ids[0]-1]; |
| 169 |
} |
| 170 |
ids[0] = NOID; |
| 171 |
|
| 172 |
} else { |
| 173 |
/* insert id */ |
| 174 |
AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) ); |
| 175 |
ids[x] = id; |
| 176 |
} |
| 177 |
|
| 178 |
#if IDL_DEBUG > 1 |
| 179 |
wt_idl_dump( ids ); |
| 180 |
#elif IDL_DEBUG > 0 |
| 181 |
wt_idl_check( ids ); |
| 182 |
#endif |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int wt_idl_delete( ID *ids, ID id ) |
| 188 |
{ |
| 189 |
unsigned x; |
| 190 |
|
| 191 |
#if IDL_DEBUG > 1 |
| 192 |
Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x ); |
| 193 |
idl_dump( ids ); |
| 194 |
#elif IDL_DEBUG > 0 |
| 195 |
wt_idl_check( ids ); |
| 196 |
#endif |
| 197 |
|
| 198 |
if (WT_IDL_IS_RANGE( ids )) { |
| 199 |
/* If deleting a range boundary, adjust */ |
| 200 |
if ( ids[1] == id ) |
| 201 |
ids[1]++; |
| 202 |
else if ( ids[2] == id ) |
| 203 |
ids[2]--; |
| 204 |
/* deleting from inside a range is a no-op */ |
| 205 |
|
| 206 |
/* If the range has collapsed, re-adjust */ |
| 207 |
if ( ids[1] > ids[2] ) |
| 208 |
ids[0] = 0; |
| 209 |
else if ( ids[1] == ids[2] ) |
| 210 |
ids[1] = 1; |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
x = wt_idl_search( ids, id ); |
| 215 |
assert( x > 0 ); |
| 216 |
|
| 217 |
if( x <= 0 ) { |
| 218 |
/* internal error */ |
| 219 |
return -2; |
| 220 |
} |
| 221 |
|
| 222 |
if( x > ids[0] || ids[x] != id ) { |
| 223 |
/* not found */ |
| 224 |
return -1; |
| 225 |
|
| 226 |
} else if ( --ids[0] == 0 ) { |
| 227 |
if( x != 1 ) { |
| 228 |
return -3; |
| 229 |
} |
| 230 |
|
| 231 |
} else { |
| 232 |
AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) ); |
| 233 |
} |
| 234 |
|
| 235 |
#if IDL_DEBUG > 1 |
| 236 |
wt_idl_dump( ids ); |
| 237 |
#elif IDL_DEBUG > 0 |
| 238 |
wt_idl_check( ids ); |
| 239 |
#endif |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
static char * |
| 245 |
wt_show_key( |
| 246 |
char *buf, |
| 247 |
void *val, |
| 248 |
size_t len ) |
| 249 |
{ |
| 250 |
if ( len == 4 /* LUTIL_HASH_BYTES */ ) { |
| 251 |
unsigned char *c = val; |
| 252 |
sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] ); |
| 253 |
return buf; |
| 254 |
} else { |
| 255 |
return val; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* idl_intersection - return a = a intersection b |
| 261 |
*/ |
| 262 |
int |
| 263 |
wt_idl_intersection( |
| 264 |
ID *a, |
| 265 |
ID *b ) |
| 266 |
{ |
| 267 |
ID ida, idb; |
| 268 |
ID idmax, idmin; |
| 269 |
ID cursora = 0, cursorb = 0, cursorc; |
| 270 |
int swap = 0; |
| 271 |
|
| 272 |
if ( WT_IDL_IS_ZERO( a ) || WT_IDL_IS_ZERO( b ) ) { |
| 273 |
a[0] = 0; |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
idmin = IDL_MAX( WT_IDL_FIRST(a), WT_IDL_FIRST(b) ); |
| 278 |
idmax = IDL_MIN( WT_IDL_LAST(a), WT_IDL_LAST(b) ); |
| 279 |
if ( idmin > idmax ) { |
| 280 |
a[0] = 0; |
| 281 |
return 0; |
| 282 |
} else if ( idmin == idmax && |
| 283 |
(( WT_IDL_FIRST(a) == WT_IDL_LAST(b)) || |
| 284 |
( WT_IDL_FIRST(b) == WT_IDL_LAST(a)))) { |
| 285 |
a[0] = 1; |
| 286 |
a[1] = idmin; |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
if ( WT_IDL_IS_RANGE( a ) ) { |
| 291 |
if ( WT_IDL_IS_RANGE(b) ) { |
| 292 |
/* If both are ranges, just shrink the boundaries */ |
| 293 |
a[1] = idmin; |
| 294 |
a[2] = idmax; |
| 295 |
return 0; |
| 296 |
} else { |
| 297 |
/* Else swap so that b is the range, a is a list */ |
| 298 |
ID *tmp = a; |
| 299 |
a = b; |
| 300 |
b = tmp; |
| 301 |
swap = 1; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/* If a range completely covers the list, the result is |
| 306 |
* just the list. If idmin to idmax is contiguous, just |
| 307 |
* turn it into a range. |
| 308 |
*/ |
| 309 |
if ( WT_IDL_IS_RANGE( b ) |
| 310 |
&& WT_IDL_RANGE_FIRST( b ) <= WT_IDL_FIRST( a ) |
| 311 |
&& WT_IDL_RANGE_LAST( b ) >= WT_IDL_LLAST( a ) ) { |
| 312 |
if (idmax - idmin + 1 == a[0]) |
| 313 |
{ |
| 314 |
a[0] = NOID; |
| 315 |
a[1] = idmin; |
| 316 |
a[2] = idmax; |
| 317 |
} |
| 318 |
goto done; |
| 319 |
} |
| 320 |
|
| 321 |
/* Fine, do the intersection one element at a time. |
| 322 |
* First advance to idmin in both IDLs. |
| 323 |
*/ |
| 324 |
cursora = cursorb = idmin; |
| 325 |
ida = wt_idl_first( a, &cursora ); |
| 326 |
idb = wt_idl_first( b, &cursorb ); |
| 327 |
cursorc = 0; |
| 328 |
|
| 329 |
while( ida <= idmax || idb <= idmax ) { |
| 330 |
if( ida == idb ) { |
| 331 |
a[++cursorc] = ida; |
| 332 |
ida = wt_idl_next( a, &cursora ); |
| 333 |
idb = wt_idl_next( b, &cursorb ); |
| 334 |
} else if ( ida < idb ) { |
| 335 |
ida = wt_idl_next( a, &cursora ); |
| 336 |
} else { |
| 337 |
idb = wt_idl_next( b, &cursorb ); |
| 338 |
} |
| 339 |
} |
| 340 |
a[0] = cursorc; |
| 341 |
done: |
| 342 |
if (swap) |
| 343 |
WT_IDL_CPY( b, a ); |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/* |
| 350 |
* idl_union - return a = a union b |
| 351 |
*/ |
| 352 |
int |
| 353 |
wt_idl_union( |
| 354 |
ID *a, |
| 355 |
ID *b ) |
| 356 |
{ |
| 357 |
ID ida, idb; |
| 358 |
ID cursora = 0, cursorb = 0, cursorc; |
| 359 |
|
| 360 |
if ( WT_IDL_IS_ZERO( b ) ) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
if ( WT_IDL_IS_ZERO( a ) ) { |
| 365 |
WT_IDL_CPY( a, b ); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
if ( WT_IDL_IS_RANGE( a ) || WT_IDL_IS_RANGE(b) ) { |
| 370 |
over: ida = IDL_MIN( WT_IDL_FIRST(a), WT_IDL_FIRST(b) ); |
| 371 |
idb = IDL_MAX( WT_IDL_LAST(a), WT_IDL_LAST(b) ); |
| 372 |
a[0] = NOID; |
| 373 |
a[1] = ida; |
| 374 |
a[2] = idb; |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
ida = wt_idl_first( a, &cursora ); |
| 379 |
idb = wt_idl_first( b, &cursorb ); |
| 380 |
|
| 381 |
cursorc = b[0]; |
| 382 |
|
| 383 |
/* The distinct elements of a are cat'd to b */ |
| 384 |
while( ida != NOID || idb != NOID ) { |
| 385 |
if ( ida < idb ) { |
| 386 |
if( ++cursorc > WT_IDL_UM_MAX ) { |
| 387 |
goto over; |
| 388 |
} |
| 389 |
b[cursorc] = ida; |
| 390 |
ida = wt_idl_next( a, &cursora ); |
| 391 |
|
| 392 |
} else { |
| 393 |
if ( ida == idb ) |
| 394 |
ida = wt_idl_next( a, &cursora ); |
| 395 |
idb = wt_idl_next( b, &cursorb ); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/* b is copied back to a in sorted order */ |
| 400 |
a[0] = cursorc; |
| 401 |
cursora = 1; |
| 402 |
cursorb = 1; |
| 403 |
cursorc = b[0]+1; |
| 404 |
while (cursorb <= b[0] || cursorc <= a[0]) { |
| 405 |
if (cursorc > a[0]) |
| 406 |
idb = NOID; |
| 407 |
else |
| 408 |
idb = b[cursorc]; |
| 409 |
if (cursorb <= b[0] && b[cursorb] < idb) |
| 410 |
a[cursora++] = b[cursorb++]; |
| 411 |
else { |
| 412 |
a[cursora++] = idb; |
| 413 |
cursorc++; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
#if 0 |
| 422 |
/* |
| 423 |
* wt_idl_notin - return a intersection ~b (or a minus b) |
| 424 |
*/ |
| 425 |
int |
| 426 |
wt_idl_notin( |
| 427 |
ID *a, |
| 428 |
ID *b, |
| 429 |
ID *ids ) |
| 430 |
{ |
| 431 |
ID ida, idb; |
| 432 |
ID cursora = 0, cursorb = 0; |
| 433 |
|
| 434 |
if( WT_IDL_IS_ZERO( a ) || |
| 435 |
WT_IDL_IS_ZERO( b ) || |
| 436 |
WT_IDL_IS_RANGE( b ) ) |
| 437 |
{ |
| 438 |
WT_IDL_CPY( ids, a ); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
if( WT_IDL_IS_RANGE( a ) ) { |
| 443 |
WT_IDL_CPY( ids, a ); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
ida = wt_idl_first( a, &cursora ), |
| 448 |
idb = wt_idl_first( b, &cursorb ); |
| 449 |
|
| 450 |
ids[0] = 0; |
| 451 |
|
| 452 |
while( ida != NOID ) { |
| 453 |
if ( idb == NOID ) { |
| 454 |
/* we could shortcut this */ |
| 455 |
ids[++ids[0]] = ida; |
| 456 |
ida = wt_idl_next( a, &cursora ); |
| 457 |
|
| 458 |
} else if ( ida < idb ) { |
| 459 |
ids[++ids[0]] = ida; |
| 460 |
ida = wt_idl_next( a, &cursora ); |
| 461 |
|
| 462 |
} else if ( ida > idb ) { |
| 463 |
idb = wt_idl_next( b, &cursorb ); |
| 464 |
|
| 465 |
} else { |
| 466 |
ida = wt_idl_next( a, &cursora ); |
| 467 |
idb = wt_idl_next( b, &cursorb ); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |
| 473 |
#endif |
| 474 |
|
| 475 |
ID wt_idl_first( ID *ids, ID *cursor ) |
| 476 |
{ |
| 477 |
ID pos; |
| 478 |
|
| 479 |
if ( ids[0] == 0 ) { |
| 480 |
*cursor = NOID; |
| 481 |
return NOID; |
| 482 |
} |
| 483 |
|
| 484 |
if ( WT_IDL_IS_RANGE( ids ) ) { |
| 485 |
if( *cursor < ids[1] ) { |
| 486 |
*cursor = ids[1]; |
| 487 |
} |
| 488 |
return *cursor; |
| 489 |
} |
| 490 |
|
| 491 |
if ( *cursor == 0 ) |
| 492 |
pos = 1; |
| 493 |
else |
| 494 |
pos = wt_idl_search( ids, *cursor ); |
| 495 |
|
| 496 |
if( pos > ids[0] ) { |
| 497 |
return NOID; |
| 498 |
} |
| 499 |
|
| 500 |
*cursor = pos; |
| 501 |
return ids[pos]; |
| 502 |
} |
| 503 |
|
| 504 |
ID wt_idl_next( ID *ids, ID *cursor ) |
| 505 |
{ |
| 506 |
if ( WT_IDL_IS_RANGE( ids ) ) { |
| 507 |
if( ids[2] < ++(*cursor) ) { |
| 508 |
return NOID; |
| 509 |
} |
| 510 |
return *cursor; |
| 511 |
} |
| 512 |
|
| 513 |
if ( ++(*cursor) <= ids[0] ) { |
| 514 |
return ids[*cursor]; |
| 515 |
} |
| 516 |
|
| 517 |
return NOID; |
| 518 |
} |
| 519 |
|
| 520 |
/* Add one ID to an unsorted list. We ensure that the first element is the |
| 521 |
* minimum and the last element is the maximum, for fast range compaction. |
| 522 |
* this means IDLs up to length 3 are always sorted... |
| 523 |
*/ |
| 524 |
int wt_idl_append_one( ID *ids, ID id ) |
| 525 |
{ |
| 526 |
if (WT_IDL_IS_RANGE( ids )) { |
| 527 |
/* if already in range, treat as a dup */ |
| 528 |
if (id >= WT_IDL_RANGE_FIRST(ids) && id <= WT_IDL_RANGE_LAST(ids)) |
| 529 |
return -1; |
| 530 |
if (id < WT_IDL_RANGE_FIRST(ids)) |
| 531 |
ids[1] = id; |
| 532 |
else if (id > WT_IDL_RANGE_LAST(ids)) |
| 533 |
ids[2] = id; |
| 534 |
return 0; |
| 535 |
} |
| 536 |
if ( ids[0] ) { |
| 537 |
ID tmp; |
| 538 |
|
| 539 |
if (id < ids[1]) { |
| 540 |
tmp = ids[1]; |
| 541 |
ids[1] = id; |
| 542 |
id = tmp; |
| 543 |
} |
| 544 |
if ( ids[0] > 1 && id < ids[ids[0]] ) { |
| 545 |
tmp = ids[ids[0]]; |
| 546 |
ids[ids[0]] = id; |
| 547 |
id = tmp; |
| 548 |
} |
| 549 |
} |
| 550 |
ids[0]++; |
| 551 |
if ( ids[0] >= WT_IDL_UM_MAX ) { |
| 552 |
ids[0] = NOID; |
| 553 |
ids[2] = id; |
| 554 |
} else { |
| 555 |
ids[ids[0]] = id; |
| 556 |
} |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
/* Append sorted list b to sorted list a. The result is unsorted but |
| 561 |
* a[1] is the min of the result and a[a[0]] is the max. |
| 562 |
*/ |
| 563 |
int wt_idl_append( ID *a, ID *b ) |
| 564 |
{ |
| 565 |
ID ida, idb, tmp, swap = 0; |
| 566 |
|
| 567 |
if ( WT_IDL_IS_ZERO( b ) ) { |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
if ( WT_IDL_IS_ZERO( a ) ) { |
| 572 |
WT_IDL_CPY( a, b ); |
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
ida = WT_IDL_LAST( a ); |
| 577 |
idb = WT_IDL_LAST( b ); |
| 578 |
if ( WT_IDL_IS_RANGE( a ) || WT_IDL_IS_RANGE(b) || |
| 579 |
a[0] + b[0] >= WT_IDL_UM_MAX ) { |
| 580 |
a[2] = IDL_MAX( ida, idb ); |
| 581 |
a[1] = IDL_MIN( a[1], b[1] ); |
| 582 |
a[0] = NOID; |
| 583 |
return 0; |
| 584 |
} |
| 585 |
|
| 586 |
if ( b[0] > 1 && ida > idb ) { |
| 587 |
swap = idb; |
| 588 |
a[a[0]] = idb; |
| 589 |
b[b[0]] = ida; |
| 590 |
} |
| 591 |
|
| 592 |
if ( b[1] < a[1] ) { |
| 593 |
tmp = a[1]; |
| 594 |
a[1] = b[1]; |
| 595 |
} else { |
| 596 |
tmp = b[1]; |
| 597 |
} |
| 598 |
a[0]++; |
| 599 |
a[a[0]] = tmp; |
| 600 |
|
| 601 |
if ( b[0] > 1 ) { |
| 602 |
int i = b[0] - 1; |
| 603 |
AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID)); |
| 604 |
a[0] += i; |
| 605 |
} |
| 606 |
if ( swap ) { |
| 607 |
b[b[0]] = swap; |
| 608 |
} |
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
#if 1 |
| 613 |
|
| 614 |
/* Quicksort + Insertion sort for small arrays */ |
| 615 |
|
| 616 |
#define SMALL 8 |
| 617 |
#define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp |
| 618 |
|
| 619 |
void |
| 620 |
wt_idl_sort( ID *ids, ID *tmp ) |
| 621 |
{ |
| 622 |
int *istack = (int *)tmp; /* Private stack, not used by caller */ |
| 623 |
int i,j,k,l,ir,jstack; |
| 624 |
ID a, itmp; |
| 625 |
|
| 626 |
if ( WT_IDL_IS_RANGE( ids )) |
| 627 |
return; |
| 628 |
|
| 629 |
ir = ids[0]; |
| 630 |
l = 1; |
| 631 |
jstack = 0; |
| 632 |
for(;;) { |
| 633 |
if (ir - l < SMALL) { /* Insertion sort */ |
| 634 |
for (j=l+1;j<=ir;j++) { |
| 635 |
a = ids[j]; |
| 636 |
for (i=j-1;i>=1;i--) { |
| 637 |
if (ids[i] <= a) break; |
| 638 |
ids[i+1] = ids[i]; |
| 639 |
} |
| 640 |
ids[i+1] = a; |
| 641 |
} |
| 642 |
if (jstack == 0) break; |
| 643 |
ir = istack[jstack--]; |
| 644 |
l = istack[jstack--]; |
| 645 |
} else { |
| 646 |
k = (l + ir) >> 1; /* Choose median of left, center, right */ |
| 647 |
SWAP(ids[k], ids[l+1]); |
| 648 |
if (ids[l] > ids[ir]) { |
| 649 |
SWAP(ids[l], ids[ir]); |
| 650 |
} |
| 651 |
if (ids[l+1] > ids[ir]) { |
| 652 |
SWAP(ids[l+1], ids[ir]); |
| 653 |
} |
| 654 |
if (ids[l] > ids[l+1]) { |
| 655 |
SWAP(ids[l], ids[l+1]); |
| 656 |
} |
| 657 |
i = l+1; |
| 658 |
j = ir; |
| 659 |
a = ids[l+1]; |
| 660 |
for(;;) { |
| 661 |
do i++; while(ids[i] < a); |
| 662 |
do j--; while(ids[j] > a); |
| 663 |
if (j < i) break; |
| 664 |
SWAP(ids[i],ids[j]); |
| 665 |
} |
| 666 |
ids[l+1] = ids[j]; |
| 667 |
ids[j] = a; |
| 668 |
jstack += 2; |
| 669 |
if (ir-i+1 >= j-l) { |
| 670 |
istack[jstack] = ir; |
| 671 |
istack[jstack-1] = i; |
| 672 |
ir = j-1; |
| 673 |
} else { |
| 674 |
istack[jstack] = j-1; |
| 675 |
istack[jstack-1] = l; |
| 676 |
l = i; |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
#else |
| 683 |
|
| 684 |
/* 8 bit Radix sort + insertion sort |
| 685 |
* |
| 686 |
* based on code from http://www.cubic.org/docs/radix.htm |
| 687 |
* with improvements by [email protected] and [email protected] |
| 688 |
* |
| 689 |
* This code is O(n) but has a relatively high constant factor. For lists |
| 690 |
* up to ~50 Quicksort is slightly faster; up to ~100 they are even. |
| 691 |
* Much faster than quicksort for lists longer than ~100. Insertion |
| 692 |
* sort is actually superior for lists <50. |
| 693 |
*/ |
| 694 |
|
| 695 |
#define BUCKETS (1<<8) |
| 696 |
#define SMALL 50 |
| 697 |
|
| 698 |
void |
| 699 |
wt_idl_sort( ID *ids, ID *tmp ) |
| 700 |
{ |
| 701 |
int count, soft_limit, phase = 0, size = ids[0]; |
| 702 |
ID *idls[2]; |
| 703 |
unsigned char *maxv = (unsigned char *)&ids[size]; |
| 704 |
|
| 705 |
if ( WT_IDL_IS_RANGE( ids )) |
| 706 |
return; |
| 707 |
|
| 708 |
/* Use insertion sort for small lists */ |
| 709 |
if ( size <= SMALL ) { |
| 710 |
int i,j; |
| 711 |
ID a; |
| 712 |
|
| 713 |
for (j=1;j<=size;j++) { |
| 714 |
a = ids[j]; |
| 715 |
for (i=j-1;i>=1;i--) { |
| 716 |
if (ids[i] <= a) break; |
| 717 |
ids[i+1] = ids[i]; |
| 718 |
} |
| 719 |
ids[i+1] = a; |
| 720 |
} |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
tmp[0] = size; |
| 725 |
idls[0] = ids; |
| 726 |
idls[1] = tmp; |
| 727 |
|
| 728 |
#if BYTE_ORDER == BIG_ENDIAN |
| 729 |
for (soft_limit = 0; !maxv[soft_limit]; soft_limit++); |
| 730 |
#else |
| 731 |
for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--); |
| 732 |
#endif |
| 733 |
|
| 734 |
for ( |
| 735 |
#if BYTE_ORDER == BIG_ENDIAN |
| 736 |
count = sizeof(ID)-1; count >= soft_limit; --count |
| 737 |
#else |
| 738 |
count = 0; count <= soft_limit; ++count |
| 739 |
#endif |
| 740 |
) { |
| 741 |
unsigned int num[BUCKETS], * np, n, sum; |
| 742 |
int i; |
| 743 |
ID *sp, *source, *dest; |
| 744 |
unsigned char *bp, *source_start; |
| 745 |
|
| 746 |
source = idls[phase]+1; |
| 747 |
dest = idls[phase^1]+1; |
| 748 |
source_start = ((unsigned char *) source) + count; |
| 749 |
|
| 750 |
np = num; |
| 751 |
for ( i = BUCKETS; i > 0; --i ) *np++ = 0; |
| 752 |
|
| 753 |
/* count occurrences of every byte value */ |
| 754 |
bp = source_start; |
| 755 |
for ( i = size; i > 0; --i, bp += sizeof(ID) ) |
| 756 |
num[*bp]++; |
| 757 |
|
| 758 |
/* transform count into index by summing elements and storing |
| 759 |
* into same array |
| 760 |
*/ |
| 761 |
sum = 0; |
| 762 |
np = num; |
| 763 |
for ( i = BUCKETS; i > 0; --i ) { |
| 764 |
n = *np; |
| 765 |
*np++ = sum; |
| 766 |
sum += n; |
| 767 |
} |
| 768 |
|
| 769 |
/* fill dest with the right values in the right place */ |
| 770 |
bp = source_start; |
| 771 |
sp = source; |
| 772 |
for ( i = size; i > 0; --i, bp += sizeof(ID) ) { |
| 773 |
np = num + *bp; |
| 774 |
dest[*np] = *sp++; |
| 775 |
++(*np); |
| 776 |
} |
| 777 |
phase ^= 1; |
| 778 |
} |
| 779 |
|
| 780 |
/* copy back from temp if needed */ |
| 781 |
if ( phase ) { |
| 782 |
ids++; tmp++; |
| 783 |
for ( count = 0; count < size; ++count ) |
| 784 |
*ids++ = *tmp++; |
| 785 |
} |
| 786 |
} |
| 787 |
#endif /* Quick vs Radix */ |
| 788 |
|