PAGRINDINIS FAILAS BWT.CPP:
// rle < raw-file | bwt | mtf | rle | ari > suspaustas_failasFAILAS
// kompiliavimas // : g++ -o mtf mtf.cpp
#include
#include
#include
#include
#include
#if !defined( unix )
#include
#endif
#include
#if ( INT_MAX == 32767 )
#define BLOCK_SIZE 20000
#else
#define BLOCK_SIZE 200000
#endif
long length;
unsigned char buffer[ BLOCK_SIZE ];
int indices[ BLOCK_SIZE + 1 ];
int memcmp_signed;
int unsigned_memcmp( void *p1, void *p2, unsigned int i )
{
unsigned char *pc1 = (unsigned char *) p1;
unsigned char *pc2 = (unsigned char *) p2;
while ( i– ) {
if ( *pc1 < *pc2 )
return -1;
else if ( *pc1++ > *pc2++ )
return 1;
}
return 0;
}
int
#if defined( _MSC_VER )
_cdecl
#endif
bounded_compare( const unsigned int *i1,
const unsigned int *i2 )
{
static int ticker = 0;
if ( ( ticker++ % 4096 ) == 0 )
fprintf( stderr, „.“ );
unsigned int l1 = (unsigned int) ( length – *i1 );
unsigned int l2 = (unsigned int) ( length – *i2 );
int result;
if ( memcmp_signed )
result = unsigned_memcmp( buffer + *i1,
buffer + *i2,
l1 < l2 ? l1 : l2 );
else
result = memcmp( buffer + *i1,
buffer + *i2,
l1 < l2 ? l1 : l2 );
if ( result == 0 )
return l2 – l1;
else
return result;
};
main( int argc, char *argv[] )
{
int debug = 0;
if ( argc > 1 && strcmp( argv[ 1 ], „-d“ ) == 0 ) {
debug = 1;
argv++;
argc–;
}
fprintf( stderr, „Performing BWT on “ );
if ( argc > 1 ) {
freopen( argv[ 1 ], „rb“, stdin );
fprintf( stderr, „%s“, argv[ 1 ] );
} else
fprintf( stderr, „stdin“ );
fprintf( stderr, “ to “ );
if ( argc > 2 ) {
freopen( argv[ 2 ], „wb“, stdout );
fprintf( stderr, „%s“, argv[ 2 ] );
} else
fprintf( stderr, „stdout“ );
fprintf( stderr, „n“ );
#if !defined( unix )
setmode( fileno( stdin ), O_BINARY );
setmode( fileno( stdout ), O_BINARY );
#endif
if ( memcmp( „x070“, „x080“, 1 ) < 0 ) {
memcmp_signed = 0;
fprintf( stderr, „memcmp() treats character data as unsignedn“ );
} else {
memcmp_signed = 1;
fprintf( stderr, „memcmp() treats character data as signedn“ );
}
for ( ; ; ) {
length = fread( (char *) buffer, 1, BLOCK_SIZE, stdin );
if ( length == 0 )
break;
fprintf( stderr, „Performing BWT on %ld bytesn“, length );
long l = length + 1;
fwrite( (char *) &l, 1, sizeof( long ), stdout );
int i;
for ( i = 0 ; i <= length ; i++ )