Advertisement
Guest User

GG

a guest
Apr 8th, 2008
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <cstring>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <cstdio>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. static const size_t g_junk_size = 4096;
  10. static int g_dictionary_data[ g_junk_size * 3072 ];
  11. static int g_dictionaries = 0;
  12. static int g_dictionary_count = 0;
  13.  
  14. static size_t total_byte_count = 0;
  15. static size_t total_word_count = 0;
  16. static size_t total_line_count = 0;
  17.  
  18. void dump_dictionary( int dindex, char [], int buflen );
  19.  
  20. //------------------------------------------------------------------------------
  21. int main( int argc, char *argv[] )
  22. {
  23.     const clock_t start = clock();
  24.  
  25.     for( int idx = 1; idx != argc; ++idx )
  26.     {
  27.         FILE *file_handle = fopen( argv[idx], "rb" );
  28.         if( !file_handle )
  29.             continue;
  30.  
  31.         fseek( file_handle, 0L, SEEK_END );
  32.         size_t byte_count = ftell( file_handle );
  33.         fseek( file_handle, 0L, SEEK_SET );
  34.  
  35.         size_t dindex = 0;
  36.  
  37.         //buffered read
  38.         char *buf = (char *)malloc( g_junk_size * sizeof(char) );
  39.         if( !buf )
  40.         {
  41.             printf( "Not enough memory!!!\n" );
  42.             return 1;
  43.         }
  44.  
  45.         for( int bytes_left = (int)byte_count; bytes_left > 0; bytes_left-= g_junk_size )
  46.         {
  47.             size_t len = fread( buf, 1, g_junk_size, file_handle );
  48.             size_t word_count = 0;
  49.             size_t line_count = 0;
  50.             int *dictionary_data = g_dictionary_data;
  51.             int dictionary_count = g_dictionary_count;
  52.  
  53.             for( size_t idx = 0; idx != len; ++idx )
  54.             {
  55.                 int c = buf[ idx ];
  56.                 if( ( 'a' <= c && c <= 'z') || ( 'A' <= c && c <= 'Z' ) )
  57.                 {
  58.                     int index = ( c - 'A') + dindex;
  59.                     dindex = dictionary_data[index];
  60.  
  61.                     if( !dindex )
  62.                     {
  63.                         dictionary_data[index] = (++dictionary_count) * 64;
  64.                         dindex = dictionary_data[index];
  65.                     }
  66.                 }
  67.                 else
  68.                 {
  69.                     if(  c == '\n' )
  70.                         ++line_count;
  71.                     if( dindex )
  72.                     {
  73.                         ++word_count;
  74.                         ++dictionary_data[ dindex + 26 ];
  75.                         dindex = 0;
  76.                     }
  77.                 }
  78.             }
  79.  
  80.             total_word_count += word_count;
  81.             total_line_count += line_count;
  82.  
  83.             g_dictionary_count = dictionary_count;
  84.         }
  85.  
  86.         total_byte_count += byte_count;
  87.  
  88.         free( buf );
  89.  
  90.         fclose( file_handle );
  91.     }
  92.  
  93.     const clock_t end = clock();
  94.  
  95.     printf( "Lines\tWords\tBytes\n");
  96.     printf( "-------------------------------------\n" );
  97.     printf( "%ld\t%ld\t%ld\tTotal\n", total_line_count, total_word_count, total_byte_count );
  98.     printf( "-------------------------------------\n" );
  99.  
  100.     if( argc > 1 )
  101.     {
  102.         char buff[1024];
  103.         dump_dictionary( 0, buff, 0 );
  104.     }
  105.  
  106.     printf( "Time: %.0fms\n", (float)(end - start) / (float)CLOCKS_PER_SEC * (float)1000 );
  107.  
  108.     return 0;
  109. }
  110.  
  111. void dump_dictionary( int dindex, char buf[], int buflen )
  112. {
  113.     if( g_dictionary_data[ dindex + 26 ] != 0 )
  114.     {
  115.         char word[1024];
  116.         strncpy( word, buf, buflen );
  117.         word[buflen + 1] = 0;
  118.         printf( "%d\t %s\n", g_dictionary_data[dindex + 26], word);
  119.     }
  120.  
  121.     for( int idx = 0; idx != 64; ++idx )
  122.     {
  123.         if( g_dictionary_data[dindex + idx] && (idx != 26) )
  124.         {
  125.             buf[buflen] = (char)('A' + (idx) );
  126.             dump_dictionary( g_dictionary_data[dindex + idx], buf, buflen + 1 );
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement