14 void fail(
const char* fmt, ... )
18 vfprintf( stderr, fmt, ap );
38 this->buffSize = sizeIn;
39 void* res = malloc( this->buffSize );
40 if( !res && buffSize > 0 )
fail(
"%s: allocation of %d bytes failed\n", file, (
int)buffSize );
46 if( this->buffSize < min )
48 size_t newSize = this->buffSize;
49 newSize += newSize / 2 + 1;
50 if( newSize < min ) newSize = min;
51 void* res = realloc( ptr, newSize );
52 if( !res && newSize > 0 )
fail(
"%s: reallocation of %d bytes failed\n", file, newSize );
54 this->buffSize = newSize;
72 : vi_rd( NULL ), vl_rd( NULL ), vul_rd( NULL ), vr_rd( NULL ), mi( 0 ), ml( 0 ), mul( 0 ), mr( 0 ), n( 0 ),
73 max( 0 ), vi( NULL ), vl( NULL ), vul( NULL ), vr( NULL ), last_sorted( -1 )
92 void* resi = malloc( sz );
93 if( !resi &&
max *
mi > 0 )
fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
101 void* resl = malloc( sz );
102 if( !resl &&
max *
ml > 0 )
fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
110 void* resu = malloc( sz );
111 if( !resu &&
max *
mul > 0 )
fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
119 void* resr = malloc( sz );
120 if( !resr &&
max *
ml > 0 )
fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
145 void* resi = realloc(
vi, sz );
146 if( !resi &&
max *
mi > 0 )
148 fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
156 void* resl = realloc(
vl, sz );
157 if( !resl &&
max *
ml > 0 )
159 fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
167 void* resu = realloc(
vul, sz );
168 if( !resu &&
max *
mul > 0 )
170 fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
178 void* resr = realloc(
vr, sz );
179 if( !resr &&
max *
mr > 0 )
181 fail(
"%s: allocation of %d bytes failed\n", __FILE__, (
int)sz );
239 long uvalue = (long)value;
240 if( !( key_num >
mi ) )
245 int lb = 0, ub =
n, index;
248 index = ( lb + ub ) / 2;
249 if(
vi[index *
mi + key_num] == uvalue )
251 else if(
vi[index *
mi + key_num] > uvalue )
253 else if(
vi[index *
mi + key_num] < uvalue )
260 for(
uint index = 0; index <
n; index++ )
262 if(
vi[index *
mi + key_num] == uvalue )
return index;
271 long uvalue = (long)value;
272 if( !( key_num >
ml ) )
276 int lb = 0, ub =
n, index;
279 index = ( lb + ub ) / 2;
280 if(
vl[index *
ml + key_num] == uvalue )
282 else if(
vl[index *
ml + key_num] > uvalue )
284 else if(
vl[index *
ml + key_num] < uvalue )
291 for(
uint index = 0; index <
n; index++ )
293 if(
vl[index *
ml + key_num] == uvalue )
return index;
302 if( !( key_num >
mul ) )
306 int lb = 0, ub =
n - 1, index;
309 index = ( lb + ub ) / 2;
310 if(
vul[index *
mul + key_num] == value )
312 else if(
vul[index *
mul + key_num] > value )
314 else if(
vul[index *
mul + key_num] < value )
321 for(
uint index = 0; index <
n; index++ )
323 if(
vul[index *
mul + key_num] == value )
return index;
332 if( !( key_num >
mr ) )
335 for(
uint index = 0; index <
n; index++ )
337 if(
vr[index *
mr + key_num] == value )
return index;
345 if(
mi > m &&
n > index )
return vi[index *
mi + m];
351 if(
ml > m &&
n > index )
return vl[index *
ml + m];
357 if(
mul > m &&
n > index )
return vul[index *
mul + m];
363 if(
mr > m &&
n > index )
return vr[index *
mr + m];
372 *&sp = &
vi[index *
mi];
376 *&ip = &
vl[index *
ml];
384 *&dp = &
vr[index *
mr];
396 if(
mi ) memcpy( &
vi[
mi * (
n - 1 )], sp,
mi *
sizeof(
sint ) );
397 if(
ml ) memcpy( &
vl[
ml * (
n - 1 )], ip,
ml *
sizeof(
long ) );
455 std::cout <<
"Printing Tuple " << name <<
"===================" << std::endl;
456 unsigned long i = 0, l = 0, ul = 0, r = 0;
457 for(
uint k = 0; k <
n; k++ )
459 for(
uint j = 0; j <
mi; j++ )
461 std::cout <<
vi[i++] <<
" | ";
463 for(
uint j = 0; j <
ml; j++ )
465 std::cout <<
vl[l++] <<
" | ";
467 for(
uint j = 0; j <
mul; j++ )
469 std::cout <<
vul[ul++] <<
" | ";
471 for(
uint j = 0; j <
mr; j++ )
473 std::cout <<
vr[r++] <<
" | ";
475 std::cout << std::endl;
477 std::cout <<
"=======================================" << std::endl << std::endl;
482 ofs.open( filename, std::ofstream::out | std::ofstream::app );
484 ofs <<
"Printing Tuple " << filename <<
"===================" << std::endl;
485 unsigned long i = 0, l = 0, ul = 0, r = 0;
486 for(
uint k = 0; k <
n; k++ )
488 for(
uint j = 0; j <
mi; j++ )
490 ofs <<
vi[i++] <<
" | ";
492 for(
uint j = 0; j <
ml; j++ )
494 ofs <<
vl[l++] <<
" | ";
496 for(
uint j = 0; j <
mul; j++ )
498 ofs <<
vul[ul++] <<
" | ";
500 for(
uint j = 0; j <
mr; j++ )
502 ofs <<
vr[r++] <<
" | ";
506 ofs <<
"=======================================" << std::endl << std::endl;
512 const unsigned int_size =
mi *
sizeof(
sint ), long_size =
ml *
sizeof(
slong ), Ulong_size =
mul *
sizeof(
Ulong ),
516 uint *p = perm, *pe = p +
n;
517 char* sorted = (
char*)work;
519 memcpy( (
void*)sorted, &
vi[
mi * ( *p++ )], int_size ), sorted += int_size;
520 memcpy(
vi, work, int_size *
n );
524 uint *p = perm, *pe = p +
n;
525 char* sorted = (
char*)work;
527 memcpy( (
void*)sorted, &
vl[
ml * ( *p++ )], long_size ), sorted += long_size;
528 memcpy(
vl, work, long_size *
n );
532 uint *p = perm, *pe = p +
n;
533 char* sorted = (
char*)work;
535 memcpy( (
void*)sorted, &
vul[
mul * ( *p++ )], Ulong_size ), sorted += Ulong_size;
536 memcpy(
vul, work, Ulong_size *
n );
540 uint *p = perm, *pe = p +
n;
541 char* sorted = (
char*)work;
543 memcpy( (
void*)sorted, &
vr[
mr * ( *p++ )], real_size ), sorted += real_size;
544 memcpy(
vr, work, real_size *
n );
548 #define umax_2( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
552 const unsigned int_size =
mi *
sizeof(
sint );
553 const unsigned long_size =
ml *
sizeof(
slong );
554 const unsigned Ulong_size =
mul *
sizeof(
Ulong );
555 const unsigned real_size =
mr *
sizeof(
realType );
556 const unsigned width =
umax_2(
umax_2( int_size, long_size ),
umax_2( Ulong_size, real_size ) );
558 #if defined( WIN32 ) || defined( _WIN32 )
564 buf->buffer_reserve( work_min );
568 else if( key <
mi +
ml )
584 #define DIGIT_VALUES ( 1 << DIGIT_BITS )
585 #define DIGIT_MASK ( (Value)( DIGIT_VALUES - 1 ) )
586 #define CEILDIV( a, b ) ( ( ( a ) + (b)-1 ) / ( b ) )
587 #define DIGITS CEILDIV( CHAR_BIT * sizeof( Value ), DIGIT_BITS )
588 #define VALUE_BITS ( DIGIT_BITS * DIGITS )
589 #define COUNT_SIZE ( DIGITS * DIGIT_VALUES )
592 #define COUNT_DIGIT_01( n, i ) \
593 if( ( n ) > ( i ) ) count[i][val & DIGIT_MASK]++, val >>= DIGIT_BITS
594 #define COUNT_DIGIT_02( n, i ) \
595 COUNT_DIGIT_01( n, i ); \
596 COUNT_DIGIT_01( n, ( i ) + 1 )
597 #define COUNT_DIGIT_04( n, i ) \
598 COUNT_DIGIT_02( n, i ); \
599 COUNT_DIGIT_02( n, ( i ) + 2 )
600 #define COUNT_DIGIT_08( n, i ) \
601 COUNT_DIGIT_04( n, i ); \
602 COUNT_DIGIT_04( n, ( i ) + 4 )
603 #define COUNT_DIGIT_16( n, i ) \
604 COUNT_DIGIT_08( n, i ); \
605 COUNT_DIGIT_08( n, ( i ) + 8 )
606 #define COUNT_DIGIT_32( n, i ) \
607 COUNT_DIGIT_16( n, i ); \
608 COUNT_DIGIT_16( n, ( i ) + 16 )
609 #define COUNT_DIGIT_64( n, i ) \
610 COUNT_DIGIT_32( n, i ); \
611 COUNT_DIGIT_32( n, ( i ) + 32 )
613 template <
class Value >
629 }
while( A += stride, A != end );
633 #undef COUNT_DIGIT_01
634 #undef COUNT_DIGIT_02
635 #undef COUNT_DIGIT_04
636 #undef COUNT_DIGIT_08
637 #undef COUNT_DIGIT_16
638 #undef COUNT_DIGIT_32
639 #undef COUNT_DIGIT_64
645 t = *c, *c++ =
sum,
sum += t;
649 template <
class Value >
652 unsigned digits = 0, sh = 0;
653 Index* c = &count[0][0];
661 template <
class Value >
674 d->
v = v, d->
i = i++;
675 }
while( A += stride, i !=
n );
678 template <
class Value >
688 d->
v = src->
v, d->
i = src->
i;
689 }
while( ++src != end );
692 template <
class Value >
701 while( ++src != end );
704 template <
class Value >
710 while( A += stride, i !=
n );
713 template <
class Value >
717 Value bitorkey =
radix_count( A, A +
n * stride, stride, count );
720 unsigned digits =
radix_zeros( bitorkey, count, shift, offsets );
728 else if( digits == 1 )
736 if( ( digits & 1 ) == 0 )
737 dst = work, src = dst +
n;
739 src = work, dst = src +
n;
741 for( d = 1; d != digits - 1; ++d )
745 t = src, src = dst, dst = t;
751 template <
class Value >
755 Index n = An, base = -
n, odd = 0, c = 0, b = 1;
762 base +=
n,
n += ( odd & 1 ), c |= 1, b ^= 1;
764 odd <<= 1, odd |= (
n & 1 ),
n >>= 1, c <<= 1, b ^= 1;
767 base -=
n - ( odd & 1 ),
n <<= 1,
n -= ( odd & 1 ), odd >>= 1, c >>= 1;
773 v[0] = *A, A += stride, v[1] = *A, A += stride;
775 p[0].
v = v[1], p[0].
i = i + 1, p[1].
v = v[0], p[1].
i = i;
777 p[0].
v = v[0], p[0].
i = i, p[1].
v = v[1], p[1].
i = i + 1;
783 v[0] = *A, A += stride, v[1] = *A, A += stride, v[2] = *A, A += stride;
787 p[0].
v = v[2], p[1].
v = v[1], p[2].
v = v[0], p[0].
i = i + 2, p[1].
i = i + 1, p[2].
i = i;
791 p[0].
v = v[1], p[1].
v = v[2], p[2].
v = v[0], p[0].
i = i + 1, p[1].
i = i + 2, p[2].
i = i;
793 p[0].
v = v[1], p[1].
v = v[0], p[2].
v = v[2], p[0].
i = i + 1, p[1].
i = i, p[2].
i = i + 2;
799 p[0].
v = v[2], p[1].
v = v[0], p[2].
v = v[1], p[0].
i = i + 2, p[1].
i = i, p[2].
i = i + 1;
803 p[0].
v = v[0], p[1].
v = v[2], p[2].
v = v[1], p[0].
i = i, p[1].
i = i + 2, p[2].
i = i + 1;
805 p[0].
v = v[0], p[1].
v = v[1], p[2].
v = v[2], p[0].
i = i, p[1].
i = i + 1, p[2].
i = i + 2;
812 const Index na =
n >> 1, nb = (
n + 1 ) >> 1;
820 if( bp != be )
continue;
829 if( ap == ae )
break;
842 template <
class Value >
864 #undef sort_data_long